Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test that createPolicy correctly fires securitypolicyviolation events #47176

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,41 @@
<meta http-equiv="Content-Security-Policy" content="trusted-types 'none'">
<body>
<script>
test(t => {
function promise_violation(t, filter_arg) {
return _ => new Promise((resolve) => {
function handler(e) {
let matches = e.originalPolicy.includes(filter_arg);
if (matches) {
document.removeEventListener("securitypolicyviolation", handler);
lukewarlow marked this conversation as resolved.
Show resolved Hide resolved
e.stopPropagation();
resolve(e);
}
}

document.addEventListener("securitypolicyviolation", handler);
t.add_cleanup(() => {
document.removeEventListener("securitypolicyviolation", handler);
});
});
}

promise_test(t => {
let p = Promise.resolve()
.then(promise_violation(t, "trusted-types 'none'"));

assert_throws_js(TypeError, _ => {
window.trustedTypes.createPolicy('SomeName', { createHTML: s => s } );
});
return p;
}, "Cannot create policy with name 'SomeName' - policy creation throws");

test(t => {
promise_test(t => {
let p = Promise.resolve()
.then(promise_violation(t, "trusted-types 'none'"));

assert_throws_js(TypeError, _ => {
window.trustedTypes.createPolicy('default', { createHTML: s => s } );
});
return p;
}, "Cannot create policy with name 'default' - policy creation throws");
</script>
42 changes: 40 additions & 2 deletions trusted-types/TrustedTypePolicyFactory-createPolicy-cspTests.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,63 @@
<script src="/resources/testharnessreport.js"></script>
<script src="support/helper.sub.js"></script>

<meta http-equiv="Content-Security-Policy" content="trusted-types SomeName JustOneMoreName">
<meta http-equiv="Content-Security-Policy" content="trusted-types SomeName JustOneMoreName AnotherName">
<body>
<script>
// Allowed name test
test(t => {
let policy = window.trustedTypes.createPolicy('SomeName', { createHTML: s => s } );
assert_true(policy instanceof TrustedTypePolicy);
assert_equals(policy.name, 'SomeName');
}, "Allowed-name policy creation works.");

// Another allowed name test
test(t => {
let policy = window.trustedTypes.createPolicy('JustOneMoreName', { createHTML: s => s } );
assert_true(policy instanceof TrustedTypePolicy);
assert_equals(policy.name, 'JustOneMoreName');
}, "Another allowed-name policy creation works.");

function promise_violation(t, filter_arg) {
return _ => new Promise((resolve) => {
function handler(e) {
let matches = e.originalPolicy.includes(filter_arg);
if (matches) {
document.removeEventListener("securitypolicyviolation", handler);
e.stopPropagation();
resolve(e);
}
}

document.addEventListener("securitypolicyviolation", handler);
t.add_cleanup(() => {
document.removeEventListener("securitypolicyviolation", handler);
});
});
}

// Non-allowed names test
test(t => {
promise_test(t => {
let p = Promise.resolve()
.then(promise_violation(t, "trusted-types SomeName JustOneMoreName AnotherName"));

assert_throws_js(TypeError, _ => {
window.trustedTypes.createPolicy('SomeOtherName', { createHTML: s => s } );
});
return p;
}, "Non-allowed name policy creation throws.");

// Duplicate names test
promise_test(t => {
let p = Promise.resolve()
.then(promise_violation(t, "trusted-types SomeName JustOneMoreName AnotherName"));

let policy = window.trustedTypes.createPolicy('AnotherName', { createHTML: s => s } );
assert_equals(policy.name, 'AnotherName');

assert_throws_js(TypeError, _ => {
window.trustedTypes.createPolicy('AnotherName', { createHTML: s => s } );
});
return p;
}, "Duplicate name policy creation throws.");
</script>
23 changes: 0 additions & 23 deletions trusted-types/TrustedTypePolicyFactory-createPolicy-nameTests.html

This file was deleted.