Skip to content
XSS Audit finding Beginner

Bio Protocol - tokenURI JSON injection

The tokenURI function of the Bio NFT contract did not escape user-supplied bio text, allowing an attacker to inject JSON keys and alter the returned metadata. This could let an attacker spoof the NFT name, change the image, or potentially trigger cross-site scripting if the JSON is mishandled by a frontend.

reassor Follow with Wander, ulqiorra and 21 others

Originally disclosed on Code4rena

Submitted by reassor, also found by Wander, ulqiorra, ulqiorra, mjmoonwalker, reassor, dingo2077, Jerry0x, Parad0x, adriro, Kek, Kek, Respx, Viktor_Cortess, Viktor_Cortess, Haipls, J4de, Haipls, Englave, bin2chen, MiniGlome, djxploit, jasonxiale, and rokso

The Bio Protocol allows users to mint Bio NFTs that represent user’s bio. Once NFT is minted anyone can trigger tokenURI to retrieve JSON data with the bio and generated svg image. Example JSON content (decoded from Base64):

{"name": "Bio #1", "description": "Test", "image": "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHByZXNlcnZlQXNwZWN0UmF0aW89InhNaW5ZTWluIG1lZXQiIHZpZXdCb3g9IjAgMCA0MDAgMTAwIj48c3R5bGU+dGV4dCB7IGZvbnQtZmFtaWx5OiBzYW5zLXNlcmlmOyBmb250LXNpemU6IDEycHg7IH08L3N0eWxlPjx0ZXh0IHg9IjUwJSIgeT0iNTAlIiBkb21pbmFudC1iYXNlbGluZT0ibWlkZGxlIiB0ZXh0LWFuY2hvcj0ibWlkZGxlIj48dHNwYW4geD0iNTAlIiBkeT0iMjAiPlRlc3Q8L3RzcGFuPjwvdGV4dD48L3N2Zz4="}

The issue is that function Bio.tokenURI is vulnerable to JSON injection and it is possible to inject " character into bio completely altering the integrity of JSON data. User sends following data as bio:

Test\", \"name\": \"Bio #999999999

This is will result in following JSON data (new name key was injected):

{"name": "Bio #1", "description": "Test", "name": "Bio #999999999", "image": "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHByZXNlcnZlQXNwZWN0UmF0aW89InhNaW5ZTWluIG1lZXQiIHZpZXdCb3g9IjAgMCA0MDAgMTAwIj48c3R5bGU+dGV4dCB7IGZvbnQtZmFtaWx5OiBzYW5zLXNlcmlmOyBmb250LXNpemU6IDEycHg7IH08L3N0eWxlPjx0ZXh0IHg9IjUwJSIgeT0iNTAlIiBkb21pbmFudC1iYXNlbGluZT0ibWlkZGxlIiB0ZXh0LWFuY2hvcj0ibWlkZGxlIj48dHNwYW4geD0iNTAlIiBkeT0iMjAiPlRlc3QiLCAibmFtZSI6ICJCaW8gIzk5OTk5OTk5OTwvdHNwYW4+PC90ZXh0Pjwvc3ZnPg=="}

Depending on the usage of Bio Protocol by frontend or 3rd party applications this will have serious impact on the security. Some examples of the attacks that can be carried:

  1. Attacker might create a Bio NFT that mimics copies the name of different Bio NFT.
  2. Attacker might change the generated svg image to completely different one.
  3. Attacker might point image to external URL.
  4. Attacker might trigger cross-site scripting attacks if the data from JSON is not properly handled.

Proof of Concept

https://github.com/code-423n4/2023-03-canto-identity/blob/077372297fc419ea7688ab62cc3fd4e8f4e24e66/canto-bio-protocol/src/Bio.sol#L103-L115

Tools Used

Manual Review / Foundry / VSCode

Recommended Mitigation Steps

It is recommended to properly encode user’s bio to make sure it cannot alter returned JSON data.

OpenCoreCH (Canto Identity) disagreed with severity and commented:

Good point that will be fixed, have some doubts about the severity. The attacks that are mentioned on the frontend in various issues (XSS / external resources) should be handled there in any case because the payload of a NFT should not be considered trusted. And if a user manipulates his own NFT, the impact is limited to him.

0xleastwood (judge) commented:

@OpenCoreCH - It doesn’t seem like this sort of tokenURI() sanitisation can be made within the contract but instead must be handled by front-ends right?

OpenCoreCH (Canto Identity) commented:

@0xleastwood - I implemented some sanitisation in the mean time (escaping all characters that need to be escaped for the JSON / SVG) using Solady’s escape functions. But that’s mainly to ensure that the produced JSON / SVG will always be valid (and the user does not accidentally produce an invalid one by choosing some characters in his bio). I think perfect sanitisation on-chain is not feasible in practice and the frontends should generally treat this payload as untrusted and sanitise it. Otherwise, you could also actively deploy malicious NFTs to e.g. target NFT marketplace users.

0xleastwood (judge) decreased severity to Medium and commented:

@OpenCoreCH - Completely agree. I think medium severity would make more sense then.


The original report as published on Code4rena, by reassor. 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

The contract did not escape or encode user input before embedding it into a JSON string, allowing injection of JSON syntax.

Impact

Depending on how frontends or third-party applications use the JSON, an attacker could spoof the NFT name, change the image, point to an external URL, or potentially trigger cross-site scripting if the data is not properly handled.

What the attacker needed

Ability to mint a Bio NFT

The fix

The contract was updated to escape characters that need escaping for JSON and SVG using Solady's escape functions, though the team noted that perfect on-chain sanitisation is not feasible and frontends should treat the payload as untrusted.

How it was found

  1. Mint a Bio NFT with a bio containing a double quote and additional JSON key-value pairs.
  2. Call tokenURI to retrieve the JSON metadata.
  3. Observe that the injected JSON keys appear in the output, altering the metadata.

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.

When you create a Bio NFT, you can write a short bio. The contract puts that bio into a JSON data structure without cleaning it up. If you include special characters like a double quote, you can break out of the bio field and add new fields, like a fake name or a different image. This is called JSON injection.

What is XSS?

Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.

More by reassor

View all

5 more disclosed reports, 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.

Cross-site scripting (XSS)

The lesson behind this report.

Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.