Skip to content
Access control Write-up Intermediate

[web3] Critical NFT Bridge Vulnerability: Potential Theft of Deposited NFTs. | by Heuss | Jun, 2023 | Medium

The NFT bridge contracts on L1 and L2 failed to verify that the L2 token corresponds to the L1 token during withdrawals. An attacker could create a worthless L2 token and use it to withdraw any deposited NFT from the L1 bridge.

medium.com

Published on medium.com

Smart Contract Security

Heuss, on medium.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 6 min read.

Context

This write-up covers a critical vulnerability in an NFT bridge contract that moves ERC721 tokens between a Layer 1 (L1) chain and a Layer 2 (L2) chain built on an optimistic rollup. The bridge is split across two contracts: L1ERC721Bridge.sol on L1 and L2ERC721Bridge.sol on L2, connected by cross-domain messaging.

The bug sits in the withdrawal path, the direction that goes from L2 back to L1. When a user withdraws, the L2 contract burns the L2 token and sends a message to L1 telling it which L1 token to release. The L1 contract then transfers the NFT out of its own balance to the recipient. The vulnerability lets an attacker make that L1 release happen for an NFT they never deposited, so any NFT held by the L1ERC721Bridge contract can be taken.

The author reported it the same day the project was listed on Immunefi.

Setup

There are no accounts, tools or versions to set up in the usual sense. The relevant preconditions are:

  1. A deployed pair of bridge contracts, L1ERC721Bridge.sol and L2ERC721Bridge.sol, connected by the rollup's cross-domain messaging.
  2. At least one NFT deposited and held by the L1ERC721Bridge contract, which is the normal state whenever users have bridged tokens to L2.
  3. The ability to deploy a contract on L2, so the attacker can create their own token.
  4. The ability to call the public withdrawTo() function on L2ERC721Bridge.sol.

The L1 side only accepts the finalising call from the authorised cross-domain account, which is the L2 bridge. The attacker never needs to call L1 directly: the L2 bridge makes that call for them.

Finding it

The problem is a missing link between the two tokens named in a withdrawal. On L1, finalizeERC721Withdrawal takes both an _l1Token and an _l2Token, but it never checks that the two correspond to each other. It simply transfers _l1Token with the given _tokenId out of the bridge:

Solidity
function finalizeERC721Withdrawal(        address _l1Token,        address _l2Token,        address _from,        address _to,        uint256 _tokenId,        bytes calldata _data    ) external onlyFromCrossDomainAccount(l2ERC721Bridge) {                        IERC721(_l1Token).transferFrom(address(this), _to, _tokenId);                emit ERC721WithdrawalFinalized(_l1Token, _l2Token, _from, _to, _tokenId, _data);    }

The only guard is onlyFromCrossDomainAccount(l2ERC721Bridge), which proves the message came from the L2 bridge but says nothing about whether the withdrawal is legitimate.

On L2, _initiateWithdrawal trusts whatever token address it is given. It burns the token from the caller and then asks that same token contract which L1 token it maps to:

Solidity
function _initiateWithdrawal(        address _l2Token,        address _from,        address _to,        uint256 _tokenId,        uint32 _l1Gas,        bytes calldata _data    ) internal {                      IL2StandardERC721(_l2Token).burn(msg.sender, _tokenId);                address l1Token = IL2StandardERC721(_l2Token).l1Token();        bytes memory message;        message = abi.encodeWithSelector(            IL1ERC721Bridge.finalizeERC721Withdrawal.selector,            l1Token,            _l2Token,            _from,            _to,            _tokenId,            _data        );                        sendCrossDomainMessage(l1ERC721Bridge, _l1Gas, message);                emit WithdrawalInitiated(l1Token, _l2Token, msg.sender, _to, _tokenId, _data);    }

There is no check that _l2Token is a token the bridge actually knows about, and no check that the l1Token it reports is genuine. Because l1Token() is just a function on the supplied contract, the attacker controls its return value. That is the whole bug: the L2 side lets the attacker choose the L1 token that the L1 side will release.

Exploitation

Step 1: deploy a worthless L2 token

