Truly independent web browser
LibWeb: Use the requested URL in stack traces of redirected scripts
Tim Ledbetter0b96cacc1fe4
diff --git a/Libraries/LibWeb/HTML/Scripting/Fetching.cpp b/Libraries/LibWeb/HTML/Scripting/Fetching.cppindex bfe7e907f5f..54ba555897e 100644--- a/Libraries/LibWeb/HTML/Scripting/Fetching.cpp+++ b/Libraries/LibWeb/HTML/Scripting/Fetching.cpp@@ -699,7 +699,9 @@ void fetch_classic_script(GC::Ref<HTMLScriptElement> element, URL::URL const& urauto on_complete_root = GC::make_root(on_complete);auto settings_root = GC::make_root(settings_object);- auto response_url_string = response_url.to_byte_string();+ // NB: The filename is the request's URL. The response's URL could reveal where a cross-origin request was+ // redirected.+ auto filename = request->url().to_byte_string();auto source_byte_storage = body_bytes.template get<Core::ImmutableBytes>();auto source_bytes = source_byte_storage.bytes();auto const& bytecode = response->javascript_bytecode_cache();@@ -715,7 +717,7 @@ void fetch_classic_script(GC::Ref<HTMLScriptElement> element, URL::URL const& urauto source_encoding = ByteString { extracted_character_encoding };auto source_length = TextCodec::decoded_length_in_utf16_code_units(source_bytes, *fallback_decoder).release_value_but_fixme_should_propagate_errors();prepare_bytecode_cache_off_thread(settings_object, *bytecode, JS::ProgramType::Script, source_length, *source_hash,- [response_url = move(response_url), response_url_string = move(response_url_string),+ [response_url = move(response_url), filename = move(filename),source_byte_storage = move(source_byte_storage),bytecode_cache_context = move(bytecode_cache_context),source_hash = move(source_hash),@@ -726,11 +728,11 @@ void fetch_classic_script(GC::Ref<HTMLScriptElement> element, URL::URL const& urOptional<NonnullRefPtr<JS::SourceCode const>> source_code;if (bytecode_cache) {source_code = JS::SourceCode::create(- utf16_string_from_url_ascii(response_url_string.view()),+ utf16_string_from_url_ascii(filename.view()),source_length,source_encoding,source_byte_storage);- auto script = ClassicScript::create_from_bytecode_cache(response_url_string, *source_code, *settings_root, response_url, bytecode_cache.release_nonnull(), muted_errors);+ auto script = ClassicScript::create_from_bytecode_cache(filename, *source_code, *settings_root, response_url, bytecode_cache.release_nonnull(), muted_errors);if (script->parse_error().is_null()) {on_complete_root->function()(script);return;@@ -742,14 +744,14 @@ void fetch_classic_script(GC::Ref<HTMLScriptElement> element, URL::URL const& urauto fallback_decoder = TextCodec::decoder_for(source_encoding.view());VERIFY(fallback_decoder.has_value());source_code = JS::SourceCode::create(- utf16_string_from_url_ascii(response_url_string.view()),+ utf16_string_from_url_ascii(filename.view()),TextCodec::decode_to_utf16(source_byte_storage.bytes(), *fallback_decoder).release_value_but_fixme_should_propagate_errors());}auto retain_parse_for_cache = bytecode_cache_context.has_value();auto& settings = *settings_root;compile_off_thread(settings, source_code.release_value(), JS::ProgramType::Script, 1, retain_parse_for_cache,- [response_url = move(response_url), response_url_string = move(response_url_string),+ [response_url = move(response_url), filename = move(filename),bytecode_cache_context = move(bytecode_cache_context),source_hash = move(source_hash),muted_errors, on_complete_root = move(on_complete_root),@@ -757,8 +759,8 @@ void fetch_classic_script(GC::Ref<HTMLScriptElement> element, URL::URL const& urauto source_code_for_cache = source_code;auto should_generate_bytecode_cache = result.compiled && bytecode_cache_context.has_value();auto script = result.compiled- ? ClassicScript::create_from_pre_compiled(move(response_url_string), move(source_code), *settings_root, move(response_url), move(result.compiled), muted_errors)- : ClassicScript::create_from_pre_parsed(move(response_url_string), move(source_code), *settings_root, move(response_url), move(result.parsed), muted_errors);+ ? ClassicScript::create_from_pre_compiled(move(filename), move(source_code), *settings_root, move(response_url), move(result.compiled), muted_errors)+ : ClassicScript::create_from_pre_parsed(move(filename), move(source_code), *settings_root, move(response_url), move(result.parsed), muted_errors);BytecodeCacheInstallTarget install_target;if (auto script_record = script->script_record()) {install_target.script = *script_record;@@ -778,13 +780,13 @@ void fetch_classic_script(GC::Ref<HTMLScriptElement> element, URL::URL const& urif (!source_code.has_value()) {source_code = JS::SourceCode::create(- utf16_string_from_url_ascii(response_url_string.view()),+ utf16_string_from_url_ascii(filename.view()),TextCodec::decode_to_utf16(source_bytes, *fallback_decoder).release_value_but_fixme_should_propagate_errors());}auto retain_parse_for_cache = bytecode_cache_context.has_value();compile_off_thread(settings_object, source_code.release_value(), JS::ProgramType::Script, 1, retain_parse_for_cache,- [response_url = move(response_url), response_url_string = move(response_url_string),+ [response_url = move(response_url), filename = move(filename),bytecode_cache_context = move(bytecode_cache_context),source_hash = move(source_hash),muted_errors, on_complete_root = move(on_complete_root),@@ -792,8 +794,8 @@ void fetch_classic_script(GC::Ref<HTMLScriptElement> element, URL::URL const& urauto source_code_for_cache = source_code;auto should_generate_bytecode_cache = result.compiled && bytecode_cache_context.has_value();auto script = result.compiled- ? ClassicScript::create_from_pre_compiled(move(response_url_string), move(source_code), *settings_root, move(response_url), move(result.compiled), muted_errors)- : ClassicScript::create_from_pre_parsed(move(response_url_string), move(source_code), *settings_root, move(response_url), move(result.parsed), muted_errors);+ ? ClassicScript::create_from_pre_compiled(move(filename), move(source_code), *settings_root, move(response_url), move(result.compiled), muted_errors)+ : ClassicScript::create_from_pre_parsed(move(filename), move(source_code), *settings_root, move(response_url), move(result.parsed), muted_errors);BytecodeCacheInstallTarget install_target;if (auto script_record = script->script_record()) {install_target.script = *script_record;@@ -954,7 +956,7 @@ WebIDL::ExceptionOr<GC::Ref<ClassicScript>> fetch_a_classic_worker_imported_scri// 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({});- auto script = ClassicScript::create(response_url.to_byte_string(), source_text, settings_object, response_url, 1, muted_errors);+ auto script = ClassicScript::create(url.to_byte_string(), source_text, settings_object, response_url, 1, muted_errors);// 11. Return script.return script;diff --git a/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp b/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cppindex 5ff3e5ae07e..6e6b640c288 100644--- a/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp+++ b/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp@@ -901,7 +901,7 @@ WebIDL::ExceptionOr<i32> WindowOrWorkerGlobalScopeMixin::run_timer_initializatio}// 6. Let script be the result of creating a classic script given handler, settings object, base URL, and fetch options.- auto filename = base_url.to_byte_string();+ auto filename = initiating_script ? initiating_script->filename() : base_url.to_byte_string();auto script = ClassicScript::create(move(filename), source, settings_object, move(base_url));script->set_fetch_options(move(options));diff --git a/Tests/LibWeb/Text/expected/HTML/error-stack-hides-script-redirect-target.txt b/Tests/LibWeb/Text/expected/HTML/error-stack-hides-script-redirect-target.txtnew file mode 100644index 00000000000..4fb147935e7--- /dev/null+++ b/Tests/LibWeb/Text/expected/HTML/error-stack-hides-script-redirect-target.txt@@ -0,0 +1,3 @@+<script src>: hides the redirect target+setTimeout() string: hides the redirect target+importScripts(): hides the redirect targetdiff --git a/Tests/LibWeb/Text/input/HTML/error-stack-hides-script-redirect-target.html b/Tests/LibWeb/Text/input/HTML/error-stack-hides-script-redirect-target.htmlnew file mode 100644index 00000000000..eb5b45026d8--- /dev/null+++ b/Tests/LibWeb/Text/input/HTML/error-stack-hides-script-redirect-target.html@@ -0,0 +1,49 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ asyncTest(async done => {+ const server = httpTestServer();+ const token = "redirect-target-token";++ const target = new URL(await server.createEcho("GET", "/script-redirect-target", {+ status: 200,+ headers: { "Content-Type": "text/javascript" },+ body: `report("script"); setTimeout("report('setTimeout() string')");`,+ }));+ target.hostname = "127.0.0.1";+ target.search = token;+ const start = await server.createEcho("GET", "/script-redirect-start", {+ status: 302,+ headers: { "Location": target.href },+ });++ function describe(stack) {+ return stack.includes(token) ? "exposes the redirect target" : "hides the redirect target";+ }++ function testImportScripts() {+ const workerSource = `+ self.report = kind => {+ if (kind === "script")+ postMessage(new Error().stack);+ };+ importScripts(${JSON.stringify(start)});+ `;+ const worker = new Worker(URL.createObjectURL(new Blob([workerSource], { type: "text/javascript" })));+ worker.onmessage = event => {+ println(`importScripts(): ${describe(event.data)}`);+ done();+ };+ }++ window.report = kind => {+ println(`${kind === "script" ? "<script src>" : kind}: ${describe(new Error().stack)}`);+ if (kind !== "script")+ testImportScripts();+ };++ const script = document.createElement("script");+ script.src = start;+ document.body.appendChild(script);+ });+</script>