0

Example — Security-Focused Code Review

Demonstration of prompts/dev/security-review.txt with the variables filled in. Illustrative example of what such a prompt can produce. The response was generated by an AI in one pass — not guaranteed, and not a substitute for a professional audit. The

Prompt · برومبت

Works with

claude-code (compatible), claude (compatible), codex (compatible), chatgpt (compatible), gemini-cli (compatible), cursor (compatible), copilot (compatible), opencode (compatible), amp (compatible), roo (compatible), kiro (compatible), goose (compatible), junie (compatible), trae (compatible), cline (compatible), continue (compatible), antigravity (compatible), windsurf (compatible), augment (compatible), qwen-code (compatible), aider (compatible)

Content

Example — Security-Focused Code Review

Demonstration of [prompts/dev/security-review.txt](../prompts/dev/security-review.txt) with the variables filled in. Illustrative example of what such a prompt can produce. The response was generated by an AI in one pass — not guaranteed, and not a substitute for a professional audit. The code is a deliberately small toy endpoint.

Prompt (filled)

You are an application security engineer doing a defensive review of MY code. You think like an attacker only to protect: your job is to find where my code can be abused and show me the fix, in priority order — not to lecture.
--- CODE START ---
```python
@app.post("/orders")
def create_order(item: dict):
qty = item["qty"]
name = item["name"]
db = sqlite3.connect("shop.db")
cur = db.cursor()
cur.execute(f"INSERT INTO orders (product, qty) VALUES ('{name}', {qty})")
db.commit()
total = PRICE_LIST.get(name, 0) * qty
send_mail(ADMIN, f"new order: {name} x{qty} = {total}",
password=SMTP_PASSWORD)
return {"total": total}
```
--- CODE END ---
- Stack and where this runs: FastAPI + SQLite, exposed as public API behind nginx
- Who can reach it and what auth protects it: JWT session, any registered user
- What sensitive data flows through: user email, order total, saved shipping address
YOUR TASK — answer in this exact order:
*(verdict → findings table → fixes → hardening → what's already good)*

Example AI response

1. VERDICT

Not safe to ship. The endpoint concatenates user input directly into SQL — any registered user can read or destroy the entire database — and it trusts a client-supplied payload for both product name and quantity. Fix findings 1 and 3 before this sees traffic.

2. FINDINGS

| Severity | Location | Issue | Why it's exploitable | |---|---|---|---| | Critical | cur.execute(f"INSERT…") | SQL injection — name and qty are formatted straight into the query | A registered user POSTs {"name": "x'); DROP TABLE orders;--"} and wipes the orders table; with SQLite stacked queries disabled they can still exfiltrate or corrupt data via crafted expressions | | Critical | password=SMTP_PASSWORD | Hardcoded credential reaching a mail send | If SMTP_PASSWORD is a literal in this file (or any committed module), it's in git history and every checkout — anyone with repo access can send mail as your domain and it will be flagged by secret scanners | | High | total = PRICE_LIST.get(name, 0) * qty | Price and quantity taken from unvalidated input; qty is never type/range checked | qty=-5 yields a negative total and could corrupt downstream billing; qty=10**9 memory/CPU abuse; unknown products silently price at 0 | | Medium | create_order error path | Raw exceptions bubble to the client | SQLite/Python tracebacks leak table names and schema details to any registered user |

3. FIXES

from pydantic import BaseModel, Field

class OrderIn(BaseModel):
    name: str = Field(max_length=100)
    qty: int = Field(ge=1, le=100)          # server-side range validation

@app.post("/orders")
def create_order(item: OrderIn):
    if item.name not in PRICE_LIST:
        raise HTTPException(404, "unknown product")   # fail loudly, not silently 0
    total = PRICE_LIST[item.name] * item.qty          # price decided server-side
    db = sqlite3.connect("shop.db")
    cur = db.cursor()
    cur.execute("INSERT INTO orders (product, qty) VALUES (?, ?)",   # parameterized
                (item.name, item.qty))
    db.commit()
    send_mail(ADMIN, f"new order: {item.name} x{item.qty} = {total}",
              password=SMTP_PASSWORD_FROM_ENV)        # from os.environ, rotated
    return {"total": total}
  • The parameterized query (? placeholders) makes injection structurally impossible regardless of input content.
  • Move the SMTP password to an environment variable, rotate the current value (it must be considered burned), and add a global exception handler that returns a generic 500 while logging details server-side only.

4. HARDENING

  • Add a per-user rate limit on /orders (e.g. slowapi) — order endpoints are spam magnets.
  • Return 201 with an order id instead of echoing the computed total; let clients fetch state from a GET endpoint.
  • Add an integration test that POSTs a payload containing ' ; -- and asserts a clean 400/404 — this is your regression guard for finding 1.
  • Pin and audit dependencies (pip-audit) — SQLite drivers and mail libraries get CVEs too.
  • Log order creation with user id (from the JWT), never request bodies — bodies may contain addresses.

5. WHAT'S ALREADY GOOD

  • The route does one thing and is small enough to review in a single screen.
  • Price computation lives server-side (conceptually right) — it just needs the client-supplied product name validated against PRICE_LIST before use.

--- *Generated for demonstration as part of Prompt Stash. Outputs vary by model and run. This example reviews fictional code; run real reviews only on code you own or are authorized to test.*

Source and license

Source
Prompt Stash
Original
Ziadtareks/prompt-stash/examples/dev-security-review.md
License
MIT