The attacker creates their own ERC721-style contract on L2 that implements the interface the bridge expects, including a burn function and an l1Token() function. The l1Token() function returns a valid L1 token address that the attacker wants the L1 bridge to release. The token itself has no value and no connection to the bridge.

Step 2: call withdrawTo() with a valid token ID

The attacker calls the public withdrawal entry point on L2ERC721Bridge.sol, passing their fake token and a _tokenId that exists in the target L1 collection and is currently held by the bridge:

Solidity
function withdrawTo(        address _l2Token,        address _to,        uint256 _tokenId,        uint32 _l1Gas,        bytes calldata _data    ) external virtual {        _initiateWithdrawal(_l2Token, msg.sender, _to, _tokenId, _l1Gas, _data);    }

This forwards straight into _initiateWithdrawal. The bridge calls burn on the fake token, which succeeds because the attacker wrote it, and then reads l1Token() from the fake token, which returns the real L1 token address. The bridge then encodes a finalizeERC721Withdrawal message with that L1 token, the fake L2 token, the attacker's chosen recipient and the valid token ID, and sends it across the domain.

Step 3: the L1 bridge releases the NFT

The cross-domain message arrives at L1ERC721Bridge.sol and is delivered by the authorised L2 bridge account, so onlyFromCrossDomainAccount passes. finalizeERC721Withdrawal runs with the attacker-supplied _l1Token and _tokenId and executes:

Solidity
IERC721(_l1Token).transferFrom(address(this), _to, _tokenId);

The NFT moves from the bridge contract to the attacker's address. No deposit ever happened, and the fake L2 token is irrelevant to the transfer. The attacker can steal any deposited NFT held by the bridge.

Root cause

The bridge treats the L2 token as the authority on which L1 token a withdrawal refers to. _initiateWithdrawal calls l1Token() on an arbitrary address and forwards the result to L1 without validating that the L2 token was ever registered by the bridge. On the L1 side, finalizeERC721Withdrawal trusts the _l1Token it receives and transfers it without checking that a matching deposit exists. The two contracts never agree on a set of legitimate token pairs, so the only thing standing between an attacker and the bridge's balance is the cross-domain authentication, which proves origin but not legitimacy.

Impact

An attacker can steal any NFT deposited in the L1ERC721Bridge contract. The only requirements are a valid _tokenId for a token the bridge holds and the ability to deploy and call a contract on L2. The fake token costs nothing to create, and the burn on it has no economic effect. Theft is limited to NFTs the bridge currently custodies; the report does not describe any effect on tokens held elsewhere or on the L2 side beyond the burn of the attacker's own token.

Fix

The author recommended tracking valid deposits explicitly. Add a mapping in L1ERC721Bridge.sol keyed by the L1 token and its corresponding L2 token, storing a count of deposits. Increment it in depositERC721():

Solidity
deposits[_l1Token][_l2Token] = deposits[_l1Token][_l2Token] + 1;

And decrement it in finalizeERC721Withdrawal():

Solidity
deposits[_l1Token][_l2Token] = deposits[_l1Token][_l2Token] - 1;

With this in place, a withdrawal for a pair that was never deposited has a count of zero and can be rejected, so the attacker's fake token cannot unlock a real L1 NFT. The author recommended this change.

  1. Unvalidated token pair in other bridge directions. Look for finalise functions that accept both a local and a remote token address and transfer the local one. A positive result is a finalise function whose only guard is the cross-domain sender check, with no lookup of a registered pair.
  2. Attacker-controlled metadata functions. Search for calls such as l1Token(), underlying(), asset() or similar on a user-supplied address inside a bridge or vault. A positive result is a path where the return value is forwarded into a privileged action without checking that the address is registered.
  3. Deposit accounting that is never consulted. Check whether a deposit mapping exists but is only written, never read during withdrawal. A positive result is a mapping incremented on deposit with no corresponding check on the withdrawal path.
  4. Fake token contracts against mint-and-burn bridges. Deploy a minimal contract implementing the expected interface and point it at a real remote token. A positive result is a successful release of a token the attacker never deposited.
  5. Token ID reuse across collections. Where a bridge keys accounting by token ID alone rather than by collection and ID, try a valid ID from one collection against another. A positive result is a release that the accounting should have blocked.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 14 min read.

