SmartDocQ implements a defense-in-depth security model across its presentation layer (React SPA), business logic server (Node.js/Express), and AI processing layer (Python/Flask).
- HTTP-Only Cookie Auth: JWT session tokens are stored in strict
httpOnly,sameSite, andsecure(in production) cookies (auth_token), protecting tokens from client-side XSS extraction. - Server-Side Session Validation: Every JWT payload contains a
sessionIdverified against active server-sideUserSessionrecords in MongoDB, allowing immediate session invalidation and global revocation ("logout from all devices"). - Role-Based Access Control (RBAC): Endpoint access is enforced by middleware checks requiring explicit user roles (
user,admin,moderator). Admin routes checkreq.user.isAdmin === true. - User Enumeration Protection: Auth endpoints (login, password reset) return generic error responses (
Invalid email or password) to prevent database user existence probing.
- Session-Bound Double-Submit Token Pattern: State-changing API requests require an
X-CSRF-TokenHTTP header matching the session-boundcsrf_tokencookie. - Timing-Safe Token Comparison: CSRF token validation uses
crypto.timingSafeEqualto reduce timing-attack risk. - Origin & Referer Verification: State-changing routes verify incoming
OriginandRefererheaders against the allowed domain list. - Anti-Caching Headers: Authenticated and CSRF endpoints emit explicit anti-caching response headers (
Cache-Control: no-store,Pragma: no-cache).
- Centralized Schema Validation: API payloads are validated with centralized Zod schemas (
servers/validators/) before controller or database operations. - Rate Limiting:
- Auth endpoints (login, registration) enforce strict route-level limits (e.g., max 10 failed logins per 15 minutes).
- Public document sharing and feature APIs enforce sliding window IP/user rate limits via
express-rate-limit.
- Hardened Error Handling: Production environments return generic error messages to clients while logging detailed tracebacks server-side. Stack traces are exposed only when
FLASK_DEBUG=1. - Security Response Headers: Express uses
helmetto set standard security headers (X-Content-Type-Options: nosniff,X-Frame-Options: DENY,Strict-Transport-Securityin production).
- Isolated AI Service Access: The Python Flask AI service runs as an internal microservice and is not exposed directly to browser clients.
- Shared Service Token: All inter-service calls require the
x-service-tokenHTTP header matchingSERVICE_TOKEN. - Constant-Time Comparison: Token checks use
hmac.compare_digestin Python to reduce timing-attack risk. - Audit Context Forwarding: The Node.js gateway forwards
x-user-idto Flask for server-side logging without exposing user credentials.
- File Type & MIME Validation: Uploads are validated against permitted MIME types (
application/pdf,text/csv,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, etc.). - Size Limits: File uploads are capped at 15 MB (
MAX_UPLOAD_SIZE_MB = 15). - SHA-256 Fingerprinting: Uploaded document binaries are hashed using SHA-256 to detect duplicates, avoid redundant re-processing, and ensure index integrity.
- Path Traversal Protection: File paths are sanitized using strict basename resolution to prevent directory traversal vulnerabilities during extraction and processing.
- Automatic PII Detection: Pre-processing scanners check document content for sensitive personal data including emails, phone numbers, Aadhaar numbers, PAN identifiers, credit card numbers (Luhn validated), and SSN formats.
- User Consent Workflow: Identifies sensitive items and requires explicit user consent before document indexing or sending context to external LLM APIs.
- Prompt Injection Defenses: User questions are checked against injection and jailbreak heuristics before invoking retrieval or LLM endpoints.
- Context Sanitization: Retrieved document content is treated as untrusted input and sanitized before LLM processing (
sanitize_context).
- Versioned Indexing: Vector and BM25 chunks include metadata hashes (
file_hash,pipeline_version,chunking_version). - Atomic Shadow Indexing: Reindexing creates isolated candidate vector generations and activates them via compare-and-swap (CAS) operations, preventing partial index state corruption.
- Watchdog Recovery: Optimistic versioning and background maintenance jobs detect and recover stalled indexing tasks.
Run the automated Python security test suite:
python -m pytest tests/test_security.py -vThe test suite covers:
- Service token validation & header enforcement
- Constant-time string comparisons
- Prompt injection & jailbreak threshold scoring
- Input sanitization & HTML/script tag stripping
- Rate limiting and unauthorized access prevention
- Network Isolation: Deploy the Flask AI service on a private virtual network or bind to
127.0.0.1so that only the Node.js backend can communicate with it. - Secret Management: Never hardcode or commit
SERVICE_TOKEN,JWT_SECRET, or API keys (GEMINI_API_KEY,GROQ_API_KEY,CEREBRAS_API_KEY). Inject them via environment variables or secret managers. - HTTPS / TLS Enforcement: Terminate TLS at a reverse proxy (e.g., Nginx, Cloudflare, Vercel) and enforce HTTPS to secure
httpOnlyauth cookies and CSRF headers. - Disable Debug Mode in Production: Ensure
FLASK_DEBUG=falsein production environments to prevent sensitive stack trace leakage.