Skip to content

Truly independent web browser

LibWeb: Keep an iframe's sandbox on a javascript: URL's result document

Shannon Booth42d6dfd0612f
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cpp
index 7f94afaa7ae..e19233f625e 100644
--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp
+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp
@@ -4121,8 +4121,12 @@ GC::Ptr<DOM::Document> LocalNavigable::evaluate_javascript_url(URL::URL const& u
// 12. Let policyContainer be targetNavigable's active document's policy container.
auto const& policy_container = active_document()->policy_container();
+ // AD-HOC: Get the target snapshot params. This is missing from the spec, see https://github.com/whatwg/html/issues/12563
+ auto target_snapshot_params = snapshot_target_snapshot_params(*this);
+
// 13. Let finalSandboxFlags be policyContainer's CSP list's CSP-derived sandboxing flags.
- auto final_sandbox_flags = policy_container->csp_list.csp_derived_sandboxing_flags();
+ // AD-HOC: Include targetSnapshotParams's sandboxing flags, as every other navigation does.
+ auto final_sandbox_flags = target_snapshot_params.sandboxing_flags | policy_container->csp_list.csp_derived_sandboxing_flags();
// 14. Let coop be targetNavigable's active document's opener policy.
auto const& coop = active_document()->opener_policy();
@@ -4137,9 +4141,6 @@ GC::Ptr<DOM::Document> LocalNavigable::evaluate_javascript_url(URL::URL const& u
.opener_policy = coop,
};
- // AD-HOC: Get the target snapshot params. This is missing from the spec, see https://github.com/whatwg/html/issues/12563
- auto target_snapshot_params = snapshot_target_snapshot_params(*this);
-
// 16. Let navigationParams be a new navigation params, with
// id: navigationId
// navigable: targetNavigable
diff --git a/Tests/LibWeb/Text/expected/HTML/javascript-url-result-keeps-iframe-sandbox.txt b/Tests/LibWeb/Text/expected/HTML/javascript-url-result-keeps-iframe-sandbox.txt
new file mode 100644
index 00000000000..f50dc5c4428
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/javascript-url-result-keeps-iframe-sandbox.txt
@@ -0,0 +1,2 @@
+allow-scripts allow-same-origin: SecurityError
+allow-scripts: SecurityError
diff --git a/Tests/LibWeb/Text/input/HTML/javascript-url-result-keeps-iframe-sandbox.html b/Tests/LibWeb/Text/input/HTML/javascript-url-result-keeps-iframe-sandbox.html
new file mode 100644
index 00000000000..7a07bade878
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/javascript-url-result-keeps-iframe-sandbox.html
@@ -0,0 +1,24 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ promiseTest(async () => {
+ const result = `<script>
+ try {
+ top.location.href = ${JSON.stringify(location.href + "#escaped")};
+ parent.postMessage("top navigated", "*");
+ } catch (e) {
+ parent.postMessage(e.name, "*");
+ }
+ <\/script>`;
+ const url = "javascript:" + encodeURIComponent(JSON.stringify(result));
+ for (const sandbox of ["allow-scripts allow-same-origin", "allow-scripts"]) {
+ const message = new Promise(resolve => addEventListener("message", resolve, { once: true }));
+ const iframe = document.createElement("iframe");
+ iframe.sandbox = sandbox;
+ iframe.srcdoc = `<script>location.replace(${JSON.stringify(url)});<\/script>`;
+ document.body.appendChild(iframe);
+ println(`${sandbox}: ${(await message).data}`);
+ iframe.remove();
+ }
+ });
+</script>