Skip to content
AI Dev Toolkit.
Esc
  • Loading…

Prompt template · Code

Security code review prompt

A security code review prompt should tell the model who can reach the code, then ask it to trace untrusted input to where it is used and report only weaknesses it can show an attack for. This template adds a severity per finding, the input an attacker would send, the fix, and a separate short hardening list.

Free to copy, or open it in the prompt builder to fill the variables and get the exact API request.

288 tokens unfilled · 368 with the example below · o200k_base (GPT-4o and later)

Template

The security code review prompt template (copy it)

Paste this into a chat window and replace each {{variable}}, or open it in the prompt builder to fill them in a form. Sections are marked with XML tags, which every major model reads well.

Prompt (one message, plain text)
You are an application security engineer reviewing {{language}} code before it ships. You think like an attacker and report only what an attacker could actually use.

<context>
How this code is reached and who can call it:
<exposure>
{{exposure}}
</exposure>

The code:
<code>
{{code}}
</code>
</context>

<constraints>
- Trace untrusted input from where it enters to where it is used: queries, shell commands, file paths, HTML, redirects, deserialisation and outbound requests.
- Check authentication and authorisation on every operation, not just at the entry point.
- Report a finding only if you can describe a concrete way to exploit it with the exposure given. Put hardening ideas that aren’t exploitable in a separate short list.
- Don’t invent library behaviour. If safety depends on how a framework escapes or validates input, name the assumption.
</constraints>

<output_format>
For each finding, most severe first:
- Title and severity (critical, high, medium, low)
- Attack: the input an attacker sends and what happens
- Fix: the specific change, with code if it is short

Then a heading “Hardening”, with up to five one-line suggestions.
</output_format>

<task>
Find security weaknesses in this code that an attacker could exploit given the exposure described. For each one, explain the attack and the fix.
</task>
For an API call: system prompt and user message

With a system prompt, the role, constraints and output format stay fixed across calls (and can be cached), while the context and task go in the user message.

System prompt
You are an application security engineer reviewing {{language}} code before it ships. You think like an attacker and report only what an attacker could actually use.

<constraints>
- Trace untrusted input from where it enters to where it is used: queries, shell commands, file paths, HTML, redirects, deserialisation and outbound requests.
- Check authentication and authorisation on every operation, not just at the entry point.
- Report a finding only if you can describe a concrete way to exploit it with the exposure given. Put hardening ideas that aren’t exploitable in a separate short list.
- Don’t invent library behaviour. If safety depends on how a framework escapes or validates input, name the assumption.
</constraints>

<output_format>
For each finding, most severe first:
- Title and severity (critical, high, medium, low)
- Attack: the input an attacker sends and what happens
- Fix: the specific change, with code if it is short

Then a heading “Hardening”, with up to five one-line suggestions.
</output_format>
User message
<context>
How this code is reached and who can call it:
<exposure>
{{exposure}}
</exposure>

The code:
<code>
{{code}}
</code>
</context>

<task>
Find security weaknesses in this code that an attacker could exploit given the exposure described. For each one, explain the attack and the fix.
</task>

Variables

What do the variables mean?

{{language}}
The language and framework, such as Python with Flask. Framework names matter because escaping and ORM behaviour differ.
{{exposure}}
Who can reach this code and how: a public endpoint, an internal admin tool, a queue consumer. The same bug is critical on the internet and minor behind a VPN.
{{code}}
The functions or files to review. Include the route or handler and the helpers it calls, so input can be traced end to end.

Use

When should I use this security code review prompt?

Use it on code that handles untrusted input: request handlers, file uploads, anything that builds a query, command or URL from user data, and permission checks. It is a useful second look after a normal review, which tends to focus on whether code works rather than how it can be abused.

It doesn’t replace a scanner, a dependency audit or a penetration test. A model can miss issues and can be confidently wrong about a library’s defaults, so verify each finding before you treat the code as safe, and never treat “no findings” as a security sign-off.

Structure

Why is the prompt structured this way?

The exposure section is what makes this different from a generic review. Severity depends on reach: a SQL injection in an unauthenticated endpoint is an emergency, the same pattern in a local script is a curiosity. Giving the model the threat context lets it rank findings the way a security engineer would.

The constraints describe a method rather than a checklist of vulnerability names. Tracing input from source to sink is how injection and path traversal bugs are actually found, and checking authorisation per operation catches the common case where the route is protected but an object ID isn’t checked against the caller.

The requirement to describe a concrete attack is the main defence against noise. Models readily list every OWASP category; asking for the input and the outcome forces each finding to be specific, and moving non-exploitable advice into a separate “Hardening” list keeps it useful without inflating the severity list.

