Incorrect isApprovedForAll check in the NukeFund.nuke() function
The NukeFund.nuke function incorrectly checks whether the caller (msg.sender) has approved the contract for all tokens, rather than checking the token owner's approval. This prevents approved users from nuking tokens they are authorised to burn.
In the NukeFund.nuke function, it checks whether nftContract.isApprovedForAll(msg.sender, address(this)) is true from L160 and tries to burn the NFT token of tokenID from L176.
function nuke(uint256 tokenId) public whenNotPaused nonReentrant {
require(
nftContract.isApprovedOrOwner(msg.sender, tokenId),
'ERC721: caller is not token owner or approved'
);
require(
nftContract.getApproved(tokenId) == address(this) ||
L160: nftContract.isApprovedForAll(msg.sender, address(this)),
'Contract must be approved to transfer the NFT.'
);
[...]
L176: nftContract.burn(tokenId); // Burn the token
[...]
}
In the TraitForgeNft.burn function, it checks whether isApprovedOrOwner(msg.sender, tokenId) is true from L143.
Totally nuking tokens requires nftContract.isApprovedOrOwner(msg.sender, tokenId) && (nftContract.getApproved(tokenId) == address(this) ||nftContract.isApprovedForAll(msg.sender, address(this))) where this is NukeFund contract.
Burning tokens requires isApprovedOrOwner(msg.sender, tokenId) where msg.sender is NukeFund contract.
Checking isApprovedOrOwner(msg.sender, tokenId) is same as msg.sender == owner || getApproved(tokenId) == msg.sender || isApprovedForAll(owner, msg.sender).
The nuke function should check nftContract.isApprovedForAll(nftContract.ownerOf(tokenId), address(this)), instead of nftContract.isApprovedForAll(msg.sender, address(this)).
Recommended Mitigation Steps
It is recommended to change the code as follows:
function nuke(uint256 tokenId) public whenNotPaused nonReentrant {
require(
nftContract.isApprovedOrOwner(msg.sender, tokenId),
'ERC721: caller is not token owner or approved'
);
require(
nftContract.getApproved(tokenId) == address(this) ||
- nftContract.isApprovedForAll(msg.sender, address(this)),
+ nftContract.isApprovedForAll(nftContract.ownerOf(tokenId), address(this)),
'Contract must be approved to transfer the NFT.'
);
[...]
}
This report clearly demonstrates the presence of a bug.
Scenario
Consider the following scenario:
Alice approved the NukeFund contract as an operator.(Alice called setApprovalForAll(nukeFund, true)).
Alice is the owner of an entity.
Alice gave permission to Bob for the entity. (Alice called approve(Bob's address, the entity's tokenId)).
Bob called nuke(the entity's tokenId).
In the scenario described above, it should be allowed for Bob to call nuke(the entity's tokenId). However, Bob’s call will be reverted due to the bug highlighted in the report.
nftContract.getApproved(tokenId) returns Bob, not address(this). Additionally, msg.sender at L160 is Bob who have not approved the NukeFund contract as an operator. Consequently, the nuke() function will be reverted unexpectedly.
So, msg.sender at L160 should be replaced by nftContract.ownerOf(tokenId):.
require(
nftContract.getApproved(tokenId) == address(this) ||
160: nftContract.isApprovedForAll(msg.sender, address(this)),
'Contract must be approved to transfer the NFT.'
);
This unexpected reversal also takes place when Alice approves Bob as an operator in step 3 of the scenario. Therefore, I believe this issue is valid.
Coded PoC
Add the following code into test/NukeFund.test.ts: In the PoC, Alice is the owner and Bob is the user1.
it('should revert to nuke a token', async function () {
const tokenId = 1;
// Mint a token
await nft.connect(owner).mintToken(merkleInfo.whitelist[0].proof, {
value: ethers.parseEther('1'),
});
// Send some funds to the contract
await user1.sendTransaction({
to: await nukeFund.getAddress(),
value: ethers.parseEther('1'),
});
const prevNukeFundBal = await nukeFund.getFundBalance();
// Ensure the token can be nuked
expect(await nukeFund.canTokenBeNuked(tokenId)).to.be.true;
const prevUserEthBalance = await ethers.provider.getBalance(
await owner.getAddress()
);
// await nft.connect(owner).approve(await nukeFund.getAddress(), tokenId);
await nft.connect(owner).approve(user1, tokenId);
await nft.connect(owner).setApprovalForAll(await nukeFund.getAddress(), true);
const finalNukeFactor = await nukeFund.calculateNukeFactor(tokenId);
const fund = await nukeFund.getFundBalance();
// await expect(nukeFund.connect(owner).nuke(tokenId))
await expect(nukeFund.connect(user1).nuke(tokenId)).to.be.revertedWith("Contract must be approved to transfer the NFT.");
});
To summarize this, Alice approves Bob, Bob calls nuke but it reverts because it expect Bob to approve the contract which shouldn’t be a requirement.
I believe this is a valid issue due to the broken functionality.
The original report as published on Code4rena, by KupiaSec. 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.
Generating the AI write-up for this report.
This usually takes about 40 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
How it worked
Root cause
The code uses msg.sender instead of the token owner when checking isApprovedForAll, so it verifies the caller's approval rather than the owner's approval.
Impact
Approved users cannot nuke NFTs on behalf of the owner, breaking the intended functionality.
What the attacker needed
Owner approves NukeFund as operator; owner approves another user for the token; approved user calls nuke
The fix
The check should use the token owner's address instead of msg.sender when calling isApprovedForAll.
How it was found
Alice approves the NukeFund contract as an operator.
Alice owns an NFT.
Alice approves Bob for that NFT.
Bob calls nuke with the token ID.
The call reverts because the contract checks Bob's approval instead of Alice's.
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.
Generating the AI write-up for this report.
This usually takes about 40 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
In this smart contract, users can 'nuke' (burn) an NFT to get a reward. The contract checks if the person calling nuke is allowed to burn the NFT. But it checks the wrong person's approval: it checks if the caller has approved the contract, instead of checking if the NFT's owner has approved the contract. So even if the owner approved someone else to nuke on their behalf, that someone else cannot do it because the contract wrongly requires them to have approved the contract themselves.
What is Access control?
A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.
A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.