mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-07 21:17:07 +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:
parent
64aaf73775
commit
086a921213
Notes:
github-actions[bot]
2025-03-20 09:51:24 +00:00
Author: https://github.com/trflynn89
Commit: 086a921213
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/4008
Reviewed-by: https://github.com/gmta ✅
4 changed files with 13 additions and 7 deletions
|
@ -18,6 +18,12 @@ constexpr bool is_space(int 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
|
||||
// Boils down to
|
||||
// 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");
|
||||
}
|
||||
|
||||
ErrorOr<JsonValue> JsonParser::parse()
|
||||
ErrorOr<JsonValue> JsonParser::parse_json()
|
||||
{
|
||||
auto result = TRY(parse_helper());
|
||||
ignore_while(is_space);
|
||||
|
|
|
@ -13,14 +13,15 @@ namespace AK {
|
|||
|
||||
class JsonParser : private GenericLexer {
|
||||
public:
|
||||
static ErrorOr<JsonValue> parse(StringView);
|
||||
|
||||
private:
|
||||
explicit JsonParser(StringView input)
|
||||
: GenericLexer(input)
|
||||
{
|
||||
}
|
||||
|
||||
ErrorOr<JsonValue> parse();
|
||||
|
||||
private:
|
||||
ErrorOr<JsonValue> parse_json();
|
||||
ErrorOr<JsonValue> parse_helper();
|
||||
|
||||
ErrorOr<ByteString> consume_and_unescape_string();
|
||||
|
|
|
@ -190,7 +190,7 @@ JsonValue::JsonValue(JsonArray&& value)
|
|||
|
||||
ErrorOr<JsonValue> JsonValue::from_string(StringView input)
|
||||
{
|
||||
return JsonParser(input).parse();
|
||||
return JsonParser::parse(input);
|
||||
}
|
||||
|
||||
String JsonValue::serialized() const
|
||||
|
|
|
@ -9,7 +9,6 @@
|
|||
extern "C" int LLVMFuzzerTestOneInput(uint8_t const* data, size_t size)
|
||||
{
|
||||
AK::set_debug_enabled(false);
|
||||
JsonParser parser({ data, size });
|
||||
(void)parser.parse();
|
||||
(void)JsonParser::parse({ data, size });
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue