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:
- Missing input validation — trusting query parameters and JSON bodies as-is.
- Injection — SQL, shell commands or HTML built from strings.
- Secrets in code — API keys pasted into source "to make it work".
- Over-broad permissions — a database user or cloud role that can do everything.
- Outdated or invented dependencies — old package versions with known vulnerabilities, or package names that don't exist (and can be registered by an attacker).
- Leaky errors — stack traces and internal details returned to callers.
- Missing authorization — checking that a user is signed in, but not that the record is theirs.
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:
- Static analysis for injection, unsafe deserialization and weak crypto (tools such as Semgrep or CodeQL).
- Dependency audit against vulnerability databases (
npm audit,pip-audit, OSV-Scanner), and confirm every new package actually exists and is the one you meant. - Secret scanning for keys and tokens in code and history (for example gitleaks).
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:
- Anything touching auth, payments, personal data or deletes.
- New dependencies and new outbound calls.
- Changes to permissions, CORS or visibility.
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:
- Run each service isolated, with its own identity and only the permissions it needs.
- Keep secrets in a vault, injected at runtime — never in prompts, code or logs.
- Put rate limits in front of every public endpoint.
- Log every request and alert on error spikes.
- Ship changes as drafts, test them, and keep one-click rollback.
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
- Security constraints written into the request
- Input schema on every endpoint, 4xx on anything else
- Static analysis, dependency audit and secret scan on every change
- Abuse-case tests
- Human review for auth, payments, data and new dependencies
- Isolation, least privilege, encrypted secrets, rate limits, logs
- Drafts, tests, then publish — with rollback ready
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.