What happened, in plain words

A security researcher found a serious flaw in a piece of software called a bridge. A bridge, in the blockchain world, is a program that moves digital items from one blockchain to another. Think of it like a courier service that carries your parcel from one country to another: you hand the parcel to the courier on one side, and the courier hands it to you on the other side.

In this case the items being moved are NFTs. An NFT is a unique digital token on a blockchain, often used to represent ownership of a piece of art, a collectible or a game item. Each NFT has an owner, and ownership is recorded on the blockchain, which is a public ledger that many computers keep copies of.

The bridge in question connects two blockchains: a Layer 1 (L1) chain and a Layer 2 (L2) chain. L1 is the main blockchain. L2 is a second chain built on top of it, designed to be cheaper and faster. The two chains talk to each other through a mechanism called an optimistic rollup, which bundles up activity on L2 and periodically reports it back to L1. You do not need to understand the details of the rollup to follow the bug; what matters is that the two chains can send messages to each other.

The bridge is made of two programs, called smart contracts. A smart contract is code that lives on a blockchain and runs automatically when someone calls it. One contract, L1ERC721Bridge.sol, sits on L1. The other, L2ERC721Bridge.sol, sits on L2. When a user wants to move an NFT back from L2 to L1, the L2 contract burns the L2 token and sends a message to L1 telling it to release the original NFT. When they want to move it back, the L2 contract destroys (burns) the L2 token and sends a message to L1 telling it to release the original NFT.

The bug is in that second direction, the withdrawal from L2 back to L1. The researcher showed that an attacker could trick the L1 contract into releasing an NFT that the attacker never deposited. Any NFT sitting inside the L1 bridge contract could be stolen this way.

The researcher reported the bug the same day the project was listed on Immunefi, a website where projects post bug bounties (rewards for finding security problems) and researchers report them.

Background you need

Before the details, a few ideas come up again and again.

  • Blockchain: a shared record book that many computers keep in sync. Once something is written, it is very hard to change.
  • Smart contract: a program stored on a blockchain. Anyone can call its functions, and it runs exactly as written.
  • Token: a digital item recorded on a blockchain. An ERC721 token is the standard kind of NFT: each one is unique and has an ID number.
  • Address: a long string of characters that identifies an account or a contract on the blockchain. It works like an account number.
  • Function: a named block of code inside a contract that someone can call, optionally passing in values called parameters or arguments.
  • Burn: to destroy a token so it no longer exists.
  • Mint: to create a new token.
  • Cross-domain message: a message sent from one blockchain to another. The bridge uses these to tell the other side what to do.
  • onlyFromCrossDomainAccount: a guard placed on a function that makes it refuse to run unless the caller is the authorised cross-domain account (in this case, the L2 bridge). It is like a bouncer who only lets in people on a guest list.

With those in hand, the rest of the write-up should make sense.

The setup

There is nothing to install. The researcher describes the conditions that have to be true for the bug to be exploitable:

  1. A deployed pair of bridge contracts, L1ERC721Bridge.sol and L2ERC721Bridge.sol, connected by the rollup's cross-domain messaging.
  2. At least one NFT deposited and held by the L1ERC721Bridge contract. This is the normal state whenever users have bridged tokens to L2, because the L1 contract keeps custody of the original NFT while the L2 copy exists.
  3. The ability to deploy a contract on L2, so the attacker can create their own token.
  4. The ability to call the public withdrawTo() function on L2ERC721Bridge.sol.

The L1 side only accepts the finalising call from the authorised cross-domain account, which is the L2 bridge. The attacker never needs to call L1 directly: the L2 bridge makes that call for them. That is an important point, because it means the attacker does not have to defeat the L1 guard. They just have to make the L2 bridge send a message that the L1 bridge will accept.

Finding the bug

The problem is a missing link between the two tokens named in a withdrawal. On L1, the function finalizeERC721Withdrawal takes both an _l1Token (the address of the original NFT contract on L1) and an _l2Token (the address of the matching token contract on L2), but it never checks that the two correspond to each other. It simply transfers _l1Token with the given _tokenId out of the bridge:

Solidity
function finalizeERC721Withdrawal(        address _l1Token,        address _l2Token,        address _from,        address _to,        uint256 _tokenId,        bytes calldata _data    ) external onlyFromCrossDomainAccount(l2ERC721Bridge) {                        IERC721(_l1Token).transferFrom(address(this), _to, _tokenId);                emit ERC721WithdrawalFinalized(_l1Token, _l2Token, _from, _to, _tokenId, _data);    }

Line by line:

  • function finalizeERC721Withdrawal(...) declares a function called finalizeERC721Withdrawal. Its job is to finish a withdrawal that was started on L2.
  • The parameters are the pieces of information the function needs: _l1Token (which L1 NFT contract to use), _l2Token (which L2 token was burned), _from (who started the withdrawal), _to (who should receive the NFT), _tokenId (which specific NFT), and _data (extra bytes, unused here).
  • external means the function can be called from outside the contract.
  • onlyFromCrossDomainAccount(l2ERC721Bridge) is the guard. It makes the function revert (refuse to run) unless the caller is the L2 bridge. This is the only check on the whole function.
  • IERC721(_l1Token).transferFrom(address(this), _to, _tokenId); is the actual transfer. It treats _l1Token as an ERC721 contract and asks it to move the token with ID _tokenId from address(this) (the bridge contract itself, which is holding the NFT) to _to (the recipient).
  • emit ERC721WithdrawalFinalized(...) writes a log entry recording that the withdrawal was finalised. Logs are useful for tracking but do not affect the transfer.

The only guard is onlyFromCrossDomainAccount(l2ERC721Bridge), which proves the message came from the L2 bridge but says nothing about whether the withdrawal is legitimate. In other words, the bouncer checks that the message came from the right office, but not that the request inside the message makes sense.

On L2, _initiateWithdrawal trusts whatever token address it is given. It burns the token from the caller and then asks that same token contract which L1 token it maps to:

Solidity
function _initiateWithdrawal(        address _l2Token,        address _from,        address _to,        uint256 _tokenId,        uint32 _l1Gas,        bytes calldata _data    ) internal {                      IL2StandardERC721(_l2Token).burn(msg.sender, _tokenId);                address l1Token = IL2StandardERC721(_l2Token).l1Token();        bytes memory message;        message = abi.encodeWithSelector(            IL1ERC721Bridge.finalizeERC721Withdrawal.selector,            l1Token,            _l2Token,            _from,            _to,            _tokenId,            _data        );                        sendCrossDomainMessage(l1ERC721Bridge, _l1Gas, message);                emit WithdrawalInitiated(l1Token, _l2Token, msg.sender, _to, _tokenId, _data);    }

Line by line:

  • function _initiateWithdrawal(...) internal declares an internal helper function. internal means it can only be called from inside the same contract, not directly from outside.
  • The parameters are the L2 token address, the sender, the recipient, the token ID, a gas limit for the L1 side, and extra data.
  • IL2StandardERC721(_l2Token).burn(msg.sender, _tokenId); treats _l2Token as an ERC721 token and asks it to burn the token with ID _tokenId owned by msg.sender (the person who called the function).
  • address l1Token = IL2StandardERC721(_l2Token).l1Token(); asks the same token contract which L1 token it corresponds to. This is the crucial line: the answer comes from the token contract itself, and the bridge does not check that the token contract is one it recognises.
  • bytes memory message; declares an empty byte array to hold the outgoing message.
  • message = abi.encodeWithSelector(...) builds the message. It packs the function selector (a short identifier for finalizeERC721Withdrawal) together with the arguments: the L1 token address, the L2 token address, the sender, the recipient, the token ID and the extra data. This is the standard way to encode a function call as bytes.
  • sendCrossDomainMessage(l1ERC721Bridge, _l1Gas, message); sends the encoded message to the L1 bridge contract, with a gas limit for the L1 side.
  • emit WithdrawalInitiated(...) logs that a withdrawal was started.

There is no check that _l2Token is a token the bridge actually knows about, and no check that the l1Token it reports is genuine. Because l1Token() is just a function on the supplied contract, the attacker controls its return value. That is the whole bug: the L2 side lets the attacker choose the L1 token that the L1 side will release.

