Santa's List

AI First Flight #3
Beginner FriendlyFoundry
EXP
View results
Submission Details
Severity: high
Valid

# `buyPresent()` burns tokens from the receiver and mints the NFT to the buyer instead of gifting it

Description

According to the protocol specification, `buyPresent()` should allow a user to spend 2 SantaToken in exchange for minting an NFT present to another user.

However, the implementation does the opposite of the intended flow. The function burns SantaToken from `presentReceiver`, while the NFT is minted to `msg.sender`.

function buyPresent(address presentReceiver) external {
// @> Burns from the receiver instead of the buyer
i_santaToken.burn(presentReceiver);
// @> Mints the NFT to msg.sender instead of presentReceiver
_mintAndIncrement();
}
function _mintAndIncrement() private {
// @> NFT is minted to the caller, not to the intended present receiver
_safeMint(msg.sender, s_tokenCounter++);
}
function burn(address from) external {
if (msg.sender != i_santasList) {
revert SantaToken__NotSantasList();
}
// @> Only burns 1 SantaToken, while the spec requires 2 SantaToken
_burn(from, 1e18);
}

Risk

Likelihood:

  • This occurs whenever a user calls buyPresent()with a presentReceiverthat owns at least 1 SantaToken.

  • The function does not check that the caller owns or spends the required 2 SantaToken.

  • The function always mints the NFT to the `msg.sender`.

Impact:

  • A malicious user can burn another user's SantaToken balance without their consent.

  • A malicious user can mint NFTs to themselves without paying the required token price.

  • The intended gift mechanism is broken because the receiver loses tokens and does not receive the NFT.

  • The SantaToken/NFT accounting becomes inconsistent with the application specification.

Proof of Concept

In this test usercalls buyPresent(receiver). Instead of spending 2 SantaToken and gifting an NFT to the receiver, user receives the newly minted NFT and keeps their SantaToken balance. Meanwhile, receiver loses 1 SantaToken and receives no NFT.

