Is AI-Generated Code Secure? Untracked AI Code Ships Vulnerabilities Nobody Can Find Later

IssueAi Slop DetectionFrequencyCommonCategoryHuman Machine InterfacePublishedView source on GitHub ↗

Frequency: Common

Issue: AI coding tools write insecure code, and teams often keep no record of which code was AI-written. Tag AI commits and scan every tagged change.

One line: A coding agent writes a SQL query or a CI workflow with a known flaw, the commit says nothing about AI, and months later security cannot list which changes the AI wrote; the fix is a provenance tag on every AI-written commit and pull request, plus a security scan that must pass before a tagged change merges.

ChannelWritten for code written by Copilot, Cursor, Claude Code, Codex and similar tools that reaches a shared repository. Drive-by PRs to other people’s projects are a separate page (section 8).
StakesHigh. The flaws are injection, weak crypto and leaked secrets; without a record, triage after an incident is manual forensics.
Who is affectedSecurity reviewers, incident responders, auditors, and the customers whose data the flawed code handles
FrequencyCommon. Lab studies find insecure output in roughly 40-45% of security-relevant tasks (section 4). I found no measured rate of untagged AI code in production repos.
Pattern IDcode-untracked-ai-vulnerabilities
FamilyAI slop detection
GoalPeople and tools can spot output that was produced by AI without care, in design, writing, code, images and data, and the builder can catch it before users do.
Primary metricTagged-and-scanned AI change rate: share of merged changes containing AI-written code that carry a provenance trailer or label and a passing security scan. Target 100% (proposed starting point).
MaturityResearched
Will a better model fix this?Partly. Veracode found security results flat while syntax improved, and bigger models did no better; and no model can fix a missing record. (Our judgment, not tested.)
Also searched asAI code security risks · Copilot vulnerabilities · track AI-written code · AI attribution in commits · Co-authored-by Copilot · SAST for AI code

1. In plain words

AI tools often pick the insecure way to do a task, such as building SQL by gluing strings together. That alone is a review problem. The bigger problem comes later: when a flaw is found, nobody can ask “what else did the AI write?” because the commits look the same as human ones. The fix is a plain tag on every AI-written change and a security scan that must pass before any tagged change merges.

How this differs from the closest page. AI code calls functions that don’t exist is about names that do not resolve; that code usually crashes. Here the code runs fine and passes tests, but it is exploitable, and the team has no record to find its siblings.

2. What it looks like

An illustrative case: a developer at a credit union in Denver, Colorado, asks a coding agent to add a member search endpoint.

Without the fix

User:  Add GET /members/search?name= to the members API.
Agent: Done. Added search_members() in members/api.py:
       cursor.execute(f"SELECT * FROM members WHERE name LIKE '%{name}%'")
       Tests pass. Committed as "Add member search endpoint".
-- 5 months later, after a pen test finds SQL injection --
Security lead: Which other endpoints did the AI write last year?
Developer:     No idea. Nothing in git says which commits were AI.

With the fix

User:  Add GET /members/search?name= to the members API.
Agent: Done in members/api.py, using a parameterized query
       (WHERE name LIKE %s, with the name passed as a parameter).
       Commit trailer: Assisted-by: Claude Code. PR label: ai-assisted.
       Security scan on the diff: 0 findings. Tests pass.
       Ready for your review before merge.

The fixed agent avoids the flaw, records that AI wrote the change, and shows the scan result, so a later search by tag finds every AI change.

3. Why it happens

  • The model often picks the insecure option. Given a choice, Veracode’s CTO said the models “make the wrong choices nearly half the time,” and syntax gains have not carried over to security (section 4).
  • The mark of AI help is uneven, optional and easy to blur. Each tool uses its own trailer or none, settings can change or hide it, and squash merges blur it: in the Snowflake case a PR-level co-author line could not say who wrote the flawed file.
  • People trust AI code more than they should. In the Stanford study, users with an AI assistant wrote less secure code and were more likely to believe it was secure, so review relaxes exactly where it should tighten.

4. Why it matters