Exploitation

Step 1: deploy a worthless L2 token

The attacker creates their own ERC721-style contract on L2 that implements the interface the bridge expects, including a burn function and an l1Token() function. The l1Token() function returns a valid L1 token address. The token itself has no value and no connection to the bridge.

Why this step: the bridge will call burn and l1Token() on whatever address it is given. If the attacker supplies their own contract, they control what those functions do. The burn can simply succeed without doing anything meaningful, and l1Token() can return any address the attacker likes, including the address of a real NFT collection on L1.

Step 2: call withdrawTo() with a valid token ID

The attacker calls the public withdrawal entry point on L2ERC721Bridge.sol, passing their fake token and a _tokenId that exists in the target L1 collection and is currently held by the bridge:

Solidity
function withdrawTo(        address _l2Token,        address _to,        uint256 _tokenId,        uint32 _l1Gas,        bytes calldata _data    ) external virtual {        _initiateWithdrawal(_l2Token, msg.sender, _to, _tokenId, _l1Gas, _data);    }

Line by line:

  • function withdrawTo(...) external virtual declares a public function that anyone can call. virtual means it can be overridden by a contract that inherits from this one; it does not affect the logic here.
  • The parameters are the L2 token address, the recipient, the token ID, the gas limit for L1, and extra data.
  • _initiateWithdrawal(_l2Token, msg.sender, _to, _tokenId, _l1Gas, _data); passes everything straight through to the internal helper described above, using msg.sender (the caller) as the sender.

This forwards straight into _initiateWithdrawal. The bridge calls burn on the fake token, which succeeds because the attacker wrote it, and then reads l1Token() from the fake token, which returns the real L1 token address. The bridge then encodes a finalizeERC721Withdrawal message with that L1 token, the fake L2 token, the attacker's chosen recipient and the valid token ID, and sends it across the domain.

Why this step: the attacker needs the L2 bridge to send a message to L1. The bridge will do that for any token address it is handed, so the attacker just has to hand it the fake one and a token ID that the L1 bridge is holding.

Step 3: the L1 bridge releases the NFT

The cross-domain message arrives at L1ERC721Bridge.sol and is delivered by the authorised L2 bridge account, so onlyFromCrossDomainAccount passes. finalizeERC721Withdrawal runs with the attacker-supplied _l1Token and _tokenId and executes:

Solidity
IERC721(_l1Token).transferFrom(address(this), _to, _tokenId);

Line by line:

  • IERC721(_l1Token) treats the attacker-supplied address as an ERC721 contract.
  • .transferFrom(address(this), _to, _tokenId) moves the token with ID _tokenId from the bridge contract (address(this)) to the recipient _to.

The NFT moves from the bridge contract to the attacker's address. No deposit ever happened, and the fake L2 token is irrelevant to the transfer. The attacker can steal any deposited NFT held by the bridge.

Why this step matters: this is where the theft actually happens. The L1 contract has no way to tell that the message came from a fake token, because the message was sent by the real L2 bridge, which was itself fooled by the fake token.

Root cause

The bridge treats the L2 token as the authority on which L1 token a withdrawal refers to. _initiateWithdrawal calls l1Token() on an arbitrary address and forwards the result to L1 without validating that the L2 token was ever registered by the bridge. On the L1 side, finalizeERC721Withdrawal trusts the _l1Token it receives and transfers it without checking that a matching deposit exists. The two contracts never agree on a set of legitimate token pairs, so the only thing standing between an attacker and the bridge's balance is the cross-domain authentication, which proves origin but not legitimacy.

A simple comparison: imagine a parcel office that accepts any delivery note as long as it is stamped by the right post office. The stamp proves the note came from the post office, but not that the parcel described on the note was ever handed in. Anyone who can get the post office to stamp a note can collect a parcel that was never sent.

Impact

An attacker can steal any NFT deposited in the L1ERC721Bridge contract. The only requirements are a valid _tokenId for a token the bridge holds and the ability to deploy and call a contract on L2. The fake token costs nothing to create, and the burn on it has no economic effect. Theft is limited to NFTs the bridge currently custodies; the report does not describe any effect on tokens held elsewhere or on the L2 side beyond the burn of the attacker's own token.

