Skip to content

fix: strict balance-delta check on ERC-3009 escrow collections - #189

Merged
luiz-lvj merged 4 commits into
mainfrom
fix/erc3009-balance-delta-check
Aug 14, 2026
Merged

luiz-lvj merged 4 commits into
mainfrom
fix/erc3009-balance-delta-check

Conversation

@reednaa

@reednaa reednaa commented Jul 21, 2026 •

Copy link
Copy Markdown
Member

Problem

InputSettlerEscrow._openForWithAuthorization collects order inputs via ERC-3009 receiveWithAuthorization. The single-input fast path performed a raw token.call(...) and trusted the success flag (if (success) return;). Tokens with a permissive fallback — notably WETH9, whose fallback treats unknown calldata as a deposit — make that call "succeed" without transferring anything. An order could therefore be marked Deposited while the escrow received no funds. The multi-signature loop similarly trusted the token to revert, with no evidence tokens actually moved.

Fix

Both ERC-3009 call sites now snapshot balanceOf(address(this)) before collecting and revert with a new InvalidBalanceDelta(expectedBalance, actualBalance) error unless the balance increased by exactly the input amount (checked arithmetic, since the amount is attacker-controlled).

Note: exact-delta enforcement makes under-transferring tokens (e.g. fee-on-transfer) explicitly unsupported on the ERC-3009 path, which is the intended escrow-solvency behavior.

Tests

  • New MockERC20Fallback: a WETH-style mock whose fallback() swallows any call while balanceOf never changes.
  • test_open_for_3009_single_fallback_reverts: the previously vulnerable single-input raw-call path now reverts with InvalidBalanceDelta and the order stays unopened.
  • test_open_for_3009_two_fallback_as_array_reverts: same for the multi-signature array branch (two inputs, since a single input hits the fast path first).
  • Full suite passes (280 tests); existing 3009 happy-path tests confirm honest transfers still work. Gas snapshots updated for the affected entries (~2–4k gas from the two balanceOf calls).

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Improved ERC-3009 escrow validation to ensure token balances increase by the exact requested amounts.
    • Invalid transfers now fail safely, preserving the order’s status.
    • Added clearer error reporting for unexpected balance changes.
  • Tests

    • Added coverage for single- and multi-input transfers that produce incorrect balance increases.
    • Updated related snapshots to reflect current transaction identifiers and results.

The single-input fast path in _openForWithAuthorization performed a raw
call to receiveWithAuthorization and trusted its success flag. Tokens
with a permissive fallback (e.g. WETH9, whose fallback treats unknown
calldata as a deposit) make that call "succeed" without transferring
anything, letting an order be marked Deposited while the escrow received
no funds. The multi-signature loop similarly trusted the token to revert
without any evidence tokens moved.

Both ERC-3009 call sites now snapshot the escrow's token balance before
collecting and revert with InvalidBalanceDelta unless the balance
increased by exactly the input amount. This also rejects under-transfers
such as fee-on-transfer tokens on this path.

Adds MockERC20Fallback (a WETH-style fallback-swallowing mock) and
regression tests for both the single-input raw-call path and the
multi-signature array path.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jul 21, 2026 •

Copy link
Copy Markdown

Review Change Stack

Note

.coderabbit.yaml has unrecognized properties

CodeRabbit is using all valid settings from your configuration. Unrecognized properties (listed below) have been ignored and may indicate typos or deprecated fields that can be removed.

⚠️ Parsing warnings (1)
Validation error: Unrecognized key: "enabled"
⚙️ Configuration instructions
  • Please see the configuration documentation for more information.
  • You can also validate your configuration using the online YAML validator.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: e099cafb-85dc-4766-9393-4d40e63ca488

📥 Commits

Reviewing files that changed from the base of the PR and between 0847f41 and 94743be.

📒 Files selected for processing (4)
  • snapshots/inputSettler.json
  • snapshots/oracle.json
  • snapshots/outputSettler.json
  • src/input/escrow/InputSettlerEscrow.sol
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/input/escrow/InputSettlerEscrow.sol

📝 Walkthrough

Walkthrough

Changes

ERC-3009 balance validation

Layer / File(s) Summary
Implement exact balance validation
src/input/escrow/InputSettlerEscrow.sol
ERC-3009 single- and multi-input collection now requires the token balance to increase by the requested amount. InvalidBalanceDelta handles mismatches and overflow.
Test failed transfer handling
test/mocks/MockERC20Fallback.sol, test/input/escrow/InputSettlerEscrow.t.sol, snapshots/*.json
Fallback-token tests verify reverts and unchanged order status for single- and multi-input calls. Snapshot values are updated.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Mergeability Score: ⚪ Minimal · up to 94743

The change adds exact balance-delta validation to ERC-3009 escrow collection and covers the previously vulnerable paths with regression tests; no actionable merge-blocking risk remains beyond normal checks and review.

Sequence Diagram(s)

sequenceDiagram
  participant InputSettlerEscrow
  participant ERC3009Token
  participant OrderStatus
  InputSettlerEscrow->>ERC3009Token: Read pre-transfer balance
  InputSettlerEscrow->>ERC3009Token: Execute authorized transfer
  InputSettlerEscrow->>ERC3009Token: Read post-transfer balance
  InputSettlerEscrow->>InputSettlerEscrow: Validate exact balance increase
  InputSettlerEscrow->>OrderStatus: Continue only after validation
Loading

Possibly related PRs

Suggested reviewers: luiz-lvj

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the strict balance-delta validation added to ERC-3009 escrow collections.
Description check ✅ Passed The description clearly explains the problem, fix, unsupported token behavior, regression tests, and gas impact, but does not use the template headings.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/erc3009-balance-delta-check

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@luiz-lvj

Copy link
Copy Markdown
Collaborator

Hey @reednaa is this ready to merge?

@reednaa
reednaa marked this pull request as ready for review August 13, 2026 15:51
@codecov

codecov Bot commented Aug 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@luiz-lvj
luiz-lvj requested a review from pepebndc August 14, 2026 17:32

@reednaa reednaa left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@luiz-lvj
luiz-lvj merged commit 48b1430 into main Aug 14, 2026
5 checks passed
@luiz-lvj
luiz-lvj deleted the fix/erc3009-balance-delta-check branch August 14, 2026 17:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants