Resources / Code generation

How to secure AI-generated code: a review workflow that scales

A step-by-step workflow to secure AI-generated code: threat-model the prompt, scan code and dependencies, test abuse cases, and run it with least privilege.

AI models write code faster than anyone can read it line by line. Securing AI-generated code isn't about reading harder — it's about a workflow where the risky parts are caught automatically and the rest is contained if something slips through.

The mistakes AI code makes most

Generated code tends to fail in predictable ways:

Step 1: Put security in the request

The cheapest fix is in the prompt. Say who may call the endpoint, what input is valid, and what must never happen: "only the account owner can read their invoices; amounts are positive integers in cents; never return other customers' data". Models follow explicit constraints far better than implied ones.

Step 2: Require schemas

Make every endpoint declare an input schema and reject anything that doesn't match with a 4xx. Validation then lives in one obvious place instead of being scattered through the code — and reviewers can read the schema in seconds.

Step 3: Scan automatically

Run the same checks on every change, human or AI:

Step 4: Test the abuse cases

Happy-path tests won't catch security bugs. Add tests that send what an attacker would: missing fields, wrong types, huge payloads, another user's ids, ' OR 1=1 --, ../../etc/passwd. AI is good at writing these too — ask for them explicitly. More in testing AI-generated code.

Step 5: Review the diff, not the whole file

Human review is still the last line of defence — focus it where it counts:

A small diff with a clear description is reviewable in minutes; a regenerated 400-line file isn't. Ask for minimal, targeted changes.

Step 6: Contain what you missed

Assume something will get through, and limit the damage:

Step 7: Watch out when the code calls a model

If generated code itself calls an LLM, treat model output as untrusted input: validate it, never execute it, and don't let it choose which tools or records to touch without checks. Prompt injection arrives through emails, web pages and documents the model reads.

A checklist to keep

How Wisp helps

Wisp handles the containment layer for you: every project runs isolated with its own identity, secrets are encrypted and never shown to the model, endpoints get input schemas, limits are enforced before code runs, every request is logged, and every change is a draft you test before publishing. See Securing AI-generated backend code and the security overview.

Build it with Wisp — describe an API, get it live.

Related