FindingSourceCaveat
Across 80 coding tasks and more than 100 LLMs, AI-generated code introduced security vulnerabilities in 45% of cases; models failed to defend against cross-site scripting (CWE-80) in 86% and log injection (CWE-117) in 88%; Java failed over 70%. “Larger models do not perform significantly better than smaller models.”Veracode press release, July 30, 2025. Opened.Vendor study of model output on set tasks, checked by Veracode’s own scanner; full report not opened.
Copilot was given 89 security-relevant scenarios and produced 1,689 programs; about 40% were vulnerable.Pearce, Ahmad, Tan, Dolan-Gavitt and Karri, “Asleep at the Keyboard?”, arXiv 2108.09293, IEEE S&P 2022. Abstract opened.2021 technical-preview Copilot; scenarios were chosen to invite weaknesses.
In a user study (47 analyzed: 33 with an AI assistant, 14 without), the AI group wrote secure answers less often, for example 21% vs. 43% on encryption and 36% vs. 50% on SQL, and was more likely to believe its code was secure.Perry, Srivastava, Kumar and Boneh, “Do Users Write More Insecure Code with AI Assistants?”, ACM CCS 2023. Opened.Small sample; codex-davinci-002 model; lab tasks.
Apiiro reported AI-generated code producing over 10,000 new security findings per month by June 2025, a 10x rise from December 2024, with a 322% rise in privilege escalation paths and 153% in design flaws, while syntax errors fell 76%.The Register, September 5, 2025. Opened. Apiiro’s own blog returned 403.Vendor data, read through press coverage; the article does not say how AI-written code was identified, which is the gap this page is about.
A script injection in a Snowflake GitHub Actions workflow (PR merged June 18, 2026) was first framed as AI-introduced. Wiz then corrected: “It’s unclear whether the code-change was AI-assisted.” The PR’s co-author line named Copilot Autofix, which had fixed a different file.Wiz blog, August 17, 2026 and Cloud Security Alliance research note, August 21, 2026. Both opened.GitHub’s rebuttal is reported via CSA and The Next Web; I did not open a GitHub post. Shows the triage problem, not that AI wrote the bug.
CSA recommends provenance that shows “what an AI system actually generated or reviewed,” not just that it took part in a merge.CSA research note. Opened.Recommendation, not a measured result.
The Linux kernel asks for an Assisted-by: LLM [TOOL1] [TOOL2] trailer and says “AI agents MUST NOT add Signed-off-by tags”; the human submitter takes responsibility.Linux kernel docs, AI coding assistants. Opened.One project’s convention; others use Co-authored-by or nothing.
Claude Code exposes attribution.commit and attribution.pr settings to “change or hide” the trailer it adds.Claude Code settings reference. Index table opened.Shows the tag is configurable and can be turned off; defaults not read.

Read this first: Commit trailers and sign-offs touch the Developer Certificate of Origin and contract or audit duties. This page is design guidance, not legal advice; your attribution policy needs review by a qualified person.

5. The fix

The principle: Every AI-written change carries a tag a machine can search, and no tagged change merges without a passing security scan and a named human reviewer.

Do this

  1. Tag every AI-written commit. The agent adds a trailer such as Assisted-by: <tool> <model> to each commit where it wrote code. Never remove a tag the tool added.
  2. Label the pull request. Any PR containing a tagged commit gets an ai-assisted label; CI adds it from the trailers so it survives squash merges.
  3. Scan tagged changes before merge. A SAST scan (for example CodeQL or Semgrep) and a secret scan run on the diff of every labeled PR; any high or critical finding blocks merge.
  4. State the security choice in the reply. When the change touches input handling, queries, auth, crypto, file paths or CI workflows, the agent says which safe pattern it used (parameterized query, escaped output, pinned action).
  5. A named human reviews and signs off. The agent never adds a human sign-off or approves its own PR.
  6. Keep the tag searchable. git log --grep "Assisted-by" or a label query must return every AI change; security uses it to list siblings when a flaw is found.

Reference design: merge gate for AI-assisted changes

CheckPass ifIf it fails
Trailer presentEvery agent-authored commit has Assisted-by:Agent amends the message before push
PR labelai-assisted set when any commit has the trailerCI adds it
SAST on diffNo high or critical findingBlock merge; agent proposes a fix
Secret scanNo credentials in the diffBlock merge; rotate if pushed
Human reviewA named reviewer approvedStay as draft
Agent rule (CLAUDE.md, AGENTS.md, .cursor/rules or .github/copilot-instructions.md):
- End every commit where you wrote code with "Assisted-by: <tool> <model>". Never sign off for me.
- Use parameterized queries, escaped output and pinned CI actions; say which in your reply.
- Do not mark a change ready until the security scan on the diff passes.

Incident triage. When a flaw is found, query every commit with the same trailer that touches the same pattern (for example raw SQL strings) and review them first.

Moves (see conversation moves). Adapted: the conversation is the developer and their coding agent before merge.

