From 4d0301d2d2abae9b8e665fd49023eb4054d34eb7 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sat, 19 Apr 2025 16:09:47 -0400 Subject: [PATCH] LibWeb: Mark fetched body streams with a TypeError if the request failed This will cause an exception to be thrown if user attempts to read from the response stream of a failed request. This is unfortunately not testable in CI. It requires a network response (i.e. not a file:// URL). We also cannot import relevant WPT tests; they exercise this condition with a python-generated response. --- Libraries/LibWeb/Fetch/Fetching/Fetching.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp b/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp index b928089b78e..1f2c90543fa 100644 --- a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp +++ b/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp @@ -2470,17 +2470,24 @@ WebIDL::ExceptionOr> nonstandard_resource_loader_file_o } else { response->set_type(Infrastructure::Response::Type::Error); response->set_status(status_code.value_or(400)); + auto [body, _] = TRY_OR_IGNORE(extract_body(realm, data)); - response->set_body(move(body)); + response->set_body(body); + auto body_info = response->body_info(); body_info.encoded_size = timing_info.encoded_body_size; body_info.decoded_size = data.size(); response->set_body_info(body_info); + for (auto const& [name, value] : response_headers.headers()) { auto header = Infrastructure::Header::from_latin1_pair(name, value); response->header_list()->append(move(header)); } + // 16.1.2.2. Otherwise, if stream is readable, error stream with a TypeError. + if (body->stream()->is_readable()) + body->stream()->error(JS::TypeError::create(realm, error)); + if (reason_phrase.has_value()) response->set_status_message(MUST(ByteBuffer::copy(reason_phrase.value().bytes()))); }