Paj Digital Labs
All articles
Web3 & Crypto Tech

Smart Contract Security Starts Before the Audit: The Checks We Run Ourselves

An audit is a snapshot, not a shield. The pre-audit checklist that catches the embarrassing bugs — reentrancy patterns, access control matrices, and the fork test that simulates mainnet before mainnet does.

Paj Digital Labs 3 min read
Hardware security key beside a connected circuit board
Editorial illustration · Web3 & Crypto Tech

The audit report arrives, the findings get fixed, the badge goes on the website — and six months later the exploit happens anyway, in code written after the audit, through an integration the audit never covered. Audits are valuable and insufficient, and treating one as a finish line is the most expensive misconception in the space.

Security is a development practice. Here is the practice we run before any contract gets near an auditor, because bugs found in-house cost an afternoon and bugs found on mainnet cost the treasury.

The access control matrix

Most exploits are not clever cryptography. They are a function that should have been onlyOwner and was not. Before anything else, write the matrix — every external function, every role, who may call what:

| Function | Owner | Operator | Anyone | | :--- | :--- | :--- | :--- | | pause() | ✓ | ✓ | — | | setFee() | ✓ | — | — | | withdraw() | ✓ | — | — | | deposit() | — | — | ✓ |

Then write a test for every cell — including the cells that should revert. The matrix is where you discover that setFee is callable by anyone because it was public during debugging and nobody flipped it back.

solidity
function test_setFee_reverts_for_stranger() public {
    vm.prank(address(0xBEEF));
    vm.expectRevert();
    vault.setFee(500);
}

Reentrancy: checks-effects-interactions, every time

The pattern is old enough to vote and still drains protocols. The rule: update your state before calling out to anything external.

solidity
class="text-code-comment">// Vulnerable: state updated after the external call
function withdraw(uint256 amount) external {
    require(balances[msg.sender] >= amount);
    (bool ok,) = msg.sender.call{value: amount}(class="text-code-string">"");
    require(ok);
    balances[msg.sender] -= amount;   // too late
}
class="text-code-comment">
// Safe: effects before interactions
function withdraw(uint256 amount) external {
    require(balances[msg.sender] >= amount);
    balances[msg.sender] -= amount;   // first
    (bool ok,) = msg.sender.call{value: amount}(class="text-code-string">"");
    require(ok);
}

The malicious receiver re-enters withdraw during the call, and in the vulnerable version the balance has not been debited yet — so it passes the check again. And again. Use the checks-effects-interactions order, add a reentrancy guard on anything moving value, and treat every external call as hostile.

Fork tests: mainnet before mainnet

Unit tests with mocked tokens prove your logic. They do not prove your assumptions about the real contracts you integrate with — the actual Uniswap pool's fee tier, the real oracle's decimals, the true behavior of the token that takes a fee on transfer.

Foundry's fork testing runs your tests against a live copy of mainnet state:

bash
forge test --fork-url $MAINNET_RPC --fork-block-number 21000000
solidity
function test_swap_against_real_pool() public {
    uint256 before = usdc.balanceOf(address(this));
    router.swapExactTokensForTokens(1000e6, 0, path, address(this), block.timestamp);
    assertGt(usdc.balanceOf(address(this)), before);
}

Fork tests have caught, in our own work: a token with 6 decimals where we assumed 18, a pool with insufficient liquidity for our test size, and an integration that worked perfectly against the mock and reverted against reality. Every one of those would have been a mainnet incident.

Static analysis is a pre-commit hook

Slither and similar tools catch the known patterns — reentrancy, unchecked returns, shadowed variables — in seconds. There is no excuse for a finding in an audit report that a free tool would have flagged.

bash
slither . --filter-paths class="text-code-string">"test|script"

Run it in CI. Treat new warnings as build failures. The auditor's time is for the novel bugs, not the ones a linter knows by name.

The mindset

Assume the contract will hold more value than you planned, be called in orders you did not expect, by contracts cleverer than yours. The audit is one layer. The matrix, the fork tests, the static analysis, and the paranoia are the others — and they are all cheaper than the alternative.

Paj Digital Labs

The engineering journal of Paj Digital Solutions — hosting, server infrastructure and web engineering, written by the people who run the servers.

Related reading