Securing AI Coding Workflows Against Hallucinated Packages and Slopsquatting Attacks
A USENIX study of 576,000 AI code samples highlights supply chain security risks from hallucinated dependencies, requiring strict package verification before installation.

AI coding assistants streamline software development, but they can also introduce critical supply chain vulnerabilities by recommending non-existent dependencies. This issue, known as package hallucination, occurs when an AI model generates code that references a library absent from public package registries. When malicious actors monitor these recurring recommendations and register those precise names on platforms like PyPI or npm, the vulnerability evolves into an attack pattern called slopsquatting. The empirical scope of this issue was highlighted in the USENIX Security 2025 study titled "We Have a Package for You!", which evaluated 16 AI models across 576,000 code samples in Python and JavaScript, establishing a clear failure mode in automated code generation.
How AI package hallucinations become target vectors
The sequence of a slopsquatting attack demonstrates why simple package existence is an inadequate defense. A model might invent a plausible library name on Monday, an attacker can register that identifier on a public registry on Tuesday, and a developer running the model's generated code on Wednesday will successfully fetch a real, potentially malicious package. Beyond completely fabricated names, AI models can also suggest misspelled lookalike packages, trigger dependency confusion when private packages resolve against public repositories via flags like pip's --extra-index-url, or recommend legitimate projects that contain compromised releases.
In Python environments, technical distinctions between import tokens and distribution names complicate verification. Python packaging specifications explicitly treat import names separately from installer distribution names. Furthermore, distribution names undergo strict normalization, where uppercase letters are lowercased and sequences of hyphens, underscores, or periods are collapsed into a single hyphen. For instance, Beagle_Demo.Parser and beagle-demo-parser resolve to the exact same normalized distribution name, meaning naive policy checks based purely on string matching can easily fail.
Installation hooks and the limits of hash verification
Granting an AI agent unrestricted installation capabilities is dangerous because package managers often execute code before tests even run. In Node.js environments, npm supports lifecycle hooks like preinstall, install, and postinstall. While using npm ci --ignore-scripts suppresses package scripts during installation, explicitly invoked script commands will still execute, making the flag a targeted control rather than a broad isolation sandbox. Similarly, building Python source distributions can run arbitrary build-system code, whereas pre-compiled wheel files bypass the source-building phase.
Cryptographic digests offer another layer of integrity, but matching a hash only proves byte consistency—it does not confirm that the approved file is safe. If the same unreviewed AI agent provides both the package download and the expected hash, it can validate its own selection by hashing whatever binary it chose. Verification must rely on an immutable policy record maintained outside the agent's write authority. A similar principle applies to GitHub Actions, where third-party actions should be pinned to full commit hashes rather than mutable version tags.
As developers integrate diverse model architectures into automated pipelines—often accessing top AI models cheaply through one API at https://apixoai.online —implementing strict boundary checks around agent tool calls remains essential to prevent unintended code execution.
What it means for developers
To safeguard software pipelines against unreviewed AI recommendations, engineering teams should establish concrete verification procedures:
- Separate proposal from policy authority: Allow AI agents to propose manifest edits or lockfile updates, but prevent them from altering the security policies or approval stores against which those proposals are validated.
- Execute installs in disposable environments: Perform initial package assessments inside credential-free, isolated environments with strict network controls and resource limits to contain potential execution hooks.
- Enforce strict lockfiles and hash matching: Utilize package manager controls like pip's hash-checking mode and
npm cito verify that the entire resolved dependency graph, including transitive packages, matches known records. - Verify package identity beyond spelling: Confirm the distribution name, maintained source repository, release history, and configured registry before converting a dependency suggestion into an executable build step.
Source: Beagle Lesson #4: How to Check a Package Before an AI Agent Installs It — Towards AI. Written by the Apixo team from that report.
One key for Claude, GPT, GLM, DeepSeek and more. Pay per token with crypto.
Get your API key

