# The receive_cross_chain_msg function has potential replay attack risks

Chakra handler trusts settlement contract for replay checks; swapping settlement address resets storage, letting old cross-chain messages be replayed.

- Payout: Audit contest
- Severity: medium
- Program: Chakra
- Found by: shaflow2
- Disclosed: 2025-02-20
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2024-08-chakra-findings/issues/85
- Library page: https://bugbountylibrary.com/r/c4%3A2024-08-chakra%3AM-08
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:code4rena sev:medium tech:solidity technique:chained technique:source-review vuln:access-control vuln:signature-replay

**Target:** Cairo cross-chain message handler contract (receive_cross_chain_msg)
**Root cause:** receive_cross_chain_msg does not track executed message IDs itself; it relies on the settlement contract's state. Replacing settlement_address via upgrade_settlement gives fresh storage, erasing prior execution records.
**Steps:**
1. Owner calls `upgrade_settlement(new_settlement)` pointing handler at a new settlement contract with empty storage.
2. Attacker resubmits a previously executed cross-chain message (same cross_chain_msg_id, from_chain, from_handler, payload) through the new settlement.
3. Handler's asserts (to_handler, caller==settlement, support_handler) pass; no local status check rejects the replay.
4. Message executes again, causing duplicate token transfers/state changes.
**How found:** Code reading: noticed receive_cross_chain_msg lacks local execution-status storage and delegates replay protection entirely to settlement; combined with owner-mutable settlement_address, hypothesized storage reset enables replay.
**Exploitation:** Requires settlement address replacement without validator rotation; attacker replays old valid messages through the new settlement to re-trigger handler execution and duplicate effects.
**Insight:** When a contract delegates replay/dedup checks to an external contract, check whether that external address is mutable — swapping it resets the dedup state and reopens old messages.

## Proof of concept (copied exactly from the original report)

**receive_cross_chain_msg missing replay status check**

```
fn receive_cross_chain_msg(ref self: ContractState, cross_chain_msg_id: u256, from_chain: felt252, to_chain: felt252,
        from_handler: u256, to_handler: ContractAddress, payload: Array<u8>) -> bool{
            assert(to_handler == get_contract_address(),'error to_handler');

            assert(self.settlement_address.read() == get_caller_address(), 'not settlement');

            assert(self.support_handler.read((from_chain, from_handler)) && 
                    self.support_handler.read((to_chain, contract_address_to_u256(to_handler))), 'not support handler');

            ...
            
            return true;
        }
```

**Owner-upgradeable settlement address function**

```
fn upgrade_settlement(ref self:ContractState, new_settlement: ContractAddress){
            self.ownable.assert_only_owner();
            self.settlement_address.write(new_settlement);
        }
```

