Blog · AI security

Securing AI-generated code: a developer's roadmap for AI-assisted development

What goes wrong when code is written with AI assistants and coding agents, from hallucinated packages to leaked secrets, and the checks, pipeline gates and review habits that keep AI-generated code safe to ship.

AI assistants are now part of everyday development, and coding agents can edit files, run tests and open pull requests on their own. Building by prompt, often called vibe coding, has made prototypes fast. It has also made it easy to ship code nobody has read closely.

This roadmap folds together our earlier posts on AI-generated code and secure coding. It covers what goes wrong, what to check and where to put the checks.

What goes wrong in AI-generated code

The flaws are mostly the classics. What changes is volume and speed: more code, written faster, reviewed less.

FlawHow it appearsThe fix
InjectionUser input concatenated into SQL, shell commands or templatesParameterised queries, contextual output encoding
Unsafe deserialisationPython pickle or similar used on data from clientsSafe formats such as JSON, with schema validation
Hard-coded secretsAPI keys pasted into source "just to make it work"A secrets manager or environment variables, plus secret scanning
Missing access controlEndpoints that check login but not permissionAuthorisation on every route, tested explicitly
Weak session settingsCookies without HttpOnly, Secure or SameSiteSecure defaults in the framework configuration
Invented dependenciesImports of packages that do not existVerify every new package before install

Hallucinated packages: a supply-chain risk

Code models sometimes suggest packages that do not exist. A USENIX Security 2025 study of package hallucinations found that both commercial and open-source models invent package names, in Python and in JavaScript, and that mitigations reduce the problem but do not remove it. An attacker who registers one of those invented names can have malicious code installed by anyone who trusts the suggestion. The tactic is often called slopsquatting.

The check: before installing a new dependency, confirm it exists, has a real history and maintainer, and is the package you meant. Pin versions and use a lockfile.

Coding agents are privileged agents

An assistant that suggests code is one thing. An agent that runs commands, installs packages and pushes branches has the access of a developer, and reads untrusted text all day: issues, READMEs, dependency code, web pages. Instructions hidden in any of these can steer it. This is prompt injection, ranked first in the OWASP Top 10 for LLM Applications.

  • Run coding agents in a sandbox or container, not on a machine holding production credentials.
  • Give them short-lived, narrowly scoped tokens, never a personal access token with full rights.
  • Require a person to approve merges, deployments and anything that touches secrets.
  • Keep a record of the commands the agent ran, not just the diff it produced.

Shift left: checks on every commit

Automate the checks so they run whether or not anyone remembers. Open-source tools cover most of it:

  • Static analysis on every pull request, for example Semgrep or language linters with security rules.
  • Secret scanning before commit and in CI, for example Gitleaks or TruffleHog.
  • Dependency and container scanning, for example OSV-Scanner or Trivy.
  • Quality gates in CI/CD that block a merge when a high-severity finding is open.

Tell the assistant what secure means

Most assistants read a project instruction file. The OpenSSF Security-Focused Guide for AI Code Assistant Instructions shows how to write one: state secure coding rules up front, ask the assistant to review its own output for flaws, and cover dependencies explicitly. OWASP also publishes a Secure Coding with AI Cheat Sheet.

Useful review prompts at each checkpoint:

  • Before commit: "Check this change for secrets, unvalidated input and hard-coded configuration."
  • Before merge: "List every new dependency and permission this change adds."
  • Before deploy: "Which endpoints lack authorisation checks?"

An AI review supplements human review; it does not replace it.

A pre-ship checklist

  • Is all user input validated, and all output encoded for its context?
  • Are secrets out of the repository and its history?
  • Is authorisation enforced on every route, and tested?
  • Does every dependency exist, come from a trusted source and have a pinned version?
  • Do errors avoid leaking stack traces or internal data?
  • Are static analysis, secret scanning and dependency scanning running in the pipeline?
  • Has a person read the logic, not just the style?

Where to start

Pick one repository where AI assistants are used heavily. Turn on secret scanning and dependency checks, add a project instruction file, and sandbox any coding agent. Then widen it to the rest.

When AI features are part of the product itself, test them as an AI system as well as an application. The ZySec AI Assurance Lab does that agent and application testing, and our guide to AI security assessment sets out what it covers.

All posts