Skip to content

Commit

Permalink
LibWeb: Use ISO-8859-1 decoder for "isomorphic decode"
Browse files Browse the repository at this point in the history
  • Loading branch information
Gingeh committed Oct 21, 2024
1 parent 07400b5 commit b835430
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
5 changes: 4 additions & 1 deletion Userland/Libraries/LibWeb/DOM/Document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/

#include "LibTextCodec/Decoder.h"
#include <AK/CharacterTypes.h>
#include <AK/Debug.h>
#include <AK/GenericLexer.h>
Expand Down Expand Up @@ -347,7 +348,9 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Document>> Document::create_and_initialize(
// 16. If navigationParams's response has a `Refresh` header, then:
if (auto maybe_refresh = navigation_params.response->header_list()->get("Refresh"sv.bytes()); maybe_refresh.has_value()) {
// 1. Let value be the isomorphic decoding of the value of the header.
auto const& value = maybe_refresh.value();
auto decoder = TextCodec::decoder_for_exact_name("ISO-8859-1"sv);
VERIFY(decoder.has_value());
auto value = MUST(decoder->to_utf8(StringView { maybe_refresh.value() }));

// 2. Run the shared declarative refresh steps with document and value.
document->shared_declarative_refresh_steps(value, nullptr);
Expand Down
16 changes: 12 additions & 4 deletions Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ Optional<Vector<String>> get_decode_and_split_header_value(ReadonlyBytes value)
// To get, decode, and split a header value value, run these steps:

// 1. Let input be the result of isomorphic decoding value.
auto input = StringView { value };
auto decoder = TextCodec::decoder_for_exact_name("ISO-8859-1"sv);
VERIFY(decoder.has_value());
auto input = MUST(decoder->to_utf8(StringView { value }));

// 2. Let position be a position variable for input, initially pointing at the start of input.
auto lexer = GenericLexer { input };
Expand Down Expand Up @@ -523,7 +525,10 @@ bool is_cors_safelisted_request_header(Header const& header)
return false;

// 2. Let mimeType be the result of parsing the result of isomorphic decoding value.
auto mime_type = MimeSniff::MimeType::parse(StringView { value });
auto decoder = TextCodec::decoder_for_exact_name("ISO-8859-1"sv);
VERIFY(decoder.has_value());
auto decoded = MUST(decoder->to_utf8(StringView { value }));
auto mime_type = MimeSniff::MimeType::parse(decoded);

// 3. If mimeType is failure, then return false.
if (!mime_type.has_value())
Expand Down Expand Up @@ -726,6 +731,7 @@ bool is_forbidden_request_header(Header const& header)
auto parsed_values = get_decode_and_split_header_value(header.value);

// 2. For each method of parsedValues: if the isomorphic encoding of method is a forbidden method, then return true.
// Note: The values returned from get_decode_and_split_header_value have already been decoded.
if (parsed_values.has_value() && any_of(*parsed_values, [](auto method) { return is_forbidden_method(method.bytes()); }))
return true;
}
Expand Down Expand Up @@ -826,10 +832,12 @@ Variant<Vector<ByteBuffer>, ExtractHeaderParseFailure, Empty> extract_header_lis
Optional<RangeHeaderValue> parse_single_range_header_value(ReadonlyBytes value)
{
// 1. Let data be the isomorphic decoding of value.
auto data = StringView { value };
auto decoder = TextCodec::decoder_for_exact_name("ISO-8859-1"sv);
VERIFY(decoder.has_value());
auto data = MUST(decoder->to_utf8(StringView { value }));

// 2. If data does not start with "bytes=", then return failure.
if (!data.starts_with("bytes="sv))
if (!data.starts_with_bytes("bytes="sv))
return {};

// 3. Let position be a position variable for data, initially pointing at the 6th code point of data.
Expand Down
5 changes: 4 additions & 1 deletion Userland/Libraries/LibWeb/Fetch/Infrastructure/URL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/

#include "LibTextCodec/Decoder.h"
#include <AK/Base64.h>
#include <LibURL/URL.h>
#include <LibWeb/Fetch/Infrastructure/URL.h>
Expand Down Expand Up @@ -75,7 +76,9 @@ ErrorOr<DataURL> process_data_url(URL::URL const& data_url)
trimmed_substring_view = trimmed_substring_view.trim(" "sv, TrimMode::Right);
if (trimmed_substring_view.ends_with(';')) {
// 1. Let stringBody be the isomorphic decode of body.
auto string_body = StringView(body);
auto decoder = TextCodec::decoder_for_exact_name("ISO-8859-1"sv);
VERIFY(decoder.has_value());
auto string_body = MUST(decoder->to_utf8(StringView { body }));

// 2. Set body to the forgiving-base64 decode of stringBody.
// 3. If body is failure, then return failure.
Expand Down

0 comments on commit b835430

Please sign in to comment.