Truly independent web browser
LibWeb: Only let an SVG image fetch data: URLs
Shannon Booth241fe045563c
diff --git a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp b/Libraries/LibWeb/Fetch/Fetching/Fetching.cppindex 24e7d75fa3f..1f216172e26 100644--- a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp+++ b/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp@@ -561,6 +561,13 @@ void populate_request_from_client(Infrastructure::Request& request, Optional<HTMrequest.set_client_snapshot(client->visit([](auto const& client) { return snapshot_client_context(*client); }));}+static bool is_request_from_an_svg_image(Infrastructure::Request const& request)+{+ auto client = HTML::settings_object_of_fetch_client(resolve_fetch_client(request));+ auto* window = client ? HTML::window_from_global_object(client->global_object()) : nullptr;+ return window && window->associated_document().is_decoded_svg();+}+// https://fetch.spec.whatwg.org/#concept-main-fetchGC::Ptr<PendingResponse> main_fetch(JS::Realm& realm, Infrastructure::FetchParams const& fetch_params, Recursive recursive){@@ -580,6 +587,11 @@ GC::Ptr<PendingResponse> main_fetch(JS::Realm& realm, Infrastructure::FetchParamif (request->local_urls_only() && !Infrastructure::is_local_url(request->current_url()))response = Infrastructure::Response::network_error("Request with 'local-URLs-only' flag must have a local URL"_string);+ // AD-HOC: An SVG image is processed in a secure mode, where any URL but a data: URL is a network error.+ // https://svgwg.org/svg2-draft/linking.html#processingURL-fetch+ if (request->current_url().scheme() != "data"sv && is_request_from_an_svg_image(*request))+ response = Infrastructure::Response::network_error("An SVG image may only fetch data: URLs"_string);+// 4. Run report Content Security Policy violations for request.auto report_violation = ContentSecurityPolicy::violation_reporter_for_request(realm, request);ContentSecurityPolicy::report_content_security_policy_violations_for_request(request, report_violation);diff --git a/Libraries/LibWeb/HTML/HTMLLinkElement.cpp b/Libraries/LibWeb/HTML/HTMLLinkElement.cppindex 3803849ca30..1c3a4bda30c 100644--- a/Libraries/LibWeb/HTML/HTMLLinkElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLLinkElement.cpp@@ -658,6 +658,9 @@ void HTMLLinkElement::fetch_and_process_linked_dns_prefetch_resource()// FIXME: This should go through Fetch: https://fetch.spec.whatwg.org/#resolve-an-originif (!ResourceLoader::is_initialized())return;+ // AD-HOC: Until then, keep an SVG image off the network as Fetch would.+ if (document().is_decoded_svg())+ return;ResourceLoader::the().prefetch_dns(document(), url.value(), document().fallback_base_url());}@@ -959,6 +962,9 @@ void HTMLLinkElement::preconnect(LinkProcessingOptions const& options)// FIXME: This should go through Fetch: https://fetch.spec.whatwg.org/#concept-connection-obtainif (!ResourceLoader::is_initialized())return;+ // AD-HOC: Until then, keep an SVG image off the network as Fetch would.+ if (options.document && options.document->is_decoded_svg())+ return;auto source_url = options.document ? options.document->fallback_base_url() : options.environment->api_base_url();ResourceLoader::the().preconnect(options.document, *url, source_url);diff --git a/Tests/LibWeb/Text/expected/SVG/svg-image-does-not-load-external-resources.txt b/Tests/LibWeb/Text/expected/SVG/svg-image-does-not-load-external-resources.txtnew file mode 100644index 00000000000..6beca5f375e--- /dev/null+++ b/Tests/LibWeb/Text/expected/SVG/svg-image-does-not-load-external-resources.txt@@ -0,0 +1,4 @@+External image: 0,0,0,0+data: image: 18,52,86,255+blob: image: 0,0,0,0+Requests for the external image: 0diff --git a/Tests/LibWeb/Text/input/SVG/svg-image-does-not-load-external-resources.html b/Tests/LibWeb/Text/input/SVG/svg-image-does-not-load-external-resources.htmlnew file mode 100644index 00000000000..59cc5dcdd3e--- /dev/null+++ b/Tests/LibWeb/Text/input/SVG/svg-image-does-not-load-external-resources.html@@ -0,0 +1,37 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<canvas id="canvas" width="30" height="10"></canvas>+<script>+ asyncTest(async done => {+ const pixelSVG = `<svg xmlns="http://www.w3.org/2000/svg" width="10" height="10"><rect width="10" height="10" fill="#123456"/></svg>`;++ const server = httpTestServer();+ const externalURL = await server.createEcho("GET", "/svg-image-does-not-load-external-resources-pixel.svg", {+ status: 200,+ headers: { "Content-Type": "image/svg+xml", "Cache-Control": "no-store" },+ body: pixelSVG,+ });+ const dataURL = `data:image/svg+xml,${encodeURIComponent(pixelSVG)}`;+ const blobURL = URL.createObjectURL(new Blob([pixelSVG], { type: "image/svg+xml" }));++ const outerSVG = `<svg xmlns="http://www.w3.org/2000/svg" width="30" height="10">+ <image href="${externalURL}" width="10" height="10"/>+ <image href="${dataURL}" x="10" width="10" height="10"/>+ <image href="${blobURL}" x="20" width="10" height="10"/>+ </svg>`;++ const image = new Image();+ image.onload = async () => {+ const context = canvas.getContext("2d");+ context.drawImage(image, 0, 0);+ println(`External image: ${context.getImageData(5, 5, 1, 1).data}`);+ println(`data: image: ${context.getImageData(15, 5, 1, 1).data}`);+ println(`blob: image: ${context.getImageData(25, 5, 1, 1).data}`);++ const requestCount = await (await fetch(server.recordedRequestCountURL(externalURL))).json();+ println(`Requests for the external image: ${requestCount}`);+ done();+ };+ image.src = `data:image/svg+xml,${encodeURIComponent(outerSVG)}`;+ });+</script>