The fix

The author recommended tracking valid deposits explicitly. Add a mapping in L1ERC721Bridge.sol keyed by the L1 token and its corresponding L2 token, storing a count of deposits. A mapping is a lookup table inside a smart contract: you give it a key and it returns a value, like a dictionary or a spreadsheet lookup.

Increment it in depositERC721():

Solidity
deposits[_l1Token][_l2Token] = deposits[_l1Token][_l2Token] + 1;

Line by line:

  • deposits[_l1Token][_l2Token] looks up the current count for this pair of tokens.
  • = deposits[_l1Token][_l2Token] + 1 sets it to one more than it was. This runs when a user deposits an NFT, so the count records how many NFTs of this pair are currently held by the bridge.

And decrement it in finalizeERC721Withdrawal():

Solidity
deposits[_l1Token][_l2Token] = deposits[_l1Token][_l2Token] - 1;

Line by line:

  • deposits[_l1Token][_l2Token] looks up the current count for this pair.
  • = deposits[_l1Token][_l2Token] - 1 sets it to one less. This runs when a withdrawal is finalised, so the count goes down as NFTs leave the bridge.

With this in place, a withdrawal for a pair that was never deposited has a count of zero and can be rejected, so the attacker's fake token cannot unlock a real L1 NFT. The author recommended this change.

The researcher also lists patterns worth looking for in other bridges. These are general hunting ideas, not steps that were run against this project.

  1. Unvalidated token pair in other bridge directions. Look for finalise functions that accept both a local and a remote token address and transfer the local one. A positive result is a finalise function whose only guard is the cross-domain sender check, with no lookup of a registered pair.
  2. Attacker-controlled metadata functions. Search for calls such as l1Token(), underlying(), asset() or similar on a user-supplied address inside a bridge or vault. A positive result is a path where the return value is forwarded into a privileged action without checking that the address is registered.
  3. Deposit accounting that is never consulted. Check whether a deposit mapping exists but is only written, never read during withdrawal. A positive result is a mapping incremented on deposit with no corresponding check on the withdrawal path.
  4. Fake token contracts against mint-and-burn bridges. Deploy a minimal contract implementing the expected interface and point it at a real remote token. A positive result is a successful release of a token the attacker never deposited.
  5. Token ID reuse across collections. Where a bridge keys accounting by token ID alone rather than by collection and ID, try a valid ID from one collection against another. A positive result is a release that the accounting should have blocked.

Why bridges are worth auditing

The researcher closes by noting that cross-chain bridges are attractive targets. They move assets and data between networks, so a flaw in one can expose user funds. The complexity of messaging between chains adds risk on top of that. The researcher says they specialise in auditing bridges because breaking them requires more creativity than most other kinds of protocol.

Glossary

  • Address: a string of characters that identifies an account or contract on a blockchain.
  • Blockchain: a shared, append-only record book kept in sync by many computers.
  • Bridge: software that moves tokens or data between two blockchains.
  • ERC721: the standard kind of NFT token, where each token is unique and has an ID.
  • Function: a named block of code inside a contract that can be called with parameters.
  • Gas: a unit that measures the computational cost of an action on a blockchain.
  • Immunefi: a website where projects post bug bounties and researchers report security problems.
  • Layer 1 (L1): the main blockchain.
  • Layer 2 (L2): a second chain built on top of L1, usually cheaper and faster.
  • Mapping: a lookup table inside a smart contract, keyed by one or more values.
  • Mint: to create a new token.
  • NFT: a unique digital token on a blockchain, often representing ownership of an item.
  • Optimistic rollup: a kind of L2 that bundles activity and reports it back to L1, assuming transactions are valid unless challenged.
  • Parameter (argument): a value passed into a function when it is called.
  • Revert: to refuse to run and undo any changes, usually because a check failed.
  • Smart contract: a program stored on a blockchain that runs automatically when called.
  • Token ID: the number that identifies a specific NFT within a collection.
  • Withdrawal: the direction of a bridge that moves a token back from L2 to L1.

Similar reports

View all

Same bug class and techniques, clearest first.

Broken access control

The lesson behind this report.

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.