Security Auditing, Threat Modeling & Design Review
Security is not complete when code compiles or login works.
A production security process asks:
what are we protecting?
who can attack it?
which boundaries can they reach?
what assumptions are trusted?
what fails if one control is bypassed?
how do we detect and contain failure?
This chapter turns the previous Phase 13 topics into a repeatable review system.
Start with assetsβ
List what matters.
Examples:
user accounts
organization membership
billing data
private documents
session credentials
API keys
OAuth tokens
webhook secrets
database access
admin operations
customer data
availability / compute budget
If an asset is not listed, it is easy for architecture to protect it accidentally rather than deliberately.
Identify actorsβ
Not every actor is simply βuser.β
Examples:
anonymous visitor
authenticated member
organization admin
organization owner
internal support user
API client
webhook provider
third-party script
malicious external user
malicious tenant member
compromised administrator
Many serious SaaS bugs come from an authenticated but unauthorized actor.
Identify entry pointsβ
In an App Router application, review:
pages
Server Actions
Route Handlers
Proxy
auth callbacks
webhooks
uploads
image optimizer sources
third-party scripts
client-side APIs
background workers
internal service calls
Security is only as strong as the least-reviewed entry point reaching the same data.
Draw trust boundariesβ
A simple system diagram:
browser
β
CDN / reverse proxy / WAF
β
Next.js Proxy
β
App Router server code
β
DAL / domain services
β
database / cache / queue
β
external providers
Label which boundaries:
- authenticate
- authorize
- terminate TLS
- rate limit
- validate input
- store secrets
- emit logs
- cache data
Threat categoriesβ
A practical review can group threats into:
Identityβ
credential stuffing
session theft
session fixation
account recovery abuse
MFA bypass
OAuth callback abuse
Authorizationβ
IDOR/BOLA
cross-tenant access
role escalation
stale permission claims
hidden-button-only protection
Injectionβ
XSS
SQL/NoSQL injection
command injection
path traversal
unsafe HTML/SVG
Request forgeryβ
CSRF
SSRF
open redirect
forged webhook
replay
Data exposureβ
secret in Client Component
RSC/HTML leak
verbose API response
log leak
analytics leak
cache leak
metadata/JSON-LD leak
Availability / abuseβ
large bodies
large uploads
expensive queries
rate abuse
algorithmic complexity
third-party timeout cascades
Defense in depthβ
For a sensitive mutation, aim for multiple independent controls:
secure session cookie
+ input validation
+ Server Action origin protection
+ current session verification
+ resource authorization
+ tenant-scoped query
+ database constraint
+ audit event
+ rate limit where appropriate
The goal is not duplicate code everywhere.
The goal is that one missed matcher or UI check does not become a full compromise.
Review use clientβ
The Next.js data-security guide recommends paying special attention to client boundaries.
Ask:
What props enter this component?
Are they broader than needed?
Do types include private fields?
Does this import a server-only module?
Does it receive session/token data unnecessarily?
Does third-party code see these values?
Review use serverβ
For every Server Action:
Is input validated?
Is mass assignment prevented?
Is the actor authenticated?
Is the exact operation authorized?
Is tenant/resource scope trusted from the server?
Does it return minimal output?
Can it be replayed?
Does it need rate limiting?
Are side effects durable/idempotent?
Review dynamic route paramsβ
A folder such as:
/app/projects/[projectId]
contains attacker-controlled input.
The framework validates route shape, not business authorization.
Review every use of:
params
searchParams
headers
cookies
formData
request JSON
as untrusted unless established otherwise.
Review Proxyβ
Proxy has broad request influence.
Audit:
- matchers
- redirects
- rewrites
- auth gating
- tenant routing
- forwarded headers
- CSP nonce creation
- rate-limit hooks
- request header mutation
Then verify protected operations do not depend on Proxy alone.
Review Route Handlersβ
Traditional API audit questions apply:
method semantics
content type
schema validation
authentication
authorization
CORS
CSRF
rate limits
body limits
timeouts
SSRF
safe errors
cache headers
Review DAL boundariesβ
A DAL should make the secure path easy and the insecure path hard.
Ask:
Can callers query DB directly elsewhere?
Are database packages imported outside DAL/domain modules?
Does every protected query verify actor and scope?
Are DTOs explicit?
Are tenant filters structural?
Are permission decisions testable?
Review cache securityβ
For every cached function/value:
- Is data public or private?
- What identifies the actor?
- What identifies tenant/resource?
- Can permissions change during TTL?
- Is output different by locale/role?
- Can one request populate data another actor receives?
- What invalidates permission-sensitive output?
Never approve a cache simply because it improves latency.
Review environment and build outputβ
Check:
NEXT_PUBLIC_ variables
committed .env files
client bundles for embedded config
static HTML/RSC output
source maps
build logs
CI environment access
preview environment secrets
Secrets can leak at build time even if runtime code is correct.
Review third partiesβ
Inventory:
analytics
chat widgets
payment SDKs
tag managers
maps
video embeds
A/B testing
support tooling
error monitoring
For each ask:
- who owns the vendor account?
- what browser privileges does it gain?
- what data is sent?
- where is consent handled?
- is CSP updated?
- what is the failure mode?
- how is it disabled during incident response?
Review CSP with renderingβ
If using nonce CSP, verify:
nonce generated per request
nonce unpredictable
Proxy matcher covers intended pages
request and response CSP agree
third-party scripts carry correct nonce/policy
route is dynamically rendered
CDN is not serving one nonce to many requests
A nonce reused through cache behavior defeats the intended model.
Review authentication lifecycleβ
Do not review only sign-in.
Check:
signup
verification
login
MFA
password reset
email change
password change
provider link/unlink
logout
logout-all
account disablement
admin role change
session expiry
Authorization must respond to lifecycle changes.
Review session compromise responseβ
If a session token is stolen:
Can it be revoked?
How long is it valid?
Can sensitive operations require re-auth?
Can all sessions be invalidated?
Can support see active sessions?
Are unusual session events detected?
Design incident response before compromise.
Review error messagesβ
Public errors should not reveal:
SQL
stack traces
internal file paths
provider secrets
token contents
password/account existence beyond policy
internal authorization rules
Internal logs should still be useful through structured reason codes and request IDs.
Security logging modelβ
Useful event categories:
auth.login.success
auth.login.failure
auth.mfa.challenge
auth.session.created
auth.session.revoked
authz.denied
security.csrf.rejected
security.webhook.invalid_signature
security.rate_limited
security.upload.rejected
security.ssrf.rejected
admin.role_changed
Avoid raw secrets/credentials.
Audit log vs application logβ
Application logs help debug systems.
Audit logs record security-relevant actor actions.
An audit event may need:
actor ID
tenant ID
action
resource ID
result
reason code
timestamp
request ID
source/session identifier
Depending on requirements, audit logs may need stronger retention and tamper resistance than ordinary logs.
Incident runbook: leaked secretβ
1. identify secret and blast radius
2. rotate/revoke immediately
3. remove from active config
4. inspect logs for use
5. invalidate derived credentials if needed
6. fix source of exposure
7. scan history/build artifacts
8. document incident
Deleting a Git commit is not rotation.
Incident runbook: cross-tenant authorization bugβ
1. disable vulnerable operation if necessary
2. identify affected resource paths
3. patch authorization at DAL/query boundary
4. invalidate relevant caches
5. inspect access/audit logs
6. determine data exposure
7. notify according to policy/legal obligations
8. add regression tests across tenants
Incident runbook: XSSβ
1. disable vulnerable content/script path
2. identify injection source and output sink
3. sanitize/encode correctly
4. tighten CSP where appropriate
5. rotate credentials exposed to script if necessary
6. inspect third-party/tag-manager state
7. add payload regression tests
Incident runbook: credential stuffingβ
1. measure attack pattern
2. apply layered rate limits/risk controls
3. notify users if warranted
4. require step-up/MFA for risk cases
5. inspect session creation anomalies
6. protect reset flow from secondary abuse
Security testing strategyβ
Phase 16 will implement automated testing depth, but Phase 13 defines what must be tested.
Authentication testsβ
- invalid credentials
- expired session
- revoked session
- role change
- disabled account
- callback state failure
- reset token replay
Authorization testsβ
For every protected resource:
owner allowed
same-tenant unauthorized role denied
other tenant denied
anonymous denied
stale session denied when policy changes
Injection testsβ
- HTML/script payload
- malicious URL schemes
- SQL/query manipulation
- oversized/nested input
Request-forgery testsβ
- cross-origin mutation
- forged Origin/Host scenarios
- SSRF private target
- redirect to private target
- webhook replay
Resource testsβ
- oversized upload
- wrong content type
- too many batch IDs
- timeouts
- rate-limit behavior
Dependency and supply-chain reviewβ
Your application also trusts:
- npm dependencies
- auth SDKs
- database drivers
- build plugins
- GitHub Actions
- deployment adapters
- third-party scripts
Controls include:
lockfile review
minimal dependency set
security advisories
provenance/signing where available
restricted CI permissions
pinned actions/dependencies where appropriate
secret isolation
Framework patch policy is security policyβ
Next.js has an Active/Maintenance LTS policy and publishes security releases.
Production should track the current supported stable patch rather than treating βNext.js 16β as sufficiently precise.
At this handbook snapshot the baseline is Next.js 16.2.12, with 16.x Active LTS.
Re-check before release/deploy decisions.
Experimental features need explicit risk acceptanceβ
Examples in the current security area include:
experimental auth interrupts
React taint integration
experimental CSP SRI support
Do not silently adopt experiments into a production security control plane.
Document:
- why needed
- version pinned
- fallback
- upgrade owner
- tests
Senior architecture review scenarioβ
Design security for:
multi-tenant SaaS
OAuth + password login
admin/member roles
projects and invoices
Stripe-like billing webhook
CSV imports
public share links
customer API keys
analytics/tag manager
A strong review discusses:
identity provider
session model
revocation
Proxy optimistic routing
DAL secure authorization
tenant-scoped queries
Server Action checks
Route Handler checks
CSRF/CORS
CSP/XSS
uploads
webhooks
SSRF
API keys
cache isolation
secrets
logs/audit
rate limits
incident response
Design principles to keepβ
- Authenticate identity; authorize operations.
- Treat browser input as untrusted.
- Keep secure checks close to protected data.
- Treat Server Actions and Route Handlers as public entry points.
- Use Proxy for optimistic/front-door policy, not sole authorization.
- Return minimal DTOs.
- Keep secrets out of client graphs and serialized output.
- Design session revocation and permission freshness.
- Scope queries structurally to user/tenant/resource.
- Bound body size, time, concurrency, and cost.
- Use layered browser controls: CSRF defenses, XSS prevention, CSP.
- Measure and audit denials without logging credentials.
- Prefer mature auth/security libraries over custom cryptography.
- Keep framework and platform security responsibilities separate.
- Patch supported framework versions promptly.
Interview questionsβ
How would you secure a multi-tenant Next.js app?
Use a hardened identity/session system, optimistic Proxy routing only where useful, server-only DAL authorization, tenant-scoped queries, minimal DTOs, independent Server Action/Route Handler checks, cache isolation, secure cookies, CSRF/XSS/CSP controls, rate limits, audit logs, and tested session/permission revocation.
What would you inspect first in a Next.js security audit?
use client data boundaries, use server actions, dynamic params, Proxy, Route Handlers, DAL/database access, environment exposure, caches, auth lifecycle, and third-party scripts.
Why is patch management part of application security?
Framework and dependency vulnerabilities can bypass otherwise-correct application code, so supported stable versions and timely security patches are part of the control system.
Final Phase 13 review exerciseβ
Create a one-page threat model for your application containing:
assets
actors
entry points
trust boundaries
session model
authorization model
tenant/resource scoping
browser threats
server-side fetch/upload threats
third-party trust
rate limits
logging/audit
incident controls
patch policy
Then choose the three most dangerous failure modes and write a concrete test and incident response for each.