-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for trusted types enforcement on setHTMLUnsafe and parseHTM…
…LUnsafe (#44266)
- Loading branch information
1 parent
6d6d4b1
commit 71b03d2
Showing
3 changed files
with
211 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
trusted-types/block-string-assignment-to-Document-parseHTMLUnsafe.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,48 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta name="author" title="Luke Warlow" href="mailto:lwarlow@igalia.com"> | ||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
<script src="support/helper.sub.js"></script> | ||
|
||
<meta http-equiv="Content-Security-Policy" content="require-trusted-types-for 'script';"> | ||
</head> | ||
<body> | ||
<script> | ||
test(t => { | ||
let p = createHTML_policy(window, 1); | ||
let html = p.createHTML(INPUTS.HTML); | ||
let doc = Document.parseHTMLUnsafe(html); | ||
assert_equals(doc.body.innerText, RESULTS.HTML); | ||
}, "Document.parseHTMLUnsafe assigned via policy (successful HTML transformation)."); | ||
|
||
// String assignments throw. | ||
test(t => { | ||
assert_throws_js(TypeError, _ => { | ||
var doc = Document.parseHTMLUnsafe("Fail"); | ||
}); | ||
}, "`Document.parseHTMLUnsafe(string)` throws."); | ||
|
||
// Null assignment throws. | ||
test(t => { | ||
assert_throws_js(TypeError, _ => { | ||
var doc = Document.parseHTMLUnsafe(null); | ||
}); | ||
}, "'Document.parseHTMLUnsafe(null)' throws"); | ||
|
||
// After default policy creation string assignment implicitly calls createHTML. | ||
test(t => { | ||
let p = window.trustedTypes.createPolicy("default", { createHTML: createHTMLJS }, true); | ||
let doc = Document.parseHTMLUnsafe(INPUTS.HTML, "text/html"); | ||
assert_equals(doc.body.innerText, RESULTS.HTML); | ||
}, "'Document.parseHTMLUnsafe(string)' assigned via default policy (successful HTML transformation)."); | ||
|
||
// After default policy creation null assignment implicitly calls createHTML. | ||
test(t => { | ||
var doc = Document.parseHTMLUnsafe(null, "text/html"); | ||
assert_equals(doc.body.innerText, "null"); | ||
}, "'Document.parseHTMLUnsafe(null)' assigned via default policy does not throw"); | ||
</script> | ||
</body> | ||
</html> |
79 changes: 79 additions & 0 deletions
79
trusted-types/block-string-assignment-to-Element-setHTMLUnsafe.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,79 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta name="author" title="Luke Warlow" href="mailto:lwarlow@igalia.com"> | ||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
<script src="support/helper.sub.js"></script> | ||
|
||
<meta http-equiv="Content-Security-Policy" content="require-trusted-types-for 'script';"> | ||
</head> | ||
<body> | ||
<div id="container"></div> | ||
<script> | ||
var container = document.querySelector('#container') | ||
|
||
// TrustedHTML assignments do not throw. | ||
test(t => { | ||
let p = createHTML_policy(window, 1); | ||
let html = p.createHTML(INPUTS.HTML); | ||
|
||
var d = document.createElement('div'); | ||
document.querySelector('#container').appendChild(d); | ||
d.setHTMLUnsafe(html); | ||
assert_equals(container.innerText, RESULTS.HTML); | ||
|
||
while (container.firstChild) | ||
container.firstChild.remove(); | ||
}, "element.setHTMLUnsafe(html) assigned via policy (successful HTML transformation)."); | ||
|
||
// String assignments throw. | ||
test(t => { | ||
var d = document.createElement('div'); | ||
container.appendChild(d); | ||
assert_throws_js(TypeError, _ => { | ||
d.setHTMLUnsafe("Fail"); | ||
}); | ||
assert_equals(container.innerText, ""); | ||
while (container.firstChild) | ||
container.firstChild.remove(); | ||
}, "`element.setHTMLUnsafe(string)` throws."); | ||
|
||
// Null assignment throws. | ||
test(t => { | ||
var d = document.createElement('div'); | ||
container.appendChild(d); | ||
assert_throws_js(TypeError, _ => { | ||
d.outerHTML = null; | ||
}); | ||
assert_equals(container.innerText, ""); | ||
while (container.firstChild) | ||
container.firstChild.remove(); | ||
}, "`element.setHTMLUnsafe(null)` throws."); | ||
|
||
// After default policy creation string assignment implicitly calls createHTML. | ||
test(t => { | ||
let p = window.trustedTypes.createPolicy("default", { createHTML: createHTMLJS }, true); | ||
|
||
var d = document.createElement('div'); | ||
document.querySelector('#container').appendChild(d); | ||
d.setHTMLUnsafe(INPUTS.HTML); | ||
assert_equals(container.innerText, RESULTS.HTML); | ||
|
||
while (container.firstChild) | ||
container.firstChild.remove(); | ||
}, "`element.setHTMLUnsafe(string)` assigned via default policy (successful HTML transformation)."); | ||
|
||
// After default policy creation null assignment implicitly calls createHTML. | ||
test(t => { | ||
var d = document.createElement('div'); | ||
container.appendChild(d); | ||
d.setHTMLUnsafe(null); | ||
assert_equals(container.innerText, "null"); | ||
|
||
while (container.firstChild) | ||
container.firstChild.remove(); | ||
}, "`element.setHTMLUnsafe(string)` assigned via default policy does not throw"); | ||
</script> | ||
</body> | ||
</html> |
84 changes: 84 additions & 0 deletions
84
trusted-types/block-string-assignment-to-ShadowRoot-setHTMLUnsafe.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,84 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta name="author" title="Luke Warlow" href="mailto:lwarlow@igalia.com"> | ||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
<script src="support/helper.sub.js"></script> | ||
|
||
<meta http-equiv="Content-Security-Policy" content="require-trusted-types-for 'script';"> | ||
</head> | ||
<body> | ||
<div id="container"></div> | ||
<script> | ||
var container = document.querySelector('#container') | ||
|
||
// TrustedHTML assignments do not throw. | ||
test(t => { | ||
let p = createHTML_policy(window, 1); | ||
let html = p.createHTML(INPUTS.HTML); | ||
|
||
let d = document.createElement('div'); | ||
let s = d.attachShadow({mode: 'open'}); | ||
document.querySelector('#container').appendChild(d); | ||
s.setHTMLUnsafe(html); | ||
assert_equals(s.innerHTML, RESULTS.HTML); | ||
|
||
while (container.firstChild) | ||
container.firstChild.remove(); | ||
}, "shadowRoot.setHTMLUnsafe(html) assigned via policy (successful HTML transformation)."); | ||
|
||
// String assignments throw. | ||
test(t => { | ||
let d = document.createElement('div'); | ||
let s = d.attachShadow({mode: 'open'}); | ||
container.appendChild(d); | ||
assert_throws_js(TypeError, _ => { | ||
s.setHTMLUnsafe("Fail"); | ||
}); | ||
assert_equals(s.innerHTML, ""); | ||
while (container.firstChild) | ||
container.firstChild.remove(); | ||
}, "`shadowRoot.setHTMLUnsafe(string)` throws."); | ||
|
||
// Null assignment throws. | ||
test(t => { | ||
let d = document.createElement('div'); | ||
let s = d.attachShadow({mode: 'open'}); | ||
container.appendChild(d); | ||
assert_throws_js(TypeError, _ => { | ||
s.setHTMLUnsafe(null); | ||
}); | ||
assert_equals(s.innerHTML, ""); | ||
while (container.firstChild) | ||
container.firstChild.remove(); | ||
}, "`shadowRoot.setHTMLUnsafe(null)` throws."); | ||
|
||
// After default policy creation string assignment implicitly calls createHTML. | ||
test(t => { | ||
let p = window.trustedTypes.createPolicy("default", { createHTML: createHTMLJS }, true); | ||
|
||
let d = document.createElement('div'); | ||
let s = d.attachShadow({mode: 'open'}); | ||
document.querySelector('#container').appendChild(d); | ||
s.setHTMLUnsafe(INPUTS.HTML); | ||
assert_equals(s.innerHTML, RESULTS.HTML); | ||
|
||
while (container.firstChild) | ||
container.firstChild.remove(); | ||
}, "`shadowRoot.setHTMLUnsafe(string)` assigned via default policy (successful HTML transformation)."); | ||
|
||
// After default policy creation null assignment implicitly calls createHTML. | ||
test(t => { | ||
let d = document.createElement('div'); | ||
let s = d.attachShadow({mode: 'open'}); | ||
container.appendChild(d); | ||
s.setHTMLUnsafe(null); | ||
assert_equals(s.innerHTML, "null"); | ||
|
||
while (container.firstChild) | ||
container.firstChild.remove(); | ||
}, "`shadowRoot.setHTMLUnsafe(string)` assigned via default policy does not throw"); | ||
</script> | ||
</body> | ||
</html> |