Skip to content

Commit

Permalink
Merge pull request #104 from perpetual-protocol/immunefi-34300
Browse files Browse the repository at this point in the history
add invalid withdraw test
  • Loading branch information
tailingchen authored Aug 12, 2024
2 parents a875876 + 58020fc commit f0cb25b
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,12 @@ jobs:
run: npm run foundry-build-uniswap

- name: Run tests
env:
OPTIMISM_WEB3_ENDPOINT_ARCHIVE: ${{ vars.OPTIMISM_WEB3_ENDPOINT_ARCHIVE }} # For fork tests
run: npm run foundry-test

- name: Run snapshot
env:
OPTIMISM_WEB3_ENDPOINT_ARCHIVE: ${{ vars.OPTIMISM_WEB3_ENDPOINT_ARCHIVE }} # For fork tests
run: forge snapshot

14 changes: 13 additions & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,16 @@ cache_path = 'forge-cache'
no_match_path = 'contracts/test/*'
fs_permissions = [{ access = "read", path = "./out"}]

# See more config options https://github.com/foundry-rs/foundry/tree/master/config
# It's to solve vm.getCode is failed due to "No matching artifact found" error.
# https://github.com/foundry-rs/foundry/issues/7569#issuecomment-2040694197
unchecked_cheatcode_artifacts = true

# In some foundry tests are failed due to outOfGas.
gas_limit = "18446744073709551615"

# See more config options https://github.com/foundry-rs/foundry/tree/master/config

[rpc_endpoints]
# All available network keywords:
# https://github.com/foundry-rs/forge-std/blob/ff4bf7db008d096ea5a657f2c20516182252a3ed/src/StdCheats.sol#L255-L271
optimism = "${OPTIMISM_WEB3_ENDPOINT_ARCHIVE}"
2 changes: 1 addition & 1 deletion lib/forge-std
Submodule forge-std updated 64 files
+1 −0 .gitattributes
+128 −0 .github/workflows/ci.yml
+31 −0 .github/workflows/sync.yml
+0 −27 .github/workflows/tests.yml
+1 −1 .gitignore
+0 −3 .gitmodules
+1 −1 LICENSE-APACHE
+1 −1 LICENSE-MIT
+9 −5 README.md
+19 −0 foundry.toml
+0 −1 lib/ds-test
+16 −0 package.json
+635 −0 scripts/vm.py
+35 −0 src/Base.sol
+24 −41 src/Script.sol
+669 −0 src/StdAssertions.sol
+259 −0 src/StdChains.sol
+817 −0 src/StdCheats.sol
+15 −0 src/StdError.sol
+122 −0 src/StdInvariant.sol
+122 −61 src/StdJson.sol
+43 −0 src/StdMath.sol
+473 −0 src/StdStorage.sol
+333 −0 src/StdStyle.sol
+179 −0 src/StdToml.sol
+226 −0 src/StdUtils.sol
+29 −1,134 src/Test.sol
+1,865 −222 src/Vm.sol
+401 −382 src/console.sol
+1 −1,535 src/console2.sol
+105 −0 src/interfaces/IERC1155.sol
+12 −0 src/interfaces/IERC165.sol
+43 −0 src/interfaces/IERC20.sol
+190 −0 src/interfaces/IERC4626.sol
+164 −0 src/interfaces/IERC721.sol
+73 −0 src/interfaces/IMulticall3.sol
+234 −0 src/mocks/MockERC20.sol
+231 −0 src/mocks/MockERC721.sol
+13,937 −0 src/safeconsole.sol
+0 −20 src/test/Script.t.sol
+0 −602 src/test/StdAssertions.t.sol
+0 −282 src/test/StdCheats.t.sol
+0 −200 src/test/StdMath.t.sol
+0 −321 src/test/StdStorage.t.sol
+145 −0 test/StdAssertions.t.sol
+226 −0 test/StdChains.t.sol
+618 −0 test/StdCheats.t.sol
+14 −18 test/StdError.t.sol
+49 −0 test/StdJson.t.sol
+212 −0 test/StdMath.t.sol
+471 −0 test/StdStorage.t.sol
+110 −0 test/StdStyle.t.sol
+49 −0 test/StdToml.t.sol
+342 −0 test/StdUtils.t.sol
+15 −0 test/Vm.t.sol
+10 −0 test/compilation/CompilationScript.sol
+10 −0 test/compilation/CompilationScriptBase.sol
+10 −0 test/compilation/CompilationTest.sol
+10 −0 test/compilation/CompilationTestBase.sol
+0 −0 test/fixtures/broadcast.log.json
+8 −0 test/fixtures/test.json
+6 −0 test/fixtures/test.toml
+441 −0 test/mocks/MockERC20.t.sol
+721 −0 test/mocks/MockERC721.t.sol
1 change: 1 addition & 0 deletions test/foundry/base/SafeOwnable.t.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pragma solidity 0.7.6;
pragma abicoder v2;

import "../../../contracts/base/SafeOwnable.sol";
import "../interface/ISafeOwnableEvent.sol";
Expand Down
1 change: 1 addition & 0 deletions test/foundry/helper/Constant.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pragma solidity 0.7.6;
pragma abicoder v2;

import "forge-std/Test.sol";

Expand Down
37 changes: 37 additions & 0 deletions test/foundry/immunefi/34300.InvalidWithdrawal.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.7.6;
pragma abicoder v2;

import "forge-std/Test.sol";
import "../../../contracts/Vault.sol";
import "../../../contracts/test/TestERC20.sol";

contract InvalidWithdrawalTest is Test {
uint256 forkBlock = 105_302_472; // Optimiam mainnet @ Thu Jun 8 05:55:21 UTC 2023

Vault vault;
TestERC20 usdc;
TestERC20 weth;

function setUp() public {
vm.createSelectFork(vm.rpcUrl("optimism"), forkBlock);
vault = Vault(0xAD7b4C162707E0B2b5f6fdDbD3f8538A5fbA0d60);
usdc = TestERC20(vault.getSettlementToken());
weth = TestERC20(0x4200000000000000000000000000000000000006);

deal(address(usdc), address(this), 1000 * 1e6, true);
}

function test_exploit() external payable {
// Step 1: Deposit 1000 USDC into the Vault
// Assume the attacker already has 1000 USDC
usdc.approve(address(vault), 1000 * 1e6); // Approve Vault to spend USDC
vault.deposit(address(usdc), 1000 * 1e6); // Deposit 1000 USDC
assertEq(vault.getBalanceByToken(address(this), address(usdc)), 1000 * 1e6);
assertEq(vault.getBalanceByToken(address(this), address(weth)), 0);

// Step 2: Withdraw 1 wei
vm.expectRevert("V_NEFC");
vault.withdrawEther(1); // Attempt to withdraw 1 wei
}
}

0 comments on commit f0cb25b

Please sign in to comment.