Skip to content

Back to the catalog

Anti-Pattern Avoidance: A Prompt Pattern for Safer AI-Generated Code | Blog | Endor Labs
endorlabs.comarticle

Anti-Pattern Avoidance: A Prompt Pattern for Safer AI-Generated Code | Blog | Endor Labs

Endor Labs describes the "anti-pattern avoidance" prompt pattern: explicitly instructing an LLM to avoid named CWEs (e.g., CWE-89, CWE-20, CWE-200) when generating code, rather than just asking it to "write secure code." It shows a before/after JavaScript form-handling example where naming the CWEs causes the model to add input validation/sanitization, and cites an academic study showing this reduced weakness density by 64% in GPT-3 and 59% in GPT-4 versus baseline prompts.

TL;DR Naming specific CWEs in the prompt (e.g., "avoid CWE-89, CWE-20, CWE-200") measurably reduces vulnerabilities in LLM-generated code.
Document

What it is

Endor Labs' "anti-pattern avoidance" prompt pattern — the third of a five-part series on secure code prompt patterns (the others: Design-Spec, Secure vs Insecure Diff, Toolchain-Aligned, Test-First). It is a zero-shot technique for steering LLM code generation away from specific CWE classes.

How it works

The summary states the outcome; the page gives the reusable template the summary does not. The pattern is two steps: (1) identify the relevant CWEs for the code you're generating, and (2) instruct the model to avoid them and apply known mitigations. The literal skeleton:

Generate secure <language> code that: <coding task>.
The code should avoid critical CWEs, including <list of relevant CWEs>.

The stated reasoning: LLMs pattern-match to the most common (often insecure) solutions in their training data, but can reproduce concrete mitigations reliably when explicitly cued toward named anti-patterns (e.g., unvalidated input, hard-coded secrets).

Preconditions

This requires more security knowledge than the diff pattern — the developer must already know which CWEs apply to the code being generated. If you can't name the relevant weakness classes, the pattern gives you nothing to put in the prompt.

What to do

Adopt the template above, substituting real CWE identifiers (the worked example uses CWE-89, CWE-20, CWE-200). Note the page frames this as one layer only: it explicitly recommends pairing prompting with other controls — "MCP servers for real-time security context and guardrails."

Caveats

The page states plainly that prompting alone isn't enough to ensure security of AI-generated code. Two context figures worth flagging (each stated, not verified in-page): 62% of AI-generated solutions are claimed to contain design flaws/vulnerabilities, and the 64%/59% weakness-density reductions come from a "recent academic study" that the post does not name or link. The before/after example only demonstrates that the model "added some basic input validation and sanitization" — a qualitative improvement, not a verified elimination of the named CWEs.

Organisations named here

  • CWE-20
  • CWE-200
  • CWE-89

Technologies named here

  • OpenAI GPT-3
  • Endor Labs
  • Model Context Protocol
  • OpenAI GPT-4
  • OpenAI
  • GPT-4
  • JavaScript

Techniques named here

  • Anti-pattern avoidance prompt pattern
  • Injection attacks