Skip to content

Truly independent web browser

LibWeb: Don’t fetch an @import that would import its own ancestor

sideshowbarker0f46d55c5049
diff --git a/Libraries/LibWeb/CSS/StyleSheetImport.cpp b/Libraries/LibWeb/CSS/StyleSheetImport.cpp
index a6ddcdd4c6b..61d67aab67d 100644
--- a/Libraries/LibWeb/CSS/StyleSheetImport.cpp
+++ b/Libraries/LibWeb/CSS/StyleSheetImport.cpp
@@ -228,12 +228,6 @@ void StyleSheetImport::fetch()
return;
}
- // NB: Track pending import rules for stylesheet load accounting.
- m_document->add_pending_css_import_rule({}, *this);
- set_loading_state(StyleSheetState::LoadingState::Loading);
-
- // 3. Fetch a style resource from rule’s URL, with ruleOrDeclaration rule, destination "style", CORS mode "no-cors", and
- // processResponse being the following steps given response response and byte stream, null or failure byteStream:
RuleOrDeclaration rule_or_declaration {
.environment_settings_object = HTML::relevant_settings_object(*m_document),
.value = RuleOrDeclaration::Rule {
@@ -242,6 +236,29 @@ void StyleSheetImport::fetch()
.style_resource_base_url = {},
.parent_style_sheet_origin_clean = {},
};
+
+ // AD-HOC: An import of the style sheet the rule is in, or of a sheet that imports that one, would fetch and import
+ // that sheet again, and again without end. So it imports nothing, as an unmet supports condition does. A
+ // sheet is matched by the URL it was requested from as well as the one a redirect ended at.
+ m_requested_url = resolve_a_style_resource_url(URL { href() }, rule_or_declaration);
+ if (m_requested_url.has_value()) {
+ auto is_import_url = [&](Optional<::URL::URL> const& url) {
+ return url.has_value() && url->equals(*m_requested_url, ::URL::ExcludeFragment::Yes);
+ };
+ for (auto* sheet = &parent_style_sheet; sheet; sheet = sheet->parent_style_sheet()) {
+ if (is_import_url(sheet->location()) || is_import_url(sheet->base_url()) || is_import_url(sheet->requested_url())) {
+ set_loading_state(StyleSheetState::LoadingState::Loaded);
+ return;
+ }
+ }
+ }
+
+ // NB: Track pending import rules for stylesheet load accounting.
+ m_document->add_pending_css_import_rule({}, *this);
+ set_loading_state(StyleSheetState::LoadingState::Loading);
+
+ // 3. Fetch a style resource from rule’s URL, with ruleOrDeclaration rule, destination "style", CORS mode "no-cors", and
+ // processResponse being the following steps given response response and byte stream, null or failure byteStream:
(void)fetch_a_style_resource(URL { href() }, rule_or_declaration, Fetch::Infrastructure::Request::Destination::Style, CorsMode::NoCors,
GC::create_function(GC::Heap::the(), GC::captures(NonnullRefPtr { *this }, NonnullRefPtr { parent_style_sheet }, m_document, DOM::DocumentLoadEventDelayer(*m_document, DOM::DocumentLoadEventDelayerReason::StyleSheetRequest)), [](auto& strong_this, auto& parent_style_sheet, auto& document, auto& load_event_delayer, GC::Ref<Fetch::Infrastructure::ResponseView> response, Fetch::Infrastructure::FetchAlgorithms::BodyBytes maybe_byte_stream) {
// AD-HOC: Stop delaying the load event.
@@ -310,8 +327,9 @@ void StyleSheetImport::fetch()
Parser::ParsingParams { *document }, decoded_or_error.release_value(),
[strong_this, parent_style_sheet, document = GC::make_root(document), url = move(url), is_cors_same_origin = response->is_cors_same_origin(), referrer_policy = ReferrerPolicy::parse_a_referrer_policy_from_a_referrer_policy_header(*response->unsafe_response()->record()), finish_loading = move(finish_loading)](Parser::RustStyleSheetParse parsed) mutable {
ScopeGuard guard = move(finish_loading);
- // NB: The sheet records its referrer policy as it is created, before its own imports start to load.
- auto imported_style_sheet = StyleSheetState::create(parsed.native_rules(), document.ptr(), strong_this->m_media_list.retain(), move(url), referrer_policy);
+ // NB: The sheet records its referrer policy, its parent and its requested URL as it is created, before its
+ // own imports start to load, which read them.
+ auto imported_style_sheet = StyleSheetState::create(parsed.native_rules(), document.ptr(), strong_this->m_media_list.retain(), move(url), referrer_policy, strong_this.ptr());
// 5. Set importedStylesheet’s origin-clean flag to parentStylesheet’s origin-clean flag.
imported_style_sheet->set_origin_clean(parent_style_sheet->is_origin_clean());
diff --git a/Libraries/LibWeb/CSS/StyleSheetImport.h b/Libraries/LibWeb/CSS/StyleSheetImport.h
index ecedc572987..fc4b72a1efc 100644
--- a/Libraries/LibWeb/CSS/StyleSheetImport.h
+++ b/Libraries/LibWeb/CSS/StyleSheetImport.h
@@ -32,6 +32,7 @@ public:
GC::Ref<MediaList> media() const;
StyleSheetState* parent_style_sheet() const { return m_parent_style_sheet.ptr(); }
+ Optional<::URL::URL> const& requested_url() const { return m_requested_url; }
StyleSheetState* loaded_style_sheet() const { return m_style_sheet.ptr(); }
void set_parent_style_sheet(StyleSheetState*);
@@ -52,6 +53,7 @@ private:
mutable Optional<RustRule> m_native_rule;
RustImportRule m_rule;
mutable Optional<URL> m_cached_url;
+ Optional<::URL::URL> m_requested_url;
GC::Ptr<DOM::Document> m_document;
WeakPtr<StyleSheetState> m_parent_style_sheet;
RustMediaList m_media_list;
diff --git a/Libraries/LibWeb/CSS/StyleSheetState.cpp b/Libraries/LibWeb/CSS/StyleSheetState.cpp
index e0d4f217da3..0f051d95494 100644
--- a/Libraries/LibWeb/CSS/StyleSheetState.cpp
+++ b/Libraries/LibWeb/CSS/StyleSheetState.cpp
@@ -35,9 +35,9 @@
namespace Web::CSS {
-NonnullRefPtr<StyleSheetState> StyleSheetState::create(RustRuleList rules, GC::Ptr<DOM::Document> document, RustMediaList media, Optional<::URL::URL> location, Optional<ReferrerPolicy::ReferrerPolicy> fetched_referrer_policy)
+NonnullRefPtr<StyleSheetState> StyleSheetState::create(RustRuleList rules, GC::Ptr<DOM::Document> document, RustMediaList media, Optional<::URL::URL> location, Optional<ReferrerPolicy::ReferrerPolicy> fetched_referrer_policy, StyleSheetImport const* import)
{
- return adopt_ref(*new StyleSheetState(move(rules), document, move(media), move(location), fetched_referrer_policy));
+ return adopt_ref(*new StyleSheetState(move(rules), document, move(media), move(location), fetched_referrer_policy, import));
}
WebIDL::ExceptionOr<NonnullRefPtr<StyleSheetState>> StyleSheetState::create_constructed(DOM::Document const& associated_document, CSSStyleSheetOptions const& options)
@@ -101,12 +101,17 @@ WebIDL::ExceptionOr<NonnullRefPtr<StyleSheetState>> StyleSheetState::create_cons
return sheet;
}
-StyleSheetState::StyleSheetState(RustRuleList rules, GC::Ptr<DOM::Document> document, RustMediaList media, Optional<::URL::URL> location, Optional<ReferrerPolicy::ReferrerPolicy> fetched_referrer_policy)
+StyleSheetState::StyleSheetState(RustRuleList rules, GC::Ptr<DOM::Document> document, RustMediaList media, Optional<::URL::URL> location, Optional<ReferrerPolicy::ReferrerPolicy> fetched_referrer_policy, StyleSheetImport const* import)
: m_native_sheet(move(rules), move(media))
, m_style_engine_occurrence_id(m_native_sheet.identity())
+ , m_parent_style_sheet(import ? import->parent_style_sheet() : nullptr)
, m_fetched_referrer_policy(fetched_referrer_policy)
, m_parsing_document(document)
{
+ // The sheet takes its import's requested URL, but not the import itself: It would report its loading state to the
+ // import while its own imports are still being attached.
+ if (import)
+ m_requested_url = import->requested_url();
if (location.has_value())
set_location(move(location));
diff --git a/Libraries/LibWeb/CSS/StyleSheetState.h b/Libraries/LibWeb/CSS/StyleSheetState.h
index da6414a3caa..9165eb08b28 100644
--- a/Libraries/LibWeb/CSS/StyleSheetState.h
+++ b/Libraries/LibWeb/CSS/StyleSheetState.h
@@ -70,7 +70,7 @@ public:
Error,
};
- [[nodiscard]] static NonnullRefPtr<StyleSheetState> create(RustRuleList, GC::Ptr<DOM::Document>, RustMediaList, Optional<::URL::URL> location, Optional<ReferrerPolicy::ReferrerPolicy> fetched_referrer_policy = {});
+ [[nodiscard]] static NonnullRefPtr<StyleSheetState> create(RustRuleList, GC::Ptr<DOM::Document>, RustMediaList, Optional<::URL::URL> location, Optional<ReferrerPolicy::ReferrerPolicy> fetched_referrer_policy = {}, StyleSheetImport const* import = nullptr);
static WebIDL::ExceptionOr<NonnullRefPtr<StyleSheetState>> create_constructed(DOM::Document const&, CSSStyleSheetOptions const& options = {});
~StyleSheetState();
@@ -178,6 +178,8 @@ public:
Optional<Utf16String> href_for_bindings() const;
Optional<::URL::URL> location() const { return m_location; }
+ // The URL the import that loaded this sheet requested, before any redirect.
+ Optional<::URL::URL> const& requested_url() const { return m_requested_url; }
void set_location(Optional<::URL::URL> location) { m_location = move(location); }
// AD-HOC: The referrer policy of the response that this style sheet was fetched from, or none if it was not fetched.
@@ -204,7 +206,7 @@ public:
void set_parent_css_style_sheet(StyleSheetState*);
private:
- StyleSheetState(RustRuleList, GC::Ptr<DOM::Document>, RustMediaList, Optional<::URL::URL> location, Optional<ReferrerPolicy::ReferrerPolicy> fetched_referrer_policy);
+ StyleSheetState(RustRuleList, GC::Ptr<DOM::Document>, RustMediaList, Optional<::URL::URL> location, Optional<ReferrerPolicy::ReferrerPolicy> fetched_referrer_policy, StyleSheetImport const* import);
void set_rules(RustRuleList);
bool has_document_owner() const;
@@ -231,6 +233,7 @@ private:
GC::Ptr<DOM::Element> m_owner_node;
WeakPtr<StyleSheetState> m_parent_style_sheet;
Optional<::URL::URL> m_location;
+ Optional<::URL::URL> m_requested_url;
Optional<ReferrerPolicy::ReferrerPolicy> m_fetched_referrer_policy;
Utf16String m_title;
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index 0348d4bb91c..5dcef45b141 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -1274,6 +1274,8 @@ Text/input/SVG/use-element-nested-external-fragment-after-redirect.html
Text/input/SVG/use-element-relative-external-reference-after-adoption.html
Text/input/SVG/use-element-repeated-href-changes-on-stalled-sprite.html
Text/input/SVG/use-element-same-document-href-stops-delaying-load.html
+Text/input/css/import-cycle-through-redirect.html
+Text/input/css/import-cycle-before-a-slow-import.html
[LoadFromHttpsServer]
Text/input/SiteIsolation/iframe/embedded-page-cookie-request.html
diff --git a/Tests/LibWeb/Text/expected/css/import-cycle-before-a-slow-import.txt b/Tests/LibWeb/Text/expected/css/import-cycle-before-a-slow-import.txt
new file mode 100644
index 00000000000..e35d7a5b0fe
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/import-cycle-before-a-slow-import.txt
@@ -0,0 +1 @@
+slow import applied at the link's load: true
diff --git a/Tests/LibWeb/Text/expected/css/import-cycle-through-redirect.txt b/Tests/LibWeb/Text/expected/css/import-cycle-through-redirect.txt
new file mode 100644
index 00000000000..b548ad1b61c
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/import-cycle-through-redirect.txt
@@ -0,0 +1,2 @@
+readyState: complete
+imported: rgb(0, 128, 0)
diff --git a/Tests/LibWeb/Text/expected/css/import-cycle.txt b/Tests/LibWeb/Text/expected/css/import-cycle.txt
new file mode 100644
index 00000000000..57e02be6109
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/import-cycle.txt
@@ -0,0 +1,4 @@
+readyState: complete
+direct: rgb(0, 128, 0)
+indirect-a: rgb(0, 128, 0)
+indirect-b: rgb(0, 128, 0)
diff --git a/Tests/LibWeb/Text/input/css/import-cycle-before-a-slow-import.html b/Tests/LibWeb/Text/input/css/import-cycle-before-a-slow-import.html
new file mode 100644
index 00000000000..d04197bcdbc
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/import-cycle-before-a-slow-import.html
@@ -0,0 +1,50 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ // A link whose imported sheet imports itself before a slow import fires its load event only once the slow import has
+ // loaded.
+ asyncTest(async done => {
+ const server = httpTestServer();
+ const slowURL = await server.createEcho("GET", "/import-cycle-before-a-slow-import/slow.css", {
+ status: 200,
+ headers: { "Content-Type": "text/css" },
+ body: `.slow { color: rgb(0, 128, 0); }`,
+ delay_ms: 500,
+ });
+ const middleURL = server.echoURL("/import-cycle-before-a-slow-import/middle.css");
+ await server.createEcho("GET", "/import-cycle-before-a-slow-import/middle.css", {
+ status: 200,
+ headers: { "Content-Type": "text/css" },
+ body: `@import url("${middleURL}"); @import url("${slowURL}");`,
+ });
+ const rootURL = await server.createEcho("GET", "/import-cycle-before-a-slow-import/root.css", {
+ status: 200,
+ headers: { "Content-Type": "text/css" },
+ body: `@import url("${middleURL}");`,
+ });
+ const scriptStartTag = "<scr" + "ipt";
+ const scriptEndTag = "</scr" + "ipt>";
+ const pageURL = await server.createEcho("GET", "/import-cycle-before-a-slow-import.html", {
+ status: 200,
+ headers: { "Content-Type": "text/html" },
+ body: `<!DOCTYPE html>
+ <p class="slow">slow</p>
+ ${scriptStartTag}>
+ const link = document.createElement("link");
+ link.rel = "stylesheet";
+ link.href = "${rootURL}";
+ link.onload = () => parent.postMessage({
+ color: getComputedStyle(document.querySelector(".slow")).color,
+ }, "*");
+ document.head.appendChild(link);
+ ${scriptEndTag}`,
+ });
+ addEventListener("message", event => {
+ println(`slow import applied at the link's load: ${event.data.color === "rgb(0, 128, 0)"}`);
+ done();
+ });
+ const iframe = document.createElement("iframe");
+ iframe.src = pageURL;
+ document.body.appendChild(iframe);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/css/import-cycle-resources/imports-a.css b/Tests/LibWeb/Text/input/css/import-cycle-resources/imports-a.css
new file mode 100644
index 00000000000..ced537b7db9
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/import-cycle-resources/imports-a.css
@@ -0,0 +1,2 @@
+@import url("imports-b.css");
+.indirect-a { color: rgb(0, 128, 0); }
diff --git a/Tests/LibWeb/Text/input/css/import-cycle-resources/imports-b.css b/Tests/LibWeb/Text/input/css/import-cycle-resources/imports-b.css
new file mode 100644
index 00000000000..06490cfe8a8
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/import-cycle-resources/imports-b.css
@@ -0,0 +1,2 @@
+@import url("imports-a.css#fragment");
+.indirect-b { color: rgb(0, 128, 0); }
diff --git a/Tests/LibWeb/Text/input/css/import-cycle-resources/imports-itself.css b/Tests/LibWeb/Text/input/css/import-cycle-resources/imports-itself.css
new file mode 100644
index 00000000000..4cbdd6a4508
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/import-cycle-resources/imports-itself.css
@@ -0,0 +1,2 @@
+@import url("");
+.direct { color: rgb(0, 128, 0); }
diff --git a/Tests/LibWeb/Text/input/css/import-cycle-through-redirect.html b/Tests/LibWeb/Text/input/css/import-cycle-through-redirect.html
new file mode 100644
index 00000000000..48ee795c10f
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/import-cycle-through-redirect.html
@@ -0,0 +1,47 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ // An imported style sheet that a redirect moved imports the URL it was requested from.
+ asyncTest(async done => {
+ const server = httpTestServer();
+ const redirectedURL = server.echoURL("/import-cycle-through-redirect/a.css");
+ const targetURL = await server.createEcho("GET", "/import-cycle-through-redirect/target/a.css", {
+ status: 200,
+ headers: { "Content-Type": "text/css" },
+ body: `@import url("${redirectedURL}"); .imported { color: rgb(0, 128, 0); }`,
+ });
+ await server.createEcho("GET", "/import-cycle-through-redirect/a.css", {
+ status: 302,
+ headers: { "Location": targetURL },
+ body: "",
+ });
+ const rootURL = await server.createEcho("GET", "/import-cycle-through-redirect/root.css", {
+ status: 200,
+ headers: { "Content-Type": "text/css" },
+ body: `@import url("${redirectedURL}");`,
+ });
+ const scriptStartTag = "<scr" + "ipt";
+ const scriptEndTag = "</scr" + "ipt>";
+ const pageURL = await server.createEcho("GET", "/import-cycle-through-redirect.html", {
+ status: 200,
+ headers: { "Content-Type": "text/html" },
+ body: `<!DOCTYPE html>
+ <link rel="stylesheet" href="${rootURL}">
+ <p class="imported">imported</p>
+ ${scriptStartTag}>
+ addEventListener("load", () => parent.postMessage({
+ readyState: document.readyState,
+ color: getComputedStyle(document.querySelector(".imported")).color,
+ }, "*"));
+ ${scriptEndTag}`,
+ });
+ addEventListener("message", event => {
+ println(`readyState: ${event.data.readyState}`);
+ println(`imported: ${event.data.color}`);
+ done();
+ });
+ const iframe = document.createElement("iframe");
+ iframe.src = pageURL;
+ document.body.appendChild(iframe);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/css/import-cycle.html b/Tests/LibWeb/Text/input/css/import-cycle.html
new file mode 100644
index 00000000000..448196d8ba1
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/import-cycle.html
@@ -0,0 +1,17 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<link rel="stylesheet" href="import-cycle-resources/imports-itself.css">
+<link rel="stylesheet" href="import-cycle-resources/imports-a.css">
+<p class="direct">direct</p>
+<p class="indirect-a">indirect a</p>
+<p class="indirect-b">indirect b</p>
+<script>
+ asyncTest(done => {
+ window.addEventListener("load", () => {
+ println(`readyState: ${document.readyState}`);
+ for (const name of ["direct", "indirect-a", "indirect-b"])
+ println(`${name}: ${getComputedStyle(document.querySelector(`.${name}`)).color}`);
+ done();
+ });
+ });
+</script>