Commit 136008de authored by Orsi Batiz's avatar Orsi Batiz Committed by Commit Bot

TrustedTypes: Added TrustedTypes.getPolicyNames & minor changes

Changed window.trustedTypes to window.TrustedTypes.
Added TrustedTypes.getPolicyNames.
Throw exception for TrustedTypes.createPolicy with non-unique policy name.
Adapted tests to match.
Most existing applications are synchronous, createPolicy was made synchronous.
Changed tests from async_test to test, because of change mentioned above.

Bug: 739170
Change-Id: Ie4e2702994a8674e65376b7bf4b42d4c41e9cec4
Reviewed-on: https://chromium-review.googlesource.com/1181362
Commit-Queue: Orsolya Bernadett Batiz <orsibatiz@google.com>
Reviewed-by: default avatarMike West <mkwst@chromium.org>
Reviewed-by: default avatarDaniel Vogelheim <vogelheim@chromium.org>
Cr-Commit-Position: refs/heads/master@{#586630}
parent eb0dd58a
...@@ -4,14 +4,12 @@ ...@@ -4,14 +4,12 @@
<script src="support/helper.sub.js"></script> <script src="support/helper.sub.js"></script>
<body> <body>
<script> <script>
async_test(t => { test(t => {
createHTML_policy(window) let p = createHTML_policy(window, 1);
.then(t.step_func_done(p => {
let html = p.createHTML(INPUTS.HTML); let html = p.createHTML(INPUTS.HTML);
let parser = new DOMParser(); let parser = new DOMParser();
let doc = parser.parseFromString(html, "text/html"); let doc = parser.parseFromString(html, "text/html");
assert_equals(doc.body.innerText, RESULTS.HTML); assert_equals(doc.body.innerText, RESULTS.HTML);
}));
}, "document.innerText assigned via policy (successful HTML transformation)."); }, "document.innerText assigned via policy (successful HTML transformation).");
test(t => { test(t => {
......
...@@ -4,12 +4,10 @@ ...@@ -4,12 +4,10 @@
<script src="support/helper.sub.js"></script> <script src="support/helper.sub.js"></script>
<body> <body>
<script> <script>
async_test(t => { test(t => {
createHTML_policy(window) let p = createHTML_policy(window, 1);
.then(t.step_func_done(p => {
let html = p.createHTML(INPUTS.HTML); let html = p.createHTML(INPUTS.HTML);
document.write(html); document.write(html);
assert_equals(document.body.innerText, RESULTS.HTML); assert_equals(document.body.innerText, RESULTS.HTML);
}));
}, "document.write with html assigned via policy (successful URL transformation)."); }, "document.write with html assigned via policy (successful URL transformation).");
</script> </script>
...@@ -7,9 +7,8 @@ ...@@ -7,9 +7,8 @@
<script> <script>
var container = document.querySelector('#container'); var container = document.querySelector('#container');
async_test(t => { test(t => {
createHTML_policy(window) let p = createHTML_policy(window, 1);
.then(t.step_func_done(p => {
let html = p.createHTML(INPUTS.HTML); let html = p.createHTML(INPUTS.HTML);
var d = document.createElement('div'); var d = document.createElement('div');
...@@ -33,6 +32,5 @@ ...@@ -33,6 +32,5 @@
while (container.firstChild) while (container.firstChild)
container.firstChild.remove(); container.firstChild.remove();
}));
}, "insertAdjacentHTML with html assigned via policy (successful HTML transformation)."); }, "insertAdjacentHTML with html assigned via policy (successful HTML transformation).");
</script> </script>
...@@ -5,11 +5,10 @@ ...@@ -5,11 +5,10 @@
<body> <body>
<div id="container"></div> <div id="container"></div>
<script> <script>
var container = document.querySelector('#container') var container = document.querySelector('#container');
async_test(t => { test(t => {
createHTML_policy(window) let p = createHTML_policy(window, 1);
.then(t.step_func_done(p => {
let html = p.createHTML(INPUTS.HTML); let html = p.createHTML(INPUTS.HTML);
var d = document.createElement('div'); var d = document.createElement('div');
...@@ -19,6 +18,5 @@ ...@@ -19,6 +18,5 @@
while (container.firstChild) while (container.firstChild)
container.firstChild.remove(); container.firstChild.remove();
}));
}, "outerHTML with html assigned via policy (successful HTML transformation)."); }, "outerHTML with html assigned via policy (successful HTML transformation).");
</script> </script>
...@@ -24,8 +24,8 @@ ...@@ -24,8 +24,8 @@
]; ];
testCases.forEach(c => { testCases.forEach(c => {
async_test(t => { test(t => {
assert_element_accepts_trusted_url(window, t, c[0], c[1], RESULTS.URL); assert_element_accepts_trusted_url(window, c, t, c[0], c[1], RESULTS.URL);
}, c[0] + "." + c[1] + " assigned via policy (successful URL transformation)"); }, c[0] + "." + c[1] + " assigned via policy (successful URL transformation)");
}); });
...@@ -36,8 +36,8 @@ ...@@ -36,8 +36,8 @@
]; ];
scriptTestCases.forEach(c => { scriptTestCases.forEach(c => {
async_test(t => { test(t => {
assert_element_accepts_trusted_script_url(window, t, c[0], c[1], RESULTS.SCRIPTURL); assert_element_accepts_trusted_script_url(window, c, t, c[0], c[1], RESULTS.SCRIPTURL);
}, c[0] + "." + c[1] + " assigned via policy (successful ScriptURL transformation)"); }, c[0] + "." + c[1] + " assigned via policy (successful ScriptURL transformation)");
}); });
...@@ -48,8 +48,8 @@ ...@@ -48,8 +48,8 @@
]; ];
HTMLTestCases.forEach(c => { HTMLTestCases.forEach(c => {
async_test(t => { test(t => {
assert_element_accepts_trusted_html(window, t, c[0], c[1], RESULTS.HTML); assert_element_accepts_trusted_html(window, c, t, c[0], c[1], RESULTS.HTML);
}, c[0] + "." + c[1] + " assigned via policy (successful HTML transformation)"); }, c[0] + "." + c[1] + " assigned via policy (successful HTML transformation)");
}); });
</script> </script>
...@@ -4,12 +4,10 @@ ...@@ -4,12 +4,10 @@
<script src="support/helper.sub.js"></script> <script src="support/helper.sub.js"></script>
<body> <body>
<script> <script>
async_test(t => { test(t => {
createURL_policy(window) let p = createURL_policy(window, 1);
.then(t.step_func_done(p => {
let url = p.createURL(location.href + "#xxx"); let url = p.createURL(location.href + "#xxx");
location.assign(url); location.assign(url);
assert_equals("" + url, location.href, "location href"); assert_equals("" + url, location.href, "location href");
}));
}, "location.assign via policy (successful URL transformation)."); }, "location.assign via policy (successful URL transformation).");
</script> </script>
...@@ -4,12 +4,10 @@ ...@@ -4,12 +4,10 @@
<script src="support/helper.sub.js"></script> <script src="support/helper.sub.js"></script>
<body> <body>
<script> <script>
async_test(t => { test(t => {
createURL_policy(window) let p = createURL_policy(window, 1);
.then(t.step_func_done(p => {
let url = p.createURL(location.href + "#xxx"); let url = p.createURL(location.href + "#xxx");
location.href = url; location.href = url;
assert_equals("" + url, location.href, "location href"); assert_equals("" + url, location.href, "location href");
}));
}, "location.href assigned via policy (successful URL transformation)."); }, "location.href assigned via policy (successful URL transformation).");
</script> </script>
...@@ -4,12 +4,10 @@ ...@@ -4,12 +4,10 @@
<script src="support/helper.sub.js"></script> <script src="support/helper.sub.js"></script>
<body> <body>
<script> <script>
async_test(t => { test(t => {
createURL_policy(window) let p = createURL_policy(window, 1);
.then(t.step_func_done(p => {
let url = p.createURL(location.href + "#xxx"); let url = p.createURL(location.href + "#xxx");
location.replace(url); location.replace(url);
assert_equals("" + url, location.href, "location href"); assert_equals("" + url, location.href, "location href");
}));
}, "location.replace via policy (successful URL transformation)."); }, "location.replace via policy (successful URL transformation).");
</script> </script>
...@@ -4,14 +4,12 @@ ...@@ -4,14 +4,12 @@
<script src="support/helper.sub.js"></script> <script src="support/helper.sub.js"></script>
<body> <body>
<script> <script>
async_test(t => { test(t => {
createHTML_policy(window) let p = createHTML_policy(window, 1);
.then(t.step_func_done(p => {
let html = p.createHTML(INPUTS.HTML); let html = p.createHTML(INPUTS.HTML);
var range = document.createRange(); var range = document.createRange();
range.selectNodeContents(document.documentElement); range.selectNodeContents(document.documentElement);
var result = range.createContextualFragment(html); var result = range.createContextualFragment(html);
assert_equals(result.textContent, RESULTS.HTML); assert_equals(result.textContent, RESULTS.HTML);
}));
}, "range.createContextualFragment assigned via policy (successful HTML transformation)."); }, "range.createContextualFragment assigned via policy (successful HTML transformation).");
</script> </script>
<!DOCTYPE html>
<script src="/resources/testharness.js" ></script>
<script src="/resources/testharnessreport.js"></script>
<script src="support/helper.sub.js"></script>
<body>
<script>
//HTML tests
function createHTMLTest(policyName, policy, expectedHTML, t) {
let p = window.TrustedTypes.createPolicy(policyName, policy);
assert_true(p.createHTML('whatever') instanceof TrustedHTML);
assert_equals(p.createHTML('whatever') + "", expectedHTML);
}
test(t => {
createHTMLTest('TestPolicyHTML1', { createHTML: s => s }, 'whatever', t);
}, "html = identity function");
test(t => {
createHTMLTest('TestPolicyHTML2', { createHTML: s => null }, "null", t);
}, "html = null");
var HTMLstr = 'well, ';
test(t => {
createHTMLTest('TestPolicyHTML3', { createHTML: s => HTMLstr + s }, HTMLstr + 'whatever', t);
}, "html = string + global string");
var HTMLx = 'global';
test(t => {
createHTMLTest('TestPolicyHTML4', { createHTML: s => { HTMLx = s; return s; } }, 'whatever', t);
assert_equals(HTMLx, 'whatever');
}, "html = identity function, global string changed");
test(t => {
let p = window.TrustedTypes.createPolicy('TestPolicyHTML5', { createHTML: s => { throw new Error(); }});
assert_throws(new Error(), _ => {
p.createHTML('whatever');
});
}, "html = callback that throws");
var obj = {
"foo": "well,"
}
function getHTML(s) {
return this.foo + " " + s;
}
test(t => {
createHTMLTest('TestPolicyHTML6', { createHTML: getHTML.bind(obj) }, 'well, whatever', t);
}, "html = this bound to an object");
var foo = "well,";
test(t => {
createHTMLTest('TestPolicyHTML7', { createHTML: s => getHTML(s) }, 'well, whatever', t);
}, "html = this without bind");
test(t => {
let p = window.TrustedTypes.createPolicy('TestPolicyHTML8', null);
assert_throws(new TypeError(), _ => {
p.createHTML('whatever');
});
}, "html - calling undefined callback throws");
test(t => {
let p = window.TrustedTypes.createPolicy('TestPolicyHTML9', { createHTML: createHTMLJS });
assert_throws(new TypeError(), _ => {
p.createScript(INPUTS.SCRIPT);
});
assert_throws(new TypeError(), _ => {
p.createScriptURL(INPUTS.SCRIPTURL);
});
assert_throws(new TypeError(), _ => {
p.createURL(INPUTS.URL);
});
}, "createHTML defined - calling undefined callbacks throws");
//Script tests
function createScriptTest(policyName, policy, expectedScript, t) {
let p = window.TrustedTypes.createPolicy(policyName, policy);
assert_true(p.createScript('whatever') instanceof TrustedScript);
assert_equals(p.createScript('whatever') + "", expectedScript);
}
test(t => {
createScriptTest('TestPolicyScript1', { createScript: s => s }, 'whatever', t);
}, "script = identity function");
test(t => {
createScriptTest('TestPolicyScript2', { createScript: s => null }, "null", t);
}, "script = null");
var Scriptstr = 'well, ';
test(t => {
createScriptTest('TestPolicyScript3', { createScript: s => Scriptstr + s }, Scriptstr + 'whatever', t);
}, "script = string + global string");
var Scriptx = 'global';
test(t => {
createScriptTest('TestPolicyScript4', { createScript: s => { Scriptx = s; return s; } }, 'whatever', t);
assert_equals(Scriptx, 'whatever');
}, "script = identity function, global string changed");
test(t => {
let p = window.TrustedTypes.createPolicy('TestPolicyScript5', {
createScript: s => { throw new Error(); }
});
assert_throws(new Error(), _ => {
p.createScript('whatever');
});
}, "script = callback that throws");
var obj = {
"foo": "well,"
}
function getScript(s) {
return this.foo + " " + s;
}
test(t => {
createScriptTest('TestPolicyScript6', { createScript: getScript.bind(obj) }, 'well, whatever', t);
}, "script = this bound to an object");
var foo = "well,";
test(t => {
createScriptTest('TestPolicyScript7', { createScript: s => getScript(s) }, 'well, whatever', t);
}, "script = this without bind");
test(t => {
let p = window.TrustedTypes.createPolicy('TestPolicyScript8', null);
assert_throws(new TypeError(), _ => {
p.createScript('whatever');
});
}, "script - calling undefined callback throws");
test(t => {
let p = window.TrustedTypes.createPolicy('TestPolicyScript9', { createScript: createScriptJS });
assert_throws(new TypeError(), _ => {
p.createHTML(INPUTS.HTML);
});
assert_throws(new TypeError(), _ => {
p.createScriptURL(INPUTS.SCRIPTURL);
});
assert_throws(new TypeError(), _ => {
p.createURL(INPUTS.URL);
});
}, "createScript defined - calling undefined callbacks throws");
//ScriptURL tests
function createScriptURLTest(policyName, policy, expectedScriptURL, t) {
let p = window.TrustedTypes.createPolicy(policyName, policy);
assert_true(p.createScriptURL(INPUTS.SCRIPTURL) instanceof TrustedScriptURL);
assert_equals(p.createScriptURL(INPUTS.SCRIPTURL) + "", expectedScriptURL);
}
test(t => {
createScriptURLTest('TestPolicyScriptURL1', { createScriptURL: s => s }, INPUTS.SCRIPTURL, t);
}, "script_url = identity function");
test(t => {
createScriptURLTest('TestPolicyScriptURL2', { createScriptURL: s => null }, "", t);
}, "script_url = null");
var scriptURLstr = '#duck';
test(t => {
createScriptURLTest('TestPolicyScriptURL3', { createScriptURL: s => s + scriptURLstr }, INPUTS.SCRIPTURL + scriptURLstr, t);
}, "script_url = string + global string");
var scriptURLx = 'global';
test(t => {
createScriptURLTest('TestPolicyScriptURL4', { createScriptURL: s => { ScriptURLx = s; return s; } }, INPUTS.SCRIPTURL, t);
assert_equals(ScriptURLx, INPUTS.SCRIPTURL);
}, "script_url = identity function, global string changed");
test(t => {
let p = window.TrustedTypes.createPolicy('TestPolicyScriptURL5', {
createScriptURL: s => { throw new Error(); }
});
assert_throws(new Error(), _ => {
p.createScriptURL(INPUTS.SCRIPTURL);
});
}, "script_url = callback that throws");
function getScriptURL(s) {
return s + this.baz;
}
var obj = {
"baz": "#duck"
}
test(t => {
createScriptURLTest('TestPolicyScriptURL6', { createScriptURL: getScriptURL.bind(obj) }, INPUTS.SCRIPTURL + "#duck", t);
}, "script_url = this bound to an object");
var baz = "#duck";
test(t => {
createScriptURLTest('TestPolicyScriptURL7', { createScriptURL: s => getScriptURL(s) }, INPUTS.SCRIPTURL + baz, t);
}, "script_url = this without bind");
test(t => {
let p = window.TrustedTypes.createPolicy('TestPolicyScriptURL8', null);
assert_throws(new TypeError(), _ => {
p.createScriptURL(INPUTS.SCRIPTURL);
});
}, "script_url - calling undefined callback throws");
test(t => {
let p = window.TrustedTypes.createPolicy('TestPolicyScriptURL9', { createScriptURL: createScriptURLJS });
assert_throws(new TypeError(), _ => {
p.createHTML(INPUTS.HTML);
});
assert_throws(new TypeError(), _ => {
p.createScript(INPUTS.SCRIPT);
});
assert_throws(new TypeError(), _ => {
p.createURL(INPUTS.URL);
});
}, "createScriptURL defined - calling undefined callbacks throws");
//URL tests
function createURLTest(policyName, policy, expectedURL, t) {
let p = window.TrustedTypes.createPolicy(policyName, policy);
assert_true(p.createURL(INPUTS.URL) instanceof TrustedURL);
assert_equals(p.createURL(INPUTS.URL) + "", expectedURL);
}
test(t => {
createURLTest('TestPolicyURL1', { createURL: s => s }, INPUTS.URL, t);
}, "url = identity function");
test(t => {
createURLTest('TestPolicyURL2', { createURL: s => null }, "", t);
}, "url = null");
var URLstr = '#x';
test(t => {
createURLTest('TestPolicyURL3', { createURL: s => s + URLstr }, INPUTS.URL + URLstr, t);
}, "url = string + global string");
var URLx = 'global';
test(t => {
createURLTest('TestPolicyURL4', { createURL: s => { URLx = s; return s; } }, INPUTS.URL, t);
assert_equals(URLx, INPUTS.URL);
}, "url = identity function, global string changed");
test(t => {
let p = window.TrustedTypes.createPolicy('TestPolicyURL5', {
createURL: s => { throw new Error(); }
});
assert_throws(new Error(), _ => {
p.createURL(INPUTS.URL);
});
}, "url = callback that throws");
function getURL(s) {
return s + this.bar;
}
var obj = {
"bar": "#x"
}
test(t => {
createURLTest('TestPolicyURL6', { createURL: getURL.bind(obj) }, INPUTS.URL + "#x", t);
}, "url = this bound to an object");
var bar = "#x";
test(t => {
createURLTest('TestPolicyURL7', { createURL: s => getURL(s) }, INPUTS.URL + bar, t);
}, "url = this without bind");
test(t => {
let p = window.TrustedTypes.createPolicy('TestPolicyURL8', null);
assert_throws(new TypeError(), _ => {
p.createURL(INPUTS.URL);
});
}, "url - calling undefined callback throws");
test(t => {
let p = window.TrustedTypes.createPolicy('TestPolicyURL9', { createURL: createURLJS });
assert_throws(new TypeError(), _ => {
p.createHTML(INPUTS.HTML);
});
assert_throws(new TypeError(), _ => {
p.createScript(INPUTS.SCRIPT);
});
assert_throws(new TypeError(), _ => {
p.createScriptURL(INPUTS.SCRIPTURL);
});
}, "createURL defined - calling undefined callbacks throws");
</script>
<!DOCTYPE html>
<script src="/resources/testharness.js" ></script>
<script src="/resources/testharnessreport.js"></script>
<script src="support/helper.sub.js"></script>
<body>
<script>
//Policy name test
test(t => {
let policy = window.TrustedTypes.createPolicy('SomeName', { createHTML: s => s } );
assert_true(policy instanceof TrustedTypePolicy);
assert_equals(policy.name, 'SomeName');
}, "policy.name = name");
//Duplicate names test
test(t => {
assert_throws(new TypeError(), _ => {
window.TrustedTypes.createPolicy('SomeName', { createURL: s => s } );
});
}, "duplicate policy name attempt throws");
//Retrieve policy names tests
test(t => {
let policy = window.TrustedTypes.createPolicy('SomeOtherName', { createURL: s => s } );
let names = window.TrustedTypes.getPolicyNames();
assert_true(names.includes('SomeName'));
assert_true(names.includes('SomeOtherName'));
}, "Retrieving policy names");
</script>
<!DOCTYPE html>
<script src="/resources/testharness.js" ></script>
<script src="/resources/testharnessreport.js"></script>
<script src="support/helper.sub.js"></script>
<body>
<script>
//Policy name test
async_test(t => {
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) {
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 => {
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 => {
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 => {
window.trustedTypes.createPolicy('SomeName', null)
.then(t.step_func_done(p => {
assert_equals(p.createHTML('whatever'), null);
}));
}, "html - calling undefined callback");
//Script tests
function createScriptTest(policy, expectedScript, t) {
let p = window.trustedTypes.createPolicy('SomeName', policy)
.then(t.step_func_done(p => {
assert_true(p.createScript('whatever') instanceof TrustedScript);
assert_equals(p.createScript('whatever') + "", expectedScript);
}));
}
async_test(t => {
createScriptTest( { createScript: s => s }, 'whatever', t);
}, "script = identity function");
async_test(t => {
createScriptTest( { createScript: s => null }, "null", t);
}, "script = null");
var Scriptstr = 'well, ';
async_test(t => {
createScriptTest( { createScript: s => Scriptstr + s }, Scriptstr + 'whatever', t);
}, "script = string + global string");
var Scriptx = 'global';
async_test(t => {
let p = window.trustedTypes.createPolicy('SomeName', {
createScript: s => { Scriptx = s; return s; }
})
.then(t.step_func_done(p => {
assert_true(p.createScript('whatever') instanceof TrustedScript);
assert_equals(p.createScript('whatever') + "", 'whatever');
assert_equals(Scriptx, 'whatever');
}));
}, "script = identity function, global string changed");
async_test(t => {
let p = window.trustedTypes.createPolicy('SomeName', {
createScript: s => { throw new Error(); }
})
.then(t.step_func_done(p => {
assert_throws(new Error(), _ => {
p.createScript('whatever');
});
}));
}, "script = callback that throws");
var obj = {
"foo": "well,"
}
function getScript(s) {
return this.foo + " " + s;
}
async_test(t => {
createScriptTest( {
createScript: getScript.bind(obj)},
'well, whatever', t);
}, "script = this bound to an object");
var foo = "well,";
async_test(t => {
createScriptTest( { createScript: s => getScript(s) }, 'well, whatever', t);
}, "script = this without bind");
async_test(t => {
let p = window.trustedTypes.createPolicy('SomeName', null)
.then(t.step_func_done(p => {
assert_equals(p.createScript('whatever'), null);
}));
}, "script - calling undefined callback");
//ScriptURL tests
function createScriptURLTest(policy, expectedScriptURL, t) {
window.trustedTypes.createPolicy('SomeName', policy)
.then(t.step_func_done(p => {
assert_true(p.createScriptURL(INPUTS.SCRIPTURL) instanceof TrustedScriptURL);
assert_equals(p.createScriptURL(INPUTS.SCRIPTURL) + "", expectedScriptURL);
}));
}
async_test(t => {
createScriptURLTest( { createScriptURL: s => s }, INPUTS.SCRIPTURL, 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 }, INPUTS.SCRIPTURL + scriptURLstr, t);
}, "script_url = string + global string");
var scriptURLx = 'global';
async_test(t => {
window.trustedTypes.createPolicy('SomeName', {
createScriptURL: s => { ScriptURLx = s; return s; }
})
.then(t.step_func_done(p => {
assert_true(p.createScriptURL(INPUTS.SCRIPTURL) instanceof TrustedScriptURL);
assert_equals(p.createScriptURL(INPUTS.SCRIPTURL) + "", INPUTS.SCRIPTURL);
assert_equals(ScriptURLx, INPUTS.SCRIPTURL);
}));
}, "script_url = identity function, global string changed");
async_test(t => {
window.trustedTypes.createPolicy('SomeName', {
createScriptURL: s => { throw new Error(); }
})
.then(t.step_func_done(p => {
assert_throws(new Error(), _ => {
p.createScriptURL(INPUTS.SCRIPTURL);
});
}));
}, "script_url = callback that throws");
function getScriptURL(s) {
return s + this.baz;
}
var obj = {
"baz": "#duck"
}
async_test(t => {
createScriptURLTest( {
createScriptURL: getScriptURL.bind(obj)},
INPUTS.SCRIPTURL + "#duck", t);
}, "script_url = this bound to an object");
var baz = "#duck";
async_test(t => {
createScriptURLTest( { createScriptURL: s => getScriptURL(s) }, INPUTS.SCRIPTURL + baz, t);
}, "script_url = this without bind");
async_test(t => {
window.trustedTypes.createPolicy('SomeName', null)
.then(t.step_func_done(p => {
assert_equals(p.createScriptURL(INPUTS.SCRIPTURL), null);
}));
}, "script_url - calling undefined callback");
//URL tests
function createURLTest(policy, expectedURL, t) {
window.trustedTypes.createPolicy('SomeName', policy)
.then(t.step_func_done(p => {
assert_true(p.createURL(INPUTS.URL) instanceof TrustedURL);
assert_equals(p.createURL(INPUTS.URL) + "", expectedURL);
}));
}
async_test(t => {
createURLTest( { createURL: s => s }, INPUTS.URL, 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 }, INPUTS.URL + URLstr, t);
}, "url = string + global string");
var URLx = 'global';
async_test(t => {
window.trustedTypes.createPolicy('SomeName', {
createURL: s => { URLx = s; return s; }
})
.then(t.step_func_done(p => {
assert_true(p.createURL(INPUTS.URL) instanceof TrustedURL);
assert_equals(p.createURL(INPUTS.URL) + "", INPUTS.URL);
assert_equals(URLx, INPUTS.URL);
}));
}, "url = identity function, global string changed");
async_test(t => {
window.trustedTypes.createPolicy('SomeName', {
createURL: s => { throw new Error(); }
})
.then(t.step_func_done(p => {
assert_throws(new Error(), _ => {
p.createURL(INPUTS.URL);
});
}));
}, "url = callback that throws");
function getURL(s) {
return s + this.bar;
}
var obj = {
"bar": "#x"
}
async_test(t => {
createURLTest( {
createURL: getURL.bind(obj)},
INPUTS.URL + "#x", t);
}, "url = this bound to an object");
var bar = "#x";
async_test(t => {
createURLTest( { createURL: s => getURL(s) }, INPUTS.URL + bar, t);
}, "url = this without bind");
async_test(t => {
window.trustedTypes.createPolicy('SomeName', null)
.then(t.step_func_done(p => {
assert_equals(p.createURL(INPUTS.URL), null);
}));
}, "url - calling undefined callback");
</script>
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
<body> <body>
<script> <script>
test(t => { test(t => {
let factory = window.trustedTypes; let factory = window.TrustedTypes;
assert_true(factory instanceof TrustedTypePolicyFactory); assert_true(factory instanceof TrustedTypePolicyFactory);
}, "factory = window.trustedTypes"); }, "factory = window.TrustedTypes");
</script> </script>
...@@ -5,23 +5,21 @@ ...@@ -5,23 +5,21 @@
<body> <body>
<script> <script>
// helper functions for the tests // helper functions for the tests
function testWindowOpen(t, win) { function testWindowOpen(t, win, testNumber) {
createURL_policy(window) let p = createURL_policy(window, testNumber);
.then(t.step_func_done(p => {
let url = p.createURL(INPUTS.URL); let url = p.createURL(INPUTS.URL);
let child_window = win.open(url, "", ""); let child_window = win.open(url, "", "");
child_window.onload = t.step_func_done(_ => { child_window.onload = t.step_func_done(_ => {
assert_equals(child_window.location.href, "" + url); assert_equals(child_window.location.href, "" + url);
child_window.close(); child_window.close();
}); });
}));
} }
async_test(t => { test(t => {
testWindowOpen(t, window); testWindowOpen(t, window, 1);
}, "window.open via policy (successful URL transformation)."); }, "window.open via policy (successful URL transformation).");
async_test(t => { test(t => {
testWindowOpen(t, document); testWindowOpen(t, document, 2);
}, "document.open via policy (successful URL transformation)."); }, "document.open via policy (successful URL transformation).");
</script> </script>
...@@ -7,14 +7,12 @@ ...@@ -7,14 +7,12 @@
<body> <body>
<script> <script>
// Trusted HTML assignments do not throw. // Trusted HTML assignments do not throw.
async_test(t => { test(t => {
createHTML_policy(window) let p = createHTML_policy(window, 1);
.then(t.step_func_done(p => {
let html = p.createHTML(INPUTS.HTML); let html = p.createHTML(INPUTS.HTML);
let parser = new DOMParser(); let parser = new DOMParser();
let doc = parser.parseFromString(html, "text/html"); let doc = parser.parseFromString(html, "text/html");
assert_equals(doc.body.innerText, RESULTS.HTML); assert_equals(doc.body.innerText, RESULTS.HTML);
}));
}, "document.innerText assigned via policy (successful HTML transformation)."); }, "document.innerText assigned via policy (successful HTML transformation).");
// String assignments throw. // String assignments throw.
......
...@@ -10,13 +10,11 @@ ...@@ -10,13 +10,11 @@
<body> <body>
<script> <script>
// TrustedURL assignments do not throw. // TrustedURL assignments do not throw.
async_test(t => { test(t => {
createHTML_policy(window) let p = createHTML_policy(window, 1);
.then(t.step_func_done(p => {
let html = p.createHTML(INPUTS.HTML); let html = p.createHTML(INPUTS.HTML);
document.write(html); document.write(html);
assert_equals(document.body.innerText, RESULTS.HTML); assert_equals(document.body.innerText, RESULTS.HTML);
}));
}, "document.write with html assigned via policy (successful URL transformation)."); }, "document.write with html assigned via policy (successful URL transformation).");
// String assignments throw. // String assignments throw.
......
...@@ -13,9 +13,8 @@ ...@@ -13,9 +13,8 @@
var container = document.querySelector('#container'); var container = document.querySelector('#container');
// Trusted HTML assignments do not throw. // Trusted HTML assignments do not throw.
async_test(t => { test(t => {
createHTML_policy(window) let p = createHTML_policy(window, 1);
.then(t.step_func_done(p => {
let html = p.createHTML(INPUTS.HTML); let html = p.createHTML(INPUTS.HTML);
var d = document.createElement('div'); var d = document.createElement('div');
...@@ -39,7 +38,6 @@ ...@@ -39,7 +38,6 @@
while (container.firstChild) while (container.firstChild)
container.firstChild.remove(); container.firstChild.remove();
}));
}, "insertAdjacentHTML with html assigned via policy (successful HTML transformation)."); }, "insertAdjacentHTML with html assigned via policy (successful HTML transformation).");
// String assignments throw. // String assignments throw.
......
...@@ -13,9 +13,8 @@ ...@@ -13,9 +13,8 @@
var container = document.querySelector('#container') var container = document.querySelector('#container')
// TrustedHTML assignments do not throw. // TrustedHTML assignments do not throw.
async_test(t => { test(t => {
createHTML_policy(window) let p = createHTML_policy(window, 1);
.then(t.step_func_done(p => {
let html = p.createHTML(INPUTS.HTML); let html = p.createHTML(INPUTS.HTML);
var d = document.createElement('div'); var d = document.createElement('div');
...@@ -25,7 +24,6 @@ ...@@ -25,7 +24,6 @@
while (container.firstChild) while (container.firstChild)
container.firstChild.remove(); container.firstChild.remove();
}));
}, "outerHTML with html assigned via policy (successful HTML transformation)."); }, "outerHTML with html assigned via policy (successful HTML transformation).");
// String assignments throw. // String assignments throw.
......
...@@ -27,8 +27,8 @@ ...@@ -27,8 +27,8 @@
]; ];
testCases.forEach(c => { testCases.forEach(c => {
async_test(t => { test(t => {
assert_element_accepts_trusted_url(window, t, c[0], c[1], RESULTS.URL); assert_element_accepts_trusted_url(window, c, t, c[0], c[1], RESULTS.URL);
assert_throws_no_trusted_type(c[0], c[1], 'A string'); assert_throws_no_trusted_type(c[0], c[1], 'A string');
assert_throws_no_trusted_type(c[0], c[1], null); assert_throws_no_trusted_type(c[0], c[1], null);
}, c[0] + "." + c[1] + " accepts only TrustedURL"); }, c[0] + "." + c[1] + " accepts only TrustedURL");
...@@ -41,8 +41,8 @@ ...@@ -41,8 +41,8 @@
]; ];
scriptTestCases.forEach(c => { scriptTestCases.forEach(c => {
async_test(t => { test(t => {
assert_element_accepts_trusted_script_url(window, t, c[0], c[1], RESULTS.SCRIPTURL); assert_element_accepts_trusted_script_url(window, c, t, c[0], c[1], RESULTS.SCRIPTURL);
assert_throws_no_trusted_type(c[0], c[1], 'A string'); assert_throws_no_trusted_type(c[0], c[1], 'A string');
assert_throws_no_trusted_type(c[0], c[1], null); assert_throws_no_trusted_type(c[0], c[1], null);
}, c[0] + "." + c[1] + " accepts only TrustedScriptURL"); }, c[0] + "." + c[1] + " accepts only TrustedScriptURL");
...@@ -55,8 +55,8 @@ ...@@ -55,8 +55,8 @@
]; ];
HTMLTestCases.forEach(c => { HTMLTestCases.forEach(c => {
async_test(t => { test(t => {
assert_element_accepts_trusted_html(window, t, c[0], c[1], RESULTS.HTML); assert_element_accepts_trusted_html(window, c, t, c[0], c[1], RESULTS.HTML);
assert_throws_no_trusted_type(c[0], c[1], 'A string'); assert_throws_no_trusted_type(c[0], c[1], 'A string');
assert_throws_no_trusted_type(c[0], c[1], null); assert_throws_no_trusted_type(c[0], c[1], null);
}, c[0] + "." + c[1] + " accepts only TrustedHTML"); }, c[0] + "." + c[1] + " accepts only TrustedHTML");
......
...@@ -10,13 +10,11 @@ ...@@ -10,13 +10,11 @@
<body> <body>
<script> <script>
// TrustedURL assignments do not throw. // TrustedURL assignments do not throw.
async_test(t => { test(t => {
createURL_policy(window) let p = createURL_policy(window, 1);
.then(t.step_func_done(p => {
let url = p.createURL(location.href + "#xxx"); let url = p.createURL(location.href + "#xxx");
location.assign(url); location.assign(url);
assert_equals("" + url, location.href, "location href"); assert_equals("" + url, location.href, "location href");
}));
}, "location.assign via policy (successful URL transformation)."); }, "location.assign via policy (successful URL transformation).");
// String assignments throw. // String assignments throw.
......
...@@ -10,13 +10,11 @@ ...@@ -10,13 +10,11 @@
<body> <body>
<script> <script>
// TrustedURL assignments do not throw. // TrustedURL assignments do not throw.
async_test(t => { test(t => {
createURL_policy(window) let p = createURL_policy(window, 1);
.then(t.step_func_done(p => {
let url = p.createURL(location.href + "#xxx"); let url = p.createURL(location.href + "#xxx");
location.href = url; location.href = url;
assert_equals("" + url, location.href, "location href"); assert_equals("" + url, location.href, "location href");
}));
}, "location.href assigned via policy (successful URL transformation)."); }, "location.href assigned via policy (successful URL transformation).");
// String assignments throw. // String assignments throw.
......
...@@ -10,13 +10,11 @@ ...@@ -10,13 +10,11 @@
<body> <body>
<script> <script>
// TrustedURL replacements do not throw. // TrustedURL replacements do not throw.
async_test(t => { test(t => {
createURL_policy(window) let p = createURL_policy(window, 1);
.then(t.step_func_done(p => {
let url = p.createURL(location.href + "#xxx"); let url = p.createURL(location.href + "#xxx");
location.replace(url); location.replace(url);
assert_equals("" + url, location.href, "location href"); assert_equals("" + url, location.href, "location href");
}));
}, "location.replace via policy (successful URL transformation)."); }, "location.replace via policy (successful URL transformation).");
// String replacements throw. // String replacements throw.
......
...@@ -7,15 +7,13 @@ ...@@ -7,15 +7,13 @@
<body> <body>
<script> <script>
// TrustedHTML assignments do not throw. // TrustedHTML assignments do not throw.
async_test(t => { test(t => {
createHTML_policy(window) let p = createHTML_policy(window, 1);
.then(t.step_func_done(p => {
let html = p.createHTML(INPUTS.HTML); let html = p.createHTML(INPUTS.HTML);
var range = document.createRange(); var range = document.createRange();
range.selectNodeContents(document.documentElement); range.selectNodeContents(document.documentElement);
var result = range.createContextualFragment(html); var result = range.createContextualFragment(html);
assert_equals(result.textContent, RESULTS.HTML); assert_equals(result.textContent, RESULTS.HTML);
}));
}, "range.createContextualFragment assigned via policy (successful HTML transformation)."); }, "range.createContextualFragment assigned via policy (successful HTML transformation).");
// String assignments throw. // String assignments throw.
......
...@@ -9,54 +9,51 @@ ...@@ -9,54 +9,51 @@
</head> </head>
<body> <body>
<script> <script>
var testnb = 0;
// helper functions for the tests // helper functions for the tests
function testWindowOpen(t, win) { function testWindowOpen(t, win, nb) {
createURL_policy(window) let p = createURL_policy(window, nb);
.then(t.step_func_done(p => {
let url = p.createURL(INPUTS.URL); let url = p.createURL(INPUTS.URL);
let child_window = win.open(url, "", ""); let child_window = win.open(url, "", "");
child_window.onload = t.step_func_done(_ => { child_window.onload = t.step_func_done(_ => {
assert_equals(child_window.location.href, "" + url); assert_equals(child_window.location.href, "" + url);
child_window.close(); child_window.close();
}); });
}));
} }
function testWindowThrows(t, url, win) { function testWindowThrows(t, url, win, nb) {
createURL_policy(window) let p = createURL_policy(window, nb);
.then(t.step_func_done(p => {
assert_throws(new TypeError(), _ => { assert_throws(new TypeError(), _ => {
let child_window = win.open(url, "", ""); let child_window = win.open(url, "", "");
child_window.close(); child_window.close();
}); });
}));
} }
// TrustedURL assignments do not throw. // TrustedURL assignments do not throw.
async_test(t => { test(t => {
testWindowOpen(t, window); testWindowOpen(t, window, ++testnb);
}, "window.open via policy (successful URL transformation)."); }, "window.open via policy (successful URL transformation).");
async_test(t => { test(t => {
testWindowOpen(t, document); testWindowOpen(t, document, ++testnb);
}, "document.open via policy (successful URL transformation)."); }, "document.open via policy (successful URL transformation).");
// String assignments throw. // String assignments throw.
async_test(t => { test(t => {
testWindowThrows(t, 'A string', window); testWindowThrows(t, 'A string', window, ++testnb);
}, "`window.open(string)` throws."); }, "`window.open(string)` throws.");
async_test(t => { test(t => {
testWindowThrows(t, 'A string', document); testWindowThrows(t, 'A string', document, ++testnb);
}, "`document.open(string)` throws."); }, "`document.open(string)` throws.");
// Null assignment throws. // Null assignment throws.
async_test(t => { test(t => {
testWindowThrows(t, null, window); testWindowThrows(t, null, window, ++testnb);
}, "`window.open(null)` throws."); }, "`window.open(null)` throws.");
async_test(t => { test(t => {
testWindowThrows(t, null, document); testWindowThrows(t, null, document, ++testnb);
}, "`document.open(null)` throws."); }, "`document.open(null)` throws.");
</script> </script>
</body> </body>
......
...@@ -31,52 +31,44 @@ function createURLJS(url) { ...@@ -31,52 +31,44 @@ function createURLJS(url) {
.replace("an.url", "successfully.transformed"); .replace("an.url", "successfully.transformed");
} }
function createHTML_policy(win) { function createHTML_policy(win, c) {
return win.trustedTypes.createPolicy('SomeName', { createHTML: createHTMLJS }); return win.TrustedTypes.createPolicy('SomeHTMLPolicyName' + c, { createHTML: createHTMLJS });
} }
function createScript_policy(win) { function createScript_policy(win, c) {
return win.trustedTypes.createPolicy('SomeName', { createScript: createScriptJS }); return win.TrustedTypes.createPolicy('SomeScriptPolicyName' + c, { createScript: createScriptJS });
} }
function createScriptURL_policy(win) { function createScriptURL_policy(win, c) {
return win.trustedTypes.createPolicy('SomeName', { createScriptURL: createScriptURLJS }); return win.TrustedTypes.createPolicy('SomeScriptURLPolicyName' + c, { createScriptURL: createScriptURLJS });
} }
function createURL_policy(win) { function createURL_policy(win, c) {
return win.trustedTypes.createPolicy('SomeName', { createURL: createURLJS }); return win.TrustedTypes.createPolicy('SomeURLPolicyName' + c, { createURL: createURLJS });
} }
function assert_element_accepts_trusted_html(win, t, tag, attribute, expected) { function assert_element_accepts_trusted_html(win, c, t, tag, attribute, expected) {
createHTML_policy(win) let p = createHTML_policy(win, c);
.then(t.step_func_done(p => {
let html = p.createHTML(INPUTS.HTML); let html = p.createHTML(INPUTS.HTML);
assert_element_accepts_trusted_type(tag, attribute, html, expected); assert_element_accepts_trusted_type(tag, attribute, html, expected);
}));
} }
function assert_element_accepts_trusted_script(win, t, tag, attribute, expected) { function assert_element_accepts_trusted_script(win, c, t, tag, attribute, expected) {
createScript_policy(win) let p = createScript_policy(win, c);
.then(t.step_func_done(p => {
let script = p.createScript(INPUTS.SCRIPT); let script = p.createScript(INPUTS.SCRIPT);
assert_element_accepts_trusted_type(tag, attribute, script, expected); assert_element_accepts_trusted_type(tag, attribute, script, expected);
}));
} }
function assert_element_accepts_trusted_script_url(win, t, tag, attribute, expected) { function assert_element_accepts_trusted_script_url(win, c, t, tag, attribute, expected) {
createScriptURL_policy(win) let p = createScriptURL_policy(win, c);
.then(t.step_func_done(p => {
let scripturl = p.createScriptURL(INPUTS.SCRIPTURL); let scripturl = p.createScriptURL(INPUTS.SCRIPTURL);
assert_element_accepts_trusted_type(tag, attribute, scripturl, expected); assert_element_accepts_trusted_type(tag, attribute, scripturl, expected);
}));
} }
function assert_element_accepts_trusted_url(win, t, tag, attribute, expected) { function assert_element_accepts_trusted_url(win, c, t, tag, attribute, expected) {
createURL_policy(win) let p = createURL_policy(win, c);
.then(t.step_func_done(p => {
let url = p.createURL(INPUTS.URL); let url = p.createURL(INPUTS.URL);
assert_element_accepts_trusted_type(tag, attribute, url, expected); assert_element_accepts_trusted_type(tag, attribute, url, expected);
}));
} }
function assert_element_accepts_trusted_type(tag, attribute, value, expected) { function assert_element_accepts_trusted_type(tag, attribute, value, expected) {
......
...@@ -7328,6 +7328,7 @@ interface TrustedTypePolicyFactory ...@@ -7328,6 +7328,7 @@ interface TrustedTypePolicyFactory
attribute @@toStringTag attribute @@toStringTag
method constructor method constructor
method createPolicy method createPolicy
method getPolicyNames
interface TrustedURL interface TrustedURL
static method create static method create
static method unsafelyCreate static method unsafelyCreate
...@@ -10375,6 +10376,7 @@ interface webkitURL ...@@ -10375,6 +10376,7 @@ interface webkitURL
attribute textInputController attribute textInputController
attribute top attribute top
attribute window attribute window
getter TrustedTypes
getter applicationCache getter applicationCache
getter caches getter caches
getter clientInformation getter clientInformation
...@@ -10526,7 +10528,6 @@ interface webkitURL ...@@ -10526,7 +10528,6 @@ interface webkitURL
getter statusbar getter statusbar
getter styleMedia getter styleMedia
getter toolbar getter toolbar
getter trustedTypes
getter visualViewport getter visualViewport
getter webgpu getter webgpu
getter webkitStorageInfo getter webkitStorageInfo
......
...@@ -220,7 +220,7 @@ ...@@ -220,7 +220,7 @@
//TrustedTypes API //TrustedTypes API
//http://github.com/wicg/trusted-types //http://github.com/wicg/trusted-types
[RuntimeEnabled=TrustedDOMTypes] readonly attribute TrustedTypePolicyFactory trustedTypes; [RuntimeEnabled=TrustedDOMTypes] readonly attribute TrustedTypePolicyFactory TrustedTypes;
}; };
Window implements GlobalEventHandlers; Window implements GlobalEventHandlers;
......
...@@ -28,8 +28,12 @@ TrustedTypePolicy* TrustedTypePolicy::Create( ...@@ -28,8 +28,12 @@ TrustedTypePolicy* TrustedTypePolicy::Create(
TrustedHTML* TrustedTypePolicy::createHTML(ScriptState* script_state, TrustedHTML* TrustedTypePolicy::createHTML(ScriptState* script_state,
const String& input, const String& input,
ExceptionState& exception_state) { ExceptionState& exception_state) {
if (!policy_options_.createHTML()) if (!policy_options_.createHTML()) {
exception_state.ThrowTypeError(
"Policy " + name_ +
"'s TrustedTypePolicyOptions did not specify a 'createHTML' member.");
return nullptr; return nullptr;
}
v8::TryCatch try_catch(script_state->GetIsolate()); v8::TryCatch try_catch(script_state->GetIsolate());
String html; String html;
if (!policy_options_.createHTML()->Invoke(nullptr, input).To(&html)) { if (!policy_options_.createHTML()->Invoke(nullptr, input).To(&html)) {
...@@ -44,8 +48,12 @@ TrustedScript* TrustedTypePolicy::createScript( ...@@ -44,8 +48,12 @@ TrustedScript* TrustedTypePolicy::createScript(
ScriptState* script_state, ScriptState* script_state,
const String& input, const String& input,
ExceptionState& exception_state) { ExceptionState& exception_state) {
if (!policy_options_.createScript()) if (!policy_options_.createScript()) {
exception_state.ThrowTypeError(
"Policy " + name_ +
"'s TrustedTypePolicyOptions did not specify a 'createScript' member.");
return nullptr; return nullptr;
}
v8::TryCatch try_catch(script_state->GetIsolate()); v8::TryCatch try_catch(script_state->GetIsolate());
String script; String script;
if (!policy_options_.createScript()->Invoke(nullptr, input).To(&script)) { if (!policy_options_.createScript()->Invoke(nullptr, input).To(&script)) {
...@@ -60,8 +68,12 @@ TrustedScriptURL* TrustedTypePolicy::createScriptURL( ...@@ -60,8 +68,12 @@ TrustedScriptURL* TrustedTypePolicy::createScriptURL(
ScriptState* script_state, ScriptState* script_state,
const String& input, const String& input,
ExceptionState& exception_state) { ExceptionState& exception_state) {
if (!policy_options_.createScriptURL()) if (!policy_options_.createScriptURL()) {
exception_state.ThrowTypeError("Policy " + name_ +
"'s TrustedTypePolicyOptions did not "
"specify a 'createScriptURL' member.");
return nullptr; return nullptr;
}
v8::TryCatch try_catch(script_state->GetIsolate()); v8::TryCatch try_catch(script_state->GetIsolate());
String script_url; String script_url;
if (!policy_options_.createScriptURL() if (!policy_options_.createScriptURL()
...@@ -77,8 +89,12 @@ TrustedScriptURL* TrustedTypePolicy::createScriptURL( ...@@ -77,8 +89,12 @@ TrustedScriptURL* TrustedTypePolicy::createScriptURL(
TrustedURL* TrustedTypePolicy::createURL(ScriptState* script_state, TrustedURL* TrustedTypePolicy::createURL(ScriptState* script_state,
const String& input, const String& input,
ExceptionState& exception_state) { ExceptionState& exception_state) {
if (!policy_options_.createURL()) if (!policy_options_.createURL()) {
exception_state.ThrowTypeError(
"Policy " + name_ +
"'s TrustedTypePolicyOptions did not specify a 'createURL' member.");
return nullptr; return nullptr;
}
v8::TryCatch try_catch(script_state->GetIsolate()); v8::TryCatch try_catch(script_state->GetIsolate());
String url; String url;
if (!policy_options_.createURL()->Invoke(nullptr, input).To(&url)) { if (!policy_options_.createURL()->Invoke(nullptr, input).To(&url)) {
......
...@@ -4,28 +4,44 @@ ...@@ -4,28 +4,44 @@
#include "third_party/blink/renderer/core/trustedtypes/trusted_type_policy_factory.h" #include "third_party/blink/renderer/core/trustedtypes/trusted_type_policy_factory.h"
#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/core/trustedtypes/trusted_type_policy.h"
#include "third_party/blink/renderer/platform/bindings/to_v8.h" #include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h" #include "third_party/blink/renderer/platform/runtime_enabled_features.h"
namespace blink { namespace blink {
ScriptPromise TrustedTypePolicyFactory::createPolicy( TrustedTypePolicy* TrustedTypePolicyFactory::createPolicy(
ScriptState* script_state,
const String& policy_name, const String& policy_name,
const TrustedTypePolicyOptions& policy_options) { const TrustedTypePolicyOptions& policy_options,
ExceptionState& exception_state) {
for (const TrustedTypePolicy* p : policies_) {
if (p->name() == policy_name) {
exception_state.ThrowTypeError("Policy with name " + policy_name +
" already exists.");
return nullptr;
}
}
TrustedTypePolicy* policy = TrustedTypePolicy* policy =
TrustedTypePolicy::Create(policy_name, policy_options); TrustedTypePolicy::Create(policy_name, policy_options);
return ScriptPromise::Cast(script_state, ToV8(policy, script_state)); policies_.push_back(policy);
return policy;
} }
TrustedTypePolicyFactory::TrustedTypePolicyFactory(LocalFrame* frame) TrustedTypePolicyFactory::TrustedTypePolicyFactory(LocalFrame* frame)
: DOMWindowClient(frame) {} : DOMWindowClient(frame) {}
Vector<String> TrustedTypePolicyFactory::getPolicyNames() const {
Vector<String> policyNames;
for (const TrustedTypePolicy* p : policies_) {
policyNames.push_back(p->name());
}
return policyNames;
}
void TrustedTypePolicyFactory::Trace(blink::Visitor* visitor) { void TrustedTypePolicyFactory::Trace(blink::Visitor* visitor) {
ScriptWrappable::Trace(visitor); ScriptWrappable::Trace(visitor);
DOMWindowClient::Trace(visitor); DOMWindowClient::Trace(visitor);
visitor->Trace(policies_);
} }
} // namespace blink } // namespace blink
...@@ -9,11 +9,13 @@ ...@@ -9,11 +9,13 @@
#include "third_party/blink/renderer/core/dom/context_lifecycle_observer.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/core/trustedtypes/trusted_type_policy_options.h"
#include "third_party/blink/renderer/platform/bindings/script_wrappable.h" #include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
#include "third_party/blink/renderer/platform/heap/heap_allocator.h"
namespace blink { namespace blink {
class ExceptionState;
class LocalFrame; class LocalFrame;
class ScriptPromise; class TrustedTypePolicy;
class CORE_EXPORT TrustedTypePolicyFactory final : public ScriptWrappable, class CORE_EXPORT TrustedTypePolicyFactory final : public ScriptWrappable,
public DOMWindowClient { public DOMWindowClient {
...@@ -25,14 +27,18 @@ class CORE_EXPORT TrustedTypePolicyFactory final : public ScriptWrappable, ...@@ -25,14 +27,18 @@ class CORE_EXPORT TrustedTypePolicyFactory final : public ScriptWrappable,
return new TrustedTypePolicyFactory(frame); return new TrustedTypePolicyFactory(frame);
} }
static ScriptPromise createPolicy(ScriptState*, TrustedTypePolicy* createPolicy(const String&,
const String&, const TrustedTypePolicyOptions&,
const TrustedTypePolicyOptions&); ExceptionState&);
Vector<String> getPolicyNames() const;
void Trace(blink::Visitor*) override; void Trace(blink::Visitor*) override;
private: private:
explicit TrustedTypePolicyFactory(LocalFrame*); explicit TrustedTypePolicyFactory(LocalFrame*);
HeapVector<Member<TrustedTypePolicy>> policies_;
}; };
} // namespace blink } // namespace blink
......
...@@ -8,5 +8,7 @@ ...@@ -8,5 +8,7 @@
Exposed=(Window), Exposed=(Window),
RuntimeEnabled=TrustedDOMTypes RuntimeEnabled=TrustedDOMTypes
] interface TrustedTypePolicyFactory { ] interface TrustedTypePolicyFactory {
[CallWith=ScriptState] Promise<TrustedTypePolicy> createPolicy(DOMString policyName, TrustedTypePolicyOptions policyOptions); [RaisesException] TrustedTypePolicy createPolicy(DOMString policyName, TrustedTypePolicyOptions policyOptions);
// All the policy object names that have been created
[Affects=Nothing] sequence<DOMString> getPolicyNames();
}; };
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