Skip to content

Commit

Permalink
Test during load; figured out a way to stop HTML escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
domenic committed Apr 14, 2021
1 parent 1aacb7f commit 991534e
Show file tree
Hide file tree
Showing 14 changed files with 283 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!DOCTYPE html>
<meta charset="utf-8">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/helpers.js"></script>

<body>
<script>
"use strict";
promise_test(async t => {
const sentinelIframe = await setupSentinelIframe(t);
const startingHistoryLength = history.length;

const code = `
window.onload = () => {
const a = document.createElement("a");
a.href = "/common/blank.html?thereplacement";
document.body.append(a);
a.click();
};
`;

const startURL = "resources/code-injector.html?pipe=sub(none)&code=" + encodeURIComponent(code);
const afterReplacementURL = "/common/blank.html?thereplacement";
const iframe = insertIframe(t, startURL);

assert_equals(history.length, startingHistoryLength, "Inserting the under-test iframe must not change history.length");

await waitForLoadAllowingIntermediateLoads(t, iframe, afterReplacementURL);
assert_equals(history.length, startingHistoryLength, "history.length must not change after waiting for the replacement");

await checkSentinelIframe(t, sentinelIframe);
assert_equals(history.length, startingHistoryLength, "history.length must not change after checking the sentinel iframe");
}, "Replace during the load event, triggered by aElement.click()");
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
const startingHistoryLength = history.length;

const code = `
const a = document.createElement(\`a\`);
a.href = \`/common/blank.html?thereplacement\`;
const a = document.createElement("a");
a.href = "/common/blank.html?thereplacement";
document.currentScript.before(a);
a.click();
`;

const startURL = "resources/code-injector.sub.html?code=" + encodeURIComponent(code);
const startURL = "resources/code-injector.html?pipe=sub(none)&code=" + encodeURIComponent(code);
const afterReplacementURL = "/common/blank.html?thereplacement";
const iframe = insertIframe(t, startURL);

Expand All @@ -29,5 +29,5 @@

await checkSentinelIframe(t, sentinelIframe);
assert_equals(history.length, startingHistoryLength, "history.length must not change after checking the sentinel iframe");
}, "Replace before load, trigger by aElement.click()");
}, "Replace before load, triggered by aElement.click()");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<!DOCTYPE html>
<meta charset="utf-8">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/helpers.js"></script>

<body>
<script>
"use strict";
promise_test(async t => {
const sentinelIframe = await setupSentinelIframe(t);
const startingHistoryLength = history.length;

const code = `
window.onload = () => {
const form = document.createElement("form");
form.action = "/common/blank.html";
const input = document.createElement("input");
input.type = "hidden";
input.name = "thereplacement";
form.append(input);
document.body.append(form);
form.submit();
};
`;

const startURL = "resources/code-injector.html?pipe=sub(none)&code=" + encodeURIComponent(code);
const afterReplacementURL = "/common/blank.html?thereplacement=";
const iframe = insertIframe(t, startURL);

assert_equals(history.length, startingHistoryLength, "Inserting the under-test iframe must not change history.length");

await waitForLoadAllowingIntermediateLoads(t, iframe, afterReplacementURL);
assert_equals(history.length, startingHistoryLength, "history.length must not change after waiting for the replacement");

await checkSentinelIframe(t, sentinelIframe);
assert_equals(history.length, startingHistoryLength, "history.length must not change after checking the sentinel iframe");
}, "Replace during the load event, triggered by formElement.submit()");
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@
const startingHistoryLength = history.length;

const code = `
const form = document.createElement(\`form\`);
form.action = \`/common/blank.html\`;
const form = document.createElement("form");
form.action = "/common/blank.html";
const input = document.createElement(\`input\`);
input.type = \`hidden\`;
input.name = \`thereplacement\`;
const input = document.createElement("input");
input.type = "hidden";
input.name = "thereplacement";
form.append(input);
document.currentScript.before(form);
form.submit();
`;

const startURL = "resources/code-injector.sub.html?code=" + encodeURIComponent(code);
const startURL = "resources/code-injector.html?pipe=sub(none)&code=" + encodeURIComponent(code);
const afterReplacementURL = "/common/blank.html?thereplacement=";
const iframe = insertIframe(t, startURL);

Expand All @@ -35,5 +35,5 @@

await checkSentinelIframe(t, sentinelIframe);
assert_equals(history.length, startingHistoryLength, "history.length must not change after checking the sentinel iframe");
}, "Replace before load, trigger by formElement.submit()");
}, "Replace before load, triggered by formElement.submit()");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<meta charset="utf-8">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/helpers.js"></script>

<body>
<script>
"use strict";
promise_test(async t => {
const sentinelIframe = await setupSentinelIframe(t);
const startingHistoryLength = history.length;

const code = `
window.onload = () => {
history.pushState(null, null, "/common/blank.html?thereplacement");
};
`;

const startURL = "resources/code-injector.html?pipe=sub(none)&code=" + encodeURIComponent(code);
const afterReplacementURL = "/common/blank.html?thereplacement";
const iframe = insertIframe(t, startURL);
assert_equals(history.length, startingHistoryLength, "Inserting the under-test iframe must not change history.length");

await waitForLoadAllowingIntermediateLoads(t, iframe, afterReplacementURL);
assert_equals(history.length, startingHistoryLength, "history.length must not change after waiting for the replacement");

await checkSentinelIframe(t, sentinelIframe);
assert_equals(history.length, startingHistoryLength, "history.length must not change after checking the sentinel iframe");
}, "Replace during the load event, triggered by history.pushState()");
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
const sentinelIframe = await setupSentinelIframe(t);
const startingHistoryLength = history.length;

const startURL = "resources/code-injector.sub.html?code=" + encodeURIComponent("history.pushState(null, null, `/common/blank.html?thereplacement`);");
const startURL = "resources/code-injector.html?pipe=sub(none)&code=" + encodeURIComponent("history.pushState(null, null, `/common/blank.html?thereplacement`);");
const afterReplacementURL = "/common/blank.html?thereplacement";
const iframe = insertIframe(t, startURL);
assert_equals(history.length, startingHistoryLength, "Inserting the under-test iframe must not change history.length");
Expand All @@ -21,5 +21,5 @@

await checkSentinelIframe(t, sentinelIframe);
assert_equals(history.length, startingHistoryLength, "history.length must not change after checking the sentinel iframe");
}, "Replace before load, trigger by history.pushState()");
}, "Replace before load, triggered by history.pushState()");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<meta charset="utf-8">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/helpers.js"></script>

<body>
<script>
"use strict";
promise_test(async t => {
const sentinelIframe = await setupSentinelIframe(t);
const startingHistoryLength = history.length;

const code = `
window.onload = () => {
location.assign("/common/blank.html?thereplacement");
};
`;

const startURL = "resources/code-injector.html?pipe=sub(none)&code=" + encodeURIComponent(code);
const afterReplacementURL = "/common/blank.html?thereplacement";
const iframe = insertIframe(t, startURL);
assert_equals(history.length, startingHistoryLength, "Inserting the under-test iframe must not change history.length");

await waitForLoadAllowingIntermediateLoads(t, iframe, afterReplacementURL);
assert_equals(history.length, startingHistoryLength, "history.length must not change after waiting for the replacement");

await checkSentinelIframe(t, sentinelIframe);
assert_equals(history.length, startingHistoryLength, "history.length must not change after checking the sentinel iframe");
}, "Replace during the load event, triggered by location.assign()");
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
const sentinelIframe = await setupSentinelIframe(t);
const startingHistoryLength = history.length;

const startURL = "resources/code-injector.sub.html?code=" + encodeURIComponent("location.assign(`/common/blank.html?thereplacement`);");
const startURL = "resources/code-injector.html?pipe=sub(none)&code=" + encodeURIComponent("location.assign(`/common/blank.html?thereplacement`);");
const afterReplacementURL = "/common/blank.html?thereplacement";
const iframe = insertIframe(t, startURL);
assert_equals(history.length, startingHistoryLength, "Inserting the under-test iframe must not change history.length");
Expand All @@ -21,5 +21,5 @@

await checkSentinelIframe(t, sentinelIframe);
assert_equals(history.length, startingHistoryLength, "history.length must not change after checking the sentinel iframe");
}, "Replace before load, trigger by location.assign()");
}, "Replace before load, triggered by location.assign()");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>Replace during the load event, triggered by location setters</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/helpers.js"></script>

<body>
<script>
"use strict";

promise_test(async t => {
const sentinelIframe = await setupSentinelIframe(t);
const startingHistoryLength = history.length;

const code = `
window.onload = () => {
location.href = "/common/blank.html?thereplacement";
};
`;

const startURL = "resources/code-injector.html?pipe=sub(none)&code=" + encodeURIComponent(code);
const afterReplacementURL = "/common/blank.html?thereplacement";
const iframe = insertIframe(t, startURL);
assert_equals(history.length, startingHistoryLength, "Inserting the under-test iframe must not change history.length");

await waitForLoadAllowingIntermediateLoads(t, iframe, afterReplacementURL);
assert_equals(history.length, startingHistoryLength, "history.length must not change after waiting for the replacement");

await checkSentinelIframe(t, sentinelIframe);
assert_equals(history.length, startingHistoryLength, "history.length must not change after checking the sentinel iframe");
}, "href");

promise_test(async t => {
const sentinelIframe = await setupSentinelIframe(t);
const startingHistoryLength = history.length;

const code = `
window.onload = () => {
location.search = "thereplacement";
};
`;

const startURL = "resources/code-injector.html?pipe=sub(none)&code=" + encodeURIComponent(code);
const afterReplacementURL = "resources/code-injector.html?thereplacement";
const iframe = insertIframe(t, startURL);
assert_equals(history.length, startingHistoryLength, "Inserting the under-test iframe must not change history.length");

await waitForLoadAllowingIntermediateLoads(t, iframe, afterReplacementURL);
assert_equals(history.length, startingHistoryLength, "history.length must not change after waiting for the replacement");

await checkSentinelIframe(t, sentinelIframe);
}, "search");