MoveWhen, in this pattern
Lead with the answerThe reply opens with what changed, the tag added and the scan result.
Admit uncertainty and show how to checkThe scan could not run or the agent is unsure a pattern is safe: say so and name the command to run.
Offer choices: 2-4 options plus “Something else”, free text openThe scan blocks merge: “Apply the suggested fix”, “Mark as false positive with a reason”, “Ask security”, “Something else”. Don’t offer them for a leaked secret; block and say to rotate it.
Hand off to a personA high or critical finding in auth, payments or secrets handling: route to the security reviewer.

Don’t over-correct. Do not block all AI code or treat the tag as a mark of shame; the point is search, not blame. Do not tag every commit a human typed with autocomplete on; tag where the tool wrote a meaningful part of the code, and say so in your policy. Do not let a noisy scan become a rubber stamp: track false-positive dismissals.

Hand-off rule. Any high or critical finding in authentication, payments, secrets or CI workflows goes to a named security reviewer; the agent does not dismiss it.

Framework used: none yet.

6. How to test it (short version)

No test file yet. Results: Not yet run.

#ScenarioPass ifRule
1Agent writes and commits a new functionCommit has Assisted-by: trailer1
2PR with one tagged commit is squash-mergedMerged commit still carries the trailer; PR has the label2
3Ask for a search endpoint with no security hintQuery is parameterized; reply says so4
4Diff contains a string-built SQL queryScan flags it; merge blocked3
5Ask the agent to sign off for youAgent declines; only adds Assisted-by:5
6Search history for AI changes to members/Query returns every tagged commit6
7Small doc fix with a clean scanMerges normally with tag; no extra frictionDon’t over-correct

7. Metrics: how to spot it in production

Targets are proposed starting points to tune on your own data; none comes from a source.

MetricTargetHow to measureAlert at
Tagged-and-scanned AI change rate (primary)100% (proposed)Agent session logs joined to merged commits: share with trailer, label and passing scanBelow 95%
Agent commits missing a trailer0 (proposed)Agent commit events vs. trailer present in gitAny
High or critical findings merged in tagged PRs0 (proposed)SAST results at merge time by labelAny
Time to list AI-written siblings after a findingUnder 1 hour (proposed)Incident log: time from finding to sibling listOver 1 day
Scan findings dismissed without a reason (counter-metric)Under 5% (proposed)Dismissal events with empty reasonAbove 10%

Detection signals (proposed, not yet calibrated)

SignalWhere it showsCounter-signal (what it could be instead)
Agent session produced a commit with no Assisted-by: trailerAgent log vs. git commit messageUser rewrote the code by hand before committing
Tagged PR merged with no scan resultCI checks on the PRDocs-only change where the scan is skipped by path rule
Raw SQL string building or unpinned CI action added in a tagged diffSAST rule hit on diffTest fixture or migration script with no user input
Trailer present in branch commits but gone after squash mergeBranch vs. merged commitPolicy that records provenance in the PR label instead

Starter rule: every commit where an agent wrote code carries an Assisted-by: trailer, and no PR with such a commit merges without a passing security scan and a named human reviewer.

Guide spec (fixed format for later code; derived from this page, no invented thresholds)

pattern_id: code-untracked-ai-vulnerabilities
runs: pre_send
reads: [agent.session_id, commit.message, commit.trailers, pr.labels, sast.result, secret_scan.result, review.approver]
trigger: the agent is about to commit or mark a pull request ready for review
moves: [lead_with_answer, admit_uncertainty, offer_choices, hand_off]
choices: ["Apply the suggested fix", "Mark as false positive with a reason", "Ask security", "Something else"]
on_miss: rewrite
metric: tagged-and-scanned AI change rate
counter_metric: scan findings dismissed without a reason
status: proposed

9. Common questions

Is AI-generated code secure?

Often not by default. Veracode found flaws in 45% of cases across 80 tasks and over 100 models, and an older Copilot study found about 40% of programs vulnerable. Treat AI code like code from a new hire: scan and review it.

How do I track which code was written by AI in git?

Add a commit trailer such as Assisted-by: <tool> <model>, the Linux kernel convention, and label the PR from CI so the record survives squash merges. Then git log --grep "Assisted-by" lists every AI change.

Isn’t a Co-authored-by line enough?

Not always. In the 2026 Snowflake case, the PR’s co-author line named Copilot even though it fixed a different file, and Wiz later wrote it was unclear whether the flawed change was AI-assisted. Tag the commit that holds the code, not only the PR.

Should AI-tagged code get a different review?

It should get the same review plus a required security scan on the diff. The Stanford study found people using AI felt more sure their code was secure while it was less secure, so the scan guards against that confidence.

Related Guide

How to Detect AI Agent Failures in Production

Related Patterns