Backend Hardening
Audits backend APIs for security, IDOR/RBAC, mass assignment, data leaks, injection, uploads, rate limits, and DB indexes.
- Do NOT use for
- frontend UI, CSS, or scaffolding new features.
- Compatible agents
- Claude CodeCursorCodexGemini CLIOpenCode
- Allowed tools
- read_filerun_commandedit_filewrite_fileask_question
On-demand references (3)
Audits backend codebases for critical security vulnerabilities, broken authorization, mass assignment, data leaks, and concurrency bugs with zero fluff and strict token economy.
1. Targeted Backend Discovery & Strict File Allowlist
Locate backend directories by checking manifests (package.json, go.mod, pyproject.toml, Cargo.toml) 1–2 levels deep.
- Stop Gate: If no backend manifests or server entrypoints exist, stop immediately: “No backend codebase detected.”
- Token-Safe Inspection Rules (Enforce Strictly):
- Always
globandgrepbefore reading files. - Allowed reads: Bootstrap entrypoints (
main.ts,app.ts,index.ts), auth & RBAC middleware/guards, route/controller files, DTOs/validators, DB schemas/models, upload utils, global error handlers, and.env.example. - Forbidden reads: Frontend folders (
client/,web/,frontend/), UI/CSS/assets, test suites, seed scripts, docs, and lockfiles.
- Always
2. Interactive Grill (Native Picker First, 1 Compact Batch)
Print a 2-line detection summary:
Backend: <path> (<framework>) | Data/Storage: <DB/Redis/Uploads detected>
Ready to harden backend.
Interactive Tool First: Call runtime question tool (ask_question in Antigravity, question in OpenCode, AskUserQuestion in Claude Code, request_user_input in Codex; plain text fallback ONLY if unavailable).
Ask ONLY relevant atomic questions (<= 16 char headers, 2–3 options, recommended first):
Backend dir(ask ONLY if multiple backends exist or non-standard path)Audit Mode:Audit + Safe Fixes (Recommended)— Report all findings; auto-apply low-risk fixes (security headers, body limits, safe projections, missing indexes, validation flags) and leave breaking/auth-design changes for approval.Report Only— Read-only audit; edit zero files.
Target Env:Public / Production (Recommended)— Full strictness (rate limits, headers, cookie flags, error redaction).Internal / Demo / Hackathon— Prioritize critical/high exploits (IDOR, mass assignment, leaked secrets/hashes, broken auth) without noisy ops overhead.
Audit Scope:Full Backend (Recommended)— Audit all backend routes/models/auth.Changed Files Only— Restrict strictly togit diffbackend files.Specific Module— Audit one resource/feature the user names.
Wait for answers before loading adapters or inspecting source files.
3. Lazy-Load Adapter Rule (Zero-Adapter Fallback)
- Load ONLY 1 matching framework adapter (
references/express.mdORreferences/nestjs.md) +references/data-layer.md(ONLY if DB or Redis is detected). NEVER load unused adapters. - Zero-Adapter Fallback: For unlisted stacks (Go, FastAPI, Rust), apply the 8-point checklist directly against their routers, handlers, middleware, and queries.
4. The 8-Point Backend Hardening Checklist
Verify issues directly in actual files. Report ONLY verified bugs with exact file:line locations (never generic textbook advice):
- Auth & Secret Hygiene: Hardcoded fallback secrets (e.g.
process.env.JWT_SECRET || "secret"), missing boot-time env validation, missing JWT expiry or algorithm pinning, weak cookie flags (httpOnly: false, missingsecurein prod orsameSite), unrevoked/unrotated refresh tokens. - Authorization, RBAC & IDOR (Highest Priority): Authenticated routes checking login without verifying ownership or tenant/role scope (e.g.,
GET/PATCH/DELETE /:idquerying purely by:idwithout scoping toreq.user.id,ownerId, or hierarchy checks). Admin/staff endpoints lacking explicit role guards. - Input Validation & Mass Assignment: Raw
req.bodypassed directly to.create(),.update(), or.findByIdAndUpdate()(allowing privilege escalation viarole,isAdmin,status,ownerId). Missing schema validation, unvalidated:idparams (raw 500 CastErrors), or unstripped unknown fields. - Data & Error Leakage: Returning whole DB entities containing
password,passwordHash,refreshToken, OTPs, or internal metadata instead of explicit projections/DTOs. Exposing stack traces, SQL/Mongo errors, or file paths in production error responses. - Injection & File Upload Hardening: Unsanitized query objects vulnerable to NoSQL operator injection (
$ne,$gt,$where), unsafe dynamic regex (ReDoS), or string-interpolated raw SQL. On uploads: missing file size caps, missing MIME/extension allowlists, trusting client filenames (path traversal), or leaking temp files. - DoS, Pagination & Abuse Controls: Unbounded queries (
.find()withoutlimitor uncapped?limit=999999—enforce ceiling likeMath.min(limit, 100)). Missing request body limits, missing rate limiting on auth/sensitive endpoints, or missingtrust proxybehind reverse proxies. - Database & Concurrency Integrity: Missing indexes on frequently queried foreign keys, lookup fields, or unique constraints. Read-modify-write races on critical state (balances, inventory, bookings) lacking atomic updates, unique constraints, transactions, or locks.
- Transport & Header Hardening: Missing security headers (
helmetor equivalent), permissive CORS (origin: '*'withcredentials: trueor reflecting arbitraryOrigin), or secrets committed to repository.
5. Safe Auto-Fix Boundary & Verification
- In
Audit + Safe Fixesmode, apply ONLY safe, non-breaking fixes:- Remove fallback secrets with fail-fast startup checks.
- Add security headers (
helmet) and strict body limits (limit: '10kb'). - Cap unbounded pagination (
Math.min(limit, 100)). - Strip sensitive fields (
passwordHash) from queries and DTOs. - Enable DTO whitelisting flags (
whitelist: true, forbidNonWhitelisted: true). - Add missing obvious indexes on schema lookup fields.
- NEVER silently alter API response schemas or guess ambiguous RBAC rules—mark them under
Needs Decision. - If files were modified, run native build/typecheck (
tsc --noEmit,go vet ./..., etc.) to verify zero regressions.
6. Exception-Only Severity Report
Omit passing checks completely. Print:
- Tally:
Audit complete: X Critical, Y High, Z Medium (A auto-fixed, B need manual/design decision). Build check: <pass/skipped>. - Table 1 — Auto-Fixed (omit if none):
Severity | File:Line | Issue | Fix Applied - Table 2 — Action Required (sorted Critical -> High -> Medium):
Severity | File:Line | Exploit / Concrete Risk | Recommended Fix
7. Lazy-Load Adapter Routing Table
[!WARNING] Token Economy: Load ONLY active stack adapters. Never load unneeded reference files.
| Stack Component | Reference File | Purpose |
|---|---|---|
| Express | Express Adapter | Async handling, trust proxy, rate limiting, Multer, 4-arg error handlers. |
| NestJS | NestJS Adapter | ValidationPipe flags, ClassSerializer, Guard ordering, Throttler. |
| DB / Redis | Data Layer | NoSQL/SQL injection, atomic race fixes, projections, Redis TTLs. |