Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
lukewarlow authored May 8, 2024
1 parent f0191b6 commit 7a8b70e
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 1 deletion.
60 changes: 60 additions & 0 deletions trusted-types/Document-write.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,64 @@
document.write(html);
assert_true(document.body.innerText.indexOf(RESULTS.HTML) !== -1);
}, "document.write with html assigned via policy (successful transformation).");

test(t => {
document.body.innerText = '';
let p = createHTML_policy(window, 1);
let html = p.createHTML(INPUTS.HTML);
document.writeln(html);
assert_true(document.body.innerText.indexOf(RESULTS.HTML) !== -1);
}, "document.writeln with html assigned via policy (successful transformation).");

test(t => {
document.body.innerText = '';
let p = createHTML_policy(window, 1);
let a = p.createHTML("abcdef");
let b = p.createHTML("ghijkl");
document.write(a, b);
assert_equals(document.body.innerText, "abcdefghijkl");
}, "document.write(TrustedHTML, TrustedHTML)");

test(t => {
document.body.innerText = '';
let p = createHTML_policy(window, 1);
let a = p.createHTML("abcdef");
let b = p.createHTML("ghijkl");
document.writeln(a, b);
assert_equals(document.body.innerText, "abcdefghijkl");
}, "document.writeln(TrustedHTML, TrustedHTML)");

test(t => {
document.body.innerText = '';
let p = createHTML_policy(window, 1);
let a = p.createHTML("abcdef");
let b = "ghijkl";
document.write(a, b);
assert_equals(document.body.innerText, "abcdefghijkl");
}, "document.write(TrustedHTML, String)");

test(t => {
document.body.innerText = '';
let p = createHTML_policy(window, 1);
let a = p.createHTML("abcdef");
let b = "ghijkl";
document.writeln(a, b);
assert_equals(document.body.innerText, "abcdefghijkl");
}, "document.writeln(TrustedHTML, String)");

test(t => {
document.body.innerText = '';
let a = "abcdef";
let b = "ghijkl";
document.write(a, b);
assert_equals(document.body.innerText, "abcdefghijkl");
}, "document.write(String, String)");

test(t => {
document.body.innerText = '';
let a = "abcdef";
let b = "ghijkl";
document.writeln(a, b);
assert_equals(document.body.innerText, "abcdefghijkl");
}, "document.writeln(String, String)");
</script>
94 changes: 93 additions & 1 deletion trusted-types/block-string-assignment-to-Document-write.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@
assert_equals(document.body.innerText, RESULTS.HTML);
}, "document.write with html assigned via policy (successful URL transformation).");

test(t => {
document.body.innerText = '';
let a = p.createHTML("abcdef");
let b = p.createHTML("ghijkl");
document.write(a,b);
assert_equals(document.body.innerText, "abcdefghijkl");
}, "document.write with multiple trusted arguments.");

// TrustedURL assignments do not throw. (Now for writeln.)
test(t => {
document.body.innerText = '';
Expand All @@ -26,6 +34,14 @@
assert_equals(document.body.innerText, RESULTS.HTML);
}, "document.writeln with html assigned via policy (successful URL transformation).");

test(t => {
document.body.innerText = '';
let a = p.createHTML("abcdef");
let b = p.createHTML("ghijkl");
document.writeln(a,b);
assert_equals(document.body.innerText, "abcdefghijkl");
}, "document.writeln with multiple trusted arguments.");

// String assignments throw.
test(t => {
const old = document.body.innerText;
Expand All @@ -35,6 +51,26 @@
assert_equals(document.body.innerText, old);
}, "`document.write(string)` throws");

test(t => {
const old = document.body.innerText;
assert_throws_js(TypeError, _ => {
let a = "abcdef";
let b = "ghijkl";
document.write(a, b);
});
assert_equals(document.body.innerText, old);
}, "`document.write(string, string)` throws");

test(t => {
const old = document.body.innerText;
assert_throws_js(TypeError, _ => {
let a = "abcdef";
let b = p.createHTML("ghijkl");
document.write(a, b);
});
assert_equals(document.body.innerText, old);
}, "`document.write(string, TrustedHTML)` throws");

// String assignments throw. (Now for writeln.)
test(t => {
const old = document.body.innerText;
Expand All @@ -44,6 +80,26 @@
assert_equals(document.body.innerText, old);
}, "`document.writeln(string)` throws");

test(t => {
const old = document.body.innerText;
assert_throws_js(TypeError, _ => {
let a = "abcdef";
let b = "ghijkl";
document.writeln(a, b);
});
assert_equals(document.body.innerText, old);
}, "`document.writeln(string, string)` throws");

test(t => {
const old = document.body.innerText;
assert_throws_js(TypeError, _ => {
let a = "abcdef";
let b = p.createHTML("ghijkl");
document.writeln(a, b);
});
assert_equals(document.body.innerText, old);
}, "`document.writeln(string, TrustedHTML)` throws");

// Null assignment throws.
test(t => {
const old = document.body.innerText;
Expand All @@ -63,7 +119,11 @@
}, "`document.writeln(null)` throws");

let default_policy = trustedTypes.createPolicy('default',
{ createHTML: createHTMLJS }, true );
{ createHTML: (html) => {
return html.replace("Hi", "Quack")
.replace("transformed", "a duck")
.replace("defghi", "zxcvbn")
} }, true );

// Default policy works.
test(t => {
Expand All @@ -72,10 +132,42 @@
assert_equals(document.body.innerText, RESULTS.HTML);
}, "`document.write(string)` observes default policy");

test(t => {
document.body.innerText = '';
let a = "abcdef";
let b = "ghijkl";
document.write(a, b);
assert_equals(document.body.innerText, "abczxcvbnjkl");
}, "`document.write(string, string)` observes default policy");

test(t => {
document.body.innerText = '';
let a = "abcdef";
let b = p.createHTML("ghijkl");
document.write(a, b);
assert_equals(document.body.innerText, "abczxcvbnjkl");
}, "`document.write(string, TrustedHTML)` observes default policy");

// Default policy works. (Now for writeln.)
test(t => {
document.body.innerText = '';
document.writeln(INPUTS.HTML);
assert_equals(document.body.innerText, RESULTS.HTML);
}, "`document.writeln(string)` observes default policy");

test(t => {
document.body.innerText = '';
let a = "abcdef";
let b = "ghijkl";
document.writeln(a, b);
assert_equals(document.body.innerText, "abczxcvbnjkl");
}, "`document.writeln(string, string)` observes default policy");

test(t => {
document.body.innerText = '';
let a = "abcdef";
let b = p.createHTML("ghijkl");
document.writeln(a, b);
assert_equals(document.body.innerText, "abczxcvbnjkl");
}, "`document.writeln(string, TrustedHTML)` observes default policy");
</script>

0 comments on commit 7a8b70e

Please sign in to comment.