Sharing is caring!

AI coding security is usually framed as one problem: “don’t paste secrets into the chat.” That is the smallest of the three. The other two are worse, and they arrive through the normal workflow:

  1. What you install — a package name the model invented, registered by someone else.
  2. What the agent reads — a README, issue, dependency, or web page that contains instructions, not information.

Both turn a helpful assistant into an untrusted third party with shell access. Here is the threat model and the gates that actually help.

Advertisement

Padlock on a laptop keyboard — application and supply chain security

The short answer

  • Slopsquatting is a supply-chain attack with a predictable trigger: models hallucinate package names, attackers register them.
  • Secrets leak through context, logs, and commits — not just through obvious pastes.
  • Prompt injection is an instruction-execution problem, not a content-moderation problem. The agent cannot reliably tell data from orders.
  • Instructions do not fix this. Permissions, allowlists, sandboxes, and CI gates do.

Slopsquatting: the 19.7% problem

The academic grounding is unusually solid. “We Have a Package for You!” (USENIX Security 2025) tested 16 models across 576,000 code samples. 19.7% contained at least one hallucinated package name; 205,474 unique fabricated names were observed. Open-source models averaged 21.7%, commercial models 5.2%. Re-running identical prompts ten times, 43% of hallucinated names appeared every single time — which is exactly what makes them worth registering. The same study found 8.7% of hallucinated Python packages were valid npm packages, so a model switching ecosystems can land on a real, unrelated package.

Researchers named the attack slopsquatting: squatting on the “slop” a model produces. A hallucinated name can be registered cheaply, published with a postinstall script, and then waits for an agent to run npm install. One documented example, huggingface-cli, reportedly got over 30,000 downloads in three months with no code, no README, and no SEO.

The defence is not “the model should be careful.” It is a dependency gate:

  • No auto-install. The agent may propose a dependency; a human approves it. Put this in AGENTS.md and — more importantly — in the permission layer.
  • Read the lockfile diff. A single line in package.json can pull dozens of transitive packages. Review the tree, not the name.
  • Prefer npm ci in CI with a committed lockfile, and --ignore-scripts for untrusted installs. Lifecycle scripts are where the payload usually lives.
  • Check provenance. npm provenance attestations, signed packages, and a registry allowlist raise the cost of an impersonation. They do not eliminate it.
  • Use a scanner that understands packages, not just files. Socket, Snyk, and GitHub’s dependency review catch behaviour that a linter never sees.

Network switch ports with Ethernet cables — dependency and infrastructure security

Secrets: the leak is usually boring

Most leaks are not dramatic. They are a .env pasted “just to debug”, a token in a test fixture, a connection string in a stack trace, or a key committed because .gitignore had an exception.

AI adds two new paths: the prompt (and whatever the provider retains) and the agent’s tool calls. An agent debugging a failed request will happily print the headers, including the authorization token, into a log or a chat summary.

What to do:

  • Scope tokens to the task and the environment. A read-only token cannot exfiltrate write access.
  • Redact before context: strip secrets from files the agent reads, and never mount production credentials into the agent’s environment.
  • Scan on commit and in CI: gitleaks or trufflehog locally, secret scanning with push protection in the repository.
  • Rotate on exposure, not after the incident review. Assume any secret that entered a prompt is compromised.
  • Check provider retention settings for the models you use. “We don’t train on your data” is not the same as “we don’t store it.”

A useful test: if the agent’s environment leaked tomorrow, what would the attacker get? If the answer is “the production database and the deploy key”, the problem is permissions, not the model.

Prompt injection: data is not an instruction

Indirect prompt injection is the one teams underestimate. The attack does not require the user to type anything malicious. It hides in content the agent reads: a README, a GitHub issue, a dependency’s documentation, a web page, a code comment, even a test fixture.

Researchers demonstrated this against agentic coding editors by poisoning external development resources — the assistant follows instructions embedded in the material it was asked to summarise. The OWASP GenAI exploit round-up for Q1 2026 documents indirect injection moving from theory to operational attacks. The pattern is consistent: the model sees text, and text can contain commands.

If an agent can read a file and run a command, then anyone who can write that file can run a command.

That is the whole threat model in one sentence. Mitigations that work:

  • Least privilege: the agent runs with the smallest token, the fewest scopes, and no production access.
  • Command allowlist: tests, lint, typecheck, build. Not curl, not ssh, not npm publish, not arbitrary shell.
  • Human approval for network and install: any outbound request or new package is a decision, not a step.
  • Sandboxing: containers or VMs with no host mounts, no cloud metadata endpoint, no ambient credentials.
  • Treat retrieved content as untrusted: summarise it in a separate context, and never let the same context both read and execute.
  • Audit trail: log tool calls, commands, and network destinations. You cannot investigate what you did not record.

