feat: implement production CSP with per-request nonces (closes #93) - #137
Merged
BarryArinze merged 2 commits intoAug 27, 2026
Merged
Conversation
…reporting Implements issue aid-linkk#93 - Content Security Policy with the following features: - Per-request nonce generation using crypto.getRandomValues() - Production CSP enforcement with strict-dynamic - Development CSP report-only mode to support HMR - Complete Stellar network endpoint allowlist (Horizon + Soroban RPC) - CSP violation reporting endpoint with rate limiting - Comprehensive test coverage Security directives implemented: - script-src: nonce-based with strict-dynamic, unsafe-eval for Stellar SDK - style-src: unsafe-inline required for Recharts and Framer Motion - connect-src: all Stellar mainnet/testnet/futurenet endpoints - frame-src, object-src: none (defense in depth) - upgrade-insecure-requests: force HTTPS Rate limiting: - 100 reports per minute per IP address - In-memory sliding window counter - Automatic cleanup to prevent memory leaks Tests: - Middleware: nonce uniqueness, CSP construction, environment handling - API route: validation, rate limiting, error handling Documentation: - Complete implementation guide in docs/CSP_IMPLEMENTATION.md - Verification checklist in CSP_VERIFICATION_GUIDE.md Closes aid-linkk#93
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implement Production CSP with Per-Request Nonces
Closes #93
Summary
This PR implements a production-grade Content Security Policy (CSP) for AidLink with per-request cryptographic nonces,
strict-dynamicdelegation, comprehensive Stellar network endpoint allowlisting, and a rate-limited violation reporting endpoint.Implementation Overview
🔒 Security Features
Per-Request Nonce Generation
crypto.getRandomValues()with 16 random bytesProduction CSP Enforcement
Content-Security-Policy)Content-Security-Policy-Report-Only)Strict Dynamic Script Loading
script-src 'nonce-{nonce}' 'strict-dynamic' 'unsafe-eval''unsafe-inline'in script-src (critical for XSS prevention)'unsafe-eval'required by Stellar SDK for RPC response parsingStellar Network Allowlisting
connect-srcdirective ensures blockchain interactions are safeCSP Violation Reporting
POST /api/csp-report📁 Files Changed
New Files
src/middleware.ts- Per-request nonce generation and CSP header injectionsrc/app/api/csp-report/route.ts- Violation reporting endpoint with rate limitingsrc/__tests__/middleware.test.ts- Comprehensive middleware testssrc/app/api/csp-report/__tests__/route.test.ts- API route testsdocs/CSP_IMPLEMENTATION.md- Complete implementation documentationCSP_VERIFICATION_GUIDE.md- Step-by-step verification checklistModified Files
src/app/layout.tsx- Thread nonce from middleware to layout via headers🎯 CSP Directives Explained
default-src'self'script-src'nonce-X' 'strict-dynamic' 'unsafe-eval'style-src'self' 'unsafe-inline'img-src'self' data: blob: https:font-src'self'connect-srcframe-src'none'object-src'none'base-uri'self'form-action'self'upgrade-insecure-requestsreport-uri/api/csp-reportStellar Endpoints in
connect-src:https://horizon.stellar.org(Mainnet Horizon)https://horizon-testnet.stellar.org(Testnet Horizon)https://horizon-futurenet.stellar.org(Futurenet Horizon)https://rpc.mainnet.stellar.org(Mainnet Soroban RPC)https://soroban-testnet.stellar.org(Testnet Soroban RPC)https://rpc-futurenet.stellar.org(Futurenet Soroban RPC)wss://horizon.stellar.org(Mainnet WebSocket)wss://horizon-testnet.stellar.org(Testnet WebSocket)wss://horizon-futurenet.stellar.org(Futurenet WebSocket)🧪 Test Coverage
Middleware Tests (7 test cases)
'unsafe-inline'in script-srcAPI Route Tests (12 test cases)
✅ Acceptance Criteria Met
All acceptance criteria from issue #93 are satisfied:
crypto.getRandomValues()(notMath.random())script-srchasnonce-and'strict-dynamic'but NOT'unsafe-inline'connect-srchas all 6 Stellar HTTP + 3 WSS endpoints'unsafe-inline'in script-src'unsafe-hashes'used🔍 Security Considerations
Why
'unsafe-eval'?The Stellar SDK uses
eval()orFunction()constructor for parsing RPC responses and dynamic code execution. Testing confirmed this is required - without it, Soroban contract interactions throwEvalError. This is documented in code comments with the specific use case.Why
'unsafe-inline'in style-src?Both Recharts (charts) and Framer Motion (animations) inject inline styles via the
styleattribute. This is safe because:This tradeoff is explicitly documented and justified.
Rate Limiting Considerations
The in-memory rate limiter is sufficient for basic DoS protection but is not distributed-safe. For production scale:
Development vs Production
Critical: Never deploy with
NODE_ENV=development. This would enable report-only mode, creating a false sense of security. The CSP would log violations but not block them, defeating the purpose.📊 Attack Surface Reduction
This CSP implementation mitigates the following attack vectors:
🚀 Deployment Notes
npm run buildto ensure production build succeedscurl -I📖 Documentation
docs/CSP_IMPLEMENTATION.md- Complete technical documentation with directive justifications, security considerations, and referencesCSP_VERIFICATION_GUIDE.md- Step-by-step testing procedures, troubleshooting, and acceptance criteria checklist🔗 References
strict-dynamic, Soroban/Horizon allowlists, and a violation reporting endpoint that does not create a false sense of security via report-only bypass #93: Implement production CSP with nonces🧑💻 Testing Instructions
Detailed verification steps in
CSP_VERIFICATION_GUIDE.md.