Commit 80510068 authored by Maja Kabus's avatar Maja Kabus Committed by Commit Bot

Introduced dictionary TrustedTypePolicyOptions

Created new dictionary TrustedTypePolicyOptions with callbacks and
adjusted TrustedTypePolicyFactory and TrustedTypePolicy to accept the
dictionary.

Added createURL() and createScriptURL, and adjusted createHTML()
in TrustedTypePolicyOptions.

Bug: 739170
Change-Id: Ie63fdf90b5aac53188fcd8141c95d287c3e726fd
Reviewed-on: https://chromium-review.googlesource.com/1167282Reviewed-by: default avatarMike West <mkwst@chromium.org>
Reviewed-by: default avatarYuki Shiino <yukishiino@chromium.org>
Commit-Queue: Maja Kabus <kabusm@google.com>
Cr-Commit-Position: refs/heads/master@{#584038}
parent 2185f5eb
......@@ -4,11 +4,226 @@
<script src="support/helper.sub.js"></script>
<body>
<script>
//Policy name test
async_test(t => {
var policy = window.trustedTypes.createPolicy('SomeName')
let policy = window.trustedTypes.createPolicy('SomeName', { createHTML: s => s } )
.then(t.step_func_done(policy => {
assert_true(policy instanceof TrustedTypePolicy);
assert_equals(policy.name, 'SomeName');
}));
}, "policy.name = name");
//HTML tests
function createHTMLTest(policy, expectedHTML, t) {
let p = window.trustedTypes.createPolicy('SomeName', policy)
.then(t.step_func_done(p => {
assert_true(p.createHTML('whatever') instanceof TrustedHTML);
assert_equals(p.createHTML('whatever') + "", expectedHTML);
}));
}
async_test(t => {
createHTMLTest( { createHTML: s => s }, 'whatever', t);
}, "html = identity function");
async_test(t => {
createHTMLTest( { createHTML: s => null }, "null", t);
}, "html = null");
var HTMLstr = 'well, ';
async_test(t => {
createHTMLTest( { createHTML: s => HTMLstr + s }, HTMLstr + 'whatever', t);
}, "html = string + global string");
var HTMLx = 'global';
async_test(t => {
let p = window.trustedTypes.createPolicy('SomeName', {
createHTML: s => { HTMLx = s; return s; }
})
.then(t.step_func_done(p => {
assert_true(p.createHTML('whatever') instanceof TrustedHTML);
assert_equals(p.createHTML('whatever') + "", 'whatever');
assert_equals(HTMLx, 'whatever');
}));
}, "html = identity function, global string changed");
async_test(t => {
let p = window.trustedTypes.createPolicy('SomeName', {
createHTML: s => { throw new Error(); }
})
.then(t.step_func_done(p => {
assert_throws(new Error(), _ => {
p.createHTML('whatever');
});
}));
}, "html = callback that throws");
var obj = {
"foo": "well,"
}
function getHTML(s) {
return this.foo + " " + s;
}
async_test(t => {
createHTMLTest( {
createHTML: getHTML.bind(obj)},
'well, whatever', t);
}, "html = this bound to an object");
var foo = "well,";
async_test(t => {
createHTMLTest( { createHTML: s => getHTML(s) }, 'well, whatever', t);
}, "html = this without bind");
async_test(t => {
let p = window.trustedTypes.createPolicy('SomeName', null)
.then(t.step_func_done(p => {
assert_equals(p.createHTML('whatever'), null);
}));
}, "html - calling undefined callback");
//ScriptURL tests
function createScriptURLTest(policy, expectedScriptURL, t) {
let p = window.trustedTypes.createPolicy('SomeName', policy)
.then(t.step_func_done(p => {
assert_true(p.createScriptURL(URLS.safe) instanceof TrustedScriptURL);
assert_equals(p.createScriptURL(URLS.safe) + "", expectedScriptURL);
}));
}
async_test(t => {
createScriptURLTest( { createScriptURL: s => s }, URLS.safe, t);
}, "script_url = identity function");
async_test(t => {
createScriptURLTest( { createScriptURL: s => null }, "", t);
}, "script_url = null");
var scriptURLstr = 'duck';
async_test(t => {
createScriptURLTest( { createScriptURL: s => s + scriptURLstr }, URLS.safe + scriptURLstr, t);
}, "script_url = string + global string");
var scriptURLx = 'global';
async_test(t => {
let p = window.trustedTypes.createPolicy('SomeName', {
createScriptURL: s => { ScriptURLx = s; return s; }
})
.then(t.step_func_done(p => {
assert_true(p.createScriptURL(URLS.safe) instanceof TrustedScriptURL);
assert_equals(p.createScriptURL(URLS.safe) + "", URLS.safe);
assert_equals(ScriptURLx, URLS.safe);
}));
}, "script_url = identity function, global string changed");
async_test(t => {
let p = window.trustedTypes.createPolicy('SomeName', {
createScriptURL: s => { throw new Error(); }
})
.then(t.step_func_done(p => {
assert_throws(new Error(), _ => {
p.createScriptURL(URLS.safe);
});
}));
}, "script_url = callback that throws");
function getScriptURL(s) {
return s + this.baz;
}
var obj = {
"baz": "duck"
}
async_test(t => {
createScriptURLTest( {
createScriptURL: getScriptURL.bind(obj)},
URLS.safe + "duck", t);
}, "script_url = this bound to an object");
var baz = "duck";
async_test(t => {
createScriptURLTest( { createScriptURL: s => getScriptURL(s) }, URLS.safe + baz, t);
}, "script_url = this without bind");
async_test(t => {
let p = window.trustedTypes.createPolicy('SomeName', null)
.then(t.step_func_done(p => {
assert_equals(p.createScriptURL('whatever'), null);
}));
}, "script_url - calling undefined callback");
//URL tests
function createURLTest(policy, expectedURL, t) {
let p = window.trustedTypes.createPolicy('SomeName', policy)
.then(t.step_func_done(p => {
assert_true(p.createURL(URLS.safe) instanceof TrustedURL);
assert_equals(p.createURL(URLS.safe) + "", expectedURL);
}));
}
async_test(t => {
createURLTest( { createURL: s => s }, URLS.safe, t);
}, "url = identity function");
async_test(t => {
createURLTest( { createURL: s => null }, "", t);
}, "url = null");
var URLstr = '#x';
async_test(t => {
createURLTest( { createURL: s => s + URLstr }, URLS.safe + URLstr, t);
}, "url = string + global string");
var URLx = 'global';
async_test(t => {
let p = window.trustedTypes.createPolicy('SomeName', {
createURL: s => { URLx = s; return s; }
})
.then(t.step_func_done(p => {
assert_true(p.createURL(URLS.safe) instanceof TrustedURL);
assert_equals(p.createURL(URLS.safe) + "", URLS.safe);
assert_equals(URLx, URLS.safe);
}));
}, "url = identity function, global string changed");
async_test(t => {
let p = window.trustedTypes.createPolicy('SomeName', {
createURL: s => { throw new Error(); }
})
.then(t.step_func_done(p => {
assert_throws(new Error(), _ => {
p.createURL(URLS.safe);
});
}));
}, "url = callback that throws");
function getURL(s) {
return s + this.bar;
}
var obj = {
"bar": "#x"
}
async_test(t => {
createURLTest( {
createURL: getURL.bind(obj)},
URLS.safe + "#x", t);
}, "url = this bound to an object");
var bar = "#x";
async_test(t => {
createURLTest( { createURL: s => getURL(s) }, URLS.safe + bar, t);
}, "url = this without bind");
async_test(t => {
let p = window.trustedTypes.createPolicy('SomeName', null)
.then(t.step_func_done(p => {
assert_equals(p.createURL('whatever'), null);
}));
}, "url - calling undefined callback");
</script>
......@@ -7305,6 +7305,9 @@ interface TrustedTypePolicy
attribute @@toStringTag
getter name
method constructor
method createHTML
method createScriptURL
method createURL
interface TrustedTypePolicyFactory
attribute @@toStringTag
method constructor
......
......@@ -148,6 +148,10 @@ generated_core_callback_function_files = [
"$bindings_core_v8_output_dir/v8_resize_observer_callback.h",
"$bindings_core_v8_output_dir/v8_scroll_state_callback.cc",
"$bindings_core_v8_output_dir/v8_scroll_state_callback.h",
"$bindings_core_v8_output_dir/v8_create_html_callback.cc",
"$bindings_core_v8_output_dir/v8_create_html_callback.h",
"$bindings_core_v8_output_dir/v8_create_url_callback.cc",
"$bindings_core_v8_output_dir/v8_create_url_callback.h",
"$bindings_core_v8_output_dir/v8_void_function.cc",
"$bindings_core_v8_output_dir/v8_void_function.h",
]
......
......@@ -1118,6 +1118,7 @@ IdlArrayOrSequenceType.literal_cpp_value = array_or_sequence_literal_cpp_value
_IDL_TYPE_TO_NATIVE_VALUE_TRAITS_TAG_MAP = {
'DOMString': 'IDLString',
'USVString': 'IDLUSVString',
'any': 'ScriptValue',
'boolean': 'IDLBoolean',
'long': 'IDLLong',
......
......@@ -664,6 +664,7 @@ core_dictionary_idl_files =
"timing/performance_mark_options.idl",
"timing/performance_measure_options.idl",
"timing/performance_observer_init.idl",
"trustedtypes/trusted_type_policy_options.idl",
"workers/worker_options.idl",
"workers/worklet_options.idl",
],
......
......@@ -4,18 +4,80 @@
#include "third_party/blink/renderer/core/trustedtypes/trusted_type_policy.h"
#include "third_party/blink/renderer/core/trustedtypes/trusted_html.h"
#include "third_party/blink/renderer/core/trustedtypes/trusted_script_url.h"
#include "third_party/blink/renderer/core/trustedtypes/trusted_url.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/bindings/to_v8.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
namespace blink {
TrustedTypePolicy* TrustedTypePolicy::Create(const String& policyName) {
return new TrustedTypePolicy(policyName);
TrustedTypePolicy::TrustedTypePolicy(
const String& policy_name,
const TrustedTypePolicyOptions& policy_options)
: name_(policy_name), policy_options_(policy_options) {}
TrustedTypePolicy* TrustedTypePolicy::Create(
const String& policy_name,
const TrustedTypePolicyOptions& policy_options) {
return new TrustedTypePolicy(policy_name, policy_options);
}
TrustedHTML* TrustedTypePolicy::createHTML(ScriptState* script_state,
const String& input,
ExceptionState& exception_state) {
if (!policy_options_.createHTML())
return nullptr;
v8::TryCatch try_catch(script_state->GetIsolate());
String html;
if (!policy_options_.createHTML()->Invoke(nullptr, input).To(&html)) {
DCHECK(try_catch.HasCaught());
exception_state.RethrowV8Exception(try_catch.Exception());
return nullptr;
}
return TrustedHTML::Create(html);
}
TrustedScriptURL* TrustedTypePolicy::createScriptURL(
ScriptState* script_state,
const String& input,
ExceptionState& exception_state) {
if (!policy_options_.createScriptURL())
return nullptr;
v8::TryCatch try_catch(script_state->GetIsolate());
String script_url;
if (!policy_options_.createScriptURL()
->Invoke(nullptr, input)
.To(&script_url)) {
DCHECK(try_catch.HasCaught());
exception_state.RethrowV8Exception(try_catch.Exception());
return nullptr;
}
return TrustedScriptURL::Create(KURL(script_url));
}
TrustedURL* TrustedTypePolicy::createURL(ScriptState* script_state,
const String& input,
ExceptionState& exception_state) {
if (!policy_options_.createURL())
return nullptr;
v8::TryCatch try_catch(script_state->GetIsolate());
String url;
if (!policy_options_.createURL()->Invoke(nullptr, input).To(&url)) {
DCHECK(try_catch.HasCaught());
exception_state.RethrowV8Exception(try_catch.Exception());
return nullptr;
}
return TrustedURL::Create(KURL(url));
}
String TrustedTypePolicy::name() const {
return name_;
}
TrustedTypePolicy::TrustedTypePolicy(const String& policyName) {
name_ = policyName;
void TrustedTypePolicy::Trace(blink::Visitor* visitor) {
visitor->Trace(policy_options_);
ScriptWrappable::Trace(visitor);
}
} // namespace blink
......@@ -6,24 +6,40 @@
#define THIRD_PARTY_BLINK_RENDERER_CORE_TRUSTEDTYPES_TRUSTED_TYPE_POLICY_H_
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/dom/context_lifecycle_observer.h"
#include "third_party/blink/renderer/core/trustedtypes/trusted_type_policy_options.h"
#include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
#include "third_party/blink/renderer/platform/bindings/trace_wrapper_member.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
namespace blink {
class ExceptionState;
class TrustedHTML;
class TrustedScriptURL;
class TrustedURL;
class CORE_EXPORT TrustedTypePolicy final : public ScriptWrappable {
DEFINE_WRAPPERTYPEINFO();
public:
static TrustedTypePolicy* Create(const String& policyName);
static TrustedTypePolicy* Create(const String& policy_name,
const TrustedTypePolicyOptions&);
TrustedHTML* createHTML(ScriptState*, const String&, ExceptionState&);
TrustedScriptURL* createScriptURL(ScriptState*,
const String&,
ExceptionState&);
TrustedURL* createURL(ScriptState*, const String&, ExceptionState&);
String name() const;
void Trace(blink::Visitor*) override;
private:
TrustedTypePolicy(const String& policyName);
TrustedTypePolicy(const String& policy_name, const TrustedTypePolicyOptions&);
String name_;
TrustedTypePolicyOptions policy_options_;
};
} // namespace blink
......
......@@ -8,4 +8,7 @@
RuntimeEnabled=TrustedDOMTypes
] interface TrustedTypePolicy {
readonly attribute DOMString name;
[CallWith=ScriptState, RaisesException] TrustedHTML createHTML(DOMString input);
[CallWith=ScriptState, RaisesException] TrustedScriptURL createScriptURL(DOMString input);
[CallWith=ScriptState, RaisesException] TrustedURL createURL(DOMString input);
};
......@@ -7,12 +7,16 @@
#include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
#include "third_party/blink/renderer/core/trustedtypes/trusted_type_policy.h"
#include "third_party/blink/renderer/platform/bindings/to_v8.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
namespace blink {
ScriptPromise TrustedTypePolicyFactory::createPolicy(ScriptState* script_state,
const String& policyName) {
TrustedTypePolicy* policy = TrustedTypePolicy::Create(policyName);
ScriptPromise TrustedTypePolicyFactory::createPolicy(
ScriptState* script_state,
const String& policy_name,
const TrustedTypePolicyOptions& policy_options) {
TrustedTypePolicy* policy =
TrustedTypePolicy::Create(policy_name, policy_options);
return ScriptPromise::Cast(script_state, ToV8(policy, script_state));
}
......
......@@ -7,6 +7,7 @@
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/dom/context_lifecycle_observer.h"
#include "third_party/blink/renderer/core/trustedtypes/trusted_type_policy_options.h"
#include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
namespace blink {
......@@ -24,7 +25,9 @@ class CORE_EXPORT TrustedTypePolicyFactory final : public ScriptWrappable,
return new TrustedTypePolicyFactory(frame);
}
static ScriptPromise createPolicy(ScriptState*, const String&);
static ScriptPromise createPolicy(ScriptState*,
const String&,
const TrustedTypePolicyOptions&);
void Trace(blink::Visitor*) override;
......
......@@ -8,5 +8,5 @@
Exposed=(Window),
RuntimeEnabled=TrustedDOMTypes
] interface TrustedTypePolicyFactory {
[CallWith=ScriptState] Promise<TrustedTypePolicy> createPolicy(DOMString policyName);
[CallWith=ScriptState] Promise<TrustedTypePolicy> createPolicy(DOMString policyName, TrustedTypePolicyOptions policyOptions);
};
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// https://github.com/wicg/trusted-types
dictionary TrustedTypePolicyOptions {
CreateHTMLCallback createHTML;
CreateURLCallback createScriptURL;
CreateURLCallback createURL;
};
callback CreateHTMLCallback = DOMString (DOMString input);
callback CreateURLCallback = USVString (DOMString input);
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment