Validation runs before database writes
Why it matters
Calling OrderSchema.parse(body) after prisma.order.create({ data: body }) already ran is not validation — it's an audit log that arrives after the crime. The bad data is committed to the database whether the parse throws or not. CWE-696 (Incorrect Behavior Order) captures this: the side effect precedes the safety check. An attacker who sends a malformed payload wins regardless of the parse result, because the DB write already succeeded.
Severity rationale
High because validating after the write means every mutation executes with unvalidated input — the check fires after the attack surface is already exploited.
Remediation
Move the parse call to the first statement after req.json(). Validation must throw before any prisma.* call is reachable.
// Bad: write happens before validation
export async function POST(req: Request) {
const body = await req.json()
const order = await prisma.order.create({ data: body }) // unvalidated write
OrderSchema.parse(body) // too late — commit already happened
return Response.json(order)
}
// Good: validate first, then write
export async function POST(req: Request) {
const body = await req.json()
const data = OrderSchema.parse(body) // throws before any DB interaction
const order = await prisma.order.create({ data })
return Response.json(order)
}
DetectionPro
The detection method for this pattern is part of AuditBuffet Pro.
The full method (what to look for, pass and fail conditions, and how results get reported) ships inside the Security Implementation Validation Audit. The pattern definition, severity rationale, and remediation remain openly licensed under CC-BY-4.0.
External references
- cwe · CWE-20 — Improper Input Validation
- cwe · CWE-696 — Incorrect Behavior Order
- owasp:2021 · A03 — Injection
Taxons
History
- 2026-04-18·v1.0.0·Initial import from ai-slop-security-theater·automated