Original title: "a16z: A Smart Contract Security Guide for Web3 Projects"
Original author: Andy Beal, Nassim Eddequouaq, Riyaz Faizullabhoy & Christian Seifert, a16zcrypto
Original compilation: Katie Koo, Odaily Planet Daily
Usually, hackers will find and exploit flaws in the entire process chain of software development (from design to deployment to maintenance) to break the security of blockchain projects barrier. If the relevant experience can be learned in advance, I believe that there will be much fewer safety accidents.
This article outlines the security elements that Web3 developers and security teams must consider when designing, developing and maintaining smart contracts, covering from threat modeling to emergency response The entire cycle of preparation.
Developing a secure software consists of the following five phases:
- Design: Development The person describes the required functionality and operation of the system, including important baselines and fixed properties;
- Development: The developer writes the system code;
- Testing and review: The developer puts all Modules are assembled in a test environment and evaluated for correctness, size, and other factors;
- Deployment: Developers put the system into production;
- Maintenance: Developers evaluate and modify the system to ensure it performs as intended.
The diagram below maps the security factors that need to be considered to the above stages.

It is important to note that the software development life cycle does not always follow a linear path: categories may overlap or extend into other phases. Steps may be repeated for each release. Some security tasks, such as testing and security reviews, may need to be followed through.
The software lifecycle steps and corresponding security considerations described above provide the basis for facilitating smart contract security. In the following, we will conduct more detailed research on three issues.
While it is very tempting for developers to focus only on the intended use of a smart contract or protocol, this sole focus may leave them "Blind spots" can be exploited by attackers.
Method: Follow known threat modeling practices. If a development team does not have security experts in-house, it should engage security consultants early in the design phase. Design systems with an "attacker" mentality and presuppose that any person, machine or service can be attacked.
Content: Implement access controls that limit the ability to privileged accounts and smart contract calls to special functions that perform administrative tasks such as upgrading contracts and setting special parameters. Follow the "principle of least privilege" - each participant should only have the minimum amount of access needed.
Reason: Maintaining the protocol through the upgrade and governance process allows developers to improve the protocol by adding new features, patching security issues, and addressing changing conditions. This could constitute a serious security breach if upgradeability is not properly controlled.
Approach: Create a multi-signature wallet or DAO contract to transparently manage changes on behalf of the community. Changes should go through a thorough review process and have a time-lock (deliberately delaying the enactment of regulations with the ability to cancel them) to ensure their correctness can be verified and rolled-back in the event of a governance attack. Ensure that privileged keys are securely stored and accessible in a self-custodial wallet or safekeeping service.
Content: Leverage existing smart contract standards (such as OpenZeppelin contracts) where possible, and evaluate security assumptions that may require protocol integration with existing protocols.
Why: Using existing battle-tested, community-audited standards and implementing measures to mitigate security risks can be of great help. Assessing the risks of protocol integration helps to develop security checks to prevent attacks on external components such as oracle manipulation.
Method: Import trusted contract libraries and interfaces that have passed security audits. The focus of Web3 is on open source usage, reusability and composability. Ensure contract dependencies and their versions are documented in the codebase to minimize code footprint. For example, importing specific submodules of a large project, rather than importing everything. Know your risk exposure and monitor supply chain attacks. Use the official interface to call external protocols, and make sure to consider potential integration risks. Monitor updates and security disclosures for reused contracts.
What: Create clear, comprehensive code documentation and build a test suite that's fast, thorough, and easy to run. Where possible, establish a test environment on the test network or simulate the main network to conduct more in-depth experiments.
Why: Writing out assumptions about the expected behavior of the codebase helps ensure that risks in the threat model are addressed and understandable to the development team by users and external auditors intention of. Creating test suites for code helps to prove (or disprove) development assumptions and encourages deeper thinking about threat models. This test suite should include mechanism design tests that check the project's token economy in extreme market scenarios, as well as unit tests and integration tests.
Approach: Implement known testing frameworks and security checkers like Hardhat, Mythril, Slither, Truffle, etc. which provide different testing techniques like fuzzing , property checking, and even formal verification. Use NatSpec comments to extensively document your code, specifying expected side effects, parameters, and return values. Generate real-time documentation using documentation generation tools as well as high-level design instructions.
Content: Take the time to pass internal and external code inspections to find vulnerabilities.
Reason: Shifting from feature development to focusing on security issues gives developers time to spot potentially obscure issues. External audits are especially useful here, as they can bring outside perspective and expertise that the development team doesn't have.
Approach: At an appropriate point in project development, freeze a feature to allow time for an internal review followed by an external audit. This should be done before any actual deployments and upgrades.
Check out the guidelines from ConsenSys, Nassent, OpenZeppelin, and Trail of Bits, which provide developers with a checklist of considerations, including timing, for any A reference for those preparing for an audit. Also make sure to check deployment transactions to make sure they use audited code versions and have appropriate parameters, especially when upgrading software.
Why: Development teams can benefit from a wide range of knowledge and experience (this is also the role of open source in the field of encryption). Such a program can help spark enthusiasm for a project, essentially turning the community and white hat hackers into evangelists. They can also help turn would-be attackers into secure assets by giving hackers a way to become defenders.
Method: Use a bug bounty platform such as Code4rena, HackenProof, mmunef, or Secureum to incentivize skilled hackers to securely disclose vulnerabilities.
Note: Some of the authors in this article work for Forta, a company that owns a network that provides a network for decentralized creation of high-quality security surveillance robots Token Incentive structure. Development teams can encourage their protocol communities to leverage both traditional and Web3-native approaches to incentivize bug bounties and potentially benefit participants through enhanced security, a win-win situation.
Content: Implement monitoring smart contracts and key operating components (such as oracles and cross-chain bridges), and report suspicious activities to the development team and community based on known threat models.
Why: Early detection of issues enables teams to respond quickly to breaches, potentially preventing or mitigating any damage.
Method: Use a monitoring platform or a distributed node to run a robot to monitor smart contract events in real time. Plug in data dashboards and alert notifications for development teams and the wider community as needed.
Content: Tools and processes to respond immediately to any security issues.
Reason: Even with the best pre-deployment safeguards, real-time issues with smart contracts and key components such as oracles and cross-chain bridges are still possible. Dedicated personnel, clear processes, and appropriate automation ensure incidents can be investigated quickly and resolved as quickly as possible.
Approach: Prepare for worst-case scenarios, plan how to respond to incidents or emergencies, and automate response capabilities to the greatest extent possible. This includes assigning responsibility for investigation and response, who can be contacted publicly about security concerns via distributed security mailing lists, instructions in code repositories, or smart contract registries. Based on the agreement's threat model, develop a set of processes that can include scenario rehearsals and expected response times needed to take urgent action. Integration of automation into emergency incident response can be considered.
Security considerations should be an integral part of successful development, not just an afterthought or addition. While this framework shares some quick guidelines for building Web3 protocols and applications that facilitate security throughout the development process, there isn't any short overview that can discuss smart contract security in its entirety. Teams that lack in-house security expertise should contact qualified Web3 security experts who can guide and help apply to their specific situation.
Remember, security is not an easy problem. Security will always be a never-ending, ongoing set of best practices. We are still in the early stages of establishing these practices, and now is the time to create and share them collaboratively for all developers.
Original link blockquote>
Welcome to join the official BlockBeats community:
Telegram Subscription Group: https://t.me/theblockbeats
Telegram Discussion Group: https://t.me/BlockBeats_App
Official Twitter Account: https://twitter.com/BlockBeatsAsia
