Skip to content
Business logic Audit finding Intermediate

Refunds sent to incorrect addresses in certain cases

The refund logic in the PhiFactory and Claimable contracts sends excess funds to the contract itself instead of the user when claims are made through batchClaim, claim, or directly via the Claimable contract. This results in users losing their overpayment, and in some cases the funds become locked in the contract.

0xCiphky Follow with MrPotatoMagic, pep7siup and 21 others

Originally disclosed on Code4rena

Submitted by 0xCiphky, also found by MrPotatoMagic, pep7siup, aldarion, hgrano, McToady, TECHFUND-inc (1, 2), pipidu83, 13u9, typicalHuman (1, 2), 0xc0ffEE (1, 2), DanielArmstrong (1, 2), Fitro, th3l1ghtd3m0n, ironside, farismaulana (1, 2), smaul, eLSeR17, federodes, robertodf99, CAUsr (1, 2), OpaBatyo (1, 2), kn0t, 0xNirix, avoloder, DigiSafe, jesusrod15, hail_the_lord, pfapostol, 0xAkira, KupiaSec, 0xrex (1, 2), and rbserver (1, 2)

The _processClaim internal function is responsible for calling the claimFromFactory function in the art contract, transferring the protocol fees to the protocolFeeDestination, and handling any applicable refunds.

    function _processClaim(
        ...
    )
        private
    {
        PhiArt storage art = arts[artId_];

        // Handle refund
        uint256 mintFee = getArtMintFee(artId_, quantity_);
        if ((etherValue_ - mintFee) > 0) {
            _msgSender().safeTransferETH(etherValue_ - mintFee);
        }
        protocolFeeDestination.safeTransferETH(mintProtocolFee * quantity_);

        (bool success_,) = art.artAddress.call{ value: mintFee - mintProtocolFee * quantity_ }(
            abi.encodeWithSignature(
                "claimFromFactory(uint256,address,address,address,uint256,bytes32,string)",
                artId_,
                minter_,
                ref_,
                verifier_,
                quantity_,
                data_,
                imageURI_
            )
        );

        if (!success_) revert ClaimFailed();
    }

When a user sends excess funds (msg.value) over the required mintFee, the difference is intended to be refunded to the user. This works as expected when the signatureClaim or merkleClaim functions are called directly in the PhiFactory contract by the user since msg.sender is the correct address (User). However, in some cases refunds are sent to the wrong address.

Case 1: Claim function and batchClaim function

In the batchClaim function, multiple claims are processed by calling the claim function with this (the current contract) as the caller, resulting in the msg.sender being the PhiFactory contract rather than the actual user.

    function batchClaim(bytes[] calldata encodeDatas_, uint256[] calldata ethValue_) external payable {
        if (encodeDatas_.length != ethValue_.length) revert ArrayLengthMismatch();

        // calc claim fee
        uint256 totalEthValue;
        for (uint256 i = 0; i < encodeDatas_.length; i++) {
            totalEthValue = totalEthValue + ethValue_[i];
        }
        if (msg.value != totalEthValue) revert InvalidEthValue();

        for (uint256 i = 0; i < encodeDatas_.length; i++) {
            this.claim{ value: ethValue_[i] }(encodeDatas_[i]);
        }
    }

Similarly, the claim function also uses this when calling either the signatureClaim or merkleClaim functions, further propagating the incorrect msg.sender value. Consequently, any refunds are sent to the PhiFactory contract instead of the actual user, causing an incorrect refund.

Case 2: signatureClaim function and merkleClaim function in the Claimable contract

The signatureClaim and merkleClaim functions are also callable through the Claimable contract, which is part of the PhiNFT1155 contract. However, like in the previous case, the msg.sender will be incorrect being the PhiNFT1155 contract itself, rather than the actual user who initiated the call. This results in refunds being directed to the PhiNFT1155 contract rather than the user. In this case, funds are also not able to be pulled out by the owner, effectively locking them in the contract.

Impact

  • Incorrect handling of msg.sender results in refunds being sent to contracts (PhiFactory or PhiNFT1155) rather than the intended users.

  • While the loss of funds due to overpayment is more of a user error, the primary concern here is that the intended logic for refunding users is incorrect which was specifically mentioned by the protocol to look out for.

    • Function Reliability: Ensure the contract behaves consistently under various conditions.
    • Function Correctness: Ensure the smart contract logic correctly implements the intended functionality without errors. Reference

Proof Of Concept

    function test_claim_1155_refund() public {
        // SET UP
        vm.warp(START_TIME + 1);
        uint256 artId = 1;
        address artAddress = phiFactory.getArtAddress(artId);
        vm.warp(START_TIME + 2);
        bytes memory data = _createSigSignData(true);
        bytes memory payload = abi.encodePacked(abi.encodeWithSignature("`signatureClaim()`"), data);
        // SET UP
        // Check 1155 contract ETH balance before
        assertEq(address(artAddress).balance, 0 ether);
        // send mint fee plus extra 0.1 ETH
        vm.startPrank(participant, participant);
        (bool success,) = artAddress.call{ value: phiFactory.getArtMintFee(artId, 1) + 0.1 ether }(payload);
        require(success, "1155 artAddress.call failed");

        // Check 1155 contract ETH balance, refund sent here instead
        assertEq(address(artAddress).balance, 0.1 ether);
    }

Recommendation

Modify the affected functions (batchClaim, claim, signatureClaim, and merkleClaim) to pass the original user address as a parameter. This way, the function can handle refunds correctly by transferring excess funds to the correct user.

ZaK3939 (Phi) confirmed and commented:

Correct. We are considering modifying the approach by reverting the transaction when the values don’t match.


The original report as published on Code4rena, by 0xCiphky. 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 uses msg.sender for refunds, but when claims are processed via internal calls using 'this', msg.sender becomes the contract itself rather than the original user.

Impact

Users who overpay may lose their excess funds, which are sent to the contract instead of being refunded; in the Claimable contract case, these funds become locked and cannot be recovered.

What the attacker needed

User sends more than required fee; claim via batchClaim, claim, or Claimable

The fix

The affected functions should be modified to pass the original user address as a parameter so refunds go to the correct user, or alternatively revert when the sent value does not match the required amount.

How it was found

  1. Set up the test environment with a specific art ID and time.
  2. Prepare a signature claim payload.
  3. Send a transaction to the art contract with the mint fee plus extra ETH.
  4. Observe that the extra ETH is held by the art contract instead of being refunded to the user.

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 send more money than needed for a purchase, the extra should be returned to you. In this smart contract, some functions pass the call through an intermediate contract, so the refund goes to that contract instead of you. This means you lose the extra money, and sometimes it gets stuck forever.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by 0xCiphky

View all

11 more disclosed reports, best first.

More from Phi

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.