This directory contains practical examples demonstrating common security vulnerabilities in Solana smart contracts that solsec can detect. Each vulnerability type includes both vulnerable and secure implementations to illustrate the difference.
examples/
βββ integer_overflow/
β βββ vulnerable.rs # Examples that trigger integer overflow detection
β βββ secure.rs # Safe arithmetic using checked operations
βββ missing_signer_check/
β βββ vulnerable.rs # Missing authorization validations
β βββ secure.rs # Proper signer verification patterns
βββ unchecked_account/
β βββ vulnerable.rs # Unsafe account access patterns
β βββ secure.rs # Type-safe account handling
βββ reentrancy/
β βββ vulnerable.rs # State changes after external calls
β βββ secure.rs # Checks-Effects-Interactions pattern
βββ README.md # This file
Severity: Medium
What it detects: Arithmetic operations that could overflow without proper checking.
Vulnerable patterns:
- Direct arithmetic:
a + b,amount * multiplier - In-place operations:
value += increment - Unchecked calculations in price/fee computations
Secure patterns:
- Checked arithmetic:
a.checked_add(b).ok_or(ErrorCode::MathOverflow)? - Validated increments:
value.checked_add(increment) - Safe multiplication:
quantity.checked_mul(price_per_unit)
Example test:
solsec scan examples/integer_overflow/vulnerable.rs
# Should detect 4+ issues
solsec scan examples/integer_overflow/secure.rs
# Should detect 0 issuesSeverity: High
What it detects: Instruction handlers that lack proper authorization validation.
Vulnerable patterns:
- Functions with "instruction" or "handler" in name without signer validation
- Missing
Signer<'info>in account contexts - No verification of caller authority
Secure patterns:
- Required
Signer<'info>accounts - Authority validation:
require!(account.owner == signer.key()) - Admin checks:
require!(config.admin == admin.key())
Example test:
solsec scan examples/missing_signer_check/vulnerable.rs
# Should detect 4+ authorization issues
solsec scan examples/missing_signer_check/secure.rs
# Should detect 0 issuesSeverity: Critical
What it detects: Unsafe account access using AccountInfo with unchecked or unsafe operations.
Vulnerable patterns:
- Raw
AccountInfowithunsafememory operations mem::transmutefor account deserialization- Direct pointer manipulation:
*ptr = value - Unchecked account type assumptions
Secure patterns:
- Strongly typed
Account<'info, T>wrappers - Anchor's automatic validation
- Type-safe account modification
- Proper initialization constraints
Example test:
solsec scan examples/unchecked_account/vulnerable.rs
# Should detect 4+ critical unsafe operations
solsec scan examples/unchecked_account/secure.rs
# Should detect 0 issuesSeverity: High
What it detects: State changes after external program calls (invoke or invoke_signed).
Vulnerable patterns:
invoke()followed by state modificationsinvoke_signed()with subsequent variable assignments- Cross-program calls before updating balances/counters
Secure patterns:
- Checks-Effects-Interactions pattern
- State changes before external calls
- Reentrancy guards (locks)
- Emergency unlock mechanisms
Example test:
solsec scan examples/reentrancy/vulnerable.rs
# Should detect 4+ reentrancy vulnerabilities
solsec scan examples/reentrancy/secure.rs
# Should detect 0 issuesRun analysis on all vulnerability examples:
# Test all vulnerable examples (should find many issues)
solsec scan examples/*/vulnerable.rs
# Test all secure examples (should find few/no issues)
solsec scan examples/*/secure.rs
# Test the entire examples directory
solsec scan examples/
# Generate detailed HTML report
solsec scan examples/ --format html --output examples-report.html| Example File | Expected Issues | Primary Detections |
|---|---|---|
integer_overflow/vulnerable.rs |
4+ | Math operations without checked arithmetic |
integer_overflow/secure.rs |
0 | All operations use checked_* methods |
missing_signer_check/vulnerable.rs |
4+ | Functions missing signer validation |
missing_signer_check/secure.rs |
0 | Proper Signer<'info> and authorization |
unchecked_account/vulnerable.rs |
4+ | Unsafe AccountInfo with unsafe operations |
unchecked_account/secure.rs |
0 | Type-safe Account<'info, T> usage |
reentrancy/vulnerable.rs |
4+ | State changes after invoke calls |
reentrancy/secure.rs |
0 | State changes before external calls |
These examples help developers:
- Understand Common Vulnerabilities: See real-world patterns that create security risks
- Learn Secure Patterns: Compare vulnerable vs secure implementations side-by-side
- Test Security Tools: Validate that solsec correctly identifies known issues
- Practice Code Review: Train on spotting security anti-patterns
- Improve Code Quality: Apply secure coding practices in your own projects
# Verify rule detection works
for file in examples/*/vulnerable.rs; do
echo "Testing $file..."
solsec scan "$file"
done- Read the vulnerable version first
- Try to identify the security issues manually
- Run solsec to see what it detects
- Compare with the secure version
- Understand the mitigation strategies
Use these examples to ensure solsec continues working correctly as the codebase evolves.
When adding new examples:
- Create both vulnerable and secure versions
- Add clear comments explaining the security issues
- Test with solsec to ensure detection works
- Update this README with the new vulnerability type
- Include expected issue counts in your PR
-
Follow Naming Convention:
examples/new_vulnerability_type/ βββ vulnerable.rs # Contains intentional security flaws βββ secure.rs # Shows proper implementation -
Test Detection Works:
# Verify vulnerable version triggers detection solsec scan examples/new_vulnerability_type/vulnerable.rs # Verify secure version has minimal/no issues solsec scan examples/new_vulnerability_type/secure.rs
-
Document Expected Results:
- Update the "Expected Results Summary" table
- Include issue counts and primary detection types
- Add learning objectives for the new vulnerability
-
Validate Integration:
# Test comprehensive scan still works solsec scan examples/ # Generate HTML report to verify formatting solsec scan examples/ --format html --output test-report.html
- Isolated Directory: Examples live in
/examplesseparate from core code - Optional Scanning: Users choose what to scan - can scan their own projects without examples
- Regression Testing: Examples validate that security rules work correctly
- Configuration Support: Can exclude examples using patterns in config:
[rule_settings.integer_overflow]
ignore_patterns = [
"examples/*", # Skip examples if desired
"test_*",
"*_test.rs"
]| Aspect | Impact | Result |
|---|---|---|
| Core Scanner | β No impact | Works on any Rust code |
| Rule Engine | β Validates rules | Better testing coverage |
| Report Generation | β More content | Richer demonstration |
| User Experience | β Better learning | Educational value |
| CI/CD Integration | β Regression tests | Quality assurance |