Skip to content
Business logic Audit finding Intermediate

Incorrect emoji displaying

The Bio contract's tokenURI function incorrectly splits bio text into lines when the text contains certain emoji sequences, such as the England flag, because it does not handle all Unicode cases. This leads to an incorrect display of the generated image, which cannot be modified afterwards, potentially causing integration problems.

glcanvas Follow with fs0c and adriro

Originally disclosed on Code4rena

Submitted by glcanvas, also found by fs0c and adriro

Possible to split bio incorrectly, this leads to incorrect display of generated image. This image will be impossible to modify. This might lead to integration problems.

Proof of Concept

Let’s consider tokenURI function of the Bio contract.

Here implemented corner case for some emojis, but current implementation doesn’t handle all cases. I suppose handling all cases is redundant, erroneously, complicated and unnecessary.

Let’s consider part of tokenURI code in python, this part just rewritten on Python part of Solidity code:

import binascii

def sol_func_same(bio):
    bioTextBytes = str.encode(bio, "utf-8")
    lengthInBytes = len(bioTextBytes)
    lines = (lengthInBytes - 1) // 40 + 1
    strLines = [None for _ in range(lines)]
    prevByteWasContinuation = False
    insertedLines = 0
    bytesLines = []
    bytesOffset = 0
    for i in range(0, lengthInBytes):
        character = bioTextBytes[i]
        bytesLines.append(character)
        bytesOffset += 1
        if ((i > 0 and (i + 1) % 40 == 0) or prevByteWasContinuation or i == lengthInBytes - 1):
            nextCharacter = 0
            if (i != lengthInBytes - 1):  # 🏴󠁧󠁢󠁥󠁮󠁧󠁿
                nextCharacter = bioTextBytes[i + 1]
            if (nextCharacter & 0xC0 == 0x80):
                prevByteWasContinuation = True
            else:
                if (
                        (nextCharacter == 0xE2 and bioTextBytes[i + 2] == 0x80 and bioTextBytes[i + 3] == 0x8D) or
                        (nextCharacter == 0xF0 and
                         bioTextBytes[i + 2] == 0x9F and
                         bioTextBytes[i + 3] == 0x8F and
                         int(bioTextBytes[i + 4]) >= 187 and
                         int(bioTextBytes[i + 4]) <= 191) or
                        (i >= 2 and
                         bioTextBytes[i - 2] == 0xE2 and
                         bioTextBytes[i - 1] == 0x80 and
                         bioTextBytes[i] == 0x8D)
                ):
                    prevByteWasContinuation = True
                    continue

                strLines[insertedLines] = binascii.unhexlify(''.join([hex(x)[2:] for x in bytesLines])).decode("utf-8")
                insertedLines += 1
                bytesLines = []

                prevByteWasContinuation = False
                bytesOffset = 0

    for idx, i in enumerate(strLines):
        print(idx, i)

if __name__ == "__main__":
    sol_func_same("0🏴󠁧󠁢󠁥󠁮󠁧󠁿")
    print("===" * 20)
    sol_func_same("000000000000000000000000000000🏴󠁧󠁢󠁥󠁮󠁧󠁿")

The result is:

0 0🏴󠁧󠁢󠁥󠁮󠁧󠁿
============================================================
0 000000000000000000000000000000🏴󠁧󠁢
1 󠁥󠁮󠁧󠁿

In the second line present 31 character.

but correct answer for the second case is:

000000000000000000000000000000🏴󠁧󠁢󠁥󠁮󠁧󠁿

This happened because Eng flag presented with combination of different emojis, this case isn’t handled by contract.

Similar test might be added to the Bio.t.sol contract:

    function testSmallLine() public {
        string memory text = unicode"000🏴󠁧󠁢󠁥󠁮󠁧󠁿";
        bio.mint(text);
        string memory result = bio.tokenURI(1);
        console.logString(result);
    }

    function testLongLine() public {
        string memory text = unicode"000000000000000000000000000000🏴󠁧󠁢󠁥󠁮󠁧󠁿";
        bio.mint(text);
        string memory result = bio.tokenURI(1);
        console.logString(result);
    }

You need to decode them and you’ll receive the same incorrect result.

Tools Used

Manual audit, Python3

Recommended Mitigation Steps

Do not try to implement spec by themselves.

  1. Add view function which is responsible to create svg image. In tokenURI call this function to generate svg result. Like this:
function generateSvg(string memory bioText) public view returns(string) {
        bytes memory bioTextBytes = bytes(bioText);
        uint lengthInBytes = bioTextBytes.length;
        // Insert a new line after 40 characters, taking into account unicode character
        uint lines = (lengthInBytes - 1) / 40 + 1;
...
...
...
        return string(abi.encodePacked("data:application/json;base64,", json));
}
  1. Add possibility to manual split by lines. i.e. with bio additionally store offsets in bytes, by which lines must be split.
function mint(string calldata _bio, uint256[] calldata byteSplit) external {
...
}

function generateSvg(string memory bioText, uint256[] memory byteSplit) public view returns(string) {
...
bytes memory bioTextBytes = bytes(bioText);
in for loop:
    slice = bioTextBytes[i:i+1]
    write slice to svg
}

OpenCoreCH (Canto Identity) confirmed


The original report as published on Code4rena, by glcanvas. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the line-splitting logic in tokenURI only handles a few specific emoji sequences and fails to account for all multi-byte Unicode characters, causing incorrect splitting.

Impact

The generated image may display incorrectly for certain emoji sequences, and since the image cannot be modified, this could lead to integration problems.

What the attacker needed

A minted bio with certain emoji sequences

The fix

The recommendation is to avoid implementing the specification manually and instead add a view function to generate the SVG, or allow manual line splitting by storing byte offsets.

How it was found

  1. Mint a bio containing the England flag emoji preceded by many zeros.
  2. Call tokenURI for the minted token.
  3. Observe that the output splits the emoji incorrectly, producing a broken line.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

The contract that creates a picture from your bio text tries to break the text into lines every 40 characters. But some emojis are made of several characters, and the code doesn't always recognise them, so it cuts the emoji in half. The resulting picture shows the emoji broken or missing, and you can't fix it later.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by glcanvas

View all

1 more disclosed report, best first.

More from Canto Identity Subprotocols contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.