feat(security): strictly enforce Cloudflare Turnstile verification and block direct backend access

This commit is contained in:
2026-11-17 11:54:51 +05:30
parent 1bb6ffc44e
commit 27128f7a5c
9 changed files with 263 additions and 85 deletions
+39 -2
View File
@@ -151,5 +151,42 @@ npm run start
### Q: What if a user has JavaScript disabled or an aggressive ad-blocker?
**A:** Cloudflare Turnstile is served from `challenges.cloudflare.com`, which is whitelisted on standard ad-blockers as a privacy-respecting CAPTCHA alternative. If a network error prevents loading, `/api/try/[toolId]` returns a friendly `retryable: true` message instructing the user to refresh.
### Q: Can Turnstile tokens be reused?
**A:** No. Cloudflare Turnstile tokens are single-use and expire within 300 seconds. After each tool execution, the React widget automatically resets to obtain a fresh token for subsequent runs.
---
## 8. Direct Backend Server Protection Architecture
To ensure **no one can send requests to the actual backend server on its own**, the system employs a two-tier zero-trust defense:
```
[ External Client / Bot / Scraper ]
│
│ (A) Attempt direct curl/script to /api/try/* without Turnstile token
▼
┌────────────────────────────────────────────────────────┐
│ Next.js Proxy Server (/api/try/[toolId]) │
│ 1. Validate 'cf-turnstile-response' / 'x-turnstile' │
│ 2. Missing/Invalid Token -> HTTP 403 FORBIDDEN │
│ Zero network traffic dispatched to backend │
└────────────────────────────────────────────────────────┘
│
│ (B) Valid Turnstile Token Verified with Cloudflare API
▼
┌────────────────────────────────────────────────────────┐
│ Next.js Server-Side Request Builder │
│ - Injects internal API_SECRET_DEFAULT (e.g. 'abcdef') │
│ - Backend URLs & secrets NEVER reach the browser │
└────────────────────────────────────────────────────────┘
│
│ (C) Proxied Server-to-Server Request with Secret
▼
┌────────────────────────────────────────────────────────┐
│ Actual FastAPI Backend (GPU Inference Servers) │
│ - Validates internal secretKey in middleware │
│ - Direct requests without secret -> HTTP 401 │
└────────────────────────────────────────────────────────┘
```
1. **Gatekeeper Enforcement**: All client requests target `/api/try/[toolId]`. If the incoming request lacks a valid Cloudflare Turnstile token, Next.js rejects it immediately with `403 Forbidden` (`BOT_VERIFICATION_FAILED`). No network packets are sent to the actual backend.
2. **Zero Client Secret Leakage**: The backend's real URLs (`https://apigp0006.creinnovations.in` or local FastAPI ports) and the backend authentication secret (`API_SECRET_DEFAULT`) exist exclusively in server-side environment variables. They are never exported into client JavaScript bundles.
3. **Backend-Side Defense**: The actual backend server enforces `FirebaseAppCheckMiddleware` / `secretKey` verification on all incoming requests. Any direct request sent to the backend server bypassing the Next.js proxy will lack the secret key and receive `401 Unauthorized` (`The provided secret key is invalid.`).