From 3bf1f7ae874918365acedc3d511b60b066b9d4aa Mon Sep 17 00:00:00 2001 From: Ben Wiederhake Date: Fri, 22 Oct 2021 23:17:54 +0200 Subject: [PATCH] AK: Don't crash on invalid Base64 input In the long-term, we should probably have a way to signal decoding failure. For now, it should suffice to at least not crash. This is particularly relevant because apparently this can be triggered while parsing a PEM certificate, which happens during every TLS connection. Found by OSS Fuzz https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=38979 --- AK/Base64.cpp | 2 +- Tests/AK/TestBase64.cpp | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/AK/Base64.cpp b/AK/Base64.cpp index 661d9c53ee9..006eeebf1d8 100644 --- a/AK/Base64.cpp +++ b/AK/Base64.cpp @@ -61,7 +61,7 @@ ByteBuffer decode_base64(const StringView& input) *is_padding = true; return 0; } - return table[input[offset]]; + return table[static_cast(input[offset])]; }; Vector output; diff --git a/Tests/AK/TestBase64.cpp b/Tests/AK/TestBase64.cpp index 3e4baa372b0..95a90d06c79 100644 --- a/Tests/AK/TestBase64.cpp +++ b/Tests/AK/TestBase64.cpp @@ -27,6 +27,14 @@ TEST_CASE(test_decode) decode_equal("Zm9vYmFy", "foobar"); } +TEST_CASE(test_decode_nocrash) +{ + // Any output is fine, we only check that we don't crash here. + decode_base64(StringView("asdf\xffqwer")); + decode_base64(StringView("asdf\x80qwer")); + // TODO: Handle decoding failure. +} + TEST_CASE(test_encode) { auto encode_equal = [&](const char* input, const char* expected) {