function testBuyPresentBadBehavior() public {
vm.startPrank(santa);
santasList.checkList(user, SantasList.Status.EXTRA_NICE);
santasList.checkTwice(user, SantasList.Status.EXTRA_NICE);
santasList.checkList(receiver, SantasList.Status.EXTRA_NICE);
santasList.checkTwice(receiver, SantasList.Status.EXTRA_NICE);
vm.stopPrank();
vm.warp(santasList.CHRISTMAS_2023_BLOCK_TIME() + 1);
vm.prank(receiver);
santasList.collectPresent();
assertEq(santasList.balanceOf(receiver), 1);
assertEq(santaToken.balanceOf(receiver), 1e18);
vm.startPrank(user);
//Irrelevant here
//santaToken.approve(address(santasList), 1e18);
santasList.collectPresent();
santasList.buyPresent(receiver);
assertEq(santasList.balanceOf(user), 2);
assertEq(santaToken.balanceOf(user), 1e18);
assertEq(santasList.balanceOf(receiver), 1);
assertEq(santaToken.balanceOf(receiver), 0);
vm.stopPrank();
} `

Recommended Mitigation

The function should burn the required amount of SantaToken from msg.sender and mint the NFT to presentReceiver. One possible fix is to update the burn function so the amount can be specified.

Note: The specification says buyPresent() should cost 2 SantaToken. However, users appear to only be able to receive 1 SantaToken per eligible year through collectPresent(). This may mean the intended design is that users must accumulate SantaToken across multiple years before buying a present, or that the specification is inconsistent. In either case, the current implementation is still incorrect because buyPresent() burns tokens from presentReceiver and mints the NFT to msg.sender, so the caller does not pay and the receiver does not receive the present.

- function burn(address from) external {
+ function burn(address from, uint256 amount) external {
if (msg.sender != i_santasList) {
revert SantaToken__NotSantasList();
}
- _burn(from, 1e18);
+ _burn(from, amount);
}
function buyPresent(address presentReceiver) external {
- i_santaToken.burn(presentReceiver);
- _mintAndIncrement();
+ i_santaToken.burn(msg.sender, 2e18);
+ _safeMint(presentReceiver, s_tokenCounter++);
}
Updates

Lead Judging Commences

ai-first-flight-judge Lead Judge 6 days ago
Submission Judgement Published
Validated
Assigned finding tags:

[H-03] SantasList::buyPresent burns token from presentReceiver instead of caller and also sends present to caller instead of presentReceiver.

## Description The `buyPresent` function sends the present to the `caller` of the function but burns token from `presentReceiver` but the correct method should be the opposite of it. Due to this implementation of the function, malicious caller can mint NFT by burning the balance of other users by passing any arbitrary address for the `presentReceiver` field and tokens will be deducted from the `presentReceiver` and NFT will be minted to the malicious caller. Also, the NatSpec mentions that one has to approve `SantasList` contract to burn their tokens but it is not required and even without approving the funds can be burnt which means that the attacker can burn the balance of everyone and mint a large number of NFT for themselves. `buyPresent` function should send the present (NFT) to the `presentReceiver` and should burn the SantaToken from the caller i.e. `msg.sender`. ## Vulnerability Details The vulnerability lies inside the SantasList contract inside the `buyPresent` function starting from line 172. The buyPresent function takes in `presentReceiver` as an argument and burns the balance from `presentReceiver` instead of the caller i.e. `msg.sender`, as a result of which an attacker can specify any address for the `presentReceiver` that has approved or not approved the SantasToken (it doesn't matter whether they have approved token or not) to be spent by the SantasList contract, and as they are the caller of the function, they will get the NFT while burning the SantasToken balance of the address specified in `presentReceiver`. This vulnerability occurs due to wrong implementation of the buyPresent function instead of minting NFT to presentReceiver it is minted to caller as well as the tokens are burnt from presentReceiver instead of burning them from `msg.sender`. Also, the NatSpec mentions that one has to approve `SantasList` contract to burn their tokens but it is not required and even without approving the funds can be burnt which means that the attacker can burn the balance of everyone and mint a large number of NFT for themselves. ```cpp /* * @notice Buy a present for someone else. This should only be callable by anyone with SantaTokens. * @dev You'll first need to approve the SantasList contract to spend your SantaTokens. */ function buyPresent(address presentReceiver) external { @> i_santaToken.burn(presentReceiver); @> _mintAndIncrement(); } ``` ## PoC Add the test in the file: `test/unit/SantasListTest.t.sol` Run the test: ```cpp forge test --mt test_AttackerCanMintNft_ByBurningTokensOfOtherUsers ``` ```cpp function test_AttackerCanMintNft_ByBurningTokensOfOtherUsers() public { // address of the attacker address attacker = makeAddr("attacker"); vm.startPrank(santa); // Santa checks user once as EXTRA_NICE santasList.checkList(user, SantasList.Status.EXTRA_NICE); // Santa checks user second time santasList.checkTwice(user, SantasList.Status.EXTRA_NICE); vm.stopPrank(); // christmas time 🌳🎁 HO-HO-HO vm.warp(santasList.CHRISTMAS_2023_BLOCK_TIME()); // User collects their NFT and tokens for being EXTRA_NICE vm.prank(user); santasList.collectPresent(); assertEq(santaToken.balanceOf(user), 1e18); uint256 attackerInitNftBalance = santasList.balanceOf(attacker); // attacker get themselves the present by passing presentReceiver as user and burns user's SantaToken vm.prank(attacker); santasList.buyPresent(user); // user balance is decremented assertEq(santaToken.balanceOf(user), 0); assertEq(santasList.balanceOf(attacker), attackerInitNftBalance + 1); } ``` ## Impact - Due to the wrong implementation of function, an attacker can mint NFT by burning the SantaToken of other users by passing their address for the `presentReceiver` argument. The protocol assumes that user has to approve the SantasList in order to burn token on their behalf but it will be burnt even though they didn't approve it to `SantasList` contract, because directly `_burn` function is called directly by the `burn` function and both of them don't check for approval. - Attacker can burn the balance of everyone and mint a large number of NFT for themselves. ## Recommendations - Burn the SantaToken from the caller i.e., `msg.sender` - Mint NFT to the `presentReceiver` ```diff + function _mintAndIncrementToUser(address user) private { + _safeMint(user, s_tokenCounter++); + } function buyPresent(address presentReceiver) external { - i_santaToken.burn(presentReceiver); - _mintAndIncrement(); + i_santaToken.burn(msg.sender); + _mintAndIncrementToUser(presentReceiver); } ``` By applying this recommendation, there is no need to worry about the approvals and the vulnerability - 'tokens can be burnt even though users don't approve' will have zero impact as the tokens are now burnt from the caller. Therefore, an attacker can't burn others token.

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.

Give us feedback!