Skip to content

Truly independent web browser

LibWeb: Load a COOP page that sandboxes itself with its own CSP

sideshowbarkercdaca44013f4
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cpp
index 05696f3dfb5..f845a57bb87 100644
--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp
+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp
@@ -2412,7 +2412,12 @@ static void perform_navigation_params_fetch(JS::Realm& realm, GC::Ref<Navigation
// 3. If finalSandboxFlags is not empty and responseCOOP's value is not "unsafe-none", then set response to an appropriate network error and break.
// NOTE: This results in a network error as one cannot simultaneously provide a clean slate to a response
// using opener policy and sandbox the result of navigating to that response.
- if (state_holder->final_sandbox_flags != SandboxingFlagSet {} && state_holder->response_coop.value != OpenerPolicyValue::UnsafeNone) {
+ // AD-HOC: The spec checks finalSandboxFlags, which include the sandboxing flags of the response's own CSP.
+ // But a response that sandboxes itself isn't one the navigation sandboxed, and its opener policy
+ // still gives it a clean slate, so check only the sandboxing flags the navigation brings. A page
+ // served with both a CSP sandbox and an opener policy then loads rather than failing.
+ // https://github.com/web-platform-tests/wpt/blob/master/html/cross-origin-opener-policy/coop-navigate-same-origin-csp-sandbox.html
+ if (state_holder->target_snapshot_params.sandboxing_flags != SandboxingFlagSet {} && state_holder->response_coop.value != OpenerPolicyValue::UnsafeNone) {
// AD-HOC: Stop the fetch, as nothing will consume the response this replaces.
state_holder->fetch_controller->stop_fetch();
state_holder->response = Fetch::Infrastructure::ResponseView::network_error("Opener policy on a sandboxed navigation response"_string);
diff --git a/Tests/LibWeb/Text/expected/HTML/opener-policy-with-a-csp-sandbox.txt b/Tests/LibWeb/Text/expected/HTML/opener-policy-with-a-csp-sandbox.txt
new file mode 100644
index 00000000000..be5f90015fc
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/opener-policy-with-a-csp-sandbox.txt
@@ -0,0 +1,3 @@
+page with its own CSP sandbox loaded: true
+page opened from a sandboxed iframe was requested: true
+page opened from a sandboxed iframe loaded: false
diff --git a/Tests/LibWeb/Text/input/HTML/opener-policy-with-a-csp-sandbox.html b/Tests/LibWeb/Text/input/HTML/opener-policy-with-a-csp-sandbox.html
new file mode 100644
index 00000000000..96069601572
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/opener-policy-with-a-csp-sandbox.html
@@ -0,0 +1,52 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ // An opener policy can't give a clean slate to a document that a navigation sandboxes, so that navigation fails.
+ // But a response's own CSP sandbox isn't the navigation's, and a page served with both still loads.
+ promiseTest(async () => {
+ const httpServer = httpTestServer();
+
+ const pageLoading = async (name, extraHeaders) => {
+ const markerUrl = await httpServer.createEcho("GET", `/opener-policy-csp-sandbox-${name}-loaded`, {
+ status: 200,
+ headers: { "Content-Type": "text/plain", "Access-Control-Allow-Origin": "*" },
+ body: "recorded",
+ });
+ const pageUrl = await httpServer.createEcho("GET", `/opener-policy-csp-sandbox-${name}`, {
+ status: 200,
+ headers: { "Content-Type": "text/html", "Cross-Origin-Opener-Policy": "same-origin", ...extraHeaders },
+ body: `<!DOCTYPE html><script>
+ fetch(${JSON.stringify(markerUrl)}).finally(() => window.close());
+ <\/script>`,
+ });
+ const requestRecorded = async (url, attempts) => {
+ for (let attempt = 0; attempt < attempts; ++attempt) {
+ try {
+ if ((await fetch(httpServer.recordedRequestHeadersURL(url))).ok)
+ return true;
+ } catch {}
+ await timeout(25);
+ }
+ return false;
+ };
+ return {
+ pageUrl,
+ requested: attempts => requestRecorded(pageUrl, attempts),
+ loaded: attempts => requestRecorded(markerUrl, attempts),
+ };
+ };
+
+ const selfSandboxed = await pageLoading("self-sandboxed", { "Content-Security-Policy": "sandbox allow-scripts" });
+ window.open(selfSandboxed.pageUrl, "_blank", "noopener");
+ println(`page with its own CSP sandbox loaded: ${await selfSandboxed.loaded(400)}`);
+
+ // A popup from a sandboxed iframe inherits its sandboxing flags.
+ const fromSandbox = await pageLoading("from-sandboxed-iframe", {});
+ const iframe = document.createElement("iframe");
+ iframe.sandbox = "allow-scripts allow-popups";
+ iframe.srcdoc = `<script>window.open(${JSON.stringify(fromSandbox.pageUrl)}, "_blank");<\/script>`;
+ document.body.appendChild(iframe);
+ println(`page opened from a sandboxed iframe was requested: ${await fromSandbox.requested(400)}`);
+ println(`page opened from a sandboxed iframe loaded: ${await fromSandbox.loaded(80)}`);
+ });
+</script>