Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parseJSON errors on non-hex strings starting with 0x #8277

Closed
2 tasks done
dyedm1 opened this issue Jun 27, 2024 · 2 comments · Fixed by #8290
Closed
2 tasks done

parseJSON errors on non-hex strings starting with 0x #8277

dyedm1 opened this issue Jun 27, 2024 · 2 comments · Fixed by #8290
Labels
A-cheatcodes Area: cheatcodes T-bug Type: bug

Comments

@dyedm1
Copy link
Contributor

dyedm1 commented Jun 27, 2024

Component

Forge, Anvil

Have you ensured that all of these are up to date?

  • Foundry
  • Foundryup

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 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 0xBitcoin token in JSON. The following JSON:

{
"name": "0xBitcoin Token",
"symbol": "0xBTC",
"decimals": 18
}

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.

@dyedm1 dyedm1 added the T-bug Type: bug label Jun 27, 2024
@mattsse
Copy link
Member

mattsse commented Jun 27, 2024

do you have a solidity repro for this?

@dyedm1
Copy link
Contributor Author

dyedm1 commented Jun 27, 2024

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\"}");
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-cheatcodes Area: cheatcodes T-bug Type: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants