summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/speculation-rules/prerender/resources/csp-script-src.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 17:32:43 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 17:32:43 +0000
commit6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch)
treea68f146d7fa01f0134297619fbe7e33db084e0aa /testing/web-platform/tests/speculation-rules/prerender/resources/csp-script-src.js
parentInitial commit. (diff)
downloadthunderbird-upstream.tar.xz
thunderbird-upstream.zip
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'testing/web-platform/tests/speculation-rules/prerender/resources/csp-script-src.js')
-rw-r--r--testing/web-platform/tests/speculation-rules/prerender/resources/csp-script-src.js57
1 files changed, 57 insertions, 0 deletions
diff --git a/testing/web-platform/tests/speculation-rules/prerender/resources/csp-script-src.js b/testing/web-platform/tests/speculation-rules/prerender/resources/csp-script-src.js
new file mode 100644
index 0000000000..866acaa09b
--- /dev/null
+++ b/testing/web-platform/tests/speculation-rules/prerender/resources/csp-script-src.js
@@ -0,0 +1,57 @@
+const params = new URLSearchParams(location.search);
+
+// Take a key used for storing a test result in the server.
+const key = params.get('key');
+
+// Speculation rules injection is blocked in the csp-script-src 'self' test.
+const block = location.pathname.endsWith('csp-script-src-self.html');
+
+// The main test page (csp-script-src-*.html) in the parent directory) will load
+// this page only with the "key" parameter. This page will then try prerendering
+// itself with the "run-test" parameter. When "run-test" is in the URL we'll
+// actually start the test process and record the results to send back to the
+// main test page. We do this because the main test page cannot navigate itself
+// but it also cannot open a popup to a prerendered browsing context so the
+// prerender triggering and activation must both happen in this popup.
+const run_test = params.has('run-test');
+if (!run_test) {
+ // Generate a new stash key so we can communicate with the prerendered page
+ // about when to close the popup.
+ const done_key = token();
+ const url = new URL(document.URL);
+ url.searchParams.append('run-test', '');
+ url.searchParams.append('done-key', done_key);
+
+ if (block) {
+ // Observe `securitypolicyviolation` event that will be triggered by
+ // startPrerendering().
+ document.addEventListener('securitypolicyviolation', e => {
+ if (e.effectiveDirective != 'script-src' &&
+ e.effectiveDirective != 'script-src-elem') {
+ const message = 'unexpected effective directive: ' + e.effectiveDirective;
+ writeValueToServer(key, message).then(() => { window.close(); });
+ } else {
+ const message = 'blocked by ' + e.effectiveDirective;
+ writeValueToServer(key, message).then(() => { window.close(); });
+ }
+ });
+ }
+
+ startPrerendering(url.toString());
+
+ // Wait until the prerendered page signals us it's ready to close.
+ nextValueFromServer(done_key).then(() => {
+ window.close();
+ });
+} else {
+ if (block) {
+ writeValueToServer(key, 'unexpected prerendering');
+ } else {
+ // Tell the harness the initial document.prerendering value.
+ writeValueToServer(key, document.prerendering);
+
+ // Tell the prerendering initiating page test being finished.
+ const done_key = params.get('done-key');
+ writeValueToServer(done_key, "done");
+ }
+}