-
Notifications
You must be signed in to change notification settings - Fork 12.1k
ref: added functionality to escapeJSONstrings (ref: #5251) #5508
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
7eb3f6f
ref: added functionality to escapeJSONstrings (ref: #5251)
DarkLord017 c30dc42
Apply suggestions from code review
DarkLord017 159822e
added size to increase while looping in memory (ref: #5251)
DarkLord017 7ed23f6
Merge branch 'master' into escape_functionality
DarkLord017 59ec335
Improve code, add tests and fix lint
Amxx 77bb93b
add changeset
Amxx 38601d2
Update contracts/utils/Strings.sol
Amxx 5bfb4cf
Merge branch 'OpenZeppelin:master' into escape_functionality
DarkLord017 c7ab6c0
Fixed linter issue and removed escaping of forward slashes (ref: #5251)
DarkLord017 9102cc1
Merge branch 'master' into escape_functionality
Amxx 74c3d8f
minimize changes
Amxx 17186f7
more readable with solhint disable
Amxx 7ffa24f
Update test/utils/Strings.test.js
Amxx ec41401
add warning
arr00 dba25a2
address PR comments
Amxx d18d78d
Merge branch 'escape_functionality' of https://github.com/DarkLord017…
Amxx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'openzeppelin-solidity': minor | ||
--- | ||
|
||
`Strings`: Add `espaceJSON` that escapes special characters in JSON strings. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -15,6 +15,14 @@ library Strings { | |||||
|
||||||
bytes16 private constant HEX_DIGITS = "0123456789abcdef"; | ||||||
uint8 private constant ADDRESS_LENGTH = 20; | ||||||
uint256 private constant SPECIAL_CHARS_LOOKUP = | ||||||
(1 << 0x08) | // backspace | ||||||
(1 << 0x09) | // tab | ||||||
(1 << 0x0a) | // newline | ||||||
(1 << 0x0c) | // form feed | ||||||
(1 << 0x0d) | // carriage return | ||||||
(1 << 0x22) | // double quote | ||||||
(1 << 0x5c); // backslash | ||||||
|
||||||
/** | ||||||
* @dev The `value` string doesn't fit in the specified `length`. | ||||||
|
@@ -426,6 +434,43 @@ library Strings { | |||||
return value; | ||||||
} | ||||||
|
||||||
/** | ||||||
* @dev Escape special characters in JSON strings. This can be useful to prevent JSON injection in NFT metadata. | ||||||
* | ||||||
* WARNING: This function should only be used in double quoted JSON strings. Single quotes are not escaped. | ||||||
*/ | ||||||
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 = bytes1(_unsafeReadBytesOffset(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 == 0x5c) output[outputLength++] = "\\"; | ||||||
else if (char == 0x22) { | ||||||
// solhint-disable-next-line quotes | ||||||
output[outputLength++] = '"'; | ||||||
} | ||||||
} else { | ||||||
output[outputLength++] = char; | ||||||
} | ||||||
} | ||||||
// write the actual length and deallocate unused memory | ||||||
assembly ("memory-safe") { | ||||||
mstore(output, outputLength) | ||||||
mstore(0x40, add(output, shl(5, shr(5, add(outputLength, 63))))) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here (audit will report consistency)
Suggested change
|
||||||
} | ||||||
|
||||||
return string(output); | ||||||
} | ||||||
|
||||||
/** | ||||||
* @dev Reads a bytes32 from a bytes array without bounds checking. | ||||||
* | ||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.