Prompt Patterns That Make Coding Agents Write Safer Code | Blog | Endor Labs
Endor Labs breaks down prompt engineering patterns that reduce vulnerabilities in AI-generated code: a design-spec pattern that front-loads security requirements, naming specific CWEs to avoid (e.g. CWE-89, CWE-79, CWE-20), and a "secure-insecure diff" recursive self-critique loop, while warning that telling a model to role-play a security expert actually performs worse. It cites research showing these patterns cut vulnerability density by up to 56–77.5%, notes that repeated AI iteration can drift toward less-secure code, and flags that AI coding agents frequently suggest hallucinated or vulnerable dependencies (34% hallucinated per Endor's own dependency research), arguing prompts must be paired with SAST/SCA/secrets scanning and reachability-based prioritization.
What it is
An Endor Labs blog post cataloguing concrete prompt patterns to make coding agents (Cursor, Copilot, Claude-based editors) emit fewer vulnerabilities, plus a mechanism the existing summary doesn't carry: rules files that apply the patterns automatically. The core framing is that in agentic editors the prompt is the design document and threat model — if security isn't in the prompt, it's usually absent from the code.
How it works
Four usable levers, with the studies attached:
- Design-Spec — structure the request as a filled-in template (Endor packages one), giving the model the same context a human reviewer expects. A 2025 benchmark (FORGE) found a security-focused prompt prefix cuts generated vulnerabilities by up to 56%.
- Anti-Pattern Avoidance — name the CWE classes to dodge (CWE-89, CWE-79, CWE-20). Concrete constraints beat "make it secure": a referenced study measured a 64% weakness-density cut on GPT-3 and 59% on GPT-4.
- Secure-Insecure Diff — a three-step generate → critique → revise loop; the critique step drives most of the 77.5% reduction because it forces the model to hunt flaws it just wrote rather than defend them.
- Rules files — encode the above so every prompt inherits it, via
CLAUDE.md,.cursorrules, or Copilot instruction files, so individual developers don't have to remember.
The patterns are meant to be stacked, not used in isolation.
What to do
- Reach for the Secure-Insecure Diff specifically on input handling, authentication, subprocess calls, and filesystem logic.
- Avoid the security-expert persona prompt — the Tony et al. study found role-playing a security expert produced the highest average number of weaknesses across tested models. Use structure (design-spec, CWE lists, recursive critique) instead.
- Seed your rules file from the OpenSSF guide for AI coding assistants (standards-based instructions) and OWASP secure coding with AI (covers hallucinated dependencies and rules files).
- Concrete phrasing shift: replace "Write a login form" with "Write a login form with input validation, parameterized queries, and secure session handling."
Caveats
The page states plainly that even solid patterns reduce risk without eliminating it — prompts are not a substitute for verifying what the agent ships. The metrics are all drawn from referenced third-party studies (FORGE, Tony et al., the 19.7% / 440,445-package hallucination research), not Endor's own testing, and the Design-Spec template is a vendor artifact.
Organisations named here
- CWE-20
- CWE-89
- slopsquatting
Technologies named here
- Endor Labs
- CLAUDE.md
- .cursorrules
- Copilot instruction files
- Claude Code
- Cursor
- GitHub Copilot
Techniques named here
- Design-Spec Pattern
- Anti-Pattern Avoidance Pattern
- Secure-Insecure Diff / recursive critique pattern
- Security-Expert Persona prompting
- SQL injection
- Cross-site scripting
- Slopsquatting
- Hardcoded secrets
- Weak input validation
- Architectural drift
- Improper Neutralization of Input During Web Page Generation
Related in AI Security
