Overview
iStory implements a layered security model addressing 34 audit findings across API routes, smart contracts, and infrastructure. The architecture follows defense-in-depth principles with multiple security layers.The security architecture was hardened in Phase 1.5 (January 2026) based on a comprehensive security audit. Current risk score: B+ (production-ready with ongoing hardening).
Security Layers
Authentication System
Dual Authentication
iStory supports two authentication methods:Wallet Authentication
Web3 wallet signatures with nonce-based replay prevention
OAuth Authentication
Google OAuth via Supabase with account linking
Wallet Authentication Flow
Nonce Storage (In-Memory)
Nonce Storage (In-Memory)
app/api/auth/nonce/route.ts
- Nonces are UUIDs (128-bit entropy)
- 5-minute expiry prevents replay attacks
- One-time use (deleted after verification)
- Cleaned up periodically to prevent memory leaks
OAuth Authentication Flow
Account Linking
Users can link their Google account and wallet for unified access.Account Linking Flow
Account Linking Flow
Step 1: Initiate Link (requires authentication)Step 2: Verify Link (requires wallet signature + linking token)Security Properties:
app/api/auth/initiate-link/route.ts
app/api/auth/link-account/route.ts
- HMAC-signed tokens prove wallet ownership
- Timing-safe token comparison prevents timing attacks
- Both accounts must be authenticated before linking
Authorization & Access Control
API Route Protection
All API routes use shared authentication middleware:Authentication Helpers
lib/auth.ts API Reference
lib/auth.ts API Reference
Rate Limiting
Route-Specific Limits
Rate limiting uses an in-memory store (Map) with automatic cleanup. For production scale, consider Redis with Upstash.
Rate Limit Response
Input Validation
File Upload Validation
Audio Upload (Transcription)
Audio Upload (Transcription)
app/api/ai/transcribe/route.ts
IPFS Upload
IPFS Upload
app/api/ipfs/upload/route.ts
Text Input Validation
Security Headers
Next.js Configuration
Security Headers Explained
Security Headers Explained
| Header | Value | Purpose |
|---|---|---|
| X-Frame-Options | DENY | Prevents clickjacking attacks |
| X-Content-Type-Options | nosniff | Prevents MIME type sniffing |
| Referrer-Policy | strict-origin-when-cross-origin | Controls referrer information leakage |
| X-XSS-Protection | 1; mode=block | Enables browser XSS filter |
| Permissions-Policy | camera=(), microphone=(self) | Restricts browser features |
| Content-Security-Policy | Multiple directives | Prevents XSS, data injection attacks |
Database Security
Row Level Security (RLS)
iStory uses Supabase RLS policies for defense-in-depth protection:Smart Contract Security
OpenZeppelin Libraries
All smart contracts use audited OpenZeppelin libraries:Security Features by Contract
EStoryToken
- MAX_SUPPLY cap (100M)
- Pausable mechanism
- Role-based minting
- Burn functionality
StoryProtocol
- SafeERC20 transfers
- ReentrancyGuard
- Pausable mechanism
- AccessControl roles
StoryNFT
- Mint fee (0.001 ETH)
- ERC2981 royalties
- AccessControl roles
- Secure withdrawal
Contract Roles
Contract Roles
Local Vault Encryption
iStory includes a client-side encryption vault for private stories:Encryption Architecture
Security Properties:
- PIN never stored or transmitted
- DEK never leaves browser memory
- AES-256-GCM provides authenticated encryption
- PBKDF2 with 100K iterations prevents brute force
Error Handling
Secure Error Responses
Secrets Management
Environment Variables
Production Secrets (Vercel)
Production Secrets (Vercel)
Best Practices:Use for CRON_SECRET and API key verification to prevent timing attacks.
- Use Vercel environment variables with scoped access
- Never commit
.env.localto git (.gitignore) - Rotate secrets regularly
- Use KMS or hardware wallets for private keys
- Separate secrets per environment (dev, staging, prod)
lib/crypto.ts
Security Audit Summary
Audit Date: January 30, 2026
Overall Risk Score: B+ (production-ready)Findings Addressed:
Overall Risk Score: B+ (production-ready)Findings Addressed:
- ✅ 11 Critical findings (authentication, nonce replay, admin client overuse)
- ✅ 8 High findings (rate limiting, input validation, RLS policies)
- ✅ 9 Medium findings (CSRF protection, error disclosure, security headers)
- ✅ 6 Low findings (timing-safe comparison, gas limits)
Key Improvements
Key Improvements
- Authentication: Nonce-based wallet signatures prevent replay attacks
- Rate Limiting: Route-specific limits protect against abuse
- Input Validation: File size, MIME type, text length checks
- RLS Policies: Defense-in-depth for database access
- Security Headers: CSP, X-Frame-Options, X-Content-Type-Options
- Error Sanitization: Generic error messages prevent information leakage
- Timing-Safe Comparison: Prevent timing attacks on secrets
- Admin Client Minimization: Explicit authorization checks before operations
What’s Next?
Database Schema
Explore PostgreSQL schema and RLS policies
CRE Integration
Learn about verifiable AI compute