AI is a remarkable security reviewer when prompted correctly — and a useless one when asked vaguely to "check this for security issues". The difference is whether you give the AI a threat model, the code in question, and a structured way to report findings.
Security review is pattern recognition. SQL injection, path traversal, IDOR, weak crypto — these are classes of bug with familiar shapes. AI has seen thousands of examples of each, which means it can often spot them faster than a human reviewer. But it has to be told what to look for, where to look, and how to report what it finds. This tutorial gives you the prompt pattern for that.
Every security review has three inputs: the code being reviewed, the trust boundary (where user-controlled input enters), and the threat model (what an attacker is trying to achieve). Without all three, AI either misses real issues or invents fake ones.
Imagine asking a security consultant to "look at the code". They would first ask: where does user input come from? What are you most worried about? What does this service protect? AI needs the same priming.
The default is to paste code and ask "is this secure?". The AI returns a long list of generic platitudes — "use HTTPS", "validate inputs" — that don't actually look at your code.
Weak prompt
is this code secure?
[pastes a 100-line function]
You will get a generic security checklist masquerading as a review. The AI is unsure which inputs are user-controlled, so it warns about everything indiscriminately. Real vulnerabilities hide inside the noise.
Strong prompt
Act as a senior application security engineer. Review the code below.
Stack: Node.js 20, Express 4, PostgreSQL via `pg` library.
Trust boundary:
- Anything inside `req.body`, `req.query`, `req.params`, `req.headers` is
attacker-controlled.
- `req.user` is set by JWT-verifying middleware and can be trusted.
- The database is internal — its rows are NOT considered user input.
Threat model (in priority order):
1. SQL injection via the search endpoint
2. IDOR (one user accessing another user's data)
3. Unsafe deserialisation, path traversal, command injection
4. Auth/session weaknesses (token leakage, missing checks)
5. Information disclosure in error messages
6. Logic flaws specific to the domain (e.g. negative amounts, race conditions)
Code:
```js
[paste 60–120 lines of one route handler or one module]
```
Output format — one entry per finding, sorted by severity:
- ## [Severity: Critical/High/Medium/Low] Finding title
- Where: file:line range
- Why it matters: 1 sentence
- Exploit sketch: 1–2 sentences showing how an attacker would trigger it
- Minimal fix: a code snippet or 1-paragraph approach
If there are no findings under a given threat, say so explicitly. Do not
invent findings to fill the list.
The AI now knows what is attacker-controlled, what to prioritise, and exactly how to present each finding. Crucially, "do not invent findings" stops it from padding the report with low-quality issues.
Tip: Always have AI security reviews verified by a human. AI is excellent at recall (finding patterns) and weaker at precision (judging exploitability). A reviewer who looks only at the AI's high/critical findings can triage a large review in minutes.
Pick the riskiest route in your project (search, file upload, anything that touches money or PII). Run the strong-prompt format. Triage the findings yourself: how many are real? How many are noise? Adjust the threat model section of your prompt to improve next time.
Take a piece of code that you know has a vulnerability (intentionally introduce a SQL injection or IDOR, in a throwaway file). Run the security prompt. Did the AI find it? If not, what was missing from your trust boundary section?
Build a reusable "security review template" prompt for your stack, with your trust boundaries and your threat model already baked in. Save it. Run it as part of every non-trivial PR. Treat AI security review as a standing pre-merge step.
Sign in to join the discussion and post comments.
Sign inAdvanced Prompt Engineering Techniques
Master the powerful techniques AI experts use every day. Chain-of-thought, RAG, agents, function calling, prompt evaluation, and much more — 20 deep-dive tutorials.
Prompt Engineering for Content & Copywriting
Write blogs, ads, emails, and social media content ten times faster with AI. 13 practical tutorials on prompt engineering for content creators and copywriters.
Prompt Engineering Projects & Real-World Applications
Twelve hands-on projects that turn prompt engineering theory into a portfolio. Build chatbots, content generators, RAG systems, and more.
Prompt Engineering for Image Generation
Turn words into stunning visuals. Master AI image generation tools like Midjourney, DALL·E 3, and Stable Diffusion with 18 focused tutorials — from first prompt to full brand identity.
Prompt Engineering for Education & Learning
Use AI as your personal tutor. Learn how to study faster, create lesson plans, generate practice questions, master languages, and prepare for competitive exams with smart prompts.
Prompt Engineering for Business & Productivity
Use AI to work smarter — automate tasks, make better decisions, and communicate professionally. 12 practical business prompt tutorials for professionals.