Skip to content

Truly independent web browser

LibWeb/SVG: Fetch external scripts with their crossorigin CORS state

Luke Wildec4679811bfc5
diff --git a/Libraries/LibWeb/SVG/SVGScriptElement.cpp b/Libraries/LibWeb/SVG/SVGScriptElement.cpp
index 64bbb23c3de..5664bf475f4 100644
--- a/Libraries/LibWeb/SVG/SVGScriptElement.cpp
+++ b/Libraries/LibWeb/SVG/SVGScriptElement.cpp
@@ -13,6 +13,9 @@
#include <LibWeb/Fetch/Infrastructure/FetchAlgorithms.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/ResponseView.h>
+#include <LibWeb/HTML/AttributeNames.h>
+#include <LibWeb/HTML/CORSSettingAttribute.h>
+#include <LibWeb/HTML/PotentialCORSRequest.h>
#include <LibWeb/HTML/Scripting/ClassicScript.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/Namespace.h>
@@ -109,13 +112,17 @@ void SVGScriptElement::process_the_script_element()
}
auto script_url = maybe_script_url.release_value();
- auto request = Fetch::Infrastructure::Request::create();
- request->set_url(script_url);
- request->set_destination(Fetch::Infrastructure::Request::Destination::Script);
- // FIXME: Use CORS state specified by the ‘crossorigin’ attribute.
- request->set_mode(Fetch::Infrastructure::Request::Mode::NoCORS);
- request->set_credentials_mode(Fetch::Infrastructure::Request::CredentialsMode::SameOrigin);
+ // https://html.spec.whatwg.org/multipage/webappapis.html#fetch-a-classic-script
+ // 1. Let request be the result of creating a potential-CORS request given url, "script", and CORS setting.
+ auto crossorigin = get_attribute(HTML::AttributeNames::crossorigin);
+ auto cors_setting = HTML::cors_setting_attribute_from_keyword(crossorigin.map([](auto const& value) { return value.utf16_view(); }));
+ auto request = HTML::create_potential_CORS_request(script_url, Fetch::Infrastructure::Request::Destination::Script, cors_setting);
+
+ // 2. Set request's client to settings object.
request->set_client(document().relevant_settings_object().address());
+
+ // FIXME: 3. Set request's initiator type to "script".
+ // FIXME: 4. Set up the classic script request given request and options.
request->set_parser_metadata(m_parser_inserted
? Fetch::Infrastructure::Request::ParserMetadata::ParserInserted
: Fetch::Infrastructure::Request::ParserMetadata::NotParserInserted);
diff --git a/Tests/LibWeb/Text/expected/SVG/svg-script-mutes-cross-origin-script-errors.txt b/Tests/LibWeb/Text/expected/SVG/svg-script-mutes-cross-origin-script-errors.txt
index 69e532eb074..16e347232f1 100644
--- a/Tests/LibWeb/Text/expected/SVG/svg-script-mutes-cross-origin-script-errors.txt
+++ b/Tests/LibWeb/Text/expected/SVG/svg-script-mutes-cross-origin-script-errors.txt
@@ -1,2 +1,3 @@
Same-origin: message "TypeError: script detail", filename "(set)", line 1, column 7, error TypeError: script detail
Cross-origin: message "Script error.", filename "", line 0, column 0, error null
+Cross-origin with CORS: message "TypeError: script detail", filename "(set)", line 1, column 7, error TypeError: script detail
diff --git a/Tests/LibWeb/Text/input/SVG/svg-script-mutes-cross-origin-script-errors.html b/Tests/LibWeb/Text/input/SVG/svg-script-mutes-cross-origin-script-errors.html
index e505d55140e..a1330eac416 100644
--- a/Tests/LibWeb/Text/input/SVG/svg-script-mutes-cross-origin-script-errors.html
+++ b/Tests/LibWeb/Text/input/SVG/svg-script-mutes-cross-origin-script-errors.html
@@ -13,12 +13,17 @@
const sameOrigin = new URL(await server.createEcho("GET", "/same-origin-svg-script.js", script));
const crossOrigin = new URL(await server.createEcho("GET", "/cross-origin-svg-script.js", script));
crossOrigin.hostname = uniqueLocalhostHostname("svg-script-errors");
+ const corsScript = { ...script, headers: { ...script.headers, "Access-Control-Allow-Origin": "*" } };
+ const crossOriginWithCORS = new URL(await server.createEcho("GET", "/cross-origin-svg-script-with-cors.js", corsScript));
+ crossOriginWithCORS.hostname = crossOrigin.hostname;
- function runScript(url) {
+ function runScript(url, crossOriginAttribute) {
return new Promise(resolve => {
addEventListener("error", resolve, { once: true });
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
const element = document.createElementNS("http://www.w3.org/2000/svg", "script");
+ if (crossOriginAttribute !== undefined)
+ element.setAttribute("crossorigin", crossOriginAttribute);
element.setAttribute("href", url.href);
svg.appendChild(element);
document.body.appendChild(svg);
@@ -33,5 +38,6 @@
printError("Same-origin", await runScript(sameOrigin));
printError("Cross-origin", await runScript(crossOrigin));
+ printError("Cross-origin with CORS", await runScript(crossOriginWithCORS, "anonymous"));
});
</script>