Memstack memstack-security-api-audit
Use this skill when the user says 'audit API', 'check API security', 'API routes security', 'endpoint audit', 'check my routes', or needs to verify API route protection. Reviews API endpoints for authentication, authorization, and input validation gaps. Do NOT use for frontend security headers or dependency scanning.
git clone https://github.com/cwinvestments/memstack
T=$(mktemp -d) && git clone --depth=1 https://github.com/cwinvestments/memstack "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/security/api-audit" ~/.claude/skills/cwinvestments-memstack-memstack-security-api-audit && rm -rf "$T"
skills/security/api-audit/SKILL.md🛡️ API Audit — Checking API Route Security...
Audit Next.js API routes for authentication, authorization, validation, and common vulnerabilities.
Activation
When this skill activates, output:
🛡️ API Audit — Checking API Route Security...
Then execute the protocol below.
Context Guard
| Context | Status |
|---|---|
| User asks to audit/check API routes | ACTIVE — full audit |
| User mentions API security | ACTIVE — full audit |
| User asks about endpoint protection | ACTIVE — full audit |
| User is writing a new API route | DORMANT — let them finish first |
| Non-Next.js project | DORMANT — not applicable (adapt if Express/Fastify detected) |
Protocol
Step 1: Discover API Routes
Find all API route files in the project:
-
App Router (Next.js 13+):
find . -path "*/app/api/*/route.ts" -o -path "*/app/api/*/route.js"Each file exports named functions:
,GET
,POST
,PUT
,PATCH
.DELETE -
Pages Router (legacy):
find . -path "*/pages/api/*.ts" -o -path "*/pages/api/*.js"Each file exports a default handler.
-
Middleware:
find . -name "middleware.ts" -o -name "middleware.js" | head -5Check if global auth middleware exists (reduces per-route auth requirements).
-
Server Actions (Next.js 14+):
grep -r "'use server'" --include="*.ts" --include="*.tsx" -lServer actions are also attack surface — treat as API routes. For each server action function, verify it performs authentication before accessing data. Server actions are callable from any client component and receive no automatic auth — they are functionally identical to unauthenticated POST endpoints unless the function explicitly calls
,getSession()
, or equivalent.getAuthContext()
Compile a list of all routes with their HTTP methods and file paths.
Step 2: Check Authentication (Check 1)
For each route, determine if it verifies the caller is authenticated:
Search for auth patterns in each route file:
/getAuthContext
/getSession
/getServerSession
— framework authauth()
/getToken
/verifyToken
— manual JWTjwt.verify
with session validation — cookie-based authcookies().get
with validation — bearer tokenheaders().get('authorization')
/createRouteHandlerClient
— Supabase authcreateServerComponentClient
Classify each route:
| Status | Meaning |
|---|---|
| ✅ Authenticated | Auth check found before data access |
| 🔴 No Auth | No authentication pattern detected |
| ℹ️ Public | Route is intentionally public (webhooks, health checks, public data) |
| ⚠️ Middleware-only | Auth handled by middleware — verify matcher covers this route |
Flag as CRITICAL if a route performs database writes or returns user-specific data with no auth.
Known public route patterns (classify as ℹ️ INFO, not CRITICAL):
,/api/health
— health checks/api/status
— external webhooks (need signature verification instead)/api/webhooks/*
— auth flow endpoints (login, callback, register)/api/auth/*
— explicitly named public routes/api/public/*
— cron jobs (need secret verification instead)/api/cron/*
Step 3: Check Authorization (Check 2)
For authenticated routes, verify they check what the user can access:
Search for authorization patterns:
/verifyOrgAccess
/checkOrgMembership
— org-level authzrequireRole- Comparing
against resourceuser.id
/user_id
— ownership checkowner_id - Role checks:
or similaruser.role === 'admin' - Supabase RLS (may handle authz at database layer — note as INFO)
Flag as WARNING if:
- Route fetches data by ID from params without ownership verification
- Route accepts
from request body instead of deriving from sessionorganization_id - Multi-tenant route returns data without org scope filter
- DELETE route has no ownership check
Pattern to enforce:
// BAD — trusts client-provided org ID const { orgId } = await req.json(); const data = await db.from('documents').select().eq('org_id', orgId); // GOOD — derives org from authenticated session const { orgId } = await getAuthContext(req); const data = await db.from('documents').select().eq('org_id', orgId);
Step 4: Check Input Validation (Check 3)
For routes that accept request body or query params:
Search for validation patterns:
/z.object
/z.string()
/.parse(
— Zod.safeParse(
/Joi.object
— Joi.validate(
/yup.object
— Yup.validate(
orbody.
followed by manual type checks — weak validationreq.json()
Flag as WARNING if:
- Route reads
orreq.json()
without schema validationrequest.body - Route uses query params in database queries without validation
- Route passes user input directly to external APIs
Flag as CRITICAL if:
- Route uses dynamic code execution with user input (see Check 5)
- Route constructs file paths from user input without sanitization
Step 5: Check Rate Limiting (Check 4)
For public-facing routes:
Search for rate limiting patterns:
/rateLimit
/rateLimiter
importslimiter
— serverless rate limiting@upstash/ratelimit
header settingX-RateLimit- Middleware-level rate limiting (check
)middleware.ts
Flag as WARNING if:
- Login/register routes have no rate limiting (brute force risk)
- Public data endpoints have no rate limiting (scraping/abuse risk)
- Webhook endpoints have no rate limiting (replay attack risk)
Step 5b: Check Request Size Limits (Check 4b)
For POST/PUT/PATCH routes that accept request bodies:
Search for size enforcement patterns:
header checks before parsing bodyContent-Length
config withbodyParser
optionsizeLimit
— Next.js Pages Routerexport const config = { api: { bodyParser: { sizeLimit: '...' } } }- Next.js App Router: check if
/request.text()
is called without upstream size limitsrequest.json() - Middleware-level body size restrictions
Flag as WARNING if:
- Routes that accept file uploads, JSON bodies, or form data have no explicit size limit
- No global body size limit configured in middleware or framework config
- A route reads
on an unbounded body — a malicious client can send gigabytes of JSON, causing memory exhaustion (DoS)await request.json()
Note: Next.js App Router does NOT enforce a default body size limit on route handlers. Unlike the Pages Router (which defaults to 1MB via
bodyParser), App Router passes the raw request through. Projects must enforce limits explicitly.
Correct pattern:
// Check Content-Length before parsing const contentLength = parseInt(request.headers.get('content-length') || '0'); if (contentLength > 1_000_000) { // 1MB return NextResponse.json({ error: 'Request too large' }, { status: 413 }); } const body = await request.json();
Step 5c: Check Idempotency Keys (Check 4c)
For routes that create payments, charges, transfers, or financial transactions:
Search for idempotency patterns:
/idempotencyKey
/idempotency_key
headerIdempotency-Key- Stripe:
— built-in supportstripe.paymentIntents.create({}, { idempotencyKey: ... }) - Square:
field in request bodies — built-in supportidempotency_key - Custom: checking for duplicate request IDs before processing
Identify payment mutation routes: Search for routes that call:
,stripe.paymentIntents.create
,stripe.charges.createstripe.invoices.pay
,stripe.checkout.sessions.createstripe.subscriptions.create
,squareClient.payments.createsquareClient.orders.create- Any route that inserts into
,invoices
,payments
, ororders
tablestransactions
Flag as WARNING if:
- Payment-creating routes don't use idempotency keys — network retries can cause duplicate charges
- Routes that create financial records have no duplicate-request protection
Correct pattern:
// Stripe — pass idempotency key from client or generate deterministically const session = await stripe.checkout.sessions.create( { ... }, { idempotencyKey: `order-${orderId}-${timestamp}` } );
Step 6: Check SQL Injection (Check 5)
Search for dangerous query patterns:
- Template literals in raw SQL strings with interpolated variables — CRITICAL
- String concatenation in queries:
— CRITICAL"SELECT * FROM " + table
calls with unsanitized user input — WARNING.rpc()- Raw SQL via
orprisma.$queryRawUnsafe
— CRITICALsql.unsafe
Safe patterns (do not flag):
- Parameterized queries with tagged templates (Prisma)
- Supabase client
chain — safe by design.from().select().eq() - Prepared statements with
placeholders$1, $2
Step 7: Check Data Exposure (Check 6)
Search for sensitive data in responses:
- Returning full user objects:
— may include password hashreturn NextResponse.json(user) - Returning
results without column filtering — WARNINGselect('*') - Logging request bodies that may contain passwords — WARNING
- Returning internal IDs, database errors, or stack traces — WARNING
Fields that should never appear in API responses:
password, password_hash, hashed_password, secret, token, refresh_token, api_key, private_key, ssn, credit_card
Search each route file for these field names, then check if they appear in return/response paths.
Step 8: Check CORS Configuration (Check 7)
Search for CORS patterns:
— overly permissive (WARNING)Access-Control-Allow-Origin: *
with wildcard origin — CRITICALAccess-Control-Allow-Credentials: true- Missing CORS headers on routes that need cross-origin access — INFO
headers configuration for CORSnext.config.js
Check
or next.config.js
:next.config.mjs
grep -A5 "Access-Control\|headers\(\)" next.config.*
Step 9: Check Error Handling (Check 8)
Search for error patterns in each route:
- Bare
— leaks stack traces (WARNING)catch (e) { return NextResponse.json(e) }
— leaks internal errors (WARNING)catch (e) { return NextResponse.json({ error: e.message }) }- No try/catch around database operations — unhandled errors become 500s with stack traces (WARNING)
with full error objects in production — log exposure (INFO)console.error
Correct pattern:
catch (error) { console.error('Route /api/items failed:', error); return NextResponse.json( { error: 'Internal server error' }, { status: 500 } ); }
Step 10: Check Webhook & Special Routes (Check 9)
For webhook routes (
/api/webhooks/*):
- Stripe: must call
— CRITICAL if missingstripe.webhooks.constructEvent(body, sig, secret) - GitHub: must verify
headerX-Hub-Signature-256 - Generic: must verify shared secret or HMAC signature
- Must use raw body (
notreq.text()
) for signature verificationreq.json()
For file upload routes:
- Must enforce file size limits — WARNING if missing
- Must validate file type / MIME type — WARNING if missing
- Must not store uploads in publicly accessible paths without auth — CRITICAL
For DELETE routes:
- Must verify resource ownership before deletion — CRITICAL if missing
- Should use soft delete pattern where appropriate — INFO
Step 11: Generate Report
🛡️ API Security Audit Report Project: <project-name> Routes found: <count> Server actions: <count> Global middleware auth: <yes/no> ## Route Audit | Route | Method | Auth | Authz | Validation | Risk | Issues | |-------|--------|------|-------|------------|------|--------| | /api/users | GET | ✅ | ✅ | ✅ | ✅ OK | — | | /api/users | POST | ✅ | ✅ | 🔴 | ⚠️ WARN | No input validation | | /api/items/[id] | DELETE | 🔴 | 🔴 | — | 🔴 CRIT | No auth, no ownership check | | /api/webhooks/stripe | POST | ℹ️ | — | — | ✅ OK | Signature verified | | /api/admin/users | GET | ✅ | ⚠️ | ✅ | ⚠️ WARN | No role check for admin route | ## Critical Issues 1. **DELETE /api/items/[id]** — No authentication. Any request can delete any item. → Fix: Add `getAuthContext(req)` and verify `item.user_id === user.id` before deleting. 2. **POST /api/upload** — No file size limit. Server vulnerable to resource exhaustion. → Fix: Add size limit config or check `Content-Length` header. ## Warnings 1. **POST /api/users** — Request body parsed without schema validation. → Fix: Add Zod schema and parse before processing. 2. **GET /api/admin/users** — Authenticated but no admin role verification. → Fix: Add role check before returning data. ## Info 1. **Supabase RLS active** — Authorization may be handled at database layer for some routes. Verify RLS policies cover the same access patterns. Run `rls-checker` for full RLS audit. ## Summary - 🔴 Critical: <count> - ⚠️ Warning: <count> - ℹ️ Info: <count> - ✅ OK: <count> - Total routes: <count> ## Checklist - [ ] All non-public routes authenticate callers - [ ] All data-access routes verify ownership/org membership - [ ] All POST/PUT/PATCH routes validate input with schema - [ ] Login/register routes have rate limiting - [ ] Webhook routes verify signatures - [ ] No raw SQL with user input - [ ] API responses exclude sensitive fields - [ ] Error responses don't leak stack traces - [ ] CORS configured for specific origins, not wildcard - [ ] POST/PUT/PATCH routes enforce request body size limits - [ ] Payment-creating routes use idempotency keys - [ ] Server actions verify auth before data access
Step 12: Suggest Fixes
For each CRITICAL and WARNING issue, provide:
- The exact code fix with file path and line number
- Any new dependencies needed (e.g.,
for rate limiting)@upstash/ratelimit - Whether the fix requires changes to other files (middleware, types, etc.)
Offer to apply fixes directly if the user approves.
Risk Levels
| Level | Meaning | Action |
|---|---|---|
| 🔴 CRITICAL | Active vulnerability exploitable without auth | Fix immediately |
| ⚠️ WARNING | Missing defense layer or weak pattern | Fix before production |
| ℹ️ INFO | Acceptable pattern worth verifying | Review and confirm intentional |
| ✅ OK | Properly secured | No action needed |
Related Skills
- rls-checker — Audit Supabase RLS policies (database-level security)
- secrets-scanner — Find exposed API keys and credentials
- owasp-top10 — Full OWASP Top 10 vulnerability assessment
Level History
- Lv.1 — Base: Route discovery (App Router, Pages Router, Server Actions, middleware), 9-point analysis (auth, authz, validation, rate limiting, SQLi, data exposure, CORS, error handling, webhooks/uploads/deletes), structured report with risk levels, inline fix suggestions. Patterns derived from AdminStack (getAuthContext, verifyOrgAccess) and 10+ production Next.js apps. (Origin: MemStack Pro v1.0, Mar 2026)
- Lv.2 — Audit feedback: Added request body size limit check (App Router has no default limit), idempotency key check for payment mutations (Stripe/Square), Server Actions auth verification (callable from any client, no automatic auth). (Origin: AdminStack audit, Mar 2026)