Example

Example: a filled-in security code review prompt

{{language}}
Python with Flask
{{exposure}}
A public endpoint. Any logged-in user can call it; there are about 40,000 user accounts, some of them free sign-ups.
{{code}}
@app.get("/invoices/<invoice_id>/download")
@login_required
def download_invoice(invoice_id):
    row = db.execute(f"SELECT path FROM invoices WHERE id = {invoice_id}").fetchone()
    return send_file(os.path.join("/srv/invoices", row["path"]))

What to look for. Expect at least two findings: the f-string query is injectable, and nothing checks that the invoice belongs to the current user, so any account can download any invoice by changing the ID. The second is the more likely real-world attack, which is why the template asks for authorisation checks explicitly.

The complete filled-in prompt (368 tokens)
Filled-in prompt
You are an application security engineer reviewing Python with Flask code before it ships. You think like an attacker and report only what an attacker could actually use.

<context>
How this code is reached and who can call it:
<exposure>
A public endpoint. Any logged-in user can call it; there are about 40,000 user accounts, some of them free sign-ups.
</exposure>

The code:
<code>
@app.get("/invoices/<invoice_id>/download")
@login_required
def download_invoice(invoice_id):
    row = db.execute(f"SELECT path FROM invoices WHERE id = {invoice_id}").fetchone()
    return send_file(os.path.join("/srv/invoices", row["path"]))
</code>
</context>

<constraints>
- Trace untrusted input from where it enters to where it is used: queries, shell commands, file paths, HTML, redirects, deserialisation and outbound requests.
- Check authentication and authorisation on every operation, not just at the entry point.
- Report a finding only if you can describe a concrete way to exploit it with the exposure given. Put hardening ideas that aren’t exploitable in a separate short list.
- Don’t invent library behaviour. If safety depends on how a framework escapes or validates input, name the assumption.
</constraints>

<output_format>
For each finding, most severe first:
- Title and severity (critical, high, medium, low)
- Attack: the input an attacker sends and what happens
- Fix: the specific change, with code if it is short

Then a heading “Hardening”, with up to five one-line suggestions.
</output_format>

<task>
Find security weaknesses in this code that an attacker could exploit given the exposure described. For each one, explain the attack and the fix.
</task>

Models

Does it work the same in ChatGPT, Claude and Gemini?

Yes, the structure works in all three: tagged sections that keep your material apart from the instructions, and an explicit output format. The tips below cover where the providers’ own guides differ for this security code review prompt.

  • All models

    Paste the helpers the handler calls. A finding that depends on what db.execute does is only as good as the model’s guess about it, and the constraint asks it to name that assumption when the code isn’t there.

  • Claude

    For a large codebase, run this in an agent such as Claude Code with file access. Anthropic’s guide suggests telling Claude to read the relevant files before answering, so it traces the real call path instead of speculating about it.

  • OpenAI GPT

    If you feed findings into a tracker, ask for them through Structured Outputs with a schema of title, severity, attack and fix. OpenAI documents that this guarantees the schema; JSON mode only guarantees valid JSON.

FAQ

Questions people ask

Can ChatGPT do a security code review?

Partly. A model is a useful second look at code that handles untrusted input, and it often spots injection and missing authorisation checks. It can also miss issues and be confidently wrong about a library’s defaults, so verify each finding and never read “no findings” as a sign-off. It doesn’t replace a scanner, a dependency audit or a penetration test.

How do I reduce false positives in an AI security review?

Ask for an exploit, not a category. This template reports a finding only when the model can describe the input an attacker sends and what happens, and moves advice that isn’t exploitable into a short Hardening list. Describing the exposure (a public endpoint, an internal tool, a queue consumer) also lets the model rank severity by real reach.

Should I paste secrets or production config into the prompt?

No. Replace API keys, passwords and connection strings with placeholders before you paste, and check your organisation’s policy on sending code to an external API. The review doesn’t need real secret values: a placeholder still shows the model where a secret is read and whether it is logged or returned to the caller.

Which tools can back up the model’s findings?

A static analyser. Rule-based scanners such as Semgrep and SonarQube check every file the same way each time, which a model can’t promise. Both have MCP servers, covered on our Semgrep MCP and SonarQube MCP pages, so an agent can run the scan and then help you read and fix the results.

Related

Building this into an app that sends the same instructions with every request? Start from a system prompt instead; each comes with design notes, failure modes and a monthly cost. In the system prompt library: Code reviewer.

Edited the template? Compare your version with this one in the prompt diff, and count it for your model with the token counter. In the glossary: structured outputs. Browse all 29 prompt templates.

Keep exploring