Skip to main content
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.

Account Validation

Always Validate Account Ownership

One of the most critical security checks is verifying that accounts are owned by the expected program:
The 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:
Bad - Using AccountInfo without verification:
Good - Using Signer type:

Use has_one Constraint

The has_one constraint verifies account relationships:
This checks that vault.authority == authority.key().

Common Security Vulnerabilities

1. Missing Signer Checks

Vulnerable:
Secure:

2. Missing Ownership Checks

Vulnerable:
An attacker could pass an account from a different program. Secure:

3. Arithmetic Overflow/Underflow

Vulnerable:
Secure:

4. Reinitialization Attacks

Vulnerable:
If an account already exists, init will fail, but without proper checks, you might allow reinitialization. Secure - Use init only once:

5. PDA Validation

Vulnerable:
Secure:

6. Account Closing Vulnerabilities

Vulnerable - Revival attacks:
Secure:

7. Duplicate Mutable Accounts

Vulnerable:
By default, Anchor prevents this. To allow it intentionally:

Security Checklist

Before deploying your program, verify:
  • All authority accounts use Signer<'info> type
  • All account relationships validated with has_one or constraint
  • All PDAs validated with seeds and bump
  • All arithmetic uses checked operations
  • All accounts use proper types (Account, Signer, etc., not raw AccountInfo)
  • Account discriminators checked (automatic with Account type)
  • 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 Clock sysvar correctly

Complete Secure Example

Here’s a secure token vault implementation:

Additional Security Resources

Auditing

Before deploying to mainnet:
  1. Self-review: Go through this security checklist
  2. Peer review: Have other developers review your code
  3. Testing: Write comprehensive tests including edge cases
  4. Professional audit: Consider hiring a security firm for critical programs
  5. 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.