From cb868cfa41072e08987e1c32f117483445ba197d Mon Sep 17 00:00:00 2001 From: Ben Wiederhake Date: Sat, 23 Oct 2021 15:43:59 +0200 Subject: AK+Everywhere: Make Base64 decoding fallible --- Userland/Libraries/LibWeb/Bindings/WindowObject.cpp | 6 +++++- Userland/Libraries/LibWeb/Loader/ResourceLoader.cpp | 14 +++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) (limited to 'Userland/Libraries/LibWeb') diff --git a/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp b/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp index c2073534b2..3de8c06233 100644 --- a/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp +++ b/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp @@ -386,11 +386,15 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(WindowObject::atob) } auto string = TRY_OR_DISCARD(vm.argument(0).to_string(global_object)); auto decoded = decode_base64(StringView(string)); + if (!decoded.has_value()) { + vm.throw_exception(global_object, JS::ErrorType::InvalidFormat, "Base64"); + return {}; + } // decode_base64() returns a byte string. LibJS uses UTF-8 for strings. Use Latin1Decoder to convert bytes 128-255 to UTF-8. auto decoder = TextCodec::decoder_for("windows-1252"); VERIFY(decoder); - return JS::js_string(vm, decoder->to_utf8(decoded)); + return JS::js_string(vm, decoder->to_utf8(decoded.value())); } JS_DEFINE_OLD_NATIVE_FUNCTION(WindowObject::btoa) diff --git a/Userland/Libraries/LibWeb/Loader/ResourceLoader.cpp b/Userland/Libraries/LibWeb/Loader/ResourceLoader.cpp index 02ba6d19b0..1c2bb06208 100644 --- a/Userland/Libraries/LibWeb/Loader/ResourceLoader.cpp +++ b/Userland/Libraries/LibWeb/Loader/ResourceLoader.cpp @@ -153,10 +153,18 @@ void ResourceLoader::load(LoadRequest& request, Function