Topic 6 of 6 · 4 min read
Asking coding agents for secure code
Goal: Write security instructions and a review prompt for a coding agent, then judge whether its claimed fixes really close the flaw.
Tell the agent which security properties the code must have, then run a second pass that tries to break the result. "Make it secure" alone gives vague changes and false confidence.
Check yourself · 1 of 3
Which instruction gives a coding agent the best chance of producing a safe endpoint?
Why it matters
A study of programmers using an AI assistant (Perry et al., 2022) found that participants with the assistant wrote significantly less secure code and were more likely to believe their code was secure. That second finding is the one to remember. The agent says "I added input validation and made it secure", and it sounds finished. Whether it is finished depends on specifics.
How to ask well
- State requirements as facts the code must satisfy. "Every query uses bound parameters. Every route that returns a record checks that it belongs to the signed-in user. No secret appears in code or logs."
- Name the risk of the feature. "This endpoint fetches user-supplied URLs, so guard against SSRF." Concrete risk names beat general words.
- Say what is out of scope so the agent does not rewrite your auth system on its own.
- Ask for a test per requirement, such as a request as user B for user A's id that must return 404.
- Run a separate review pass. Start a fresh chat or a second model with the diff and a narrow job: "Act as an attacker. List ways to read another user's data, run code, or learn a secret. Cite the line for each. Do not rewrite the code."
Before and after
Before:
Add an endpoint to download invoices. Make it secure.
After:
Add GET /invoices/:id. Requirements:
- Only the invoice owner may read it; others get 404.
- Query with bound parameters only.
- Do not log invoice contents.
- Add tests: owner gets 200, another user gets 404, anonymous gets 401.
Do not change the auth middleware.
Judging the agent's fix
A claimed fix is real when you can see the changed line and a test that fails without it. Ask the agent to show the exploit request first, then the fix, then the same request failing. Check that the fix is at the right layer: an ownership filter in the query, not a front-end message.
What goes wrong
- Treating the agent's review as an audit. A model reviewing its own output tends to agree with it.
- Long generic checklists that the agent acknowledges and ignores.
- Fixes that move the problem. Input is "sanitized" in one handler while a second route still concatenates.
- No tools behind the claim. Run a linter or scanner such as Semgrep, secret scanning and dependency audits alongside the model.
Examples
A review prompt that finds more
Review the attached diff as an attacker. For each of: injection,
missing authorization, SSRF/unsafe input, secrets, unsafe defaults,
list concrete exploit requests against this code with file and line.
If a category has no issue, say "none found" and why.
Do not rewrite the code.
The fixed categories force a statement on each class and the "cite the line" rule discourages a vague answer.
A claimed fix that did not close the hole
Agent summary: "Fixed SQL injection by escaping quotes in the search term."
The diff adds term.replace("'", "''") and keeps the string-built query. A different encoding or a numeric context still breaks it. The correct request back to the agent is: "Use a parameterized query and show a test with a quote and a UNION payload."
Key points
- Write security requirements as testable statements, not as the word "secure".
- Name the specific risk of the feature, such as SSRF or ownership checks, in the task.
- Ask for a test per requirement, including an attacker-style request that must fail.
- Use a separate review pass with an attacker role and line citations, and do not let the author review itself alone.
- Accept a fix only when you see the exploit fail and the change sits at the right layer.
Common mistakes
- Asking for "secure code" without naming a property that can be tested.
- Accepting "I validated the input" without finding the line and a failing attacker test.
- Letting the same session that wrote the code be the only reviewer.
- Skipping automated scanners because the model said it checked.
Check yourself · 2 of 3
An agent says it fixed an IDOR. What is the strongest evidence you can ask for?
Check yourself · 3 of 3
Why use a separate review pass in a fresh chat for security?
Practice items, not counted towards your mastery.
Start here
- CWE Top 25 Most Dangerous Software Weaknesses (MITRE) (opens in a new tab)DocsFree
The ranked list of weakness classes, including injection and broken access control, that generated code should be checked against.
- Do Users Write More Insecure Code with AI Assistants? (opens in a new tab)PaperFree
A user study showing assistant users wrote less secure code while feeling more sure of it.
- OWASP Top Ten (opens in a new tab)StandardFree
The standard awareness list of web application risks, from access control to misconfiguration.
More for this topic
- OWASP Cheat Sheet Series (opens in a new tab)DocsFree
Short, specific requirements per topic that can be pasted into a task.