From 7f7c7b287931f3ae43794a2954a70db775027df8 Mon Sep 17 00:00:00 2001 From: DefiAudit <117640355+DefiAudit0x@users.noreply.github.com> Date: Wed, 18 Nov 2026 16:53:39 +0100 Subject: [PATCH 1/2] Fix DOMAIN_SEPARATOR() under delegatecall Cache the verifying contract address at deployment and recompute the domain separator when address(this) differs, so permit() works through delegatecall proxies. Adds a regression test that calls permit via a delegatecall proxy. --- src/test/ERC20Delegatecall.t.sol | 88 ++++++++++++++++++++++++++++++++ src/tokens/ERC20.sol | 5 +- 2 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 src/test/ERC20Delegatecall.t.sol diff --git a/src/test/ERC20Delegatecall.t.sol b/src/test/ERC20Delegatecall.t.sol new file mode 100644 index 00000000..61214a24 --- /dev/null +++ b/src/test/ERC20Delegatecall.t.sol @@ -0,0 +1,88 @@ +// SPDX-License-Identifier: AGPL-3.0-only +pragma solidity 0.8.15; + +import {DSTestPlus} from "./utils/DSTestPlus.sol"; +import {MockERC20} from "./utils/mocks/MockERC20.sol"; + +// Minimal proxy. The implementation address lives in the EIP-1967 slot, +// not in slot 0: the token name string occupies slot 0, and a slot-0 +// implementation address would corrupt it under delegatecall. +contract ERC20DelegatecallProxy { + bytes32 internal constant IMPLEMENTATION_SLOT = + 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc; + + constructor(address _implementation) { + bytes32 slot = IMPLEMENTATION_SLOT; + assembly { + sstore(slot, _implementation) + } + } + + fallback() external payable { + bytes32 slot = IMPLEMENTATION_SLOT; + address impl; + assembly { + impl := sload(slot) + calldatacopy(0, 0, calldatasize()) + let result := delegatecall(gas(), impl, 0, calldatasize(), 0, 0) + returndatacopy(0, 0, returndatasize()) + switch result + case 0 { + revert(0, returndatasize()) + } + default { + return(0, returndatasize()) + } + } + } +} + +contract ERC20DelegatecallTest is DSTestPlus { + bytes32 constant PERMIT_TYPEHASH = + keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"); + + MockERC20 token; + ERC20DelegatecallProxy proxy; + + function setUp() public { + token = new MockERC20("Token", "TKN", 18); + proxy = new ERC20DelegatecallProxy(address(token)); + } + + function testPermitViaDelegatecall() public { + uint256 privateKey = 1; + address owner = hevm.addr(privateKey); + address spender = address(0xCAFE); + uint256 value = 1e18; + uint256 deadline = block.timestamp; + + // Under delegatecall the token reads name from the proxy storage, + // which is empty, and address(this) is the proxy. This is the + // separator the patched DOMAIN_SEPARATOR() recomputes. + bytes32 domainSeparator = keccak256( + abi.encode( + keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"), + keccak256(bytes(MockERC20(address(proxy)).name())), + keccak256("1"), + block.chainid, + address(proxy) + ) + ); + + (uint8 v, bytes32 r, bytes32 s) = hevm.sign( + privateKey, + keccak256( + abi.encodePacked( + "\x19\x01", + domainSeparator, + keccak256(abi.encode(PERMIT_TYPEHASH, owner, spender, value, 0, deadline)) + ) + ) + ); + + MockERC20(address(proxy)).permit(owner, spender, value, deadline, v, r, s); + + assertEq(MockERC20(address(proxy)).allowance(owner, spender), value); + assertEq(MockERC20(address(proxy)).nonces(owner), 1); + } +} diff --git a/src/tokens/ERC20.sol b/src/tokens/ERC20.sol index 96570446..1506d7e2 100644 --- a/src/tokens/ERC20.sol +++ b/src/tokens/ERC20.sol @@ -42,6 +42,8 @@ abstract contract ERC20 { bytes32 internal immutable INITIAL_DOMAIN_SEPARATOR; + address internal immutable INITIAL_VERIFYING_CONTRACT; + mapping(address => uint256) public nonces; /*////////////////////////////////////////////////////////////// @@ -59,6 +61,7 @@ abstract contract ERC20 { INITIAL_CHAIN_ID = block.chainid; INITIAL_DOMAIN_SEPARATOR = computeDomainSeparator(); + INITIAL_VERIFYING_CONTRACT = address(this); } /*////////////////////////////////////////////////////////////// @@ -160,7 +163,7 @@ abstract contract ERC20 { } function DOMAIN_SEPARATOR() public view virtual returns (bytes32) { - return block.chainid == INITIAL_CHAIN_ID ? INITIAL_DOMAIN_SEPARATOR : computeDomainSeparator(); + return block.chainid == INITIAL_CHAIN_ID && address(this) == INITIAL_VERIFYING_CONTRACT ? INITIAL_DOMAIN_SEPARATOR : computeDomainSeparator(); } function computeDomainSeparator() internal view virtual returns (bytes32) { From 542da26c7a601dd3c131104294b33b26f4e951b3 Mon Sep 17 00:00:00 2001 From: DefiAudit <117640355+DefiAudit0x@users.noreply.github.com> Date: Wed, 18 Nov 2026 18:53:15 +0100 Subject: [PATCH 2/2] Strengthen delegatecall regression test Add a direct DOMAIN_SEPARATOR() assertion through the proxy and a negative case (permit signed over the token\u2019s own separator must revert via the proxy). All three tests pass with the fix and fail without it. --- src/test/ERC20Delegatecall.t.sol | 58 +++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 12 deletions(-) diff --git a/src/test/ERC20Delegatecall.t.sol b/src/test/ERC20Delegatecall.t.sol index 61214a24..ff4523aa 100644 --- a/src/test/ERC20Delegatecall.t.sol +++ b/src/test/ERC20Delegatecall.t.sol @@ -49,6 +49,23 @@ contract ERC20DelegatecallTest is DSTestPlus { proxy = new ERC20DelegatecallProxy(address(token)); } + // The EIP-712 domain separator as the token computes it under + // delegatecall: name is read from the proxy storage (empty here), + // and address(this) is the proxy. This is the separator the patched + // DOMAIN_SEPARATOR() recomputes. + function proxyDomainSeparator() internal view returns (bytes32) { + return + keccak256( + abi.encode( + keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"), + keccak256(bytes(MockERC20(address(proxy)).name())), + keccak256("1"), + block.chainid, + address(proxy) + ) + ); + } + function testPermitViaDelegatecall() public { uint256 privateKey = 1; address owner = hevm.addr(privateKey); @@ -56,18 +73,7 @@ contract ERC20DelegatecallTest is DSTestPlus { uint256 value = 1e18; uint256 deadline = block.timestamp; - // Under delegatecall the token reads name from the proxy storage, - // which is empty, and address(this) is the proxy. This is the - // separator the patched DOMAIN_SEPARATOR() recomputes. - bytes32 domainSeparator = keccak256( - abi.encode( - keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"), - keccak256(bytes(MockERC20(address(proxy)).name())), - keccak256("1"), - block.chainid, - address(proxy) - ) - ); + bytes32 domainSeparator = proxyDomainSeparator(); (uint8 v, bytes32 r, bytes32 s) = hevm.sign( privateKey, @@ -85,4 +91,32 @@ contract ERC20DelegatecallTest is DSTestPlus { assertEq(MockERC20(address(proxy)).allowance(owner, spender), value); assertEq(MockERC20(address(proxy)).nonces(owner), 1); } + + function testDomainSeparatorViaDelegatecall() public { + assertEq(MockERC20(address(proxy)).DOMAIN_SEPARATOR(), proxyDomainSeparator()); + } + + function testPermitViaDelegatecallWithTokenSeparatorReverts() public { + uint256 privateKey = 1; + address owner = hevm.addr(privateKey); + address spender = address(0xCAFE); + uint256 value = 1e18; + uint256 deadline = block.timestamp; + + // Sign over the TOKEN's own (cached) separator: the signature the + // buggy code would accept through the proxy. + (uint8 v, bytes32 r, bytes32 s) = hevm.sign( + privateKey, + keccak256( + abi.encodePacked( + "\x19\x01", + token.DOMAIN_SEPARATOR(), + keccak256(abi.encode(PERMIT_TYPEHASH, owner, spender, value, 0, deadline)) + ) + ) + ); + + hevm.expectRevert(abi.encodeWithSignature("Error(string)", "INVALID_SIGNER")); + MockERC20(address(proxy)).permit(owner, spender, value, deadline, v, r, s); + } }