EIPs Explained

ERC-7746 · Last Call · ERC · 2024-07-17

ERC-7746: Composable Security Middleware Hooks

Defines ILayer, a middleware interface whose beforeCall/afterCall hooks let separate, upgradable security contracts validate calls to a protected contract and revert them if checks fail.

What problem does it solve?

Security checks in smart contracts are usually baked into each contract: access control, rate limits, input checks and invariants all live in the same code as the business logic. That makes them hard to reuse, hard to upgrade without redeploying, and hard for specialised security providers to offer as a service.

ERC-7746 borrows the middleware pattern from web servers. A protected contract hands each incoming call to one or more external layers before and after running its own logic. Any layer can veto the call by reverting.

How it works

A layer implements two functions:

  • beforeCall(configuration, selector, sender, value, data) runs before the protected function. It returns arbitrary bytes that are passed on to the after-hook, and must revert if validation fails.
  • afterCall(configuration, selector, sender, value, data, beforeCallResult) runs after the protected function and can check the outcome, again reverting on failure.

configuration is layer-specific data, so one deployed layer can serve many contracts with different settings. Layers are called with normal (non-static) calls, so they can keep state, for example counting calls per block for rate limiting.

A protected contract may register several layers and run them in a defined order. If any layer reverts, the whole call must revert. The reference implementation shows a rate-limiting layer that stops a simulated drainer contract from emptying a buggy protected contract.

Sample code

// SPDX-License-Identifier: CC0-1.0
pragma solidity ^0.8.20;

interface ILayer {
    function beforeCall(
        bytes memory configuration,
        bytes4 selector,
        address sender,
        uint256 value,
        bytes memory data
    ) external returns (bytes memory);

    function afterCall(
        bytes memory configuration,
        bytes4 selector,
        address sender,
        uint256 value,
        bytes memory data,
        bytes memory beforeCallResult
    ) external;
}

abstract contract LayerProtected {
    ILayer public immutable layer;
    bytes internal layerConfig;

    constructor(ILayer _layer, bytes memory _config) {
        layer = _layer;
        layerConfig = _config;
    }

    modifier layered() {
        bytes memory ctx = layer.beforeCall(layerConfig, msg.sig, msg.sender, msg.value, msg.data);
        _;
        layer.afterCall(layerConfig, msg.sig, msg.sender, msg.value, msg.data, ctx);
    }
}

contract Vault is LayerProtected {
    constructor(ILayer l, bytes memory cfg) LayerProtected(l, cfg) {}

    function withdraw(uint256 amount) external layered {
        // business logic; reverts if the layer rejects the call
    }
}

Things to know

  • Status: Last Call. It has not reached Final.
  • The spec's prose sometimes calls the hooks beforeCallValidation / afterCallValidation and the config layerConfig, while the interface uses beforeCall / afterCall and configuration. Follow the interface.
  • A layer is fully trusted: a malicious or buggy layer can block every call or leak information. Audit layers like any other dependency, and decide who can change which layer is used.
  • Every layer adds gas and external calls on each protected function, and a stateful layer can itself be a reentrancy or denial-of-service vector.
  • Similar hook ideas appear in modular smart-account standards such as ERC-7579 and ERC-6900, which target accounts rather than arbitrary contracts.

#security #middleware #hooks #solidity #modular

Details

Authors
Tim Pechersky (@peersky)
Status
Last Call
Type
Standards Track / ERC
Created
2024-07-17

Machine-readable: Markdown · JSON