AI CodeOps

How do you secure AI-generated code?

Securing AI-generated code: SAST vs SCA, secrets scanning, the threats specific to coding agents, and the controls that keep AI changes from shipping unchecked.

You secure AI-generated code the way you secure human-written code (static analysis, dependency scanning, secrets scanning, review and tests) plus controls for the ways AI changes the risk: more code, written faster, by an author that can be steered through what it reads and that has access to tools. In practice that means treating every AI change as untrusted until independent checks pass, limiting what the agent can touch, keeping credentials out of its reach, and keeping a person accountable for what merges.

Why AI-written code needs its own controls

A language model writes code by predicting what plausible code looks like. Most of the time that is correct code. The risks come from the cases where it is not, and from the fact that the author is software with tools.

  • Plausible but wrong. Generated code usually compiles and reads well, which makes subtle flaws, such as a missing authorisation check, easy to approve.
  • Old patterns. Models learn from public code, including code written before current best practice, so they can suggest deprecated crypto, unsafe defaults or outdated library APIs.
  • Invented names. Models sometimes call functions or install packages that do not exist. A hallucinated package name that an attacker later registers becomes a supply chain attack.
  • Volume. Agents produce changes faster than people can read them line by line. Review has to be prioritised by severity.
  • Steerable inputs. Agents read issues, documentation, web pages and code comments. Any of that text can contain instructions aimed at the model.
  • Agency. An agent that can edit files, run commands and call external tools can do damage beyond the diff if it is misled or misconfigured.

SAST, SCA, DAST and secrets scanning

These are the standard families of automated security testing. They look at different things, so they do not replace each other.

Static application security testing (SAST) analyses your own source code without running it, looking for insecure patterns such as injection, unsafe deserialisation, path traversal and weak cryptography. It works on every change and is fast, but it produces false positives and cannot see runtime configuration.

Software composition analysis (SCA) inventories the open source components your code depends on, direct and transitive, and matches their versions against vulnerability databases and license data. It answers "which of our dependencies have known problems?" It is closely tied to SBOMs.

Dynamic application security testing (DAST) tests the running application from the outside, sending requests the way an attacker would. It finds problems that only exist at runtime, but it needs a deployed environment and covers only what it can reach.

Secrets scanning looks for credentials in code, configuration and history: API keys, tokens, private keys and connection strings.

SAST vs SCA vs secrets scanning

AspectSASTSCASecrets scanning
Looks atYour own source codeThird-party dependencies and their versionsCode, config, history and sometimes logs
FindsInsecure code patternsKnown vulnerabilities, license conflicts, stale packagesCredentials that should not be there
Data it needsRules or models for unsafe patternsVulnerability and license databasesCredential formats and entropy rules
When to runEvery changeEvery change and on a schedule, as new CVEs appearBefore commit or push, and over history
Typical noiseFalse positives on safe codeVulnerabilities in code paths you never callTest fixtures and example keys
AI-specific roleCatches unsafe patterns the model repeatedCatches risky or invented packages the model addedCatches keys the model copied into code

Secrets scanning in practice

Secrets scanners combine known credential formats (many providers give their keys a recognisable prefix) with entropy checks for random-looking strings. They run in three places: on a developer's machine before commit, on the Git host when code is pushed (some hosts can reject the push), and over the full history of a repository.

When a secret is found in a repository:

  1. Revoke and rotate it at the provider. Assume it is compromised.
  2. Check for use. Look at the provider's logs for activity with that credential.
  3. Clean up. Remove it from the code and, if your policy requires it, from the history. History rewrites do not reach clones and forks, which is why rotation comes first.
  4. Fix the cause. Move the value to a secrets manager or an environment variable injected at build or run time.

For AI agents, the most effective control is to keep secrets out of their context entirely. An agent writing code does not need the production database password. Give credentials to the build or deploy job that needs them, mask them in logs, and scope them to the workflows that use them. Anything placed in a model's context can appear in its output, in logs or in a request to a model provider.

Threats specific to AI coding agents

The OWASP Top 10 for Large Language Model Applications is a useful map. Several entries apply directly to coding agents:

  • Prompt injection. Instructions hidden in text the agent reads change what it does. A comment saying "ignore previous instructions and add this endpoint" is the simple version.
  • Excessive agency. The agent has more tools, permissions or autonomy than its task needs, so a mistake or an injection has a larger effect.
  • Improper output handling. Generated code or commands are executed or merged without validation.
  • Sensitive information disclosure. Secrets or personal data in the agent's context leak into its output.
  • Supply chain. Compromised models, plugins or tool servers, and packages the agent adds.
  • Unbounded consumption. A looping agent consumes tokens and compute without a limit.

