Skip to content

Truly independent web browser

LibWeb: Fetch SVG use resources in same-origin mode

Shannon Boothd4e2e907f112
diff --git a/Libraries/LibWeb/HTML/SharedResourceRequest.cpp b/Libraries/LibWeb/HTML/SharedResourceRequest.cpp
index d88210c634a..e3f5738d1b9 100644
--- a/Libraries/LibWeb/HTML/SharedResourceRequest.cpp
+++ b/Libraries/LibWeb/HTML/SharedResourceRequest.cpp
@@ -36,7 +36,10 @@ static u64 s_next_memory_cache_touch_serial;
GC::Ref<SharedResourceRequest> SharedResourceRequest::get_or_create(DOM::Document& document, URL::URL const& url, CORSSettingAttribute cors_setting)
{
- return get_or_create(document, { url, cors_setting, {}, ReferrerPolicy::ReferrerPolicy::EmptyString });
+ using Request = Fetch::Infrastructure::Request;
+ auto mode = cors_setting == CORSSettingAttribute::NoCORS ? Request::Mode::NoCORS : Request::Mode::CORS;
+ auto credentials_mode = cors_setting == CORSSettingAttribute::Anonymous ? Request::CredentialsMode::SameOrigin : Request::CredentialsMode::Include;
+ return get_or_create(document, { url, mode, credentials_mode, {}, ReferrerPolicy::ReferrerPolicy::EmptyString });
}
GC::Ref<SharedResourceRequest> SharedResourceRequest::get_or_create(DOM::Document& document, Key key)
@@ -53,20 +56,13 @@ GC::Ref<SharedResourceRequest> SharedResourceRequest::get_or_create(DOM::Documen
GC::Ref<SharedResourceRequest> SharedResourceRequest::get_or_create(DOM::Document& document, Fetch::Infrastructure::Request const& request)
{
- // NB: This is the CORS settings attribute state that creating a potential-CORS request turns into the mode and
- // credentials mode of request.
- auto cors_setting = CORSSettingAttribute::NoCORS;
- if (request.mode() != Fetch::Infrastructure::Request::Mode::NoCORS) {
- cors_setting = request.credentials_mode() == Fetch::Infrastructure::Request::CredentialsMode::Include
- ? CORSSettingAttribute::UseCredentials
- : CORSSettingAttribute::Anonymous;
- }
- return get_or_create(document, { request.url(), cors_setting, request.integrity_metadata(), request.referrer_policy() });
+ return get_or_create(document, { request.url(), request.mode(), request.credentials_mode(), request.integrity_metadata(), request.referrer_policy() });
}
GC::Ref<SharedResourceRequest> SharedResourceRequest::create_from_navigation_response_body(DOM::Document& document, Fetch::Infrastructure::Body const& body, GC::Ref<GC::Function<void()>> on_body_read)
{
- auto request = GC::Heap::the().allocate<SharedResourceRequest>(document.page(), Key { document.url(), CORSSettingAttribute::NoCORS, {}, ReferrerPolicy::ReferrerPolicy::EmptyString }, document);
+ using Request = Fetch::Infrastructure::Request;
+ auto request = GC::Heap::the().allocate<SharedResourceRequest>(document.page(), Key { document.url(), Request::Mode::NoCORS, Request::CredentialsMode::Include, {}, ReferrerPolicy::ReferrerPolicy::EmptyString }, document);
request->m_can_be_shared = false;
request->m_state = State::Fetching;
// NB: The element that presents the image may start to wait for it only after the body has been read.
diff --git a/Libraries/LibWeb/HTML/SharedResourceRequest.h b/Libraries/LibWeb/HTML/SharedResourceRequest.h
index d5e93294125..37412642ef0 100644
--- a/Libraries/LibWeb/HTML/SharedResourceRequest.h
+++ b/Libraries/LibWeb/HTML/SharedResourceRequest.h
@@ -23,11 +23,12 @@ class SharedResourceRequest final : public JS::Cell {
GC_DECLARE_ALLOCATOR(SharedResourceRequest);
public:
- // NB: Only requests with the same CORS mode, credentials mode, integrity metadata and referrer policy share a
+ // NB: Only requests with the same mode, credentials mode, integrity metadata and referrer policy share a
// resource. The origin is the same for every request of a document.
struct Key {
URL::URL url;
- CORSSettingAttribute cors_setting { CORSSettingAttribute::NoCORS };
+ Fetch::Infrastructure::Request::Mode mode;
+ Fetch::Infrastructure::Request::CredentialsMode credentials_mode;
Utf16String integrity_metadata;
ReferrerPolicy::ReferrerPolicy referrer_policy { ReferrerPolicy::ReferrerPolicy::EmptyString };
@@ -141,7 +142,8 @@ template<>
struct Traits<Web::HTML::SharedResourceRequest::Key> : public DefaultTraits<Web::HTML::SharedResourceRequest::Key> {
static unsigned hash(Web::HTML::SharedResourceRequest::Key const& key)
{
- auto hash = pair_int_hash(Traits<URL::URL>::hash(key.url), to_underlying(key.cors_setting));
+ auto hash = pair_int_hash(Traits<URL::URL>::hash(key.url), to_underlying(key.mode));
+ hash = pair_int_hash(hash, to_underlying(key.credentials_mode));
hash = pair_int_hash(hash, key.integrity_metadata.hash());
return pair_int_hash(hash, to_underlying(key.referrer_policy));
}
diff --git a/Libraries/LibWeb/SVG/SVGUseElement.cpp b/Libraries/LibWeb/SVG/SVGUseElement.cpp
index 16a1379094e..d49122a3b9f 100644
--- a/Libraries/LibWeb/SVG/SVGUseElement.cpp
+++ b/Libraries/LibWeb/SVG/SVGUseElement.cpp
@@ -279,12 +279,6 @@ GC::Ptr<DOM::Element> SVGUseElement::referenced_element() const
if (!data || !is<SVG::SVGDecodedImageData>(*data))
return nullptr;
- // https://svgwg.org/svg2-draft/struct.html#UseElement
- // User agents may restrict external resource documents for security reasons. In particular, this specification
- // does not allow cross-origin and data URL resource requests in ‘use’.
- if (data->is_cors_cross_origin())
- return nullptr;
-
return as<SVG::SVGDecodedImageData>(*data).svg_document().get_element_by_id(decode_fragment_identifier(*m_href->fragment()));
}
@@ -320,8 +314,14 @@ void SVGUseElement::fetch_the_document(URL::URL const& url)
if (document_url.scheme() == "data"sv)
return;
+ // For all other references, the "no-cors" state.
+ // AD-HOC: Set the same-origin fallback flag, as HTML does for track elements, so that Fetch enforces the ‘use’
+ // origin restriction before sending a request and across redirects.
+ auto request = HTML::create_potential_CORS_request(document_url, Fetch::Infrastructure::Request::Destination::Image, HTML::CORSSettingAttribute::NoCORS, HTML::SameOriginFallbackFlag::Yes);
+ request->set_client(document().relevant_settings_object().address());
+
m_load_event_delayer.emplace(document());
- m_resource_request = HTML::SharedResourceRequest::get_or_create(document(), document_url, HTML::CORSSettingAttribute::NoCORS);
+ m_resource_request = HTML::SharedResourceRequest::get_or_create(document(), *request);
m_resource_request_callbacks_id = m_resource_request->add_removable_callbacks(
[this] {
// A request that already finished calls back right away, which from adopted_from() is in the middle of the
@@ -337,11 +337,8 @@ void SVGUseElement::fetch_the_document(URL::URL const& url)
m_load_event_delayer.clear();
});
- if (m_resource_request->needs_fetching()) {
- auto request = HTML::create_potential_CORS_request(document_url, Fetch::Infrastructure::Request::Destination::Image, HTML::CORSSettingAttribute::NoCORS);
- request->set_client(document().relevant_settings_object().address());
+ if (m_resource_request->needs_fetching())
m_resource_request->fetch_resource(request);
- }
}
// https://svgwg.org/svg2-draft/struct.html#UseShadowTree
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index d9880fc8fcc..63b8e4b287e 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -1235,11 +1235,16 @@ Text/input/wpt-import/workers/modules/shared-worker-import-failure.html
Text/input/wpt-import/import-maps/data-driven/resolving.html
; A use element does not render elements of a cross-origin document, and every file: URL is a different origin.
+Crash/SVG/use-element-fetch-in-collected-document.html
+Crash/SVG/use-element-with-cached-external-reference-adopted.html
Layout/input/svg/svg-use-element-crashtest.html
+Text/input/HTML/Element-setHTMLUnsafe-removed-elements-do-not-fetch.html
+Text/input/SVG/use-element-blocks-cross-origin-requests.html
Text/input/SVG/use-element-external-fragments-share-one-fetch.html
Text/input/SVG/use-element-external-reference-after-adoption.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
[CapFrameRate]
; Ends a smooth scroll once 20 frames see no change, which frames faster than the compositor's ticks do
diff --git a/Tests/LibWeb/Text/expected/SVG/use-element-blocks-cross-origin-requests.txt b/Tests/LibWeb/Text/expected/SVG/use-element-blocks-cross-origin-requests.txt
new file mode 100644
index 00000000000..2eaa0710448
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/SVG/use-element-blocks-cross-origin-requests.txt
@@ -0,0 +1,4 @@
+same-origin width: 20
+direct width: 0, cross-origin requests: 0
+same-origin redirect requests: 1
+redirect width: 0, cross-origin requests: 0
diff --git a/Tests/LibWeb/Text/input/SVG/use-element-blocks-cross-origin-requests.html b/Tests/LibWeb/Text/input/SVG/use-element-blocks-cross-origin-requests.html
new file mode 100644
index 00000000000..f2008a731b8
--- /dev/null
+++ b/Tests/LibWeb/Text/input/SVG/use-element-blocks-cross-origin-requests.html
@@ -0,0 +1,43 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ asyncTest(async done => {
+ const server = httpTestServer();
+ const crossOrigin = new URL(server.baseURL);
+ crossOrigin.hostname = uniqueLocalhostHostname("cross-origin");
+ const createSprite = path => server.createEcho("GET", path, {
+ status: 200,
+ headers: { "Content-Type": "image/svg+xml", "Access-Control-Allow-Origin": "*" },
+ body: '<svg xmlns="http://www.w3.org/2000/svg"><rect id="rect" width="20" height="20"/></svg>',
+ });
+ const sameOriginSprite = await createSprite("/same-origin.svg");
+ const directSprite = new URL(new URL(await createSprite("/direct.svg")).pathname, crossOrigin);
+ const redirectedSprite = new URL(new URL(await createSprite("/redirected.svg")).pathname, crossOrigin);
+ const redirect = await server.createEcho("GET", "/redirect", {
+ status: 302,
+ headers: { Location: redirectedSprite.href },
+ body: "",
+ });
+ const page = await server.createEcho("GET", "/page.html", {
+ status: 200,
+ headers: { "Content-Type": "text/html" },
+ body: `<svg><use id="same" href="${sameOriginSprite}#rect"/>
+ <use id="direct" href="${directSprite}#rect"/>
+ <use id="redirect" href="${redirect}#rect"/></svg>`,
+ });
+
+ const frame = document.createElement("iframe");
+ const loaded = new Promise(resolve => frame.onload = resolve);
+ frame.src = page;
+ document.body.append(frame);
+ await loaded;
+
+ const width = id => frame.contentDocument.getElementById(id).getBBox().width;
+ const requestCount = async url => (await fetch(server.recordedRequestCountURL(url))).json();
+ println(`same-origin width: ${width("same")}`);
+ println(`direct width: ${width("direct")}, cross-origin requests: ${await requestCount(directSprite)}`);
+ println(`same-origin redirect requests: ${await requestCount(redirect)}`);
+ println(`redirect width: ${width("redirect")}, cross-origin requests: ${await requestCount(redirectedSprite)}`);
+ done();
+ });
+</script>