Truly independent web browser
LibWeb: Keep origin and CSP limits on SVG use element clones
Shannon Booth42bb2eef0279
diff --git a/Libraries/LibWeb/SVG/SVGUseElement.cpp b/Libraries/LibWeb/SVG/SVGUseElement.cppindex 1d373a0d0f4..e896a24a8e3 100644--- a/Libraries/LibWeb/SVG/SVGUseElement.cpp+++ b/Libraries/LibWeb/SVG/SVGUseElement.cpp@@ -276,6 +276,12 @@ GC::Ptr<DOM::Element> SVGUseElement::referenced_element() constif (!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()));}@@ -305,6 +311,12 @@ void SVGUseElement::fetch_the_document(URL::URL const& url)stop_using_resource_request();+ // 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 (document_url.scheme() == "data"sv)+ return;+m_load_event_delayer.emplace(document());m_resource_request = HTML::SharedResourceRequest::get_or_create(document(), document_url, HTML::CORSSettingAttribute::NoCORS);m_resource_request_callbacks_id = m_resource_request->add_removable_callbacks(@@ -341,7 +353,7 @@ void SVGUseElement::clone_element_tree_as_our_shadow_tree(GC::Ptr<Element> to_clif (to_clone && is_valid_reference_element(*to_clone)) {// The ‘use’ element references another element, a copy of which is rendered in place of the ‘use’ in the document.- auto cloned_reference_node = MUST(to_clone->clone_node(nullptr, true));+ auto cloned_reference_node = MUST(to_clone->clone_node(&document(), true));if (is<SVGSVGElement>(cloned_reference_node.ptr()) || is<SVGSymbolElement>(cloned_reference_node.ptr())) {auto& cloned_element = as<SVGElement>(*cloned_reference_node);diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.iniindex 4bad52eaab9..d9880fc8fcc 100644--- a/Tests/LibWeb/TestConfig.ini+++ b/Tests/LibWeb/TestConfig.ini@@ -1234,6 +1234,13 @@ Text/input/wpt-import/workers/modules/dedicated-worker-import-failure.htmlText/input/wpt-import/workers/modules/shared-worker-import-failure.htmlText/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.+Layout/input/svg/svg-use-element-crashtest.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+[CapFrameRate]; Ends a smooth scroll once 20 frames see no change, which frames faster than the compositor's ticks doText/input/wpt-import/css/cssom-view/scroll-behavior-default-css.htmldiff --git a/Tests/LibWeb/Text/expected/SVG/use-external-reference-origin-and-csp.txt b/Tests/LibWeb/Text/expected/SVG/use-external-reference-origin-and-csp.txtnew file mode 100644index 00000000000..061054667da--- /dev/null+++ b/Tests/LibWeb/Text/expected/SVG/use-external-reference-origin-and-csp.txt@@ -0,0 +1,4 @@+same-origin: handler ran+cross-origin: handler did not run+csp: handler did not run+data-url: handler did not rundiff --git a/Tests/LibWeb/Text/input/SVG/use-external-reference-origin-and-csp.html b/Tests/LibWeb/Text/input/SVG/use-external-reference-origin-and-csp.htmlnew file mode 100644index 00000000000..8f844411e9d--- /dev/null+++ b/Tests/LibWeb/Text/input/SVG/use-external-reference-origin-and-csp.html@@ -0,0 +1,71 @@+<!doctype html>+<script src="../include.js"></script>+<script>+ asyncTest(async done => {+ const server = httpTestServer();+ const pageOrigin = `http://${uniqueLocalhostHostname("page")}:${new URL(server.baseURL).port}`;+ const crossOrigin = `http://${uniqueLocalhostHostname("other")}:${new URL(server.baseURL).port}`;++ const svgBody = name => `<svg xmlns="http://www.w3.org/2000/svg">+ <g id="x"><foreignObject width="20" height="20">+ <img xmlns="http://www.w3.org/1999/xhtml" src="data:text/plain,not-an-image"+ onerror="parent.postMessage('${name}: handler ran', '*')" />+ </foreignObject></g>+ </svg>`;+ const createSVG = async name =>+ new URL(+ await server.createEcho("GET", `/${name}.svg`, {+ status: 200,+ headers: { "Content-Type": "image/svg+xml" },+ body: svgBody(name),+ })+ ).pathname;++ const createPage = async (name, svgURL, headers = {}) => {+ const pageURL = await server.createEcho("GET", `/${name}.html`, {+ status: 200,+ headers: { "Content-Type": "text/html", ...headers },+ body: `<!DOCTYPE html>+ <svg><use href="${svgURL}#x"></use></svg>+ <script nonce="fixed">+ window.onload = () => parent.postMessage("${name}: loaded", "*");+ <\/script>`,+ });+ return pageOrigin + new URL(pageURL).pathname;+ };++ const run = async (name, svgURL, headers) => {+ const pageURL = await createPage(name, svgURL, headers);+ const messages = [];+ const finished = new Promise(resolve => {+ const listener = event => {+ messages.push(event.data);+ if (event.data === `${name}: loaded`) {+ window.removeEventListener("message", listener);+ resolve();+ }+ };+ window.addEventListener("message", listener);+ });+ const frame = document.createElement("iframe");+ frame.src = pageURL;+ document.body.appendChild(frame);+ await finished;+ frame.remove();+ println(+ messages.includes(`${name}: handler ran`) ? `${name}: handler ran` : `${name}: handler did not run`+ );+ };++ const sameOriginSVG = pageOrigin + (await createSVG("same-origin"));+ const crossOriginSVG = crossOrigin + (await createSVG("cross-origin"));+ const cspSVG = pageOrigin + (await createSVG("csp"));+ const dataURLSVG = "data:image/svg+xml," + encodeURIComponent(svgBody("data-url"));++ await run("same-origin", sameOriginSVG);+ await run("cross-origin", crossOriginSVG);+ await run("csp", cspSVG, { "Content-Security-Policy": "script-src 'nonce-fixed'" });+ await run("data-url", dataURLSVG);+ done();+ });+</script>