-
Notifications
You must be signed in to change notification settings - Fork 12k
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
ref: added functionality to escapeJSONstrings (ref: #5251) #5508
base: master
Are you sure you want to change the base?
Conversation
🦋 Changeset detectedLatest commit: 38601d2 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Is my approach correct , am i working right on this pr @Amxx ? |
contracts/utils/Strings.sol
Outdated
|
||
function _escapeJsonString(string memory input) private pure returns (string memory) { | ||
bytes memory buffer = bytes(input); | ||
bytes memory output = new bytes(buffer.length * 2); // Allocate max possible space |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm worried this allocated a lot of memory that is not freed. This is quite expensive. Alternatives are:
- count number of "extra chars" in a loop, and allocate the right size directly
- resize the output buffer without a copy, and move the free memory ptr, using assembly
U can review it now @Amxx |
You are doing quite a lot of read/write to the My version is just function escapeJson(string memory input) internal pure returns (string memory) {
bytes memory buffer = bytes(input);
bytes memory output = new bytes(2 * buffer.length); // worst case scenario
uint256 outputLength = 0;
for (uint256 i; i < buffer.length; ++i) {
bytes1 char = buffer[i];
if (((SPECIAL_CHARS_LOOKUP & (1 << uint8(char))) != 0)) {
output[outputLength++] = '\\';
if (char == 0x08) output[outputLength++] = 'b';
else if (char == 0x09) output[outputLength++] = 't';
else if (char == 0x0A) output[outputLength++] = 'n';
else if (char == 0x0C) output[outputLength++] = 'f';
else if (char == 0x0D) output[outputLength++] = 'r';
else if (char == 0x22) output[outputLength++] = '"';
else if (char == 0x2F) output[outputLength++] = '/';
else if (char == 0x5C) output[outputLength++] = '\\';
} else {
output[outputLength++] = char;
}
}
assembly ("memory-safe") {
// write the actual length
mstore(output, outputLength)
// deallocate unused memory
mstore(0x40, add(output, shl(5, shr(5, add(outputLength, 63)))))
}
return string(output);
} Note that we need testing ! For that the function has to be internal. |
(closed by missclick) |
TODO:
|
okay will do
|
Fixes #5251
A function to escape special characters in JSON strings
Handles key characters like quotes ("), backslashes (), forward slashes (/), and control characters (\n, \t, \r, etc.)
Prevents JSON injection attacks in NFT metadata and other use cases
PR Checklist
npx changeset add
)