Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
lukewarlow committed May 29, 2024
1 parent f2cdcb7 commit cb39823
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 16 deletions.
24 changes: 24 additions & 0 deletions trusted-types/eval-csp-tt-default-policy-mutate.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html>
<head>
<script nonce="abc" src="/resources/testharness.js"></script>
<script nonce="abc" src="/resources/testharnessreport.js"></script>
<script nonce="abc" src="support/helper.sub.js"></script>
<meta http-equiv="Content-Security-Policy" content="require-trusted-types-for 'script'">
</head>
<body>
<script>
trustedTypes.createPolicy("default", {createScript: s => s.replace("1", "4")});

test(t => {
assert_throws_js(EvalError, _ => eval('1+1'));
}, "eval of string where default policy mutates value throws.");

test(t => {
assert_throws_js(EvalError, _ => eval?.('1+1'));
}, "indirect eval of string where default policy mutates value throws.");

test(t => {
assert_throws_js(EvalError, _ => new Function('return 1+1'));
}, "Function constructor with string where default policy mutates value throws.");
</script>
6 changes: 3 additions & 3 deletions trusted-types/eval-csp-tt-default-policy.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</head>
<body>
<script>
trustedTypes.createPolicy("default", {createScript: s => s.replace("1", "4")});
trustedTypes.createPolicy("default", {createScript: s => s});
const p = trustedTypes.createPolicy("p", {createScript: s => s});

test(t => {
Expand All @@ -20,7 +20,7 @@
}, "indirect eval of TrustedScript works.");

test(t => {
assert_equals(eval('1+1'), 5); // '1+1' becomes '4+1'.
assert_equals(eval('1+1'), 2);
}, "eval of string works.");

test(t => {
Expand All @@ -35,7 +35,7 @@
}, "Function constructor of TrustedScript works.");

test(t => {
assert_equals(new Function('return 1+1')(), 5);
assert_equals(new Function('return 1+1')(), 2);
}, "Function constructor of string works.");
</script>

20 changes: 10 additions & 10 deletions trusted-types/eval-with-permissive-csp.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,20 @@
assert_equals(s, "Hello a cat string");
}, "new Function with TrustedScript and permissive CSP works.");

trustedTypes.createPolicy("default", { createScript: createScriptJS }, true);
trustedTypes.createPolicy("default", { createScript: (s) => s });
test(t => {
let s = eval('"Hello transformed untrusted string"');
assert_equals(s, "Hello a cat untrusted string");
}, "eval with default policy and permissive CSP still obeys default policy.");
let s = eval('1+1');
assert_equals(s, 2);
}, "eval with plain string with Trusted Types and permissive CSP works with default policy.");

test(t => {
let s = eval?.('"Hello transformed untrusted string"');
assert_equals(s, "Hello a cat untrusted string");
}, "indirect eval with default policy and permissive CSP still obeys default policy.");
let s = eval?.('1+1');
assert_equals(s, 2);
}, "indirect eval with plain string with Trusted Types and permissive CSP works with default policy.");

test(t => {
let s = new Function('return "Hello transformed untrusted string"')();
assert_equals(s, "Hello a cat untrusted string");
}, "new Function with default policy and permissive CSP still obeys default policy.");
let s = new Function('return 1+1')();
assert_equals(s, 2);
}, "new Function with plain string default policy and permissive CSP works with default policy.");
</script>

11 changes: 8 additions & 3 deletions trusted-types/support/WorkerGlobalScope-eval.https.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,15 @@ trustedTypes.createPolicy("default", {
createScript: x => x.replace("2", "5")
});
test(t => {
assert_equals(eval("2"), 5);
assert_equals(eval("4"), 4);
}, "eval(string) with default policy in " + worker_type);
test(t => {
assert_equals(eval?.("2"), 5);
assert_equals(eval?.("4"), 4);
}, "indirect eval(string) with default policy in " + worker_type);

test(t => {
assert_throws_js(EvalError, _ => eval("2"));
}, "eval(string) with default policy mutation in " + worker_type);
test(t => {
assert_throws_js(EvalError, _ => eval?.("2"));
}, "indirect eval(string) with default policy mutation in " + worker_type);
done();

0 comments on commit cb39823

Please sign in to comment.