You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As specified here, the parser will attempt to cast any JSON strings beginning with 0x to a bytearray (or an address/bytes32 if the size matches). While this interpretation likely works well enough most of the time, there is also the possibility that a string that begins with 0x is either not intended to be interpreted as a hex value or prefixes only a portion of a string interpretable as hex (where the entire string is not).
As an example, suppose you wanted to represent the 0xBitcointoken in JSON. The following JSON:
would fail with an error message like Invalid character 'i' at position 1 if passed into vm.parseJson. Similarly, a string beginning with a valid hex substring (such as an address) like "0x834629f473876e5f0d3d9d269af3dabcb0d7d520-identifier-0" would also fail once the hyphen was hit: Invalid character '-' at position 41.
This is not ideal and can cause unexpected failures if the content of the strings in JSON is unsanitized. Ideally, the parser would fallback to a string upon failure to coerce a value into a bytearray/address/bytes32 instead of returning an error.
The text was updated successfully, but these errors were encountered:
pragma solidity ^0.8.0;
import "forge-std/Test.sol";
contract A is Test {
function test_hexprefixednonhexstring() public {
vm.parseJson("{\"a\": \"0x834629f473876e5f0d3d9d269af3dabcb0d7d520-identifier-0\"}");
vm.parseJson("{\"b\": \"0xBTC\"}");
}
}
Component
Forge, Anvil
Have you ensured that all of these are up to date?
What version of Foundry are you on?
forge 0.2.0 (c8db1e4 2024-06-27T00:18:32.026850000Z)
What command(s) is the bug in?
No response
Operating System
Linux
Describe the bug
As specified here, the parser will attempt to cast any JSON strings beginning with
0x
to a bytearray (or an address/bytes32 if the size matches). While this interpretation likely works well enough most of the time, there is also the possibility that a string that begins with0x
is either not intended to be interpreted as a hex value or prefixes only a portion of a string interpretable as hex (where the entire string is not).As an example, suppose you wanted to represent the
0xBitcoin
token in JSON. The following JSON:would fail with an error message like
Invalid character 'i' at position 1
if passed intovm.parseJson
. Similarly, a string beginning with a valid hex substring (such as an address) like"0x834629f473876e5f0d3d9d269af3dabcb0d7d520-identifier-0"
would also fail once the hyphen was hit:Invalid character '-' at position 41
.This is not ideal and can cause unexpected failures if the content of the strings in JSON is unsanitized. Ideally, the parser would fallback to a string upon failure to coerce a value into a bytearray/address/bytes32 instead of returning an error.
The text was updated successfully, but these errors were encountered: