Truly independent web browser
AK+LibJS+LibURL: Match the UTF-8 decoder when replacing invalid UTF-8
Shannon Booth131248c18c8f
diff --git a/AK/String.cpp b/AK/String.cppindex c9d6060241e..6ba0e848ea4 100644--- a/AK/String.cpp+++ b/AK/String.cpp@@ -22,28 +22,34 @@namespace AK {-String String::from_utf8_with_replacement_character(StringView view, WithBOMHandling with_bom_handling)+static String from_bytes_with_replacement_character(StringView view, AllowLonelySurrogates allow_lonely_surrogates){- if (auto bytes = view.bytes(); with_bom_handling == WithBOMHandling::Yes && bytes.starts_with({ { 0xEF, 0xBB, 0xBF } }))- view = view.substring_view(3);-Utf8View utf8_view { view };if (utf8_view.validate(AllowLonelySurrogates::No))return String::from_utf8_without_validation(view.bytes());StringBuilder builder(view.length());-- for (auto code_point : utf8_view) {- if (is_unicode_surrogate(code_point))- builder.append_code_point(UnicodeUtils::REPLACEMENT_CODE_POINT);- else- builder.append_code_point(code_point);- }+ utf8_view.for_each_code_point_with_replacement_character(allow_lonely_surrogates, [&](u32 code_point) {+ builder.append_code_point(code_point);+ });return builder.to_string_without_validation();}+String String::from_utf8_with_replacement_character(StringView view, WithBOMHandling with_bom_handling)+{+ if (auto bytes = view.bytes(); with_bom_handling == WithBOMHandling::Yes && bytes.starts_with({ { 0xEF, 0xBB, 0xBF } }))+ view = view.substring_view(3);++ return from_bytes_with_replacement_character(view, AllowLonelySurrogates::No);+}++String String::from_wtf8_with_replacement_character(StringView view)+{+ return from_bytes_with_replacement_character(view, AllowLonelySurrogates::Yes);+}+String String::from_utf8_without_validation(ReadonlyBytes bytes){String result;diff --git a/AK/String.h b/AK/String.hindex 17f66e93ac0..62f7f2ef8ab 100644--- a/AK/String.h+++ b/AK/String.h@@ -52,6 +52,7 @@ public:// Creates a new String using the replacement character for invalid bytes[[nodiscard]] static String from_utf8_with_replacement_character(StringView, WithBOMHandling = WithBOMHandling::Yes);+ [[nodiscard]] static String from_wtf8_with_replacement_character(StringView);template<typename T>requires(IsOneOf<RemoveCVReference<T>, ByteString, FlyString, String>)diff --git a/AK/Utf16String.cpp b/AK/Utf16String.cppindex ce7fae99eab..c96a754d553 100644--- a/AK/Utf16String.cpp+++ b/AK/Utf16String.cpp@@ -15,28 +15,34 @@ namespace AK {static_assert(sizeof(Detail::ShortString) == sizeof(Detail::Utf16StringData*));-Utf16String Utf16String::from_utf8_with_replacement_character(StringView utf8_string, WithBOMHandling with_bom_handling)+static Utf16String from_bytes_with_replacement_character(StringView string, AllowLonelySurrogates allow_lonely_surrogates){- if (auto bytes = utf8_string.bytes(); with_bom_handling == WithBOMHandling::Yes && bytes.starts_with({ { 0xEF, 0xBB, 0xBF } }))- utf8_string = utf8_string.substring_view(3);-- Utf8View utf8_view { utf8_string };+ Utf8View utf8_view { string };if (utf8_view.validate(AllowLonelySurrogates::No))- return Utf16String::from_utf8_without_validation(utf8_string);+ return Utf16String::from_utf8_without_validation(string);Utf16StringBuilder builder;-- for (auto code_point : utf8_view) {- if (is_unicode_surrogate(code_point))- builder.append_code_point(UnicodeUtils::REPLACEMENT_CODE_POINT);- else- builder.append_code_point(code_point);- }+ utf8_view.for_each_code_point_with_replacement_character(allow_lonely_surrogates, [&](u32 code_point) {+ builder.append_code_point(code_point);+ });return builder.to_string();}+Utf16String Utf16String::from_utf8_with_replacement_character(StringView utf8_string, WithBOMHandling with_bom_handling)+{+ if (auto bytes = utf8_string.bytes(); with_bom_handling == WithBOMHandling::Yes && bytes.starts_with({ { 0xEF, 0xBB, 0xBF } }))+ utf8_string = utf8_string.substring_view(3);++ return from_bytes_with_replacement_character(utf8_string, AllowLonelySurrogates::No);+}++Utf16String Utf16String::from_wtf8_with_replacement_character(StringView wtf8_string)+{+ return from_bytes_with_replacement_character(wtf8_string, AllowLonelySurrogates::Yes);+}+Utf16String Utf16String::from_ascii_without_validation(ReadonlyBytes ascii_string){if (ascii_string.size() <= Detail::MAX_SHORT_STRING_BYTE_COUNT) {diff --git a/AK/Utf16String.h b/AK/Utf16String.hindex 28f10f34524..23a3e355c0f 100644--- a/AK/Utf16String.h+++ b/AK/Utf16String.h@@ -64,6 +64,7 @@ public:Yes,};static Utf16String from_utf8_with_replacement_character(StringView, WithBOMHandling = WithBOMHandling::Yes);+ static Utf16String from_wtf8_with_replacement_character(StringView);ALWAYS_INLINE static ErrorOr<Utf16String> try_from_utf8(StringView utf8_string){diff --git a/AK/Utf8View.h b/AK/Utf8View.hindex cb7eaee2a85..7e4ba35a7e8 100644--- a/AK/Utf8View.h+++ b/AK/Utf8View.h@@ -8,6 +8,7 @@#pragma once#include <AK/ByteString.h>+#include <AK/CharacterTypes.h>#include <AK/Debug.h>#include <AK/Format.h>#include <AK/Function.h>@@ -184,6 +185,131 @@ public:run_callback();}+ // Runs UTF-8's decoder over the view with an error mode of "replacement". With lonely surrogates allowed, the view+ // is read as WTF-8 instead, and each encoded surrogate becomes a single replacement character.+ // https://encoding.spec.whatwg.org/#utf-8-decoder+ template<typename Callback>+ void for_each_code_point_with_replacement_character(AllowLonelySurrogates allow_lonely_surrogates, Callback callback) const+ {+ u32 utf8_code_point = 0;+ size_t utf8_bytes_seen = 0;+ size_t utf8_bytes_needed = 0;+ u8 utf8_lower_boundary = 0x80;+ u8 utf8_upper_boundary = 0xBF;++ auto bytes = m_string.bytes();+ for (size_t i = 0; i < bytes.size();) {+ auto byte = bytes[i];++ // 3. If UTF-8 bytes needed is 0, based on byte:+ if (utf8_bytes_needed == 0) {+ ++i;++ // 0x00 to 0x7F+ if (byte <= 0x7F) {+ // Return a code point whose value is byte.+ callback(byte);+ }+ // 0xC2 to 0xDF+ else if (byte >= 0xC2 && byte <= 0xDF) {+ // 1. Set UTF-8 bytes needed to 1.+ utf8_bytes_needed = 1;++ // 2. Set UTF-8 code point to byte & 0x1F.+ utf8_code_point = byte & 0x1F;+ }+ // 0xE0 to 0xEF+ else if (byte >= 0xE0 && byte <= 0xEF) {+ // 1. If byte is 0xE0, then set UTF-8 lower boundary to 0xA0.+ if (byte == 0xE0)+ utf8_lower_boundary = 0xA0;++ // 2. If byte is 0xED, then set UTF-8 upper boundary to 0x9F.+ if (byte == 0xED && allow_lonely_surrogates == AllowLonelySurrogates::No)+ utf8_upper_boundary = 0x9F;++ // 3. Set UTF-8 bytes needed to 2.+ utf8_bytes_needed = 2;++ // 4. Set UTF-8 code point to byte & 0xF.+ utf8_code_point = byte & 0xF;+ }+ // 0xF0 to 0xF4+ else if (byte >= 0xF0 && byte <= 0xF4) {+ // 1. If byte is 0xF0, then set UTF-8 lower boundary to 0x90.+ if (byte == 0xF0)+ utf8_lower_boundary = 0x90;++ // 2. If byte is 0xF4, then set UTF-8 upper boundary to 0x8F.+ if (byte == 0xF4)+ utf8_upper_boundary = 0x8F;++ // 3. Set UTF-8 bytes needed to 3.+ utf8_bytes_needed = 3;++ // 4. Set UTF-8 code point to byte & 0x7.+ utf8_code_point = byte & 0x7;+ }+ // Otherwise+ else {+ // Return error.+ callback(UnicodeUtils::REPLACEMENT_CODE_POINT);+ }++ // Return continue.+ continue;+ }++ // 4. If byte is not in the range UTF-8 lower boundary to UTF-8 upper boundary, inclusive:+ if (byte < utf8_lower_boundary || byte > utf8_upper_boundary) {+ // 1. Set UTF-8 code point, UTF-8 bytes needed, and UTF-8 bytes seen to 0, set UTF-8 lower boundary to+ // 0x80, and set UTF-8 upper boundary to 0xBF.+ utf8_code_point = 0;+ utf8_bytes_needed = 0;+ utf8_bytes_seen = 0;+ utf8_lower_boundary = 0x80;+ utf8_upper_boundary = 0xBF;++ // 2. Restore byte to ioQueue.+ // NB: i is not advanced, so byte is read again.++ // 3. Return error.+ callback(UnicodeUtils::REPLACEMENT_CODE_POINT);+ continue;+ }+ ++i;++ // 5. Set UTF-8 lower boundary to 0x80 and UTF-8 upper boundary to 0xBF.+ utf8_lower_boundary = 0x80;+ utf8_upper_boundary = 0xBF;++ // 6. Set UTF-8 code point to (UTF-8 code point << 6) | (byte & 0x3F)+ utf8_code_point = (utf8_code_point << 6) | (byte & 0x3F);++ // 7. Increase UTF-8 bytes seen by one.+ ++utf8_bytes_seen;++ // 8. If UTF-8 bytes seen is not equal to UTF-8 bytes needed, then return continue.+ if (utf8_bytes_seen != utf8_bytes_needed)+ continue;++ // 9. Let codePoint be UTF-8 code point.+ auto code_point = utf8_code_point;++ // 10. Set UTF-8 code point, UTF-8 bytes needed, and UTF-8 bytes seen to 0.+ utf8_code_point = 0;+ utf8_bytes_needed = 0;+ utf8_bytes_seen = 0;++ // 11. Return a code point whose value is codePoint.+ callback(is_unicode_surrogate(code_point) ? UnicodeUtils::REPLACEMENT_CODE_POINT : code_point);+ }++ // 1. If byte is end-of-queue and UTF-8 bytes needed is not 0, then set UTF-8 bytes needed to 0 and return error.+ if (utf8_bytes_needed != 0)+ callback(UnicodeUtils::REPLACEMENT_CODE_POINT);+ }+private:friend class Utf8CodePointIterator;diff --git a/Libraries/LibJS/ScriptAndModuleABIConversions.h b/Libraries/LibJS/ScriptAndModuleABIConversions.hindex 04121b96c77..9e0da554dea 100644--- a/Libraries/LibJS/ScriptAndModuleABIConversions.h+++ b/Libraries/LibJS/ScriptAndModuleABIConversions.h@@ -93,7 +93,7 @@ GC::Ref<ModuleType> module_from_abi(JSModule* module)// The runtime borrows filenames as UTF-16 and reads them back as UTF-8, which C++ hands over as a StringView.inline Utf16String filename_to_utf16(StringView filename){- return Utf16String::from_utf8_with_replacement_character(filename, Utf16String::WithBOMHandling::No);+ return Utf16String::from_wtf8_with_replacement_character(filename);}// Collects the syntax errors that the runtime reports to a JSParserErrorSink as ParserErrors.diff --git a/Libraries/LibURL/Parser.cpp b/Libraries/LibURL/Parser.cppindex c0b2794dfbc..f96b50712b8 100644--- a/Libraries/LibURL/Parser.cpp+++ b/Libraries/LibURL/Parser.cpp@@ -105,7 +105,7 @@ static bool basic_parse_with_state_override_impl(FFI::RustUrlInput input, URL& uOptional<URL> Parser::basic_parse(StringView raw_input, Optional<URL const&> base_url, Optional<StringView> encoding){- auto input = String::from_utf8_with_replacement_character(raw_input, String::WithBOMHandling::No);+ auto input = String::from_wtf8_with_replacement_character(raw_input);return basic_parse_impl({ input.bytes().data(), nullptr, input.bytes().size() }, base_url, encoding);}@@ -116,7 +116,7 @@ Optional<URL> Parser::basic_parse(Utf16View raw_input, Optional<URL const&> basebool Parser::basic_parse(StringView raw_input, URL& url, State state_override, Optional<StringView> encoding){- auto input = String::from_utf8_with_replacement_character(raw_input, String::WithBOMHandling::No);+ auto input = String::from_wtf8_with_replacement_character(raw_input);return basic_parse_with_state_override_impl({ input.bytes().data(), nullptr, input.bytes().size() }, url, state_override, encoding);}diff --git a/Libraries/LibURL/RustIntegration.cpp b/Libraries/LibURL/RustIntegration.cppindex 17e08afd26f..2f12ae4cfd8 100644--- a/Libraries/LibURL/RustIntegration.cpp+++ b/Libraries/LibURL/RustIntegration.cpp@@ -315,7 +315,7 @@ static Optional<Host> parse_host_impl(FFI::RustUrlInput input, bool is_opaque)Optional<Host> parse_host(StringView input, bool is_opaque){- auto processed_input = String::from_utf8_with_replacement_character(input, String::WithBOMHandling::No);+ auto processed_input = String::from_wtf8_with_replacement_character(input);return parse_host_impl({ processed_input.bytes().data(), nullptr, processed_input.bytes().size() }, is_opaque);}diff --git a/Libraries/LibURL/URL.cpp b/Libraries/LibURL/URL.cppindex cdfea169741..3aefea0fb32 100644--- a/Libraries/LibURL/URL.cpp+++ b/Libraries/LibURL/URL.cpp@@ -186,7 +186,7 @@ void URL::set_scheme(StringView scheme)void URL::set_username(StringView username){auto rust_url = to_rust();- auto processed_username = String::from_utf8_with_replacement_character(username, String::WithBOMHandling::No);+ auto processed_username = String::from_wtf8_with_replacement_character(username);FFI::rust_url_set_username(&rust_url, rust_url_input(processed_username.bytes_as_string_view()), this, set_from_rust_callback);}@@ -201,7 +201,7 @@ void URL::set_username(Utf16View username)void URL::set_password(StringView password){auto rust_url = to_rust();- auto processed_password = String::from_utf8_with_replacement_character(password, String::WithBOMHandling::No);+ auto processed_password = String::from_wtf8_with_replacement_character(password);FFI::rust_url_set_password(&rust_url, rust_url_input(processed_password.bytes_as_string_view()), this, set_from_rust_callback);}diff --git a/Tests/AK/TestString.cpp b/Tests/AK/TestString.cppindex 393acf84875..756dfb49402 100644--- a/Tests/AK/TestString.cpp+++ b/Tests/AK/TestString.cpp@@ -186,9 +186,8 @@ TEST_CASE(invalid_utf8)TEST_CASE(with_replacement_character){- auto string1 = String::from_utf8_with_replacement_character("long string \xf4\x8f\xbf\xc0"sv, String::WithBOMHandling::No); // U+110000- Array<u8, 24> string1_expected { 0x6c, 0x6f, 0x6e, 0x67, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x20, 0xef, 0xbf, 0xbd, 0xef, 0xbf, 0xbd, 0xef, 0xbf, 0xbd, 0xef, 0xbf, 0xbd };- EXPECT_EQ(string1.bytes(), string1_expected);+ auto string1 = String::from_utf8_with_replacement_character("long string \xf4\x8f\xbf\xc0"sv, String::WithBOMHandling::No);+ EXPECT_EQ(string1, "long string \ufffd\ufffd"sv);auto string3 = String::from_utf8_with_replacement_character("A valid string!"sv, String::WithBOMHandling::No);EXPECT_EQ(string3, "A valid string!"sv);@@ -203,7 +202,43 @@ TEST_CASE(with_replacement_character)EXPECT_EQ(string6, "\xEF\xBB\xBFWHF!"sv);auto string7 = String::from_utf8_with_replacement_character("\xED\xA0\x80WHF!"sv); // U+D800- EXPECT_EQ(string7, "\ufffdWHF!"sv);+ EXPECT_EQ(string7, "\ufffd\ufffd\ufffdWHF!"sv);+}++TEST_CASE(with_replacement_character_matches_utf8_decoder)+{+ auto decode = [](StringView input) { return String::from_utf8_with_replacement_character(input, String::WithBOMHandling::No); };++ EXPECT_EQ(decode("\xE0\x80\x80"sv), "\ufffd\ufffd\ufffd"sv);+ EXPECT_EQ(decode("\xC0\x80"sv), "\ufffd\ufffd"sv);+ EXPECT_EQ(decode("\xF0\x9F\x98"sv), "\ufffd"sv);+ EXPECT_EQ(decode("\xF0\x9F\x98"+ "A"sv),+ "\ufffdA"sv);+ EXPECT_EQ(decode("\xF4\x90\x80\x80"sv), "\ufffd\ufffd\ufffd\ufffd"sv);+ EXPECT_EQ(decode("\xFF"sv), "\ufffd"sv);+ EXPECT_EQ(decode("a\x80"+ "b"sv),+ "a\ufffdb"sv);+ EXPECT_EQ(decode("\xFF\xF0\x9F\x98\x80"sv), "\ufffd\U0001F600"sv);+}++TEST_CASE(from_wtf8_with_replacement_character)+{+ EXPECT_EQ(String::from_wtf8_with_replacement_character("A valid string!"sv), "A valid string!"sv);+ EXPECT_EQ(String::from_wtf8_with_replacement_character("\xF0\x9F\x98\x80"sv), "\U0001F600"sv);+ EXPECT_EQ(String::from_wtf8_with_replacement_character("\xEF\xBB\xBFWHF!"sv), "\ufeffWHF!"sv);++ // Each encoded surrogate is a single code point.+ EXPECT_EQ(String::from_wtf8_with_replacement_character("\xED\xA0\x80-\xED\xB0\x80"sv), "\ufffd-\ufffd"sv);+ EXPECT_EQ(String::from_wtf8_with_replacement_character("\xED\xA0\xED\xBF\xBF"sv), "\ufffd\ufffd"sv);++ // Bytes that are not WTF-8 are replaced as UTF-8's decoder does.+ EXPECT_EQ(String::from_wtf8_with_replacement_character("\xC0\x80"sv), "\ufffd\ufffd"sv);+ EXPECT_EQ(String::from_wtf8_with_replacement_character("\xF0\x9F\x98"+ "A"sv),+ "\ufffdA"sv);+ EXPECT_EQ(String::from_wtf8_with_replacement_character("\xF4\x90\x80\x80"sv), "\ufffd\ufffd\ufffd\ufffd"sv);}TEST_CASE(from_utf16_with_replacement_character)diff --git a/Tests/AK/TestUtf16String.cpp b/Tests/AK/TestUtf16String.cppindex 7304776c1fb..d61a2325e0b 100644--- a/Tests/AK/TestUtf16String.cpp+++ b/Tests/AK/TestUtf16String.cpp@@ -113,8 +113,8 @@ TEST_CASE(from_utf8)TEST_CASE(from_utf8_with_replacement_character){- auto string1 = Utf16String::from_utf8_with_replacement_character("long string \xf4\x8f\xbf\xc0"sv, Utf16String::WithBOMHandling::No); // U+110000- EXPECT_EQ(string1, u"long string \ufffd\ufffd\ufffd\ufffd"sv);+ auto string1 = Utf16String::from_utf8_with_replacement_character("long string \xf4\x8f\xbf\xc0"sv, Utf16String::WithBOMHandling::No);+ EXPECT_EQ(string1, u"long string \ufffd\ufffd"sv);auto string3 = Utf16String::from_utf8_with_replacement_character("A valid string!"sv, Utf16String::WithBOMHandling::No);EXPECT_EQ(string3, "A valid string!"sv);@@ -129,7 +129,43 @@ TEST_CASE(from_utf8_with_replacement_character)EXPECT_EQ(string6, u"\ufeffWHF!"sv);auto string7 = Utf16String::from_utf8_with_replacement_character("\xED\xA0\x80WHF!"sv); // U+D800- EXPECT_EQ(string7, u"\ufffdWHF!"sv);+ EXPECT_EQ(string7, u"\ufffd\ufffd\ufffdWHF!"sv);+}++TEST_CASE(from_utf8_with_replacement_character_matches_utf8_decoder)+{+ auto decode = [](StringView input) { return Utf16String::from_utf8_with_replacement_character(input, Utf16String::WithBOMHandling::No); };++ EXPECT_EQ(decode("\xE0\x80\x80"sv), u"\ufffd\ufffd\ufffd"sv);+ EXPECT_EQ(decode("\xC0\x80"sv), u"\ufffd\ufffd"sv);+ EXPECT_EQ(decode("\xF0\x9F\x98"sv), u"\ufffd"sv);+ EXPECT_EQ(decode("\xF0\x9F\x98"+ "A"sv),+ u"\ufffdA"sv);+ EXPECT_EQ(decode("\xF4\x90\x80\x80"sv), u"\ufffd\ufffd\ufffd\ufffd"sv);+ EXPECT_EQ(decode("\xFF"sv), u"\ufffd"sv);+ EXPECT_EQ(decode("a\x80"+ "b"sv),+ u"a\ufffdb"sv);+ EXPECT_EQ(decode("\xFF\xF0\x9F\x98\x80"sv), u"\ufffd\U0001F600"sv);+}++TEST_CASE(from_wtf8_with_replacement_character)+{+ EXPECT_EQ(Utf16String::from_wtf8_with_replacement_character("A valid string!"sv), "A valid string!"sv);+ EXPECT_EQ(Utf16String::from_wtf8_with_replacement_character("\xF0\x9F\x98\x80"sv), u"\U0001F600"sv);+ EXPECT_EQ(Utf16String::from_wtf8_with_replacement_character("\xEF\xBB\xBFWHF!"sv), u"\ufeffWHF!"sv);++ // Each encoded surrogate is a single code point.+ EXPECT_EQ(Utf16String::from_wtf8_with_replacement_character("\xED\xA0\x80-\xED\xB0\x80"sv), u"\ufffd-\ufffd"sv);+ EXPECT_EQ(Utf16String::from_wtf8_with_replacement_character("\xED\xA0\xED\xBF\xBF"sv), u"\ufffd\ufffd"sv);++ // Bytes that are not WTF-8 are replaced as UTF-8's decoder does.+ EXPECT_EQ(Utf16String::from_wtf8_with_replacement_character("\xC0\x80"sv), u"\ufffd\ufffd"sv);+ EXPECT_EQ(Utf16String::from_wtf8_with_replacement_character("\xF0\x9F\x98"+ "A"sv),+ u"\ufffdA"sv);+ EXPECT_EQ(Utf16String::from_wtf8_with_replacement_character("\xF4\x90\x80\x80"sv), u"\ufffd\ufffd\ufffd\ufffd"sv);}TEST_CASE(from_utf16)