Security-Aware Prompting

Bake security into prompts: demand input validation, safe secrets, least privilege, and a security pass, because AI defaults are insecure.

TL;DR

  1. AI code is insecure by default; make security an explicit, standing requirement in your prompts.
  2. Demand input validation, parameterized queries, safe secret handling, and least privilege.
  3. Run a dedicated security review pass; do not rely on the model to volunteer safety.

Assume Insecure

    Default Is Unsafe

    Treat generated code as needing hardening until proven safe.

    Works != safe. Harden before trust.
    Learned Bad Patterns

    Models echo insecure idioms common in public code.

    String-built SQL, secrets in code
    -> seen often, reproduced often.
    Make It Explicit

    Security happens when you require it, not by default.

    Unstated security = missing
    security.

Standing Requirements

    Validate Input

    Validate and sanitize everything from outside the trust boundary.

    "Validate all input; reject early;
    never trust the client."
    Safe Data Access

    Parameterized queries and encoded output, always.

    "Parameterized queries only; encode
    output to stop XSS."
    Secrets & Privilege

    Secrets in env/secret manager; least privilege everywhere.

    "No secrets in code or logs.
    Least-privilege tokens."

Threat-Focused Review

    Name The Threats

    Scope the security pass to the risks relevant to the code.

    "Check: injection, authz, SSRF,
    secret leakage."
    Exploit Scenario

    Require a concrete attack for each finding.

    "For each: the exact input that
    exploits it + a fix."
    Rank & Verify

    Worst-first, and confirm each before you change code.

    Reproduce, then fix. Don't fix
    phantoms.

Keep Humans & Tools

    First Pass, Not Final

    AI review speeds triage; a human owns the sign-off.

    AI finds common issues ->
    human decides.
    Run Scanners

    Back prompts with SAST and dependency scanning.

    Add SAST + `npm audit` to CI.
    Never Weaken Security

    Refuse fixes that disable checks to 'make it work'.

    No disabling TLS/auth to pass.
    Fix the real issue.

Tips

  1. Add a standing security block to your context file so every task inherits the rules.
  2. Name the threats that matter for the code at hand (injection, authz, SSRF) so the pass is focused.

Warnings

  1. Models reproduce insecure patterns from their training data; assume generated code needs hardening.
  2. Never let the model embed secrets, disable TLS checks, or log sensitive data to 'make it work'.

In Practice

FAQ