Skip to content

Truly independent web browser

LibWeb: Stop the PDF viewer taking over windows of other origins

Tim Ledbettera4848204e02c
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cpp
index 794895e6284..62a867f9715 100644
--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp
+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp
@@ -3118,9 +3118,11 @@ GC_DEFINE_ALLOCATOR(DocumentToCreate);
DocumentToCreate::DocumentToCreate(GC::Ref<NavigationParams> navigation_params, GC::Ref<LoadSteps> load_steps, Optional<URL::Origin> origin)
: m_navigation_params(navigation_params)
, m_load_steps(load_steps)
- , m_origin(move(origin))
+ , m_replaces_response_origin(origin.has_value())
, m_environment_id(navigation_params->reserved_environment ? navigation_params->reserved_environment->id : *navigation_params->environment_id)
{
+ if (origin.has_value())
+ m_navigation_params->origin = origin.release_value();
}
void DocumentToCreate::visit_edges(Cell::Visitor& visitor)
@@ -3132,7 +3134,7 @@ void DocumentToCreate::visit_edges(Cell::Visitor& visitor)
URL::Origin DocumentToCreate::origin() const
{
- return m_origin.value_or(m_navigation_params->origin);
+ return m_navigation_params->origin;
}
// https://html.spec.whatwg.org/multipage/document-lifecycle.html#initialise-the-document-object
@@ -3172,8 +3174,6 @@ bool DocumentToCreate::takes_over_window_of(DOM::Document const& document) const
GC::Ref<DOM::Document> DocumentToCreate::create_document()
{
auto document = m_load_steps->function()(*m_navigation_params);
- if (m_origin.has_value())
- document->set_origin(*m_origin);
if (m_navigation_params->response->network_transport().has_value())
m_navigation_params->response->resume_body_delivery(*surrounding_agent().fetch_executor);
m_navigation_params = nullptr;
@@ -3190,13 +3190,14 @@ void DocumentToCreate::discard()
m_load_steps = nullptr;
}
-// NB: A document created in place of the response, or the PDF viewer, has an origin other than its navigation params'.
-// The process reports which of the two it created, and the UI process takes up that origin for the document it
-// holds.
+// NB: A document created in place of the response, or the PDF viewer, has an origin other than its response's. The
+// process reports which of the two it created, and the UI process takes up that origin for the document it holds.
static void report_a_document_with_an_origin_of_its_own(LocalNavigable& navigable, PopulateSessionHistoryEntryDocumentOutput const& output)
{
auto const* navigation_params = output.navigation_params.get_pointer<GC::Ref<NavigationParams>>();
- if (!output.document_to_create || !navigation_params || output.document_to_create->origin().is_same_origin((*navigation_params)->origin))
+ if (!output.document_to_create || !navigation_params)
+ return;
+ if (!output.document_to_create->replaces_response_origin() && output.document_to_create->origin().is_same_origin((*navigation_params)->origin))
return;
auto origin = output.inline_content_origin.has_value() && output.document_to_create->origin().is_same_origin(*output.inline_content_origin)
? PopulatedDocumentOrigin::InlineContent
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.h b/Libraries/LibWeb/HTML/LocalNavigable.h
index bf5b77b6285..ed1541f78c1 100644
--- a/Libraries/LibWeb/HTML/LocalNavigable.h
+++ b/Libraries/LibWeb/HTML/LocalNavigable.h
@@ -808,6 +808,7 @@ public:
static GC::Ref<DocumentToCreate> create(GC::Ref<NavigationParams>, GC::Ref<LoadSteps>, Optional<URL::Origin> origin = {});
URL::Origin origin() const;
+ bool replaces_response_origin() const { return m_replaces_response_origin; }
URL::URL url() const;
CrossProcessId id() const;
EnvironmentId const& environment_id() const;
@@ -825,7 +826,7 @@ private:
// Null once the document is created or discarded, so that what it was created from is released.
GC::Ptr<NavigationParams> m_navigation_params;
GC::Ptr<LoadSteps> m_load_steps;
- Optional<URL::Origin> m_origin;
+ bool m_replaces_response_origin { false };
// Creating the document moves the id out of the reserved environment.
EnvironmentId m_environment_id;
};
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index 52e5438f33d..8f2121dd058 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -1,6 +1,7 @@
[RequiresPDFJS]
Layout/input/pdf-viewer.pdf
Text/input/PDF/jbig2-image.html
+Text/input/PDF/viewer-does-not-take-over-initial-about-blank-window.html
[LoadFromHttpServer]
Text/input/fragment-scroll-after-stylesheet.html
diff --git a/Tests/LibWeb/Text/expected/PDF/viewer-does-not-take-over-initial-about-blank-window.txt b/Tests/LibWeb/Text/expected/PDF/viewer-does-not-take-over-initial-about-blank-window.txt
new file mode 100644
index 00000000000..8435d0d4e0a
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/PDF/viewer-does-not-take-over-initial-about-blank-window.txt
@@ -0,0 +1,3 @@
+Initial window document: about:blank
+document: SecurityError
+localStorage: SecurityError
diff --git a/Tests/LibWeb/Text/input/PDF/viewer-does-not-take-over-initial-about-blank-window.html b/Tests/LibWeb/Text/input/PDF/viewer-does-not-take-over-initial-about-blank-window.html
new file mode 100644
index 00000000000..5358f52db93
--- /dev/null
+++ b/Tests/LibWeb/Text/input/PDF/viewer-does-not-take-over-initial-about-blank-window.html
@@ -0,0 +1,28 @@
+<!doctype html>
+<script src="../include.js"></script>
+<iframe id="frame"></iframe>
+<script>
+ asyncTest(done => {
+ const InitialFunction = frame.contentWindow.Function;
+ const getters = {
+ document: Object.getOwnPropertyDescriptor(frame.contentWindow, "document").get,
+ localStorage: Object.getOwnPropertyDescriptor(frame.contentWindow, "localStorage").get,
+ };
+ frame.onload = () => {
+ println(`Initial window document: ${InitialFunction("return document.URL")()}`);
+ for (const [name, getter] of Object.entries(getters)) {
+ try {
+ getter.call(frame.contentWindow);
+ println(`${name}: no exception`);
+ } catch (error) {
+ println(`${name}: ${error.name}`);
+ }
+ }
+ done();
+ };
+ const pdf = Uint8Array.fromBase64(
+ "JVBERi0xLjQKMSAwIG9iago8PCAvVHlwZSAvQ2F0YWxvZyAvUGFnZXMgMiAwIFIgPj4KZW5kb2JqCjIgMCBvYmoKPDwgL1R5cGUgL1BhZ2VzIC9LaWRzIFszIDAgUl0gL0NvdW50IDEgPj4KZW5kb2JqCjMgMCBvYmoKPDwgL1R5cGUgL1BhZ2UgL1BhcmVudCAyIDAgUiAvTWVkaWFCb3ggWzAgMCA4IDhdID4+CmVuZG9iagp4cmVmCjAgNAowMDAwMDAwMDAwIDY1NTM1IGYgCjAwMDAwMDAwMDkgMDAwMDAgbiAKMDAwMDAwMDA1OCAwMDAwMCBuIAowMDAwMDAwMTE1IDAwMDAwIG4gCnRyYWlsZXIKPDwgL1NpemUgNCAvUm9vdCAxIDAgUiA+PgpzdGFydHhyZWYKMTgyCiUlRU9GCg=="
+ );
+ frame.src = URL.createObjectURL(new Blob([pdf], { type: "application/pdf" }));
+ });
+</script>