mcpField
S
solidity-security

solidity-security

github.com/wshobson安全41.9k15.5k 次安装2 周前更新
代码仓库

关于 solidity-security

面向智能合约开发者的 Solidity 安全技能,涵盖漏洞预防、安全开发模式、测试与审计准备。

主要功能

  1. 01Solidity 安全最佳实践
  2. 02漏洞预防模式
  3. 03安全测试示例
  4. 04审计准备指导
  5. 05兼顾安全的 Gas 优化

适用场景

  1. 01检查合约漏洞
  2. 02构建安全的 DeFi 协议
  3. 03防范重入和访问控制问题
  4. 04为专业审计准备合约

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/wshobson/agents --skill solidity-security

安装后在 Claude Code 中输入/solidity-security确认已加载。

附带文件references/details.md

详细介绍

Solidity 安全

掌握智能合约安全最佳实践、漏洞防护以及安全的 Solidity 开发模式。

何时使用此技能

  • 编写安全的智能合约
  • 审计现有合约中的漏洞
  • 实现安全的 DeFi 协议
  • 防止重入、溢出和访问控制问题
  • 在保持安全的同时优化 gas 用量
  • 为专业审计准备合约
  • 理解常见攻击向量

详细模式与完整示例

详细的模式文档位于 references/details.md。当上述导航层级不够用时,请阅读该文件。

安全性测试

// Hardhat test example
const { expect } = require("chai");
const { ethers } = require("hardhat");

describe("Security Tests", function () {
  it("Should prevent reentrancy attack", async function () {
    const [attacker] = await ethers.getSigners();

    const VictimBank = await ethers.getContractFactory("SecureBank");
    const bank = await VictimBank.deploy();

    const Attacker = await ethers.getContractFactory("ReentrancyAttacker");
    const attackerContract = await Attacker.deploy(bank.address);

    // Deposit funds
    await bank.deposit({ value: ethers.utils.parseEther("10") });

    // Attempt reentrancy attack
    await expect(
      attackerContract.attack({ value: ethers.utils.parseEther("1") }),
    ).to.be.revertedWith("ReentrancyGuard: reentrant call");
  });

  it("Should prevent integer overflow", async function () {
    const Token = await ethers.getContractFactory("SecureToken");
    const token = await Token.deploy();

    // Attempt overflow
    await expect(token.transfer(attacker.address, ethers.constants.MaxUint256))
      .to.be.reverted;
  });

  it("Should enforce access control", async function () {
    const [owner, attacker] = await ethers.getSigners();

    const Contract = await ethers.getContractFactory("SecureContract");
    const contract = await Contract.deploy();

    // Attempt unauthorized withdrawal
    await expect(contract.connect(attacker).withdraw(100)).to.be.revertedWith(
      "Ownable: caller is not the owner",
    );
  });
});

审计准备

contract WellDocumentedContract {
    /**
     * @title Well Documented Contract
     * @dev Example of proper documentation for audits
     * @notice This contract handles user deposits and withdrawals
     */

    /// @notice Mapping of user balances
    mapping(address => uint256) public balances;

    /**
     * @dev Deposits ETH into the contract
     * @notice Anyone can deposit funds
     */
    function deposit() public payable {
        require(msg.value > 0, "Must send ETH");
        balances[msg.sender] += msg.value;
    }

    /**
     * @dev Withdraws user's balance
     * @notice Follows CEI pattern to prevent reentrancy
     * @param amount Amount to withdraw in wei
     */
    function withdraw(uint256 amount) public {
        // CHECKS
        require(amount <= balances[msg.sender], "Insufficient balance");

        // EFFECTS
        balances[msg.sender] -= amount;

        // INTERACTIONS
        (bool success, ) = msg.sender.call{value: amount}("");
        require(success, "Transfer failed");
    }
}

评论

支持 Markdown