Server rack with blue status lights in a data center

The dependency and CI gates

GateWhat it catchesWhere
Lockfile reviewNew transitive packages, version driftPR
Install with --ignore-scriptsMalicious postinstall payloadsCI / sandbox
Provenance + registry allowlistImpersonation, unpublished namesRegistry / CI
Secret scanning + push protectionTokens in commits and historyRepo / pre-commit
SAST + dependency reviewInjection, unsafe deserialization, known CVEsCI
SBOMWhat you actually ship, for incident responseBuild
Scoped CI tokenBlast radius of a compromised pipelineCI config
Agent sandboxHost access, cloud metadata, lateral movementRuntime
Advertisement

None of these is exotic. The failure mode is not a missing tool; it is a tool that exists and is set to “warn” while everyone merges anyway.

A minimal secure setup

You do not need a security team to start. A sane default for an agent workstation looks like this: a container with no host mounts except the repository, no production credentials, an egress allowlist instead of open internet, and a read-only root filesystem. Installs happen in a separate step that a human approves.

# Run the agent with the least access that still works
docker run --rm -it \
  --network agent-egress \
  --read-only \
  --tmpfs /tmp \
  -v "$PWD":/work:rw \
  -w /work \
  agent-sandbox:latest

# CI installs exactly the lockfile, without lifecycle scripts
npm ci --ignore-scripts

Two details make this more than security theatre. First, the egress allowlist blocks the exfiltration path as well as the download path; an injected instruction cannot phone home. Second, --ignore-scripts removes the most common postinstall payload, so a slopsquatted package has to do its damage some other way.

If containers are too heavy for your setup, start with the cheapest version: a dedicated OS user, no SSH keys, no cloud CLI, and a separate browser profile. The principle is the same — the agent gets a workspace, not your laptop.

The ten-point pre-merge security checklist

  1. Every new package exists, is the intended one, and has a credible maintainer history.
  2. The lockfile diff matches the dependency diff — no surprise transitive packages.
  3. No secrets, tokens, internal URLs, or customer data in code, tests, fixtures, or prompts.
  4. Install scripts are disabled in CI and in the agent’s environment.
  5. The agent had no production credentials and no write access to the registry.
  6. User input is validated at the boundary, not trusted because the type says so.
  7. Output encoding is correct for the context (HTML, SQL, shell, URL).
  8. New endpoints have authentication, authorization, and rate limiting.
  9. Retrieved content was treated as data — no tool call was triggered by text inside a file or page.
  10. Logs exclude secrets and personal data, and the audit trail records what the agent ran.

If something slips through

Assume compromise, then narrow it. Rotate every credential the agent could reach. Review the tool-call and network logs for the exposure window. Check registry publishes, repository settings, and CI secrets. Remove the package, rebuild from a clean lockfile, and add the specific check that would have caught it.

The postmortem question is not “why did the model do that?” It is “why did the system let it?”

FAQ

Is slopsquatting only an npm problem?

No. It applies to PyPI, npm, crates.io, Go modules, and any registry where publishing is cheap. The cross-registry confusion — a Python-sounding name that is a real npm package — makes it worse.

Can I just tell the agent not to install packages?

You can, and you should. But instructions are advisory. Enforce it with permissions: the agent’s environment should not be able to publish, and installs should require approval.

Does prompt injection only matter for agents with shell access?

Shell access makes it severe. Without tools, injection can still manipulate output, leak context, or trick a user into a bad action. The severity scales with what the agent can do, not with whether it can “think”.

Are AI code scanners enough?

They help with known patterns and known CVEs. They do not know your threat model, your permissions, or whether a package should exist. Use them as one gate, not the gate.

What is the single highest-value change?

Remove production credentials from the agent’s environment. It reduces the blast radius of every other mistake on this list.

The bottom line: treat the agent like a talented contractor with a laptop you don’t control. Give it the minimum access, make installs and network calls explicit decisions, verify what it brings into the repo, and keep an audit trail. Security is not a prompt; it is the set of things the system refuses to do.

This closes the series. Start from the top: Context Engineering, then Reviewing AI-Written Code and Testing AI-Written Code. For the overview, see AI Coding Workflow 2026.

Images: Openverse (CC0), ShakataGaNai (CC BY-SA 3.0), NOIRLab/NSF/AURA/T. Slovinský (CC BY 4.0).

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Hello, my name is Mahmoud Ameara — senior front-end developer and designer, and the founder of mameara.com. Here I share design resources, tutorials, freebies, and practical frontend guides.
    mameara © 2010 – 2026