---
eip: 7746
label: ERC-7746
title: Composable Security Middleware Hooks
status: Last Call
type: Standards Track
category: ERC
created: 2024-07-17
official: https://eips.ethereum.org/EIPS/eip-7746
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-7746.md
canonical: https://eips.huatuo.xyz/zh/eip-7746/
lang: zh
tldr: 定义 ILayer 中间件接口，其 beforeCall/afterCall 钩子让独立、可升级的安全合约能够校验对受保护合约的调用，并在检查失败时使其回滚。
tags: [security, middleware, hooks, solidity, modular]
---

# ERC-7746: Composable Security Middleware Hooks

> An interface for composable, runtime security checks in smart contracts.

**TL;DR:** 定义 ILayer 中间件接口，其 beforeCall/afterCall 钩子让独立、可升级的安全合约能够校验对受保护合约的调用，并在检查失败时使其回滚。

## 它解决什么问题？

智能合约中的安全检查通常被写死在每个合约里：访问控制、速率限制、输入检查和不变量都与业务逻辑放在同一份代码中。这使得它们难以复用，不重新部署就难以升级，也不便于专业的安全服务商以服务形式提供。

ERC-7746 借用了 Web 服务器中的**中间件（middleware）**模式。受保护的合约在运行自己的逻辑之前和之后，把每个传入的调用交给一个或多个外部**层（layer）**。任何一层都可以通过回滚来否决该调用。

## 它如何运作

一个层实现两个函数：

- `beforeCall(configuration, selector, sender, value, data)` 在受保护函数之前运行。它返回任意字节，并传给 after 钩子；如果校验失败，必须回滚。
- `afterCall(configuration, selector, sender, value, data, beforeCallResult)` 在受保护函数之后运行，可以检查执行结果，失败时同样回滚。

`configuration` 是该层专属的数据，因此同一个已部署的层可以用不同的设置服务许多合约。层通过普通（非 static）调用来调用，所以可以保存状态，例如为速率限制统计每个区块的调用次数。

受保护的合约可以注册多个层，并按既定顺序运行。只要有任何一层回滚，整个调用就必须回滚。参考实现展示了一个速率限制层，它阻止了一个模拟的掏空者合约把有漏洞的受保护合约掏空。

## 示例代码

```solidity
// 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
    }
}
```

## 需要注意

- **状态：Last Call。**尚未达到 Final。
- 规范正文有时把钩子称为 `beforeCallValidation` / `afterCallValidation`，把配置称为 `layerConfig`，而接口中使用的是 `beforeCall` / `afterCall` 和 `configuration`。请以接口为准。
- 层是被完全信任的：恶意或有缺陷的层可能阻断每一次调用或泄露信息。请像审计其他依赖一样审计各个层，并决定谁可以更改所使用的层。
- 每增加一层，每次调用受保护函数都会增加 gas 和外部调用；有状态的层本身也可能成为重入或拒绝服务的攻击面。
- 类似的钩子思路也出现在 [ERC-7579](/eip-7579/) 和 [ERC-6900](/eip-6900/) 等模块化智能账户标准中，不过它们针对的是账户，而不是任意合约。


---

This is an unofficial plain-language summary. Read the full specification: https://eips.ethereum.org/EIPS/eip-7746
