Security for Legal SaaS

Episode 51 · Module 10 · Infrastructure

Trusting the AI Developer — Verifying Agent-Generated Code

19 May 2026 · 7:59 · Security for Legal SaaS

7:59 7:59

AI coding agents — tools like GitHub Copilot, Cursor, Claude Code, and Amazon Q Developer — have fundamentally changed how software is written. Developers describe what they want in natural language, and an AI generates the code. For legal tech teams building contract review tools, case management systems, and e-filing integrations, these tools accelerate development dramatically. But they introduce a threat model that traditional security practices never anticipated: the code writer itself is a source of risk.

Today’s Lesson

Security for Legal SaaS — Episode 51: Trusting the AI Developer — Verifying Agent-Generated Code

When Your Developer Is a Probabilistic Model

AI coding agents — tools like GitHub Copilot, Cursor, Claude Code, and Amazon Q Developer — have fundamentally changed how software is written. Developers describe what they want in natural language, and an AI generates the code. For legal tech teams building contract review tools, case management systems, and e-filing integrations, these tools accelerate development dramatically. But they introduce a threat model that traditional security practices never anticipated: the code writer itself is a source of risk.

Unlike a human developer who might introduce bugs through carelessness or ignorance, an AI agent can generate code that looks syntactically perfect, passes superficial review, and contains subtle security flaws — because the model is optimising for plausible-looking code, not provably correct code.

Research from Endor Labs analysing vulnerabilities in AI-generated code found recurring patterns: missing input validation, improper error handling that leaks internal state, hardcoded credentials in example code that gets shipped to production, and insecure default configurations.1

Hallucinated Dependencies: The Slopsquatting Threat

The most novel risk from AI-generated code is "slopsquatting" — a term coined to describe what happens when AI models hallucinate package names that don't exist, and attackers register those names with malicious code.

A University of Texas at San Antonio study analysing 576,000 code samples generated by 16 different large language models (LLMs — the AI systems behind tools like ChatGPT and Copilot) found that nearly 20% of package dependencies referenced by AI don't actually exist.2 That's 205,474 unique hallucinated package names across the study — each one a potential attack vector.

The attack pattern: An AI tells a developer to install flask-auth-helper. The package doesn't exist on PyPI (Python's package registry — the central repository where Python libraries are published and downloaded). An attacker registers flask-auth-helper on PyPI with malicious code. The next developer who follows the AI's suggestion installs malware. This is slopsquatting — typosquatting powered by AI hallucination.3

What makes this worse: the hallucinations aren't random. The study found that 43% of hallucinated package names appeared repeatedly across multiple queries.4 Attackers can predict which fake names the AI will suggest and pre-register them.

A security researcher demonstrated this in practice by registering `huggingface-cli` — a package name commonly hallucinated by AI tools. Within three months, it had accumulated over 30,000 downloads.5

Open-source models are worse. The study found open-source models hallucinated dependencies at a rate of nearly 22%, compared to just over 5% for commercial models.2 If your team uses locally-hosted models for cost savings (as we discussed in the context of local LLM deployments), the hallucination rate for package names may be significantly higher.

Beyond Hallucinations: Vulnerable Versions and Insecure Patterns

Hallucinated packages aren't the only risk. A separate academic study analysing 117,062 dependency changes found that AI agents select vulnerable versions of real packages at a rate of 2.46%, compared to 1.64% for human developers.6 Agent-driven development produced a net increase of 98 new vulnerabilities, while human-authored changes produced a net reduction of 1,316.

Common insecure patterns in AI-generated code include:

Pattern Risk Legal SaaS Impact
Missing input validation on API endpoints Injection attacks (SQL injection, as covered in Episode 8) Attacker modifies case records or extracts client data
Hardcoded API keys in generated code Credential exposure if code is committed to version control Third-party service compromise; billing abuse
Overly permissive CORS headers (as covered in Episode 12) Cross-origin data theft Browser-based attacks against logged-in lawyers
Default `admin/admin` credentials in scaffolded code Trivial unauthorised access Full platform compromise
Disabled TLS certificate verification Man-in-the-middle attacks (as covered in Episode 13) Intercepted client communications

Full-Auto Deployment: The Unreviewed Pipeline

