Skip to content
Agent Month

AI code security

Last verified: June 2026· definition

As agents write more code, security must cover new surfaces: vulnerabilities and hallucinated dependencies in generated code, sensitive data leaking into prompts, and prompt-injection attacks against agents that read external content and call tools.

The effective controls are mostly about making the safe path the easy path — automated, AI-aware scanning in CI, redaction of secrets and PII at the boundary, scoped tool permissions with audit logs, and private inference where data can’t leave the building.

Compliance plus AI tends to get budget in any environment, which makes this a durable area of investment.

Two distinct risk surfaces

AI code security covers problems that are easy to conflate and need different controls. The first is the code itself: vulnerabilities, insecure defaults, licence contamination, and secrets in AI-generated output. The second is the AI system’s behaviour: prompt injection through untrusted content, over-scoped agent permissions, and data leaking into prompts sent to third parties. Conventional application security tooling addresses the first and says nothing about the second.

Volume changes the review economics

When code volume rises and much of it was machine-written, review becomes the binding constraint. Machine-written code is almost always structurally sound, which makes reviewers more likely to skim it, and skimming is precisely where subtle issues survive. The practices that hold up are requiring test evidence, keeping changes small enough to read properly, and automating the checks that do not need judgement so human attention goes where it is actually needed.

Data leaving the boundary

A distinct concern is what your prompts contain. Source code, customer records, and credentials pasted into a prompt leave your environment for a third party, and whether that is acceptable depends on contracts, retention terms, and jurisdiction. This risk is invisible to code scanning entirely, since nothing in the repository reveals it — it needs policy, redaction at the gateway, and awareness of what developers are actually sending.

Point-in-time audits age immediately

A one-off review reflects the codebase on the day it ran. For teams shipping AI-assisted code continuously, that snapshot is stale within a sprint. The durable version leaves automated controls behind — scanning in CI, secret detection, dependency policy, and permission review for agent tools — so the same class of problem is caught without being rediscovered manually each time.

Licence and provenance risk

Generated code can reproduce patterns from training data closely enough to raise licence questions, and dependencies suggested by a model are not vetted by anything. Both are ordinary supply-chain concerns that AI assistance makes more frequent rather than novel: dependency scanning, licence policy in CI, and a check that suggested packages actually exist and are maintained address most of it. Hallucinated package names are a particular case worth catching automatically, since installing one is a real risk rather than a theoretical one.

Common misconceptions

  • MythAI-generated code is inherently less secure than human code.

    RealityThe failure modes overlap heavily with familiar ones. What changes is volume and review depth — more code, reviewed less carefully, which is a process risk rather than a property of the generator.

  • MythA code scanner covers AI code security.

    RealityScanners address vulnerabilities in the code. They do not address prompt injection, agent permission scope, or sensitive data leaving in prompts, which are the risks specific to AI systems.

  • MythPrompt injection is a model provider problem.

    RealityProviders can reduce susceptibility, but what a tool call is permitted to do is decided by your infrastructure. That control point is yours regardless of the model.

Frequently asked questions

What is different about securing AI-generated code?

Less than the phrase suggests at the code level, and more at the system level. The vulnerability classes are familiar, but the volume is higher and review tends to be shallower because the output looks polished. The genuinely new surface is the AI system itself — prompt injection through content the agent reads, agents holding broader permissions than needed, and sensitive data travelling in prompts to third parties. Traditional appsec covers the first area well and the second not at all.

How do I stop secrets ending up in prompts?

Handle it at the boundary rather than relying on developer discipline. Redaction at a gateway catches credentials and personal data before requests leave your environment, and it works regardless of which tool or person originated the call. Pair that with a clear policy on what may be sent to which providers, informed by your actual contractual and retention terms rather than assumptions about them.

Should we block AI coding tools for security reasons?

Blocking tends to move usage out of view rather than stopping it, which is worse than governing it. The more effective approach is providing an approved path — sanctioned tools, a gateway that handles redaction and logging, and clear guidance on what may be shared — so the compliant option is also the convenient one. Prohibition without a supported alternative reliably produces unmonitored usage.