mediumCWE-20A03:2021

No Input Validation

User-supplied data sent directly to databases or external APIs without any type, format, or content validation.

How It Works

When you pass req.body straight to a database query or API call, an attacker controls the shape and content of that data. This enables SQL injection, NoSQL injection, type confusion bugs, and unexpected behavior. AI-generated code is especially prone to this — it often skips validation for brevity.

Vulnerable Code
// BAD: raw body straight to the database
export async function POST(req: Request) {
  const body = await req.json();
  const user = await db.users.create({ data: body });
  return Response.json(user);
}
Secure Code
// GOOD: validate with Zod before touching the DB
const schema = z.object({
  name: z.string().min(1).max(100),
  email: z.string().email(),
});
export async function POST(req: Request) {
  const body = schema.parse(await req.json());
  const user = await db.users.create({ data: body });
  return Response.json(user);
}

Real-World Example

A common vibecoder mistake: AI generates an API route that destructures req.body and passes it to Prisma's create(). If the users table has a role field, an attacker can set role: 'admin' and escalate privileges instantly.

How to Prevent It

  • Use Zod, Joi, or Yup to validate every API request body and query param
  • Whitelist allowed fields explicitly — never pass the entire request body to an ORM
  • Validate types, lengths, formats, and ranges — not just presence
  • Return 400 with a clear error message on validation failure, not 500

Affected Technologies

nodejsNext.jsPythonGoPHP

Data Hogo detects this vulnerability automatically.

Scan Your Repo Free

Related Vulnerabilities