The most dangerous configuration is an AI agent that can write code, run tests, and deploy — all without human review. This "full-auto" mode is increasingly common in development workflows. The agent creates a feature branch, writes the code, generates tests, the tests pass, and the code ships.

The problem: the agent wrote both the code and the tests. An AI that generates an authentication bypass will also generate tests that don't check for authentication bypasses. The tests pass because they were designed by the same model that created the flaw.

CSO Online documented cases where supply chain attacks specifically targeted AI coding agents, exploiting their tendency to follow instructions embedded in repository files, package descriptions, and even code comments.7

The legal obligation: If your contract analysis tool processes privileged attorney-client communications, deploying AI-generated code without human security review creates a reasonable-efforts argument under ABA Model Rule 1.6(c) that you are not making adequate efforts to protect client data.8

Verification Strategies That Work

1. Lock Your Dependency Sources

Maintain a private registry or mirror of approved packages. Configure your package manager to install only from this trusted source. When the AI suggests a dependency, it must exist in your registry before it can be installed. This eliminates slopsquatting entirely.

# npm example: .npmrc restricting to internal registry
registry=https://registry.internal.yourfirm.com/

2. Require Human Review of Security-Critical Code

Define security-critical paths in your codebase: authentication, authorisation, encryption, data access layers, and anything that handles client PII (Personally Identifiable Information). Require manual code review for changes to these paths, regardless of who (or what) wrote the code.

3. Force the Agent to Write Security Tests — Then Write Your Own

Have the AI generate security tests for its own code: tests for SQL injection, tests for authentication bypass, tests for authorisation failures. Then have a human write additional security tests independently. If the human-written tests catch what the AI-written tests missed, you've found a gap in the AI's security reasoning.

4. Sandbox the Agent's Workspace

The AI coding agent should never have access to production credentials, SSH keys, `.env` files, or deployment secrets during development. ActiveState's analysis of AI supply chain risks recommends restricting agents to allow-listed tools and requiring human approval before any high-impact actions like dependency installation or deployment.9

5. Run Static Analysis on Everything

SAST tools (Static Application Security Testing — automated scanners that examine source code for vulnerabilities without running it) like Semgrep and CodeQL should run on every pull request, whether the code was written by a human or an AI. AI-generated code should receive the same — or stricter — automated scrutiny as human-written code.

6. Pin and Audit Dependencies

Lock every dependency to a specific, verified version. Use tools like `npm audit`, `pip audit`, or Snyk to continuously scan for known vulnerabilities. When the AI suggests adding a new package, verify: Does this package exist? Is it actively maintained? Does it have known vulnerabilities? Is there a more established alternative?

The Trust Calibration

AI coding agents are tools, not developers. They produce output that must be verified with the same rigour you'd apply to code from an untrusted contractor — arguably more, because the AI cannot explain its reasoning, has no accountability, and will confidently generate insecure code without hesitation.

For legal SaaS teams, the standard is not "does the code work?" but "can we demonstrate that we verified the code meets security requirements before it touched client data?" The AI writes the first draft. Humans own the final version.

Next episode: Day Zero — bootstrapping security for a brand new project, from the very first commit.

Sources & references

  1. Endor Labs, The Most Common Security Vulnerabilities in AI-Generated Code.
  2. Trax Tech / University of Texas at San Antonio, 20% of AI-Generated Code Dependencies Don't Exist.
  3. Infosecurity Magazine, AI Hallucinations Create "Slopsquatting" Supply Chain Threat.
  4. BleepingComputer, AI-hallucinated code dependencies become new supply chain risk.
  5. DevOps.com, AI-Generated Code Packages Can Lead to 'Slopsquatting' Threat.
  6. Simon Roses Femerling, The Dependency Trap: Supply Chain Risks in AI-Generated Code (Part 4).
  7. CSO Online, Supply-chain attacks take aim at your AI coding agents.
  8. ABA, When Should Law Firms Notify Clients About Data Breaches?.
  9. ActiveState, Is AI-Generated Code Poisoning Your Software Supply Chain?.
  10. Capitol Technology University, AI-Driven Hallucinations in Cyber Supply Chain Lead to New Threat: Slopsquatting.
  11. TechDebt.best, AI Code Security Risks — Hallucinated Dependencies & Beyond.