Skip to content

Truly independent web browser

LibWeb/SVG: Set muted errors on cross-origin external scripts

Luke Wilde462cb025a602
diff --git a/Libraries/LibWeb/SVG/SVGScriptElement.cpp b/Libraries/LibWeb/SVG/SVGScriptElement.cpp
index 633010f9d20..64bbb23c3de 100644
--- a/Libraries/LibWeb/SVG/SVGScriptElement.cpp
+++ b/Libraries/LibWeb/SVG/SVGScriptElement.cpp
@@ -134,14 +134,17 @@ void SVGScriptElement::process_the_script_element()
Fetch::Infrastructure::FetchAlgorithms::Input fetch_algorithms_input { Fetch::Engine::BodyIntent::ConsumeForCallback };
fetch_algorithms_input.process_response_consume_body
= [self = GC::Ref { *this }, script_url](auto response, auto body_bytes) {
+ // https://html.spec.whatwg.org/multipage/webappapis.html#fetch-a-classic-script
+ // 6. Let muted errors be true if response was CORS-cross-origin, and false otherwise.
+ auto muted_errors = response->is_cors_cross_origin() ? HTML::ClassicScript::MutedErrors::Yes : HTML::ClassicScript::MutedErrors::No;
if (response->is_network_error()) {
- self->finish_external_script_fetch(script_url, {});
+ self->finish_external_script_fetch(script_url, {}, muted_errors);
return;
}
body_bytes.visit(
- [&](Core::ImmutableBytes& bytes) { self->finish_external_script_fetch(script_url, bytes.bytes()); },
- [&](auto) { self->finish_external_script_fetch(script_url, {}); });
+ [&](Core::ImmutableBytes& bytes) { self->finish_external_script_fetch(script_url, bytes.bytes(), muted_errors); },
+ [&](auto) { self->finish_external_script_fetch(script_url, {}, muted_errors); });
};
(void)Fetch::Fetching::fetch(HTML::relevant_realm(*this), request,
@@ -164,7 +167,7 @@ void SVGScriptElement::process_the_script_element()
execute_script();
}
-void SVGScriptElement::finish_external_script_fetch(URL::URL const& script_url, ReadonlyBytes body)
+void SVGScriptElement::finish_external_script_fetch(URL::URL const& script_url, ReadonlyBytes body, HTML::ClassicScript::MutedErrors muted_errors)
{
if (!body.is_empty() && in_a_document_tree() && !is_scripting_disabled()) {
auto script_content = Utf16String::try_from_utf8({ body });
@@ -172,7 +175,7 @@ void SVGScriptElement::finish_external_script_fetch(URL::URL const& script_url,
dbgln("Failed to decode SVG external script as UTF-8");
else
m_script = HTML::ClassicScript::create(script_url.basename(), script_content.release_value(),
- HTML::relevant_settings_object(*this), m_document->base_url(), m_source_line_number);
+ HTML::relevant_settings_object(*this), m_document->base_url(), m_source_line_number, muted_errors);
}
if (m_parser_inserted) {
diff --git a/Libraries/LibWeb/SVG/SVGScriptElement.h b/Libraries/LibWeb/SVG/SVGScriptElement.h
index 6901c341d9a..820b2347e61 100644
--- a/Libraries/LibWeb/SVG/SVGScriptElement.h
+++ b/Libraries/LibWeb/SVG/SVGScriptElement.h
@@ -9,6 +9,7 @@
#include <AK/Forward.h>
#include <LibURL/URL.h>
#include <LibWeb/DOM/DocumentLoadEventDelayer.h>
+#include <LibWeb/HTML/Scripting/ClassicScript.h>
#include <LibWeb/SVG/SVGElement.h>
#include <LibWeb/SVG/SVGURIReference.h>
@@ -46,7 +47,7 @@ private:
virtual void visit_edges(Cell::Visitor&) override;
virtual void adopted_from(DOM::Document&) override;
- void finish_external_script_fetch(URL::URL const& script_url, ReadonlyBytes body);
+ void finish_external_script_fetch(URL::URL const& script_url, ReadonlyBytes body, HTML::ClassicScript::MutedErrors);
void execute_script();
bool m_already_processed { false };
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index b7702032268..e81abb8003a 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -281,6 +281,9 @@ Text/input/wpt-import/html/webappapis/scripting/processing-model-2/compile-error
; Imports a classic script from a cross-origin localhost subdomain.
Text/input/HTML/importScripts-cross-origin-errors-are-muted.html
+; Compares the errors of same-origin and cross-origin SVG scripts.
+Text/input/SVG/svg-script-mutes-cross-origin-script-errors.html
+
; Observes stylesheet image requests through the HTTP test server.
Text/input/css/native-stylesheet-image-registration.html
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
new file mode 100644
index 00000000000..69e532eb074
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/SVG/svg-script-mutes-cross-origin-script-errors.txt
@@ -0,0 +1,2 @@
+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
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
new file mode 100644
index 00000000000..e505d55140e
--- /dev/null
+++ b/Tests/LibWeb/Text/input/SVG/svg-script-mutes-cross-origin-script-errors.html
@@ -0,0 +1,37 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ promiseTest(async () => {
+ removeTestErrorHandler();
+
+ const server = httpTestServer();
+ const script = {
+ status: 200,
+ headers: { "Content-Type": "text/javascript" },
+ body: "throw new TypeError('script detail');",
+ };
+ 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");
+
+ function runScript(url) {
+ 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");
+ element.setAttribute("href", url.href);
+ svg.appendChild(element);
+ document.body.appendChild(svg);
+ });
+ }
+
+ function printError(label, event) {
+ println(
+ `${label}: message "${event.message}", filename "${event.filename === "" ? "" : "(set)"}", line ${event.lineno}, column ${event.colno}, error ${event.error}`
+ );
+ }
+
+ printError("Same-origin", await runScript(sameOrigin));
+ printError("Cross-origin", await runScript(crossOrigin));
+ });
+</script>