diff options
author | 0GreenClover0 <clovers02123@gmail.com> | 2023-05-21 02:20:13 +0200 |
---|---|---|
committer | Tim Flynn <trflynn89@pm.me> | 2023-05-25 06:03:40 -0700 |
commit | b4c6cddd96cb781d6c7f72b67fe73b66b95d1422 (patch) | |
tree | 3d7e8870f465e1b7350630dc8878766aed25f71e /Userland/Libraries/LibWeb | |
parent | 7d24c13d8b6b0762b68d44f289083369e45879ae (diff) | |
download | serenity-b4c6cddd96cb781d6c7f72b67fe73b66b95d1422.zip |
LibWeb: Handle invalid UTF-8 in Fetch's Body#text()
Diffstat (limited to 'Userland/Libraries/LibWeb')
-rw-r--r-- | Userland/Libraries/LibWeb/Fetch/Body.cpp | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/Userland/Libraries/LibWeb/Fetch/Body.cpp b/Userland/Libraries/LibWeb/Fetch/Body.cpp index f1ed223df7..50a5f3bb9a 100644 --- a/Userland/Libraries/LibWeb/Fetch/Body.cpp +++ b/Userland/Libraries/LibWeb/Fetch/Body.cpp @@ -9,6 +9,7 @@ #include <LibJS/Runtime/Completion.h> #include <LibJS/Runtime/Error.h> #include <LibJS/Runtime/PromiseCapability.h> +#include <LibTextCodec/Decoder.h> #include <LibWeb/Bindings/ExceptionOrUtils.h> #include <LibWeb/Bindings/HostDefined.h> #include <LibWeb/Bindings/MainThreadVM.h> @@ -135,9 +136,14 @@ WebIDL::ExceptionOr<JS::Value> package_data(JS::Realm& realm, ByteBuffer bytes, case PackageDataType::JSON: // Return the result of running parse JSON from bytes on bytes. return Infra::parse_json_bytes_to_javascript_value(realm, bytes); - case PackageDataType::Text: + case PackageDataType::Text: { // Return the result of running UTF-8 decode on bytes. - return JS::PrimitiveString::create(vm, TRY_OR_THROW_OOM(vm, String::from_utf8(bytes))); + auto decoder = TextCodec::decoder_for("UTF-8"sv); + VERIFY(decoder.has_value()); + + auto utf8_text = TRY_OR_THROW_OOM(vm, TextCodec::convert_input_to_utf8_using_given_decoder_unless_there_is_a_byte_order_mark(*decoder, bytes)); + return JS::PrimitiveString::create(vm, move(utf8_text)); + } default: VERIFY_NOT_REACHED(); } |