promise_test(async t => {
const sentinelIframe = await setupSentinelIframe(t);
const startingHistoryLength = history.length;

const code = `
window.onload = () => {
location.hash = "thereplacement";
};
`;

const startURL = "resources/code-injector.html?pipe=sub(none)&code=" + encodeURIComponent(code);
const afterReplacementURL = startURL + "#thereplacement";
const iframe = insertIframe(t, startURL);
assert_equals(history.length, startingHistoryLength, "Inserting the under-test iframe must not change history.length");

await waitForLoadAllowingIntermediateLoads(t, iframe, afterReplacementURL);
assert_equals(history.length, startingHistoryLength, "history.length must not change after waiting for the replacement");

await checkSentinelIframe(t, sentinelIframe);
assert_equals(history.length, startingHistoryLength, "history.length must not change after checking the sentinel iframe");
}, "hash");
</script>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>Replace before load, trigger by location setters</title>
<title>Replace before load, triggered by location setters</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/helpers.js"></script>
Expand All @@ -13,7 +13,7 @@
const sentinelIframe = await setupSentinelIframe(t);
const startingHistoryLength = history.length;

const startURL = "resources/code-injector.sub.html?code=" + encodeURIComponent("location.href = `/common/blank.html?thereplacement`;");
const startURL = "resources/code-injector.html?pipe=sub(none)&code=" + encodeURIComponent("location.href = `/common/blank.html?thereplacement`;");
const afterReplacementURL = "/common/blank.html?thereplacement";
const iframe = insertIframe(t, startURL);
assert_equals(history.length, startingHistoryLength, "Inserting the under-test iframe must not change history.length");
Expand All @@ -29,8 +29,8 @@
const sentinelIframe = await setupSentinelIframe(t);
const startingHistoryLength = history.length;

const startURL = "resources/code-injector.sub.html?code=" + encodeURIComponent("location.search = `thereplacement`;");
const afterReplacementURL = "resources/code-injector.sub.html?thereplacement";
const startURL = "resources/code-injector.html?pipe=sub(none)&code=" + encodeURIComponent("location.search = `thereplacement`;");
const afterReplacementURL = "resources/code-injector.html?thereplacement";
const iframe = insertIframe(t, startURL);
assert_equals(history.length, startingHistoryLength, "Inserting the under-test iframe must not change history.length");

Expand All @@ -44,7 +44,7 @@
const sentinelIframe = await setupSentinelIframe(t);
const startingHistoryLength = history.length;

const startURL = "resources/code-injector.sub.html?code=" + encodeURIComponent("location.hash = `thereplacement`;");
const startURL = "resources/code-injector.html?pipe=sub(none)&code=" + encodeURIComponent("location.hash = `thereplacement`;");
const afterReplacementURL = startURL + "#thereplacement";
const iframe = insertIframe(t, startURL);
assert_equals(history.length, startingHistoryLength, "Inserting the under-test iframe must not change history.length");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ window.waitForLoad = (t, iframe, urlRelativeToThisDocument) => {
});
};

window.waitForLoadAllowingIntermediateLoads = (t, iframe, urlRelativeToThisDocument) => {
return new Promise(resolve => {
const handler = t.step_func(() => {
if (iframe.contentWindow.location.href === (new URL(urlRelativeToThisDocument, location.href)).href) {
// Wait a bit longer to ensure all history stuff has settled, e.g. the document is "completely loaded"
// (which happens from a queued task).
setTimeout(resolve, 0);
iframe.removeEventListener("load", handler);
}
});

iframe.addEventListener("load", handler);
});
};

window.setupSentinelIframe = async (t) => {
// If this iframe gets navigated by history.back(), then the iframe under test did not, so we did a replace.
const sentinelIframe = document.createElement("iframe");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!DOCTYPE html>
<meta charset="utf-8">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/helpers.js"></script>

<body>
<script>
"use strict";
promise_test(async t => {
const sentinelIframe = await setupSentinelIframe(t);
const startingHistoryLength = history.length;

const code = `
window.onload = () => {
window.open("/common/blank.html?thereplacement", "_self");
};
`;

const startURL = "resources/code-injector.html?pipe=sub(none)&code=" + encodeURIComponent(code);
const afterReplacementURL = "/common/blank.html?thereplacement";
const iframe = insertIframe(t, startURL);

assert_equals(history.length, startingHistoryLength, "Inserting the under-test iframe must not change history.length");

await waitForLoadAllowingIntermediateLoads(t, iframe, afterReplacementURL);
assert_equals(history.length, startingHistoryLength, "history.length must not change after waiting for the replacement");

await checkSentinelIframe(t, sentinelIframe);
assert_equals(history.length, startingHistoryLength, "history.length must not change after checking the sentinel iframe");
}, "Replace during the load event, triggered by window.open()");
</script>
Loading

0 comments on commit 991534e

Please sign in to comment.