Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/utils/JSONParserLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ library JSONParserLib {
if iszero(eq(c, 92)) {
// Not '"'.
if iszero(eq(c, 34)) {
if lt(c, 0x20) { fail() }
mstore8(out, c)
out := add(out, 1)
continue
Expand Down Expand Up @@ -649,6 +650,7 @@ library JSONParserLib {
if eq(c_, 34) { break } // '"'.
// Not '\'.
if iszero(eq(c_, 92)) {
if lt(c_, 0x20) { fail() }
_pOut := add(_pOut, 1)
continue
}
Expand Down
18 changes: 18 additions & 0 deletions test/JSONParserLib.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,8 @@ contract JSONParserLibTest is SoladyTest {
_checkParseString('"\\n"');
_checkParseString('"\\r"');
_checkParseString('"\\t"');
_checkParseString('"\\u0000"');
_checkParseString('"\\u001f"');
_checkParseString('" \\u1234 \\"\\"\\\\ \\b\\f \\n\\r "');
_checkParseString('"\\u1234"');
_checkParseString('"\\uabcd"');
Expand Down Expand Up @@ -481,6 +483,13 @@ contract JSONParserLibTest is SoladyTest {
_checkParseReverts('"\\u1234');
}

function testParseUnescapedControlCharactersRevert() public {
for (uint256 i; i < 0x20; ++i) {
string memory s = string(abi.encodePacked('"a', bytes1(uint8(i)), 'b"'));
_checkParseReverts(s);
}
}

function _checkItemIsSolo(JSONParserLib.Item memory item) internal {
_checkItemHasNoParent(item);
assertEq(item.size(), 0);
Expand Down Expand Up @@ -723,6 +732,8 @@ contract JSONParserLibTest is SoladyTest {
assertEq(this.decodeString('"\\r"'), "\r");
assertEq(this.decodeString('"\\t"'), "\t");
assertEq(this.decodeString('"\\u0020"'), " ");
assertEq(this.decodeString('"\\u0000"'), hex"00");
assertEq(this.decodeString('"\\u001f"'), hex"1f");
bytes32 expectedHash;
expectedHash = 0x40b2b6558413427ef2da03b1452640d701458e0ce57114db6b7423ae3b5fe857;
assertEq(keccak256(bytes(this.decodeString('"\\u039e"'))), expectedHash); // Greek uppercase Xi.
Expand Down Expand Up @@ -764,6 +775,13 @@ contract JSONParserLibTest is SoladyTest {
_checkDecodeInvalidStringReverts('"\\uD83D"'); // Only half of a Smiley emoji.
}

function testDecodeUnescapedControlCharactersRevert() public {
for (uint256 i; i < 0x20; ++i) {
string memory s = string(abi.encodePacked('"a', bytes1(uint8(i)), 'b"'));
_checkDecodeInvalidStringReverts(s);
}
}

function _checkDecodeInvalidStringReverts(string memory s) internal {
vm.expectRevert(JSONParserLib.ParsingFailed.selector);
this.decodeString(s);
Expand Down