Truly independent web browser
LibWeb: Send cookies with the preloads of a document’s Link header
sideshowbarker7387594412c9
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cppindex f8ec6f08ba6..c304fee0dba 100644--- a/Libraries/LibWeb/DOM/Document.cpp+++ b/Libraries/LibWeb/DOM/Document.cpp@@ -519,6 +519,7 @@ WebIDL::ExceptionOr<GC::Ref<Document>> Document::create_and_initialize(Type typedocument->m_content_type = move(content_type);document->set_origin(navigation_params.origin);document->set_browsing_context(browsing_context);+ document->m_navigable_awaiting_activation = navigation_params.navigable.ptr();document->m_policy_container = navigation_params.policy_container;document->set_opener_policy(navigation_params.opener_policy);document->m_permissions_policy = permissions_policy.release_value();@@ -11733,6 +11734,8 @@ void Document::set_navigable(GC::Ptr<HTML::LocalNavigable> navigable)GC::Ptr<Page> previous_page = m_navigable ? &m_navigable->page() : nullptr;m_navigable = navigable.ptr();+ if (navigable)+ m_navigable_awaiting_activation = nullptr;invalidate_full_activity();HTML::main_thread_event_loop().document_navigable_did_change({});rendering_pending_document_changed(*this);diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.hindex 57dd0bb1656..91fa6639ece 100644--- a/Libraries/LibWeb/DOM/Document.h+++ b/Libraries/LibWeb/DOM/Document.h@@ -1618,6 +1618,9 @@ public:GC::Ptr<HTML::LocalNavigable> navigable() const;void set_navigable(GC::Ptr<HTML::LocalNavigable>);+ // The navigable whose active document this document, created for a navigation, is to become, until it does.+ GC::Ptr<HTML::LocalNavigable> navigable_awaiting_activation() const { return m_navigable_awaiting_activation.ptr(); }+void set_needs_repaint(Badge<Node, Painting::BoxViewRepaintAccess, HTML::LocalNavigable, CSS::VisualViewport, Web::EventHandler>, InvalidateDisplayList should_invalidate_display_list = InvalidateDisplayList::PaintCommandsAndHitTestList){set_needs_repaint(should_invalidate_display_list);@@ -2521,6 +2524,7 @@ private:// NOTE: This is GC::Weak, not GC::Ptr, on purpose. We don't want the document to keep some old detached navigable alive.GC::Weak<HTML::LocalNavigable> m_navigable;+ GC::Weak<HTML::LocalNavigable> m_navigable_awaiting_activation;Core::SharedVersion m_cookie_version { Core::INVALID_SHARED_VERSION };Optional<Core::SharedVersionIndex> m_cookie_version_index;diff --git a/Libraries/LibWeb/HTML/Scripting/WindowEnvironmentSettingsObject.cpp b/Libraries/LibWeb/HTML/Scripting/WindowEnvironmentSettingsObject.cppindex b776a890407..ff619ee93a0 100644--- a/Libraries/LibWeb/HTML/Scripting/WindowEnvironmentSettingsObject.cpp+++ b/Libraries/LibWeb/HTML/Scripting/WindowEnvironmentSettingsObject.cpp@@ -114,9 +114,16 @@ URL::Origin WindowEnvironmentSettingsObject::origin() const// https://html.spec.whatwg.org/multipage/nav-history-apis.html#script-settings-for-window-objects:concept-settings-object-has-cross-site-ancestorbool WindowEnvironmentSettingsObject::has_cross_site_ancestor() const{+ auto navigable = m_window->navigable();++ // AD-HOC: A document created for a navigation is not its navigable's active document yet when it processes the+ // preloads of its Link headers, so its window has no navigable. Answer for the navigable it is created in,+ // as its own fetches are answered once it is active.+ if (!navigable)+ navigable = m_window->associated_document().navigable_awaiting_activation();+// NB: A fetch can outlive its document's navigable, for example when following a redirect after an iframe is// removed. Without its ancestors, conservatively treat the environment as a third-party context.- auto navigable = m_window->navigable();if (!navigable)return true;diff --git a/Tests/LibWeb/Text/expected/link-header-preload-sends-first-party-cookies.txt b/Tests/LibWeb/Text/expected/link-header-preload-sends-first-party-cookies.txtnew file mode 100644index 00000000000..0516c744d89--- /dev/null+++ b/Tests/LibWeb/Text/expected/link-header-preload-sends-first-party-cookies.txt@@ -0,0 +1,2 @@+Stylesheet only the Link header names: session=1+Script the document loads: session=1diff --git a/Tests/LibWeb/Text/input/link-header-preload-sends-first-party-cookies.html b/Tests/LibWeb/Text/input/link-header-preload-sends-first-party-cookies.htmlnew file mode 100644index 00000000000..0f1bdec443c--- /dev/null+++ b/Tests/LibWeb/Text/input/link-header-preload-sends-first-party-cookies.html@@ -0,0 +1,49 @@+<!DOCTYPE html>+<script src="include.js"></script>+<script>+ // A top-level document preloads what its Link header names before it becomes its navigable's active document.+ // Those preloads are first-party requests, so they carry the site's cookies, as the document's own requests do.+ asyncTest(async done => {+ const server = httpTestServer();+ const runId = crypto.randomUUID();+ const site = uniqueLocalhostHostname("link-header-preload-site");+ const onSite = url => {+ const result = new URL(url);+ result.hostname = site;+ return result.href;+ };+ const echo = async (name, options) =>+ onSite(await server.createEcho("GET", `/link-header-preload/${runId}/${name}`, {+ status: 200,+ ...options,+ headers: { "Cache-Control": "no-store", ...options.headers },+ }));++ const setCookieURL = await echo("set-cookie", { headers: { "Set-Cookie": "session=1; Path=/" } });+ const preloadedURL = await echo("preloaded.css", { headers: { "Content-Type": "text/css" }, body: "p { color: green; }" });+ const scriptURL = await echo("script.js", { headers: { "Content-Type": "text/javascript" }, body: "" });+ const pageURL = await echo("page.html", {+ headers: { "Content-Type": "text/html", "Link": `<${preloadedURL}>; rel=preload; as=style` },+ body: `<!DOCTYPE html><script src="${scriptURL}"><\/script>`+ + `<script>window.onload = () => window.opener.postMessage("loaded", "*");<\/script>`,+ });+ const startURL = await echo("start.html", {+ headers: { "Content-Type": "text/html" },+ body: `<!DOCTYPE html><script>fetch(${JSON.stringify(setCookieURL)}).then(() => { location.href = ${JSON.stringify(pageURL)}; });<\/script>`,+ });++ const loaded = new Promise(resolve => window.addEventListener("message", event => event.data === "loaded" && resolve()));+ const popup = window.open(startURL);+ await loaded;+ await server.waitForRequest(preloadedURL);+ popup.close();++ const cookieOf = async url => {+ const headers = await (await fetch(server.recordedRequestHeadersURL(url))).json();+ return (headers["Cookie"] ?? headers["cookie"] ?? ["(none)"]).join("; ");+ };+ println(`Stylesheet only the Link header names: ${await cookieOf(preloadedURL)}`);+ println(`Script the document loads: ${await cookieOf(scriptURL)}`);+ done();+ });+</script>