Catalog My learning Blog
AI-Assisted Coding Course: Agentic Development in 30 Days · Phase 5 · Lesson 22 · 5 min read

Slopsquatting: AI Package Hallucination Attacks

On this page
  1. Learning Objective
  2. Concept Explanation
  3. Worked Example: Vetting a Package Before Anyone Installs It
  4. Real-World Examples
  5. Review Questions & Answers

Learning Objective

Understand package hallucination and slopsquatting, why AI-suggested dependencies are a supply chain risk, and how to verify every new dependency with checks, lockfiles, policies and agent permissions before it reaches your build.

Concept Explanation

Language models sometimes recommend packages that do not exist. They generate a plausible name, such as a library that would logically exist for the task, and write code that imports it. A 2025 study presented at USENIX Security analyzed 576,000 code samples from 16 models in Python and JavaScript and found that at least 5.2 percent of recommended packages from commercial models and 21.7 percent from open-source models did not exist, with more than 200,000 unique hallucinated names. Crucially, 43 percent of hallucinated names reappeared every time the same prompt was repeated.

That repeatability is what makes hallucination exploitable. If models keep suggesting the same nonexistent name, an attacker can register it on npm or PyPI with a malicious payload and wait. The next developer, or agent, who runs the suggested install command gets the attacker's code, often with install scripts that execute immediately. The technique was named slopsquatting, a play on typosquatting. Security researcher Bar Lanyado demonstrated the risk in 2024 by publishing an empty package under a name models hallucinated, huggingface-cli, which collected tens of thousands of downloads, including references in a large company's public instructions.

Agents raise the stakes because they install packages themselves. A human might pause at an unfamiliar name; an agent in a permissive mode simply runs the install, and the package's install script executes with the agent's permissions, which may include your credentials. The broader supply chain threat compounds this: compromised legitimate packages, such as the Nx build system packages in August 2025, can steal secrets on install regardless of how the dependency was chosen.

Defenses work at several points. Policy: tell agents not to add dependencies without asking, and enforce it with an ask rule on install commands (Day 20). Verification: before accepting a new dependency, check that it exists, is the package you think it is, has a real maintainer history, meaningful usage, a repository that matches, and was not published in the last few days. Lockfiles: commit them and install with frozen lockfile commands in CI, so builds use exactly the reviewed versions. Hardening: disable or allowlist install scripts where your package manager supports it, use a private registry or proxy with an allowlist for sensitive projects, and run dependency and malware scanning. Prefer the standard library or an existing dependency when it does the job; every new package is new attack surface.

ControlStopsHow
Ask rule on installsSilent agent installsBash(npm install *), Bash(pip install *) in ask
Existence and age checkHallucinated and freshly squatted namesRegistry metadata before approval
Lockfile, frozen installsUnreviewed version driftnpm ci, pnpm install --frozen-lockfile
Install script controlsCode execution on installignore-scripts, allowlisted builds
Private registry or proxyUnknown packages entirelyAllowlist of approved packages
Dependency and malware scanningKnown bad versionsRequired CI checks

Worked Example: Vetting a Package Before Anyone Installs It

This script queries the npm or PyPI registry for a package an agent suggested and flags the signals of hallucinated or squatted names. It can run by hand or from a PreToolUse hook on install commands.

Python
#!/usr/bin/env python3
"""vet_package.py npm|pypi NAME : exit 1 if the package looks risky."""
import json, sys, urllib.request
from datetime import datetime, timezone

def fetch(url):
    try:
        with urllib.request.urlopen(url, timeout=10) as r:
            return json.load(r)
    except Exception:
        return None

def main(ecosystem: str, name: str) -> int:
    if ecosystem == "npm":
        meta = fetch(f"https://registry.npmjs.org/{name}")
        if not meta:
            print(f"DOES NOT EXIST on npm: {name} (possible hallucination)"); return 1
        created = meta["time"]["created"]
        repo = (meta.get("repository") or {}).get("url", "none")
        dl = fetch(f"https://api.npmjs.org/downloads/point/last-week/{name}") or {}
        weekly = dl.get("downloads", 0)
    else:
        meta = fetch(f"https://pypi.org/pypi/{name}/json")
        if not meta:
            print(f"DOES NOT EXIST on PyPI: {name} (possible hallucination)"); return 1
        uploads = [f["upload_time_iso_8601"] for files in meta["releases"].values() for f in files]
        created = min(uploads) if uploads else None
        repo = (meta["info"].get("project_urls") or {}).get("Source", "none")
        weekly = None                                  # PyPI has no official downloads API
    age_days = (datetime.now(timezone.utc) - datetime.fromisoformat(created.replace("Z", "+00:00"))).days if created else 0
    print(f"{name}: first published {age_days} days ago, repo {repo}, weekly downloads {weekly}")
    risky = age_days < 30 or repo == "none" or (weekly is not None and weekly < 500)
    if risky:
        print("REVIEW MANUALLY: new, unlinked or rarely used package.")
    return 1 if risky else 0

if __name__ == "__main__":
    sys.exit(main(sys.argv[1], sys.argv[2]))

A legitimate, widely used library passes; a name that does not exist, or one first published last week with no repository link, fails and needs a human decision. The thresholds are starting points to tune for your organization.

A model repeatedly suggests a package that does not exist. An attacker registers that name with a malicious install script. The next developer or agent who installs the suggestion runs the attacker's code.
A model repeatedly suggests a package that does not exist. An attacker registers that name with a malicious install script. The next developer or agent who installs the suggestion runs the attacker's code.

Real-World Examples

  • The USENIX Security 2025 paper "We Have a Package for You!" by Spracklen and colleagues found hallucinated package rates of at least 5.2 percent for commercial models and 21.7 percent for open-source models across 576,000 samples, with 43 percent of hallucinations repeating across runs.
  • In 2024 Bar Lanyado of Lasso Security registered the hallucinated package name huggingface-cli on PyPI as an empty test package; it received tens of thousands of downloads in three months, showing that developers really install AI-suggested names.
Key TakeawaysModels hallucinate package names, often the same ones repeatedly, and attackers register them with malicious payloads: slopsquatting. Agents that install packages themselves raise the risk. Require approval for installs, verify existence, age, repository and usage before accepting a dependency, commit lockfiles and use frozen installs, control install scripts, scan dependencies, and prefer existing libraries over new ones.

Review Questions & Answers

Registering package names that AI models hallucinate, with malicious code, so that developers or agents who install the suggested package run the attacker's payload.

If models suggest the same nonexistent name consistently, attackers can discover those names by querying models and register them in advance, confident that future users will be told to install them.

Agents can run install commands themselves without a human reading the name, and install scripts then execute with the agent's permissions, which may include access to credentials.

It does not exist, was first published very recently, has no linked source repository, has very low usage, or its repository and maintainers do not match the library it claims to be.