- 🎯 Introduction
- 🛡️ General Security Guidelines
- 🔒 Solidity-Specific Practices
- 👥 User Interaction and Front-End
- 🔗 Conclusion & Further Reading
- 🙏 Want to Contribute?
Welcome to the Security Best Practices guide! This resource aims to provide developers and security analysts with a comprehensive set of guidelines to ensure the highest level of security for smart contracts.
- Importance: High
- Overview: Always have your code audited by third-party experts.
- Tip: Use automated tools for initial checks and then proceed with manual audits.
- Importance: Medium
- Overview: Maintain a version control system.
- Tip: Use Git and regularly commit changes to have a history of codebase modifications.
- Importance: High
- Overview: Limit permissions to the bare minimum required for each function.
- Tip: Use Solidity's
internalandprivatevisibility specifiers wisely.
- Importance: High
- Overview: Use multi-signature wallets for admin controls.
- Tip: Implement a multi-signature scheme for critical contract functions.
- Importance: Medium
- Overview: Implement time-locks for sensitive operations.
- Tip: Use a delay mechanism for administrative changes.
- Importance: Medium
- Overview: Implement rate limiting to prevent abuse.
- Tip: Use a mapping to track user interactions within a given time frame.
- Importance: High
- Overview: Always validate external data.
- Tip: Use oracles for fetching external data and validate it before use.
- Importance: High
- Overview: Implement robust error handling.
- Tip: Use
require,assert, andrevertstatements for error handling in Solidity.
- Importance: High
- Overview: Keep detailed logs and set up real-time monitoring.
- Tip: Use Solidity events for logging important contract activities.
- Importance: High
- Overview: Encrypt sensitive data before storing it.
- Tip: Use secure key management systems for encryption keys.
- Importance: Medium
- Overview: Use ACLs to specify permissions.
- Tip: Implement role-based access control.
- Importance: Medium
- Overview: Keep all dependencies and libraries up to date.
- Tip: Regularly check for updates and security patches for third-party code.
- Importance: High
- Overview: Aim for high test coverage.
- Tip: Use unit tests, integration tests, and end-to-end tests to cover different aspects.
- Importance: High
- Overview: Use secure and encrypted channels for communication.
- Tip: Use HTTPS for off-chain interactions.
- Importance: High
- Overview: Have a backup and recovery mechanism in place.
- Tip: Regularly backup contract states and have a recovery procedure.
- Importance: Medium
- Overview: Document all functions, especially those that handle funds or sensitive information.
- Tip: Use NatSpec comments in your Solidity code for better documentation.
- Importance: High
- Overview: Use libraries like OpenZeppelin's SafeMath to prevent overflows and underflows.
- Tip: Always use SafeMath functions like
add,sub,mul, anddiv.
- Importance: High
- Overview: Prevent reentrancy attacks by using mutex or the
checks-effects-interactionspattern. - Tip: OpenZeppelin's
ReentrancyGuardcan be easily integrated into your contracts.
- Importance: Medium
- Overview: Optimize contract code to minimize gas usage.
- Tip: Use
viewandpurefunctions where possible to reduce gas costs.
- Importance: Medium
- Overview: Use events to log important contract activities.
- Tip: Events are cheaper than storage and can be easily accessed off-chain.
- Importance: High
- Overview: Always set the appropriate function visibility (
public,external,internal,private). - Tip: Use
externaloverpublicwhere possible for slight gas optimization.
- Importance: High
- Overview: Use
require,assert, andrevertfor error handling. - Tip:
requireis generally more versatile and should be preferred overassert.
- Importance: Medium
- Overview: Be cautious when using
block.timestamp. - Tip: Miners can manipulate timestamps; don't rely on it for critical logic.
- Importance: High
- Overview: Use
msg.senderinstead oftx.originto represent the sender of the transaction. - Tip:
tx.origincan lead to security vulnerabilities.
- Importance: Medium
- Overview: Avoid unbounded loops that could hit the gas limit.
- Tip: Use pagination or limit the number of iterations.
- Importance: Low
- Overview: Explicitly initialize state variables.
- Tip: Solidity initializes variables to zero, but being explicit improves readability.
- Importance: Medium
- Overview: Use modifiers for reusable checks.
- Tip: Modifiers like
onlyOwnercan be reused across different functions.
- Importance: Medium
- Overview: Design contracts to be upgradeable if needed.
- Tip: Use delegate calls or create upgrade paths via contract registries.
- Importance: High
- Overview: Always validate external data, especially if using
callordelegatecall. - Tip: Never trust external contract calls; they can be compromised.
- Importance: High
- Overview: Be cautious when sending Ether.
- Tip: Use the
transferfunction for small amounts andsendfor larger amounts with error handling.
- Importance: High
- Overview: The
selfdestructfunction can introduce vulnerabilities. - Tip: If you must use it, ensure only the contract owner can call it and notify users well in advance.
- Importance: High
- Overview: Always validate user inputs in both the smart contract and the front-end.
- Tip: Use JavaScript libraries like
validatorfor front-end validation and Solidity modifiers for smart contract validation.
- Importance: High
- Overview: Provide clear error messages and handle failures gracefully.
- Tip: Use
try...catchin JavaScript andrequire,assert, andrevertstatements in Solidity for error handling.
- Importance: Medium
- Overview: Keep the user informed about the transaction status.
- Tip: Use front-end frameworks like
toastrorNotyfor non-blocking notifications.
- Importance: High
- Overview: Implement 2FA for sensitive operations.
- Tip: Use libraries like
AuthyorGoogle Authenticatorfor 2FA.
- Importance: Medium
- Overview: Implement rate limiting on your APIs and user actions to protect against abuse.
- Tip: Use libraries like
express-rate-limitfor API rate limiting.
- Importance: High
- Overview: Encrypt sensitive data before storing it.
- Tip: Use libraries like
crypto-jsfor front-end encryption.
- Importance: Medium
- Overview: Manage user sessions securely.
- Tip: Use secure, random session identifiers and implement session timeout.
- Importance: High
- Overview: Use HTTPS for secure communication.
- Tip: Implement SSL/TLS and consider using libraries like
helmetfor additional security headers.
- Importance: High
- Overview: Protect against XSS attacks.
- Tip: Use Content Security Policy (CSP) headers and sanitize user inputs.
- Importance: High
- Overview: Protect against CSRF attacks.
- Tip: Use anti-CSRF tokens and make sure to validate them server-side.
- Importance: Medium
- Overview: Log security-relevant events and set up real-time monitoring.
- Tip: Use logging services like
LogglyorSentryfor real-time monitoring.
- Importance: Medium
- Overview: Regularly backup user data and the database.
- Tip: Use automated backup solutions and ensure encryption for data at rest.
- Importance: Low
- Overview: Educate users on how to interact securely with the application.
- Tip: Provide tooltips, FAQs, and guides within the application.
Security is not a one-time task but an ongoing process. Following these best practices can significantly reduce the risk of vulnerabilities in your smart contracts.
If you have additional best practices or tips that you think should be included, feel free to open a pull request or reach out to us.