forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re-implement Trusted Types script enforcement using booleans
https://bugs.webkit.org/show_bug.cgi?id=276426 Reviewed by NOBODY (OOPS!). Scripts now have two boolean flags rather than storing a duplicate of their contents. Spec PR: w3c/trusted-types#533 * LayoutTests/imported/w3c/web-platform-tests/trusted-types/HTMLScriptElement-innerText-expected.txt: Added. * LayoutTests/imported/w3c/web-platform-tests/trusted-types/HTMLScriptElement-innerText.html: Added. * LayoutTests/imported/w3c/web-platform-tests/trusted-types/HTMLScriptElement-midparse-appendChild-expected.txt: Added. * LayoutTests/imported/w3c/web-platform-tests/trusted-types/HTMLScriptElement-midparse-appendChild.html: Added. * LayoutTests/imported/w3c/web-platform-tests/trusted-types/HTMLScriptElement-midparse-innerHTML-expected.txt: Added. * LayoutTests/imported/w3c/web-platform-tests/trusted-types/HTMLScriptElement-midparse-innerHTML.html: Added. * LayoutTests/imported/w3c/web-platform-tests/trusted-types/HTMLScriptElement-midparse-nodeValue-expected.txt: Added. * LayoutTests/imported/w3c/web-platform-tests/trusted-types/HTMLScriptElement-midparse-nodeValue.html: Added. * LayoutTests/imported/w3c/web-platform-tests/trusted-types/HTMLScriptElement-src-expected.txt: Renamed from LayoutTests/imported/w3c/web-platform-tests/trusted-types/HTMLScriptElement-internal-slot-expected.txt. * LayoutTests/imported/w3c/web-platform-tests/trusted-types/HTMLScriptElement-src.html: Renamed from LayoutTests/imported/w3c/web-platform-tests/trusted-types/HTMLScriptElement-internal-slot.html. * LayoutTests/imported/w3c/web-platform-tests/trusted-types/HTMLScriptElement-text-expected.txt: Added. * LayoutTests/imported/w3c/web-platform-tests/trusted-types/HTMLScriptElement-text.html: Added. * LayoutTests/imported/w3c/web-platform-tests/trusted-types/HTMLScriptElement-textContent-expected.txt: Added. * LayoutTests/imported/w3c/web-platform-tests/trusted-types/HTMLScriptElement-textContent.html: Added. * LayoutTests/imported/w3c/web-platform-tests/trusted-types/block-Node-multiple-arguments-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/trusted-types/block-text-node-insertion-into-script-element-expected.txt: * Source/WebCore/dom/CharacterData.cpp: (WebCore::canUseSetDataOptimization): * Source/WebCore/dom/ScriptElement.cpp: (WebCore::ScriptElement::childrenChanged): (WebCore::ScriptElement::prepareScript): (WebCore::ScriptElement::finishParsingChildren): Deleted. (WebCore::ScriptElement::setTrustedScriptText): Deleted. * Source/WebCore/dom/ScriptElement.h: (WebCore::ScriptElement::setChangedByTrustedSink): * Source/WebCore/html/HTMLScriptElement.cpp: (WebCore::HTMLScriptElement::setTextContent): (WebCore::HTMLScriptElement::setInnerText): (WebCore::HTMLScriptElement::finishParsingChildren): Deleted. * Source/WebCore/html/HTMLScriptElement.h: * Source/WebCore/svg/SVGScriptElement.cpp: (WebCore::SVGScriptElement::finishParsingChildren): Deleted. * Source/WebCore/svg/SVGScriptElement.h:
- Loading branch information
1 parent
b292ae8
commit b5b0177
Showing
23 changed files
with
370 additions
and
121 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
...ts/imported/w3c/web-platform-tests/trusted-types/HTMLScriptElement-innerText-expected.txt
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,6 @@ | ||
|
||
PASS Script set via .innerText executes on a connected HTMLScriptElement. | ||
PASS Script set via .innerText executes on an unconnected HTMLScriptElement. | ||
FAIL Multi-line script set via .innerText executes on a connected HTMLScriptElement. assert_equals: expected (number) 1 but got (undefined) undefined | ||
FAIL Multi-line script set via .innerText executes on an unconnected HTMLScriptElement. assert_equals: expected (number) 1 but got (undefined) undefined | ||
|
49 changes: 49 additions & 0 deletions
49
LayoutTests/imported/w3c/web-platform-tests/trusted-types/HTMLScriptElement-innerText.html
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,49 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
<meta http-equiv="Content-Security-Policy" content="require-trusted-types-for 'script'"> | ||
</head> | ||
<body> | ||
<script> | ||
const policy = trustedTypes.createPolicy("testpolicy", { | ||
createScript: x => x}); | ||
promise_test(t => { | ||
const s = document.createElement("script"); | ||
document.body.appendChild(s); | ||
s.innerText = policy.createScript("window.postMessage('hello');"); | ||
return new Promise(resolve => { | ||
window.addEventListener("message", e => { | ||
if (e.data == "hello") resolve(); | ||
}); | ||
}); | ||
}, "Script set via .innerText executes on a connected HTMLScriptElement."); | ||
promise_test(t => { | ||
const s = document.createElement("script"); | ||
s.innerText= policy.createScript("window.postMessage('world');"); | ||
document.body.appendChild(s); | ||
return new Promise(resolve => { | ||
window.addEventListener("message", e => { | ||
if (e.data == "world") resolve(); | ||
}); | ||
}); | ||
}, "Script set via .innerText executes on an unconnected HTMLScriptElement."); | ||
|
||
test(t => { | ||
const s = document.createElement("script"); | ||
document.body.appendChild(s); | ||
s.innerText = policy.createScript("const a = 1;\nwindow.a=a;"); | ||
assert_equals(window.a, 1); | ||
}, "Multi-line script set via .innerText executes on a connected HTMLScriptElement."); | ||
|
||
test(t => { | ||
const s = document.createElement("script"); | ||
document.body.appendChild(s); | ||
s.innerText = policy.createScript("const b = 1;\nwindow.b=b;"); | ||
assert_equals(window.b, 1); | ||
}, "Multi-line script set via .innerText executes on an unconnected HTMLScriptElement."); | ||
|
||
</script> | ||
</body> | ||
</html> |
4 changes: 4 additions & 0 deletions
4
.../w3c/web-platform-tests/trusted-types/HTMLScriptElement-midparse-appendChild-expected.txt
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,4 @@ | ||
CONSOLE MESSAGE: This requires a TrustedScript value else it violates the following Content Security Policy directive: "require-trusted-types-for 'script'" | ||
|
||
PASS Test TT application when manipulating <script> elements with appendChild during loading. | ||
|
73 changes: 73 additions & 0 deletions
73
...imported/w3c/web-platform-tests/trusted-types/HTMLScriptElement-midparse-appendChild.html
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,73 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
<meta http-equiv="Content-Security-Policy" content="require-trusted-types-for 'script'"> | ||
</head> | ||
<body> | ||
<script> | ||
// This policy allows document.write to work as that's not relevant to what's actually being tested here. | ||
trustedTypes.createPolicy('default', {createHTML: s => s}); | ||
promise_test(t => { | ||
document.write(`<script>window.postMessage("first script element executed", "*");`); | ||
var manipulator = _ => { | ||
let script = document.body.getElementsByTagName("script")[1]; | ||
if (script) { | ||
script.appendChild(document.createTextNode('/*byapi*/')); | ||
document.write('<\/script><script>window.parent.postMessage("second script element executed", "*");<\/script>'); | ||
} else { | ||
t.step_timeout(manipulator, 50); | ||
} | ||
} | ||
manipulator(); | ||
|
||
// Now we'll wait for the postMessages to arrive. We expect the iframe's | ||
// first message to be blocked by Trusted Types, since the manipulator | ||
// above should have manipulated it (while loading). The second one should | ||
// pass. | ||
return new Promise((resolve, reject) => { | ||
window.addEventListener("message", e => { | ||
if (e.data.includes("first")) { | ||
reject("First message should have been blocked: " + e.data); | ||
} else if (e.data.includes("second")) { | ||
resolve(); | ||
} else { | ||
reject("Unknown message: " + e.data); | ||
} | ||
}); | ||
}); | ||
}, "Test TT application when manipulating <script> elements with appendChild during loading."); | ||
|
||
// promise_test(t => { | ||
// document.write(`<script>window.postMessage("first script element executed", "*");`); | ||
// var manipulator = _ => { | ||
// let script = document.body.getElementsByTagName("script")[1]; | ||
// if (script) { | ||
// script.innerHTML = script.innerHTML; | ||
// document.write('<\/script><script>window.parent.postMessage("second script element executed", "*");<\/script>'); | ||
// } else { | ||
// t.step_timeout(manipulator, 50); | ||
// } | ||
// } | ||
// manipulator(); | ||
// | ||
// // Now we'll wait for the postMessages to arrive. We expect the iframe's | ||
// // first message to be blocked by Trusted Types, since the manipulator | ||
// // above should have manipulated it (while loading). The second one should | ||
// // pass. | ||
// return new Promise((resolve, reject) => { | ||
// window.addEventListener("message", e => { | ||
// if (e.data.includes("first")) { | ||
// reject("First message should have been blocked: " + e.data); | ||
// } else if (e.data.includes("second")) { | ||
// resolve(); | ||
// } else { | ||
// reject("Unknown message: " + e.data); | ||
// } | ||
// }); | ||
// }); | ||
// }, "Test TT application when manipulating <script> elements with .innerHTML during loading."); | ||
</script> | ||
</body> | ||
</html> |
4 changes: 4 additions & 0 deletions
4
...ed/w3c/web-platform-tests/trusted-types/HTMLScriptElement-midparse-innerHTML-expected.txt
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,4 @@ | ||
CONSOLE MESSAGE: This requires a TrustedScript value else it violates the following Content Security Policy directive: "require-trusted-types-for 'script'" | ||
|
||
PASS Test TT application when manipulating <script> elements with .innerHTML during loading. | ||
|
42 changes: 42 additions & 0 deletions
42
...s/imported/w3c/web-platform-tests/trusted-types/HTMLScriptElement-midparse-innerHTML.html
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,42 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
<meta http-equiv="Content-Security-Policy" content="require-trusted-types-for 'script'"> | ||
</head> | ||
<body> | ||
<script> | ||
trustedTypes.createPolicy('default', {createHTML: s => s}); | ||
promise_test(t => { | ||
document.write(`<script>window.postMessage("first script element executed", "*");`); | ||
var manipulator = _ => { | ||
let script = document.body.getElementsByTagName("script")[1]; | ||
if (script) { | ||
script.innerHTML = script.innerHTML + '/*byapi*/'; | ||
document.write('<\/script><script>window.parent.postMessage("second script element executed", "*");<\/script>'); | ||
} else { | ||
t.step_timeout(manipulator, 50); | ||
} | ||
} | ||
manipulator(); | ||
|
||
// Now we'll wait for the postMessages to arrive. We expect the iframe's | ||
// first message to be blocked by Trusted Types, since the manipulator | ||
// above should have manipulated it (while loading). The second one should | ||
// pass. | ||
return new Promise((resolve, reject) => { | ||
window.addEventListener("message", e => { | ||
if (e.data.includes("first")) { | ||
reject("First message should have been blocked: " + e.data); | ||
} else if (e.data.includes("second")) { | ||
resolve(); | ||
} else { | ||
reject("Unknown message: " + e.data); | ||
} | ||
}); | ||
}); | ||
}, "Test TT application when manipulating <script> elements with .innerHTML during loading."); | ||
</script> | ||
</body> | ||
</html> |
4 changes: 4 additions & 0 deletions
4
...ed/w3c/web-platform-tests/trusted-types/HTMLScriptElement-midparse-nodeValue-expected.txt
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,4 @@ | ||
CONSOLE MESSAGE: This requires a TrustedScript value else it violates the following Content Security Policy directive: "require-trusted-types-for 'script'" | ||
|
||
PASS Test TT application when manipulating <script> elements with .innerHTML during loading. | ||
|
42 changes: 42 additions & 0 deletions
42
...s/imported/w3c/web-platform-tests/trusted-types/HTMLScriptElement-midparse-nodeValue.html
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,42 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
<meta http-equiv="Content-Security-Policy" content="require-trusted-types-for 'script'"> | ||
</head> | ||
<body> | ||
<script> | ||
trustedTypes.createPolicy('default', {createHTML: s => s}); | ||
promise_test(t => { | ||
document.write(`<script>window.postMessage("first script element executed", "*");`); | ||
var manipulator = _ => { | ||
let script = document.body.getElementsByTagName("script")[1]; | ||
if (script) { | ||
script.firstChild.nodeValue = script.firstChild.nodeValue; | ||
document.write('<\/script><script>window.parent.postMessage("second script element executed", "*");<\/script>'); | ||
} else { | ||
t.step_timeout(manipulator, 50); | ||
} | ||
} | ||
manipulator(); | ||
|
||
// Now we'll wait for the postMessages to arrive. We expect the iframe's | ||
// first message to be blocked by Trusted Types, since the manipulator | ||
// above should have manipulated it (while loading). The second one should | ||
// pass. | ||
return new Promise((resolve, reject) => { | ||
window.addEventListener("message", e => { | ||
if (e.data.includes("first")) { | ||
reject("First message should have been blocked: " + e.data); | ||
} else if (e.data.includes("second")) { | ||
resolve(); | ||
} else { | ||
reject("Unknown message: " + e.data); | ||
} | ||
}); | ||
}); | ||
}, "Test TT application when manipulating <script> elements with .innerHTML during loading."); | ||
</script> | ||
</body> | ||
</html> |
6 changes: 0 additions & 6 deletions
6
...LScriptElement-internal-slot-expected.txt → ...-types/HTMLScriptElement-src-expected.txt
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
4 changes: 4 additions & 0 deletions
4
...utTests/imported/w3c/web-platform-tests/trusted-types/HTMLScriptElement-text-expected.txt
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,4 @@ | ||
|
||
PASS Script set via .text executes on a connected HTMLScriptElement. | ||
PASS Script set via .text executes on an unconnected HTMLScriptElement. | ||
|
35 changes: 35 additions & 0 deletions
35
LayoutTests/imported/w3c/web-platform-tests/trusted-types/HTMLScriptElement-text.html
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,35 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
<meta http-equiv="Content-Security-Policy" content="require-trusted-types-for 'script'"> | ||
</head> | ||
<body> | ||
<script> | ||
const policy = trustedTypes.createPolicy("testpolicy", { | ||
createScript: x => x}); | ||
promise_test(t => { | ||
const s = document.createElement("script"); | ||
document.body.appendChild(s); | ||
s.text = policy.createScript("window.postMessage('hello');"); | ||
return new Promise(resolve => { | ||
window.addEventListener("message", e => { | ||
if (e.data == "hello") resolve(); | ||
}); | ||
}); | ||
}, "Script set via .text executes on a connected HTMLScriptElement."); | ||
promise_test(t => { | ||
const s = document.createElement("script"); | ||
s.text = policy.createScript("window.postMessage('world');"); | ||
document.body.appendChild(s); | ||
return new Promise(resolve => { | ||
window.addEventListener("message", e => { | ||
if (e.data == "world") resolve(); | ||
}); | ||
}); | ||
}, "Script set via .text executes on an unconnected HTMLScriptElement."); | ||
|
||
</script> | ||
</body> | ||
</html> |
8 changes: 8 additions & 0 deletions
8
.../imported/w3c/web-platform-tests/trusted-types/HTMLScriptElement-textContent-expected.txt
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,8 @@ | ||
CONSOLE MESSAGE: This requires a TrustedScript value else it violates the following Content Security Policy directive: "require-trusted-types-for 'script'" | ||
CONSOLE MESSAGE: This requires a TrustedScript value else it violates the following Content Security Policy directive: "require-trusted-types-for 'script'" | ||
|
||
PASS Script set via .textContent executes on a connected HTMLScriptElement. | ||
PASS Script set via .textContent executes on an unconnected HTMLScriptElement. | ||
PASS Script set via .textContent and then set via innerHTML doesn't execute. | ||
PASS Script set via .textContent and then set via nodeValue doesn't execute. | ||
|
Oops, something went wrong.