Security is paramount when building Solana programs. Anchor provides powerful tools to help you write secure code, but understanding common vulnerabilities and best practices is essential.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/solana-foundation/anchor/llms.txt
Use this file to discover all available pages before exploring further.
Account Validation
Always Validate Account Ownership
One of the most critical security checks is verifying that accounts are owned by the expected program:Account<'info, T> type automatically verifies:
account.owner == T::owner()- Account is not owned by SystemProgram with 0 lamports
- Account discriminator matches the expected type
Validate Signers
Always verify that accounts expected to authorize operations have signed:Use has_one Constraint
Thehas_one constraint verifies account relationships:
vault.authority == authority.key().
Common Security Vulnerabilities
1. Missing Signer Checks
Vulnerable:2. Missing Ownership Checks
Vulnerable:3. Arithmetic Overflow/Underflow
Vulnerable:4. Reinitialization Attacks
Vulnerable:init will fail, but without proper checks, you might allow reinitialization.
Secure - Use init only once:
5. PDA Validation
Vulnerable:6. Account Closing Vulnerabilities
Vulnerable - Revival attacks:7. Duplicate Mutable Accounts
Vulnerable:Security Checklist
Before deploying your program, verify:- All authority accounts use
Signer<'info>type - All account relationships validated with
has_oneorconstraint - All PDAs validated with
seedsandbump - All arithmetic uses checked operations
- All accounts use proper types (
Account,Signer, etc., not rawAccountInfo) - Account discriminators checked (automatic with
Accounttype) - Close constraints used instead of manual closing
- No unintentional duplicate mutable accounts
- All
/// CHECK:comments explain why validation is skipped - Token amounts and balances validated
- Time-based logic uses
Clocksysvar correctly
Complete Secure Example
Here’s a secure token vault implementation:Additional Security Resources
- Solana Security Best Practices
- Anchor Account Constraints
- Neodyme Security Blog
- Soteria Security Tool
Auditing
Before deploying to mainnet:- Self-review: Go through this security checklist
- Peer review: Have other developers review your code
- Testing: Write comprehensive tests including edge cases
- Professional audit: Consider hiring a security firm for critical programs
- Bug bounty: Run a bug bounty program for additional security
Security is an ongoing process. Stay updated on new vulnerabilities and best practices in the Solana ecosystem.