Skip to content

Truly independent web browser

LibWeb: Use the referrer of a style sheet for its subresources

Andreas Kling9dcb50116699
diff --git a/Libraries/LibWeb/CSS/Fetch.cpp b/Libraries/LibWeb/CSS/Fetch.cpp
index c4a0d78322a..b6e3bcd831c 100644
--- a/Libraries/LibWeb/CSS/Fetch.cpp
+++ b/Libraries/LibWeb/CSS/Fetch.cpp
@@ -90,7 +90,7 @@ static GC::Ptr<Fetch::Infrastructure::Request> fetch_a_style_resource_impl(Style
// 3. Let req be a new request whose url is parsedUrl, whose destination is destination, mode is corsMode,
// origin is environmentSettings’s origin, credentials mode is "same-origin", use-url-credentials flag is set,
- // client is environmentSettings, and whose referrer is environmentSettings’s API base URL.
+ // client is environmentSettings, and whose referrer is "client".
auto request = Fetch::Infrastructure::Request::create();
request->set_url(parsed_url.release_value());
request->set_destination(destination);
@@ -99,12 +99,22 @@ static GC::Ptr<Fetch::Infrastructure::Request> fetch_a_style_resource_impl(Style
request->set_credentials_mode(Fetch::Infrastructure::Request::CredentialsMode::SameOrigin);
request->set_use_url_credentials(true);
request->set_client(&environment_settings);
- request->set_referrer(environment_settings.api_base_url());
+ request->set_referrer(Fetch::Infrastructure::Request::Referrer::Client);
// 4. If corsMode is "no-cors", set req’s credentials mode to "include".
if (cors_mode == CorsMode::NoCors)
request->set_credentials_mode(Fetch::Infrastructure::Request::CredentialsMode::Include);
+ // INTEROP: Other engines fetch the resources of a style sheet that was itself fetched with the sheet's URL as the
+ // referrer, and with the referrer policy of the response that the sheet was fetched from.
+ auto context = style_resource_context(css_rule_or_declaration);
+ if (context.sheet) {
+ if (auto referrer_policy = context.sheet->fetched_referrer_policy(); referrer_policy.has_value()) {
+ request->set_referrer(context.url);
+ request->set_referrer_policy(*referrer_policy);
+ }
+ }
+
// 5. Apply any URL request modifier steps that apply to this request.
if (auto const* css_url = url_value.get_pointer<CSS::URL>())
apply_request_modifiers_from_url_value(*css_url, request);
@@ -112,7 +122,6 @@ static GC::Ptr<Fetch::Infrastructure::Request> fetch_a_style_resource_impl(Style
// 6. If req’s mode is "cors", and sheet is not null, then set req’s referrer to the style resource base URL given cssRuleOrDeclaration. [CSSOM]
// FIXME: Spec issue - sheet is not defined as a variable, we use the sheet determined from 'style resource base URL' instead.
// https://github.com/w3c/csswg-drafts/issues/12288
- auto context = style_resource_context(css_rule_or_declaration);
if (request->mode() == Fetch::Infrastructure::Request::Mode::CORS && (context.sheet || context.parent_style_sheet_origin_clean.has_value()))
request->set_referrer(context.url);
diff --git a/Libraries/LibWeb/CSS/StyleSheetImport.cpp b/Libraries/LibWeb/CSS/StyleSheetImport.cpp
index e197e0b6b24..c9b78513240 100644
--- a/Libraries/LibWeb/CSS/StyleSheetImport.cpp
+++ b/Libraries/LibWeb/CSS/StyleSheetImport.cpp
@@ -23,6 +23,7 @@
#include <LibWeb/Fetch/Infrastructure/HTTP/MIME.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
#include <LibWeb/HTML/Window.h>
+#include <LibWeb/ReferrerPolicy/AbstractOperations.h>
namespace Web::CSS {
@@ -219,10 +220,10 @@ void StyleSheetImport::fetch()
}
Parser::Parser::parse_stylesheet_off_thread(
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(), finish_loading = move(finish_loading)](Parser::RustStyleSheetParse parsed) mutable {
+ [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()), finish_loading = move(finish_loading)](Parser::RustStyleSheetParse parsed) mutable {
ScopeGuard guard = move(finish_loading);
- Parser::Parser parser { Parser::ParsingParams { *document } };
- auto imported_style_sheet = parser.create_css_stylesheet(parsed, move(url), strong_this->m_media_list.retain());
+ // 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);
// 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/StyleSheetState.cpp b/Libraries/LibWeb/CSS/StyleSheetState.cpp
index 728c377919f..ac7668e6579 100644
--- a/Libraries/LibWeb/CSS/StyleSheetState.cpp
+++ b/Libraries/LibWeb/CSS/StyleSheetState.cpp
@@ -37,9 +37,9 @@
namespace Web::CSS {
-NonnullRefPtr<StyleSheetState> StyleSheetState::create(RustRuleList rules, GC::Ptr<DOM::Document> document, RustMediaList media, Optional<::URL::URL> location)
+NonnullRefPtr<StyleSheetState> StyleSheetState::create(RustRuleList rules, GC::Ptr<DOM::Document> document, RustMediaList media, Optional<::URL::URL> location, Optional<ReferrerPolicy::ReferrerPolicy> fetched_referrer_policy)
{
- return adopt_ref(*new StyleSheetState(move(rules), document, move(media), move(location)));
+ return adopt_ref(*new StyleSheetState(move(rules), document, move(media), move(location), fetched_referrer_policy));
}
WebIDL::ExceptionOr<NonnullRefPtr<StyleSheetState>> StyleSheetState::create_constructed(DOM::Document const& associated_document, CSSStyleSheetOptions const& options)
@@ -103,9 +103,10 @@ WebIDL::ExceptionOr<NonnullRefPtr<StyleSheetState>> StyleSheetState::create_cons
return sheet;
}
-StyleSheetState::StyleSheetState(RustRuleList rules, GC::Ptr<DOM::Document> document, RustMediaList media, Optional<::URL::URL> location)
+StyleSheetState::StyleSheetState(RustRuleList rules, GC::Ptr<DOM::Document> document, RustMediaList media, Optional<::URL::URL> location, Optional<ReferrerPolicy::ReferrerPolicy> fetched_referrer_policy)
: m_native_sheet(move(rules), move(media))
, m_style_engine_occurrence_id(m_native_sheet.identity())
+ , m_fetched_referrer_policy(fetched_referrer_policy)
, m_parsing_document(document)
{
if (location.has_value())
diff --git a/Libraries/LibWeb/CSS/StyleSheetState.h b/Libraries/LibWeb/CSS/StyleSheetState.h
index f7a76111bdb..bf26009bfef 100644
--- a/Libraries/LibWeb/CSS/StyleSheetState.h
+++ b/Libraries/LibWeb/CSS/StyleSheetState.h
@@ -29,6 +29,7 @@
#include <LibWeb/Export.h>
#include <LibWeb/TraversalOrder.h>
#include <LibWeb/WebIDL/Promise.h>
+#include <LibWebCommon/ReferrerPolicy/ReferrerPolicy.h>
#include <LibWebCommon/WebIDL/Types.h>
namespace Web::ViewTransition {
@@ -65,7 +66,7 @@ public:
};
static StringView loading_state_name(LoadingState);
- [[nodiscard]] static NonnullRefPtr<StyleSheetState> create(RustRuleList, GC::Ptr<DOM::Document>, RustMediaList, Optional<::URL::URL> location);
+ [[nodiscard]] static NonnullRefPtr<StyleSheetState> create(RustRuleList, GC::Ptr<DOM::Document>, RustMediaList, Optional<::URL::URL> location, Optional<ReferrerPolicy::ReferrerPolicy> fetched_referrer_policy = {});
static WebIDL::ExceptionOr<NonnullRefPtr<StyleSheetState>> create_constructed(DOM::Document const&, CSSStyleSheetOptions const& options = {});
~StyleSheetState();
@@ -176,6 +177,10 @@ public:
Optional<::URL::URL> location() const { return m_location; }
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.
+ // See fetch_a_style_resource().
+ Optional<ReferrerPolicy::ReferrerPolicy> fetched_referrer_policy() const { return m_fetched_referrer_policy; }
+
Utf16String const& title() const { return m_title; }
Optional<Utf16String> title_for_bindings() const;
void set_title(Utf16String title) { m_title = move(title); }
@@ -197,7 +202,7 @@ public:
void set_parent_css_style_sheet(StyleSheetState*);
private:
- StyleSheetState(RustRuleList, GC::Ptr<DOM::Document>, RustMediaList, Optional<::URL::URL> location);
+ StyleSheetState(RustRuleList, GC::Ptr<DOM::Document>, RustMediaList, Optional<::URL::URL> location, Optional<ReferrerPolicy::ReferrerPolicy> fetched_referrer_policy);
void recalculate_rule_caches();
void set_rules(RustRuleList);
@@ -225,6 +230,7 @@ private:
GC::Ptr<DOM::Element> m_owner_node;
WeakPtr<StyleSheetState> m_parent_style_sheet;
Optional<::URL::URL> m_location;
+ Optional<ReferrerPolicy::ReferrerPolicy> m_fetched_referrer_policy;
Utf16String m_title;
Optional<Utf16String> m_source_text;
diff --git a/Libraries/LibWeb/HTML/HTMLLinkElement.cpp b/Libraries/LibWeb/HTML/HTMLLinkElement.cpp
index 21bdaaa7e33..10e0351553c 100644
--- a/Libraries/LibWeb/HTML/HTMLLinkElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLLinkElement.cpp
@@ -45,6 +45,7 @@
#include <LibWeb/Loader/ResourceLoader.h>
#include <LibWeb/Page/Page.h>
#include <LibWeb/Platform/ImageCodecPlugin.h>
+#include <LibWeb/ReferrerPolicy/AbstractOperations.h>
#include <LibWeb/SVG/SVGDecodedImageData.h>
#include <LibWebCommon/Infra/CharacterTypes.h>
@@ -1484,7 +1485,7 @@ void HTMLLinkElement::process_stylesheet_resource(bool success, Fetch::Infrastru
// location stays the URL it was linked at.
CSS::Parser::Parser::parse_stylesheet_off_thread(
CSS::Parser::ParsingParams { document() }, maybe_decoded_string.release_value(),
- [link = GC::make_root(*this), loaded_document = GC::make_root(document()), fetch_generation = m_current_fetch_generation, location = response.url_list().first(), base_url = response.url_list().last(), origin_clean](CSS::Parser::RustStyleSheetParse parsed) mutable {
+ [link = GC::make_root(*this), loaded_document = GC::make_root(document()), fetch_generation = m_current_fetch_generation, location = response.url_list().first(), base_url = response.url_list().last(), origin_clean, referrer_policy = ReferrerPolicy::parse_a_referrer_policy_from_a_referrer_policy_header(response)](CSS::Parser::RustStyleSheetParse parsed) mutable {
if (fetch_generation != link->m_current_fetch_generation || !link->m_stylesheet_processing_pending)
return;
// NB: An inactive document's element tasks cannot run. Release its result here.
@@ -1497,7 +1498,7 @@ void HTMLLinkElement::process_stylesheet_resource(bool success, Fetch::Infrastru
// roots: the roots of a function that died would keep them alive through the collection that
// finds it dead.
link->queue_an_element_task(Task::Source::Networking,
- [link = GC::Ref { *link }, loaded_document = GC::Ref { *loaded_document }, fetch_generation, location = move(location), base_url = move(base_url), parsed = move(parsed), origin_clean] {
+ [link = GC::Ref { *link }, loaded_document = GC::Ref { *loaded_document }, fetch_generation, location = move(location), base_url = move(base_url), parsed = move(parsed), origin_clean, referrer_policy] {
if (fetch_generation != link->m_current_fetch_generation || !link->m_stylesheet_processing_pending)
return;
if (&link->document() != loaded_document.ptr() || !loaded_document->is_fully_active()
@@ -1507,8 +1508,8 @@ void HTMLLinkElement::process_stylesheet_resource(bool success, Fetch::Infrastru
return;
}
- CSS::Parser::Parser parser { CSS::Parser::ParsingParams { *loaded_document } };
- auto sheet = parser.create_css_stylesheet(parsed, location);
+ // NB: The sheet records its referrer policy as it is created, before its imports start to load.
+ auto sheet = CSS::StyleSheetState::create(parsed.native_rules(), loaded_document.ptr(), {}, location, referrer_policy);
if (base_url != location)
sheet->set_base_url(move(base_url));
link->associate_style_sheet(*sheet, origin_clean);
diff --git a/Tests/LibWeb/Text/expected/wpt-import/referrer-policy/css-integration/child-css/external-import-stylesheet.txt b/Tests/LibWeb/Text/expected/wpt-import/referrer-policy/css-integration/child-css/external-import-stylesheet.txt
index 3d618230761..c617052c326 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/referrer-policy/css-integration/child-css/external-import-stylesheet.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/referrer-policy/css-integration/child-css/external-import-stylesheet.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail Child css from external stylesheet.
\ No newline at end of file
+1 Pass
+Pass Child css from external stylesheet.
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/referrer-policy/css-integration/child-css/internal-import-stylesheet.txt b/Tests/LibWeb/Text/expected/wpt-import/referrer-policy/css-integration/child-css/internal-import-stylesheet.txt
index 173a334896d..8364f8a90f6 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/referrer-policy/css-integration/child-css/internal-import-stylesheet.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/referrer-policy/css-integration/child-css/internal-import-stylesheet.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail Child css from internal stylesheet.
\ No newline at end of file
+1 Pass
+Pass Child css from internal stylesheet.
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/referrer-policy/css-integration/font-face/external-import-stylesheet.txt b/Tests/LibWeb/Text/expected/wpt-import/referrer-policy/css-integration/font-face/external-import-stylesheet.txt
index a8668a710df..03260193c12 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/referrer-policy/css-integration/font-face/external-import-stylesheet.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/referrer-policy/css-integration/font-face/external-import-stylesheet.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail Font from imported stylesheet (external).
\ No newline at end of file
+1 Pass
+Pass Font from imported stylesheet (external).
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/referrer-policy/css-integration/font-face/external-stylesheet.txt b/Tests/LibWeb/Text/expected/wpt-import/referrer-policy/css-integration/font-face/external-stylesheet.txt
index b72eb4b3aa6..5f5feeb1a3e 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/referrer-policy/css-integration/font-face/external-stylesheet.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/referrer-policy/css-integration/font-face/external-stylesheet.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail Font from external stylesheet.
\ No newline at end of file
+1 Pass
+Pass Font from external stylesheet.
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/referrer-policy/css-integration/font-face/internal-import-stylesheet.txt b/Tests/LibWeb/Text/expected/wpt-import/referrer-policy/css-integration/font-face/internal-import-stylesheet.txt
index 556b4eb80dc..0be72d3e716 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/referrer-policy/css-integration/font-face/internal-import-stylesheet.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/referrer-policy/css-integration/font-face/internal-import-stylesheet.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail Font from imported stylesheet (internal).
\ No newline at end of file
+1 Pass
+Pass Font from imported stylesheet (internal).
\ No newline at end of file