Skip to content
Race condition Audit finding Intermediate

TokenShop: purchase function should add _purchasePrices parameter

The TokenShop purchase function read the NFT price from storage at execution time, so an owner price change mined ahead of a user's purchase could make the user pay more than expected. An attacker (the owner) could front-run a purchase in the same block and force the buyer to pay the higher price.

prePO contest

Originally disclosed on Code4rena

In the purchase function of the TokenShop contract, the price of the NFT is represented by _contractToIdToPrice, and _contractToIdToPrice can be set by the owner in the setContractToIdToPrice function.

If setContractToIdToPrice and the purchase function are executed in the same block, the user may suffer a loss due to the new _contractToIdToPrice.

Consider the following scenarios:

The current _contractToIdToPrice is 500. the user likes the price, and the purchase function is called.

But at this time, the setContractToIdToPrice function is called, setting the _contractToIdToPrice to 1000, and this transaction occurs before executing the purchase function, causing the user to execute the purchase function in the case of _contractToIdToPrice 1000.

Proof of Concept

https://github.com/prepo-io/prepo-monorepo/blob/9ed5587e557a1189df6c5bb0229ad8c171eaabce/apps/smart-contracts/token/contracts/token-shop/TokenShop.sol#L35-L41

https://github.com/prepo-io/prepo-monorepo/blob/9ed5587e557a1189df6c5bb0229ad8c171eaabce/apps/smart-contracts/token/contracts/token-shop/TokenShop.sol#L26-L31

Recommended Mitigation Steps

Add the *purchasePrices parameter to the purchase function of TokenShop, and verify that _purchasePrices[i] >= _contractToIdToPrice[*tokenContracts[i]][_ids[i]]

prePO (sponsor) confirmed and resolved:

Fixed in PR 355.

cccz (warden) reviewed mitigation:

Fixed by adding purchasePrices parameter in purchase() to avoid race condition and ensure that tokens are not purchased at higher prices than the user intended.

The original report as published on Code4rena. 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 purchase function reads the current price from contract storage instead of accepting a maximum price from the caller, so a price update can land before the purchase in the same block.

Impact

A buyer could pay a higher price than intended if the owner changes the price before their purchase is mined.

What the attacker needed

Owner can change price; price change mined before purchase

The fix

The purchase function was fixed by adding a purchasePrices parameter and checking it against the stored price.

How it was found

  1. The current price is 500 and the user calls purchase.
  2. The owner calls setContractToIdToPrice, setting the price to 1000.
  3. That transaction is mined before the user's purchase.
  4. The purchase executes at the new price of 1000.

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.

A smart contract is a program on a blockchain. Here the shop's owner can change an item's price, and a buyer's purchase order can be processed after that change even if the buyer sent it first. The buyer then pays the new, higher price without agreeing to it.

What is Race condition?

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.

More from prePO contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Race conditions

The lesson behind this report.

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.