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

Pashov L-05 add more special characters to escape #374

Merged
merged 5 commits into from
Oct 28, 2024
Merged
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: 1 addition & 1 deletion .forge-snapshots/positionDescriptor bytecode size.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
31236
31443
36 changes: 20 additions & 16 deletions src/libraries/Descriptor.sol
Original file line number Diff line number Diff line change
@@ -45,13 +45,13 @@ library Descriptor {
function constructTokenURI(ConstructTokenURIParams memory params) internal pure returns (string memory) {
string memory name = generateName(params, feeToPercentString(params.fee));
string memory descriptionPartOne = generateDescriptionPartOne(
escapeQuotes(params.quoteCurrencySymbol),
escapeQuotes(params.baseCurrencySymbol),
escapeSpecialCharacters(params.quoteCurrencySymbol),
escapeSpecialCharacters(params.baseCurrencySymbol),
addressToString(params.poolManager)
);
string memory descriptionPartTwo = generateDescriptionPartTwo(
params.tokenId.toString(),
escapeQuotes(params.baseCurrencySymbol),
escapeSpecialCharacters(params.baseCurrencySymbol),
addressToString(Currency.unwrap(params.quoteCurrency)),
addressToString(Currency.unwrap(params.baseCurrency)),
params.hooks == address(0) ? "No Hook" : addressToString(params.hooks),
@@ -81,23 +81,23 @@ library Descriptor {
);
}

/// @notice Escapes double quotes in a string if they are present
function escapeQuotes(string memory symbol) internal pure returns (string memory) {
/// @notice Escapes special characters in a string if they are present
function escapeSpecialCharacters(string memory symbol) internal pure returns (string memory) {
bytes memory symbolBytes = bytes(symbol);
uint8 quotesCount = 0;
// count the amount of double quotes (") in the symbol
uint8 specialCharCount = 0;
// count the amount of double quotes, form feeds, new lines, carriage returns, or tabs in the symbol
for (uint8 i = 0; i < symbolBytes.length; i++) {
if (symbolBytes[i] == '"') {
quotesCount++;
if (isSpecialCharacter(symbolBytes[i])) {
specialCharCount++;
}
}
if (quotesCount > 0) {
// create a new bytes array with enough space to hold the original bytes plus space for the backslashes to escape the quotes
bytes memory escapedBytes = new bytes(symbolBytes.length + quotesCount);
if (specialCharCount > 0) {
// create a new bytes array with enough space to hold the original bytes plus space for the backslashes to escape the special characters
bytes memory escapedBytes = new bytes(symbolBytes.length + specialCharCount);
uint256 index;
for (uint8 i = 0; i < symbolBytes.length; i++) {
// add a '\' before any double quotes
if (symbolBytes[i] == '"') {
// add a '\' before any double quotes, form feeds, new lines, carriage returns, or tabs
if (isSpecialCharacter(symbolBytes[i])) {
escapedBytes[index++] = "\\";
}
// copy each byte from original string to the new array
@@ -186,9 +186,9 @@ library Descriptor {
"Uniswap - ",
feeTier,
" - ",
escapeQuotes(params.quoteCurrencySymbol),
escapeSpecialCharacters(params.quoteCurrencySymbol),
"/",
escapeQuotes(params.baseCurrencySymbol),
escapeSpecialCharacters(params.baseCurrencySymbol),
" - ",
tickToDecimalString(
!params.flipRatio ? params.tickLower : params.tickUpper,
@@ -503,6 +503,10 @@ library Descriptor {
}
}

function isSpecialCharacter(bytes1 b) private pure returns (bool) {
return b == '"' || b == "\u000c" || b == "\n" || b == "\r" || b == "\t";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

am i mad? surely \u000c is longer than a single byte?

Copy link

@djb15 djb15 Oct 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had exactly the same thought! Turns out it gets properly escaped to 1 byte:

pragma solidity ^0.8.0;

contract RandomTest {
    function stringToBytes32() public pure returns (uint256 length) {
        string memory source = "\u000c";
        bytes memory tempEmptyStringTest = bytes(source);
        return tempEmptyStringTest.length;
    }
}

If you test this out in Remix the return value is 1 :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wild, thanks for the poc :)

}

function scale(uint256 n, uint256 inMn, uint256 inMx, uint256 outMn, uint256 outMx)
private
pure
23 changes: 14 additions & 9 deletions test/libraries/Descriptor.t.sol
Original file line number Diff line number Diff line change
@@ -39,15 +39,20 @@ contract DescriptorTest is Test {
);
}

function test_escapeQuotes_succeeds() public pure {
assertEq(Descriptor.escapeQuotes(""), "");
assertEq(Descriptor.escapeQuotes("a"), "a");
assertEq(Descriptor.escapeQuotes("abc"), "abc");
assertEq(Descriptor.escapeQuotes("a\"bc"), "a\\\"bc");
assertEq(Descriptor.escapeQuotes("a\"b\"c"), "a\\\"b\\\"c");
assertEq(Descriptor.escapeQuotes("a\"b\"c\""), "a\\\"b\\\"c\\\"");
assertEq(Descriptor.escapeQuotes("\"a\"b\"c\""), "\\\"a\\\"b\\\"c\\\"");
assertEq(Descriptor.escapeQuotes("\"a\"b\"c\"\""), "\\\"a\\\"b\\\"c\\\"\\\"");
function test_escapeSpecialCharacters_succeeds() public pure {
assertEq(Descriptor.escapeSpecialCharacters(""), "");
assertEq(Descriptor.escapeSpecialCharacters("a"), "a");
assertEq(Descriptor.escapeSpecialCharacters("abc"), "abc");
assertEq(Descriptor.escapeSpecialCharacters("a\"bc"), "a\\\"bc");
assertEq(Descriptor.escapeSpecialCharacters("a\"b\"c"), "a\\\"b\\\"c");
assertEq(Descriptor.escapeSpecialCharacters("a\"b\"c\""), "a\\\"b\\\"c\\\"");
assertEq(Descriptor.escapeSpecialCharacters("\"a\"b\"c\""), "\\\"a\\\"b\\\"c\\\"");
assertEq(Descriptor.escapeSpecialCharacters("\"a\"b\"c\"\""), "\\\"a\\\"b\\\"c\\\"\\\"");

assertEq(Descriptor.escapeSpecialCharacters("a\rbc"), "a\\\rbc");
assertEq(Descriptor.escapeSpecialCharacters("a\nbc"), "a\\\nbc");
assertEq(Descriptor.escapeSpecialCharacters("a\tbc"), "a\\\tbc");
assertEq(Descriptor.escapeSpecialCharacters("a\u000cbc"), "a\\\u000cbc");
}

function test_tickToDecimalString_withTickSpacing10() public pure {