Skip to content

Commit bd675ea

Browse files
fred-wangmoz-wptsync-bot
authored andcommitted
Only clip CSP createPolicy violation sample once.
This avoids double clipping and changes behavior for strings with surrogate pairs, aligning with what we currently do for other CSP violation reports. Differential Revision: https://phabricator.services.mozilla.com/D243400 bugzilla-url: https://bugzilla.mozilla.org/show_bug.cgi?id=1937564 gecko-commit: 6b6543ba75a8cc8151e6dbf34068d7b89fff5042 gecko-reviewers: smaug
1 parent 26376d2 commit bd675ea

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<!DOCTYPE html>
2+
<meta charset="UTF-8">
3+
<script src="/resources/testharness.js" ></script>
4+
<script src="/resources/testharnessreport.js"></script>
5+
<script src="./support/csp-violations.js"></script>
6+
<meta http-equiv="Content-Security-Policy" content="trusted-types 'none'">
7+
<meta http-equiv="Content-Security-Policy" content="require-trusted-types-for 'script'">
8+
<meta http-equiv="Content-Security-Policy" content="connect-src 'none'">
9+
<body>
10+
<script>
11+
const clippedSampleLength = 40;
12+
[
13+
// Strings with ASCII characters.
14+
'A'.repeat(clippedSampleLength - 1),
15+
'B'.repeat(clippedSampleLength),
16+
'C'.repeat(clippedSampleLength + 1),
17+
18+
// Strings with non-ASCII BMP characters.
19+
'Ð'.repeat(clippedSampleLength - 1),
20+
'É'.repeat(clippedSampleLength),
21+
'℉'.repeat(clippedSampleLength + 1),
22+
].forEach(input => {
23+
promise_test(async t => {
24+
let violation = await trusted_type_violation_for(TypeError, _ =>
25+
window.trustedTypes.createPolicy(input, { createHTML: s => s } )
26+
);
27+
assert_equals(violation.originalPolicy, "trusted-types 'none'");
28+
assert_equals(violation.sample, clipSampleIfNeeded(input));
29+
}, `Clipping of violation sample for createPolicy(${input})`);
30+
31+
promise_test(async t => {
32+
let violation = await trusted_type_violation_for(TypeError, _ =>
33+
document.createElement("div").innerHTML = input
34+
);
35+
assert_equals(violation.originalPolicy, "require-trusted-types-for 'script'");
36+
assert_equals(violation.sample, `Element innerHTML|${clipSampleIfNeeded(input)}`);
37+
}, `Clipping of violation sample for Element.innerHTML = "${input}"`);
38+
});
39+
</script>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<!DOCTYPE html>
2+
<meta charset="UTF-8">
3+
<script src="/resources/testharness.js" ></script>
4+
<script src="/resources/testharnessreport.js"></script>
5+
<script src="./support/csp-violations.js"></script>
6+
<meta http-equiv="Content-Security-Policy" content="trusted-types 'none'">
7+
<meta http-equiv="Content-Security-Policy" content="require-trusted-types-for 'script'">
8+
<meta http-equiv="Content-Security-Policy" content="connect-src 'none'">
9+
<body>
10+
<script>
11+
const clippedSampleLength = 40;
12+
const inputAndClippedInput = [
13+
// Strings with surrogate pairs
14+
// We use Firefox's behavior as expectations i.e. clip the source as UTF-16
15+
// string of length 40 ; or of length 41 if the last character is a
16+
// surrogate pair). See https://github.com/w3c/webappsec-csp/issues/704
17+
['𝐆'.repeat(clippedSampleLength / 2), '𝐆'.repeat(clippedSampleLength / 2)],
18+
[`H${'𝐇'.repeat(clippedSampleLength / 2)}`, `H${'𝐇'.repeat(clippedSampleLength / 2)}`],
19+
[`${'𝐈'.repeat(clippedSampleLength / 2)}I`, `${'𝐈'.repeat(clippedSampleLength / 2)}`],
20+
['𝐉'.repeat(clippedSampleLength / 2 + 1), '𝐉'.repeat(clippedSampleLength / 2)],
21+
];
22+
23+
for (const [input, clippedInput] of inputAndClippedInput) {
24+
promise_test(async t => {
25+
let violation = await trusted_type_violation_for(TypeError, _ =>
26+
window.trustedTypes.createPolicy(input, { createHTML: s => s } )
27+
);
28+
assert_equals(violation.originalPolicy, "trusted-types 'none'");
29+
assert_equals(violation.sample, clippedInput);
30+
}, `Clipping of violation sample for createPolicy(${input})`);
31+
32+
promise_test(async t => {
33+
let violation = await trusted_type_violation_for(TypeError, _ =>
34+
document.createElement("div").innerHTML = input
35+
);
36+
assert_equals(violation.originalPolicy, "require-trusted-types-for 'script'");
37+
assert_equals(violation.sample, `Element innerHTML|${clippedInput}`);
38+
}, `Clipping of violation sample for Element.innerHTML = "${input}"`);
39+
}
40+
</script>

0 commit comments

Comments
 (0)