Skip to content

Commit e1ec4d1

Browse files
fix(contract): forbid to call message queue and l2 messenger from l1 (#341)
Co-authored-by: HAOYUatHZ <[email protected]>
1 parent 44e27b1 commit e1ec4d1

File tree

5 files changed

+56
-1
lines changed

5 files changed

+56
-1
lines changed

contracts/src/L2/L2ScrollMessenger.sol

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,9 @@ contract L2ScrollMessenger is ScrollMessengerBase, PausableUpgradeable, IL2Scrol
262262
bytes memory _message,
263263
bytes32 _xDomainCalldataHash
264264
) internal {
265-
// @todo check `_to` address to avoid attack.
265+
// @todo check more `_to` address to avoid attack.
266+
require(_to != messageQueue, "Forbid to call message queue");
267+
require(_to != address(this), "Forbid to call self");
266268

267269
// @note This usually will never happen, just in case.
268270
require(_from != xDomainMessageSender, "Invalid message sender");

contracts/src/test/L2CustomERC20Gateway.t.sol

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ contract L2CustomERC20GatewayTest is L2GatewayTestBase {
237237
) public {
238238
// blacklist some addresses
239239
hevm.assume(recipient != address(0));
240+
hevm.assume(recipient != address(gateway));
240241

241242
gateway.updateTokenMapping(address(l2Token), address(l1Token));
242243

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
pragma solidity ^0.8.0;
4+
5+
import { DSTestPlus } from "solmate/test/utils/DSTestPlus.sol";
6+
7+
import { L1BlockContainer } from "../L2/predeploys/L1BlockContainer.sol";
8+
import { L1GasPriceOracle } from "../L2/predeploys/L1GasPriceOracle.sol";
9+
import { L2MessageQueue } from "../L2/predeploys/L2MessageQueue.sol";
10+
import { Whitelist } from "../L2/predeploys/Whitelist.sol";
11+
import { L1ScrollMessenger } from "../L1/L1ScrollMessenger.sol";
12+
import { L2ScrollMessenger } from "../L2/L2ScrollMessenger.sol";
13+
14+
contract L2ScrollMessengerTest is DSTestPlus {
15+
L1ScrollMessenger internal l1Messenger;
16+
17+
address internal feeVault;
18+
Whitelist private whitelist;
19+
20+
L2ScrollMessenger internal l2Messenger;
21+
L1BlockContainer internal l1BlockContainer;
22+
L2MessageQueue internal l2MessageQueue;
23+
L1GasPriceOracle internal l1GasOracle;
24+
25+
function setUp() public {
26+
// Deploy L1 contracts
27+
l1Messenger = new L1ScrollMessenger();
28+
29+
// Deploy L2 contracts
30+
whitelist = new Whitelist(address(this));
31+
l1BlockContainer = new L1BlockContainer(address(this));
32+
l2MessageQueue = new L2MessageQueue(address(this));
33+
l1GasOracle = new L1GasPriceOracle(address(this));
34+
l2Messenger = new L2ScrollMessenger(address(l1BlockContainer), address(l1GasOracle), address(l2MessageQueue));
35+
36+
// Initialize L2 contracts
37+
l2Messenger.initialize(address(l1Messenger), feeVault);
38+
l2MessageQueue.initialize();
39+
l2MessageQueue.updateMessenger(address(l2Messenger));
40+
l1GasOracle.updateWhitelist(address(whitelist));
41+
}
42+
43+
function testForbidCallFromL1() external {
44+
hevm.expectRevert("Forbid to call message queue");
45+
l2Messenger.relayMessage(address(this), address(l2MessageQueue), 0, 0, new bytes(0));
46+
47+
hevm.expectRevert("Forbid to call self");
48+
l2Messenger.relayMessage(address(this), address(l2Messenger), 0, 0, new bytes(0));
49+
}
50+
}

contracts/src/test/L2StandardERC20Gateway.t.sol

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ contract L2StandardERC20GatewayTest is L2GatewayTestBase {
269269
) public {
270270
// blacklist some addresses
271271
hevm.assume(recipient != address(0));
272+
hevm.assume(recipient != address(gateway));
272273

273274
amount = bound(amount, 1, l2Token.balanceOf(address(this)));
274275

contracts/src/test/L2WETHGateway.t.sol

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ contract L2WETHGatewayTest is L2GatewayTestBase {
223223
) public {
224224
// blacklist some addresses
225225
hevm.assume(recipient != address(0));
226+
hevm.assume(recipient != address(gateway));
226227

227228
amount = bound(amount, 1, l2weth.balanceOf(address(this)));
228229

0 commit comments

Comments
 (0)