XSS via SVG Construction contract
The NFTTokenURIScaffold contract generates an SVG image that includes the ERC20 token symbol without sanitisation. An attacker can create a token with a malicious symbol containing JavaScript, and when the SVG is viewed on a website, the script executes, leading to XSS.
Submitted by thankyou, also found by 0x1f8b_
SVG is a unique type of image file format that is often susceptible to Cross-site scripting. If a malicious user is able to inject malicious Javascript into a SVG file, then any user who views the SVG on a website will be susceptible to XSS. This can lead stolen cookies, Denial of Service attacks, and more.
The NFTTokenURIScaffold contract generates a SVG via the NFTSVG.constructSVG function. One of the arguments used by the NFTSVG.constructSVG function is svgTitle which represents the ERC20 symbols of both the asset and collateral ERC20 tokens. When generating an ERC20 contract, a malicious user can set malicious XSS as the ERC20 symbol.
These set of circumstances leads to XSS when the SVG is loaded on any website.
Proof of Concept
- Hacker generates an ERC20 token with a symbol that contains malicious Javascript.
- Hacker generates a TimeSwap Pair with an asset or collateral that matches the malicious ERC20 token created in Step 1.
- When
NFTTokenURIScaffold#constructTokenURIis called, a SVG is generated. This process works such that when generating the SVG the tainted ERC20 symbol created in Step 1 is passed to theNFTSVG.constructSVGfunction here. This function returns a SVG containing the tainted ERC20 symbol. - When the SVG is loaded on any site such as OpenSea, any user viewing that SVG will load the malicious Javascript from within the SVG and result in a XSS attack.
Recommended Mitigation Steps
Creating a SVG file inside of a Solidity contract is novel and thus requires the entity creating a SVG file to sanitize any potential user-input that goes into generating the SVG file.
As of this time there are no known Solidity libraries that sanitize text to prevent an XSS attack. The easiest solution is to remove all user-input data from the SVG file or not generate the SVG at all.
Mathepreneur (Timeswap) confirmed:
We plan to add Safety String library.
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
Impact
Any user viewing the SVG on a website could have malicious JavaScript executed in their browser, potentially leading to stolen cookies or denial of service.
What the attacker needed
Ability to create an ERC20 token; ability to create a TimeSwap pair; victim views the SVG on a website
The fix
The project planned to add a Safety String library to sanitise user input before generating the SVG.
How it was found
- Hacker generates an ERC20 token with a symbol that contains malicious Javascript.
- Hacker generates a TimeSwap Pair with an asset or collateral that matches the malicious ERC20 token created in Step 1.
- When NFTTokenURIScaffold#constructTokenURI is called, a SVG is generated containing the tainted ERC20 symbol.
- When the SVG is loaded on any site such as OpenSea, any user viewing that SVG will load the malicious Javascript from within the SVG and result in a XSS attack.
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.
This is a cross-site scripting (XSS) bug. A smart contract creates an image (SVG) that includes text from a token's name. If that text contains JavaScript code, the code runs when someone views the image on a website, which can steal cookies or do other harm.
What is XSS?
Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.