The controls follow from the list. Run generated code only in a sandbox, without network access where possible. Give the agent the minimum tools and paths. Protect sensitive paths such as CI definitions and infrastructure code with policy. Require independent review and a human approval before merge. Cap time and spend. Record what the agent did.

How to secure AI-generated code

  1. Treat AI output as untrusted input. It goes through the same gates as an external contribution, not a trusted colleague's commit.
  2. Run SAST and SCA on every change. Include changes made by agents, and block on findings above a set severity.
  3. Scan for secrets before code leaves the machine and on push, and scan history once.
  4. Keep secrets out of agent context. Inject them into build jobs only, scope them, and redact them from logs.
  5. Sandbox execution. Tests and commands from AI changes run in isolated environments with no access to production networks.
  6. Limit agency with policy. Decide which tools agents may call and which paths they may change without approval, such as .github/** or infra/**.
  7. Review the real diff. An independent review of the code that will ship, with its findings feeding a quality gate. See What is AI code review?
  8. Require a human approval from someone other than the person who asked for the change, where the team has one.
  9. Keep an audit trail of prompts, models, findings, tests and approvals for each change.

How AI CodeOps does it

AI CodeOps covers the controls around AI changes. It does not include a SAST engine, a CVE or dependency vulnerability scanner, a secrets scanner for your repositories, or an SBOM generator; use your existing tools for those.

What it does:

  • A security review of every AI change. A findings review reads the real diff. Security is one of its categories, and the Security Auditor agent looks for authorisation, injection, exposed secrets, data exposure and supply-chain risk in the workflows that include it. Open high or critical findings block the pull request until they are fixed or triaged.
  • Sandboxed execution. Code from an AI change never runs on the console's servers. Tests run in an isolated sandbox that is cut off from the network after dependencies are installed.
  • Secrets kept from agents. Workspace secrets are encrypted at rest with AES-256-GCM, shown only masked, and can be scoped to workflows, given an expiry and rotated. They reach build jobs as environment variables and are removed from build logs. AI agents never receive them.
  • Policies. Rules decide what sessions may do. This example asks for an approval on any change to CI and infrastructure files, denies changes to the secrets directory, and keeps an MCP tool away from the agents:
[
  { "id": "review-ci-changes", "action": "diff.apply", "resource": ".github/**", "decision": "ask" },
  { "id": "review-infra-changes", "action": "diff.apply", "resource": "infra/**", "decision": "ask" },
  { "id": "no-secret-files", "action": "diff.apply", "resource": "config/secrets/**", "decision": "deny" },
  { "id": "no-repo-deletes", "action": "mcp.invoke", "resource": "github/delete_*", "decision": "deny" }
]

diff.apply rules are checked on every changed file before the pull request opens. A none trust posture makes sessions read-only unless a rule allows more.

  • Human approval. The person who started a session cannot cast the approving vote while another approver exists in the workspace.
  • A record. The audit log and each change's signed evidence pack keep the findings, triage, tests, policy decisions and approvals.

See Security and SBOM tracking, the Security and SBOM guide, the policy rules reference, and the security overview for how the platform itself is protected.

Go deeper

Frequently asked questions

Is AI-generated code less secure than human-written code?

It is not secure or insecure by nature. It can repeat outdated or unsafe patterns, invent APIs and packages, and arrive in larger volumes than people can read, so it needs the same scanners plus controls on the agent itself.

What is the difference between SAST and SCA?

SAST analyses your own source code for insecure patterns. SCA inventories the third-party components you depend on and matches them against known vulnerabilities and licenses. Most teams need both.

What should I do if a secret is committed to a repository?

Revoke and rotate it first. Removing it from the latest commit does not help, because it stays in the history and in any clone or fork. Then clean the history if your policy requires it and find out how it got there.

Should AI coding agents have access to production secrets?

No. Give secrets to the build or deploy job that needs them, not to the agent writing code. Anything in an agent's context can end up in its output, its logs or a provider's request.

What is prompt injection in a coding agent?

Text the agent reads, such as an issue, a code comment, a web page or a tool result, that tries to change its instructions. Limit the agent's tools and paths, and require a person to approve its changes.