1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-08 05:27:14 +09:00

AK: Disallow construction of JsonParser

JsonParser has a footgun where it does not retain ownership of the
string to be parsed. For example, the following results in UAF:

    JsonParser parser(something_returning_a_string());
    parser.parse();

Let's avoid this altogether by only allowing use of JsonParser with
a static, safe method.
This commit is contained in:
Timothy Flynn 2025-03-19 17:47:25 -04:00 committed by Jelle Raaijmakers
parent 64aaf73775
commit 086a921213
Notes: github-actions[bot] 2025-03-20 09:51:24 +00:00
4 changed files with 13 additions and 7 deletions

View file

@ -18,6 +18,12 @@ constexpr bool is_space(int ch)
return ch == '\t' || ch == '\n' || ch == '\r' || ch == ' '; return ch == '\t' || ch == '\n' || ch == '\r' || ch == ' ';
} }
ErrorOr<JsonValue> JsonParser::parse(StringView input)
{
JsonParser parser(input);
return parser.parse_json();
}
// ECMA-404 9 String // ECMA-404 9 String
// Boils down to // Boils down to
// STRING = "\"" *("[^\"\\]" | "\\" ("[\"\\bfnrt]" | "u[0-9A-Za-z]{4}")) "\"" // STRING = "\"" *("[^\"\\]" | "\\" ("[\"\\bfnrt]" | "u[0-9A-Za-z]{4}")) "\""
@ -335,7 +341,7 @@ ErrorOr<JsonValue> JsonParser::parse_helper()
return Error::from_string_literal("JsonParser: Unexpected character"); return Error::from_string_literal("JsonParser: Unexpected character");
} }
ErrorOr<JsonValue> JsonParser::parse() ErrorOr<JsonValue> JsonParser::parse_json()
{ {
auto result = TRY(parse_helper()); auto result = TRY(parse_helper());
ignore_while(is_space); ignore_while(is_space);

View file

@ -13,14 +13,15 @@ namespace AK {
class JsonParser : private GenericLexer { class JsonParser : private GenericLexer {
public: public:
static ErrorOr<JsonValue> parse(StringView);
private:
explicit JsonParser(StringView input) explicit JsonParser(StringView input)
: GenericLexer(input) : GenericLexer(input)
{ {
} }
ErrorOr<JsonValue> parse(); ErrorOr<JsonValue> parse_json();
private:
ErrorOr<JsonValue> parse_helper(); ErrorOr<JsonValue> parse_helper();
ErrorOr<ByteString> consume_and_unescape_string(); ErrorOr<ByteString> consume_and_unescape_string();

View file

@ -190,7 +190,7 @@ JsonValue::JsonValue(JsonArray&& value)
ErrorOr<JsonValue> JsonValue::from_string(StringView input) ErrorOr<JsonValue> JsonValue::from_string(StringView input)
{ {
return JsonParser(input).parse(); return JsonParser::parse(input);
} }
String JsonValue::serialized() const String JsonValue::serialized() const

View file

@ -9,7 +9,6 @@
extern "C" int LLVMFuzzerTestOneInput(uint8_t const* data, size_t size) extern "C" int LLVMFuzzerTestOneInput(uint8_t const* data, size_t size)
{ {
AK::set_debug_enabled(false); AK::set_debug_enabled(false);
JsonParser parser({ data, size }); (void)JsonParser::parse({ data, size });
(void)parser.parse();
return 0; return 0;
} }