-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1942119 [wpt PR 50124] - Rewrite Trusted types tests for CSP viol…
…ations, a=testonly Automatic update from web-platform-tests Rewrite Trusted types tests for CSP violations (#50124) * Rewrite Trusted types tests for CSP violations Currently the listener to "securitypolicyviolation" is added before actually running the statement that triggers violations, so it could be possible that some violations are not caught. This bad pattern is duplicated in several `trusted-types*reporting*` tests. This patch adds a new helper file to properly wrap the listener registration and statement execution in a promise, and reuses it in existing tests. w3c/trusted-types#576 -- wpt-commits: bb5f8351e2b19b1f4cfd16ca891ca638461c1b4b wpt-pr: 50124
- Loading branch information
1 parent
1f873a0
commit a0d8a57
Showing
16 changed files
with
392 additions
and
612 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
testing/web-platform/tests/trusted-types/require-trusted-types-for-report-only.html.headers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
Content-Security-Policy-Report-Only: require-trusted-types-for 'script' | ||
Content-Security-Policy-Report-Only: require-trusted-types-for 'script' | ||
Content-Security-Policy: object-src 'none' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
testing/web-platform/tests/trusted-types/support/csp-violations.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
const cspDirectives = [ | ||
// https://w3c.github.io/trusted-types/dist/spec/#require-trusted-types-for-csp-directive | ||
"require-trusted-types-for", | ||
// https://w3c.github.io/trusted-types/dist/spec/#trusted-types-csp-directive | ||
"trusted-types", | ||
// https://w3c.github.io/webappsec-csp/#script-src | ||
"script-src", | ||
]; | ||
|
||
// A generic helper that runs function fn and return a promise resolving with | ||
// an array of reported violations for trusted type directives and a possible | ||
// exception thrown. | ||
function trusted_type_violations_and_exception_for(fn) { | ||
return new Promise((resolve, reject) => { | ||
// Listen for security policy violations. | ||
let result = { violations: [], exception: null }; | ||
let handler = e => { | ||
if (cspDirectives.includes(e.effectiveDirective)) { | ||
result.violations.push(e); | ||
} else if (e.effectiveDirective === "object-src") { | ||
document.removeEventListener("securitypolicyviolation", handler); | ||
e.stopPropagation(); | ||
resolve(result); | ||
} else { | ||
reject(`Unexpected violation for directive ${e.effectiveDirective}`); | ||
} | ||
} | ||
document.addEventListener("securitypolicyviolation", handler); | ||
|
||
// Run the specified function and record any exception. | ||
try { | ||
fn(); | ||
} catch(e) { | ||
result.exception = e; | ||
} | ||
|
||
// Force an "object-src" violation, to make sure all the previous violations | ||
// have been delivered. This assumes the test file's associated .headers | ||
// file contains Content-Security-Policy: object-src 'none'. | ||
var o = document.createElement('object'); | ||
o.type = "video/mp4"; | ||
o.data = "dummy.webm"; | ||
document.body.appendChild(o); | ||
}); | ||
} | ||
|
||
// Helper function when we expect one violation and exception. | ||
async function trusted_type_violation_for(expectedException, fn) { | ||
let {violations, exception} = | ||
await trusted_type_violations_and_exception_for(fn); | ||
assert_equals(violations.length, 1, "a single violation reported"); | ||
assert_true(exception instanceof expectedException, `${expectedException.prototype} exception reported`); | ||
return violations[0]; | ||
} | ||
|
||
// Helper function when we expect no violation or exception. | ||
async function no_trusted_type_violation_for(fn) { | ||
let {violations, exception} = | ||
await trusted_type_violations_and_exception_for(fn); | ||
assert_equals(violations.length, 0, "no violation reported"); | ||
assert_equals(exception, null, "no exception thrown"); | ||
} | ||
|
||
async function trusted_type_violation_without_exception_for(fn) { | ||
let {violations, exception} = | ||
await trusted_type_violations_and_exception_for(fn); | ||
assert_equals(violations.length, 1, "a single violation reported"); | ||
assert_equals(exception, null, "no exception thrown"); | ||
return violations[0]; | ||
} |
9 changes: 0 additions & 9 deletions
9
testing/web-platform/tests/trusted-types/support/resolve-spv.js
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.