Skip to content

Truly independent web browser

LibWeb: Mute errors from cross-origin classic scripts

Andreas Klingf387b999c7ac
diff --git a/Libraries/LibWeb/HTML/Scripting/Fetching.cpp b/Libraries/LibWeb/HTML/Scripting/Fetching.cpp
index 640c94424c9..28890aab7d6 100644
--- a/Libraries/LibWeb/HTML/Scripting/Fetching.cpp
+++ b/Libraries/LibWeb/HTML/Scripting/Fetching.cpp
@@ -666,6 +666,10 @@ void fetch_classic_script(GC::Ref<HTMLScriptElement> element, URL::URL const& ur
// or a byte sequence bodyBytes:
Fetch::Infrastructure::FetchAlgorithms::Input fetch_algorithms_input { Fetch::Engine::BodyIntent::ConsumeForCallback };
fetch_algorithms_input.process_response_consume_body = [request, &settings_object, options = move(options), character_encoding = move(character_encoding), on_complete = move(on_complete)](auto response, auto body_bytes) {
+ // NB: Whether response was CORS-cross-origin must be determined before unwrapping it, since the internal
+ // response of an opaque filtered response is never CORS-cross-origin itself.
+ auto response_was_cors_cross_origin = response->is_cors_cross_origin();
+
// 1. Set response to response's unsafe response.
response = response->unsafe_response();
@@ -693,7 +697,7 @@ void fetch_classic_script(GC::Ref<HTMLScriptElement> element, URL::URL const& ur
VERIFY(fallback_decoder.has_value());
// 6. Let muted errors be true if response was CORS-cross-origin, and false otherwise.
- auto muted_errors = response->is_cors_cross_origin() ? ClassicScript::MutedErrors::Yes : ClassicScript::MutedErrors::No;
+ auto muted_errors = response_was_cors_cross_origin ? ClassicScript::MutedErrors::Yes : ClassicScript::MutedErrors::No;
// 7. Let script be the result of creating a classic script given source text, settings object's realm, response's URL,
// options, and muted errors.
@@ -936,6 +940,10 @@ WebIDL::ExceptionOr<GC::Ref<ClassicScript>> fetch_a_classic_worker_imported_scri
return !!response;
}));
+ // NB: Whether response was CORS-cross-origin must be determined before unwrapping it, since the internal
+ // response of an opaque filtered response is never CORS-cross-origin itself.
+ auto response_was_cors_cross_origin = response->is_cors_cross_origin();
+
// 6. Set response to response's unsafe response.
response = response->unsafe_response();
@@ -954,7 +962,7 @@ WebIDL::ExceptionOr<GC::Ref<ClassicScript>> fetch_a_classic_worker_imported_scri
auto source_text = Utf16String::from_utf8_with_replacement_character(body_bytes_view(body_bytes));
// 9. Let mutedErrors be true if response was CORS-cross-origin, and false otherwise.
- auto muted_errors = response->is_cors_cross_origin() ? ClassicScript::MutedErrors::Yes : ClassicScript::MutedErrors::No;
+ auto muted_errors = response_was_cors_cross_origin ? ClassicScript::MutedErrors::Yes : ClassicScript::MutedErrors::No;
// 10. Let script be the result of creating a classic script given sourceText, settingsObject, response's URL, the default classic script fetch options, and mutedErrors.
auto response_url = response->url().value_or({});
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index c783c8395e7..cb41a452918 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -274,6 +274,10 @@ Text/input/css/css-image-snapshotted-stylesheet-referrer.html
Text/input/css/css-imported-image-origin-clean.html
Text/input/css/css-inline-import-initiator-type.html
+; Errors from file: scripts are muted, since file: URLs have an opaque origin.
+Text/input/wpt-import/html/webappapis/scripting/reporterror.any.html
+Text/input/wpt-import/html/webappapis/scripting/processing-model-2/compile-error-same-origin.html
+
; Imports a classic script from a cross-origin localhost subdomain.
Text/input/HTML/importScripts-cross-origin-errors-are-muted.html
diff --git a/Tests/LibWeb/Text/expected/HTML/importScripts-cross-origin-errors-are-muted.txt b/Tests/LibWeb/Text/expected/HTML/importScripts-cross-origin-errors-are-muted.txt
index 22fe05ff230..314ae332a98 100644
--- a/Tests/LibWeb/Text/expected/HTML/importScripts-cross-origin-errors-are-muted.txt
+++ b/Tests/LibWeb/Text/expected/HTML/importScripts-cross-origin-errors-are-muted.txt
@@ -1 +1 @@
-importScripts() threw Error: unmuted marker
+importScripts() threw NetworkError: Script error.
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/muted-errors.sub.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/muted-errors.sub.txt
index 1411a68129c..ca3ce7f3e59 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/muted-errors.sub.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/muted-errors.sub.txt
@@ -2,12 +2,11 @@ Harness status: OK
Found 7 tests
-1 Pass
-6 Fail
+7 Pass
Pass Errors for same-origin script shouldn't be muted
-Fail Errors for cross-origin script should be muted
-Fail Errors for cross-origin script should be muted even if the script is once loaded as same-origin
-Fail Errors for same-origin scripts redirected to a cross-origin url and redirected back to same-origin should be muted
-Fail Errors for cross-origin scripts redirected to a same-origin url should be muted
-Fail Non-synthetic errors for same-origin scripts redirected to a cross-origin URL and redirected back to same-origin should be muted
-Fail Syntax error for same-origin script redirected to a cross-origin URL and redirected back to same-origin should be muted
\ No newline at end of file
+Pass Errors for cross-origin script should be muted
+Pass Errors for cross-origin script should be muted even if the script is once loaded as same-origin
+Pass Errors for same-origin scripts redirected to a cross-origin url and redirected back to same-origin should be muted
+Pass Errors for cross-origin scripts redirected to a same-origin url should be muted
+Pass Non-synthetic errors for same-origin scripts redirected to a cross-origin URL and redirected back to same-origin should be muted
+Pass Syntax error for same-origin script redirected to a cross-origin URL and redirected back to same-origin should be muted
\ No newline at end of file