10.2 Package Hallucination and Slopsquatting¶
Section 6.6 introduced slopsquatting as an emerging attack vector where adversaries register package names that AI coding assistants commonly hallucinate. This section examines the phenomenon in greater technical depth, exploring why AI hallucinations occur, what makes them exploitable, and how organizations can defend against this attack in their AI-assisted development workflows.
The core vulnerability is straightforward: AI models generate plausible-sounding package names that don't exist, attackers register those names with malicious packages, and developers who trust AI suggestions install the attackers' code. What makes this attack particularly concerning is that hallucinations are not random—they're repeatable and predictable, enabling systematic exploitation.
The Hallucination Phenomenon¶
Large language models (LLMs) powering coding assistants generate text by predicting the most likely next tokens based on patterns in their training data. When generating import statements or dependency specifications, models produce package names that seem probable given the context—but "probable" doesn't mean "real."
Why Hallucinations Occur:
Several factors contribute to package name hallucinations:
-
Pattern completion over memory: Models complete patterns rather than retrieving exact facts. A model might generate
flask-security-utilsbecause "flask," "security," and "utils" commonly appear together, not because it "remembers" a specific package. -
Training data noise: Training corpora include:
- Hypothetical examples in documentation
- Packages that existed but were removed
- Typos and errors in existing code
-
Fictional examples in tutorials
-
Ecosystem fragmentation: Package ecosystems contain hundreds of thousands of packages. No model maintains a complete, accurate registry.
-
Naming convention inference: Models learn naming patterns (e.g.,
python-*,py*,*-utils) and generate novel combinations that fit patterns but don't correspond to real packages. -
Temporal misalignment: Models trained on 2022 data may reference packages removed, renamed, or subsumed since then.
The Appearance of Validity:
Hallucinated package names aren't random strings—they follow ecosystem conventions:
- Appropriate prefixes (
py-,node-,@scope/) - Conventional structure (kebab-case, lowercase)
- Semantically meaningful names that fit the programming context
- Plausible version numbers when versions are included
This plausibility is precisely what makes them dangerous. A developer seeing import flask_authentication_helper in AI-generated code has no immediate reason to suspect the package doesn't exist.
Research Quantification¶
Academic research has begun quantifying the scope of package hallucination.
The Spracklen Study:
Research by Spracklen et al. ("We Have a Package for You!") systematically tested AI models for package hallucination across multiple ecosystems:
Key Findings:
- Approximately 19.7% of AI-generated code samples overall referenced packages that don't exist in their target registries
- Hallucination rates varied significantly by model type
- The study tested Python (PyPI) and JavaScript (npm) ecosystems, which showed the highest representation in training data
- Commercial models (GPT-4, Claude, etc.) hallucinated less than open-source alternatives
Model Type Variation:
| Model Type | Hallucination Rate |
|---|---|
| Commercial models (GPT-4, Claude, etc.) | ~5.2% |
| Open-source models (CodeLlama, DeepSeek, etc.) | ~21.7% |
| Overall average across all models | 19.7% |
The study focused on Python (PyPI) and JavaScript (npm), generating 576,000 code samples to measure hallucination patterns across different AI models.
Repeatability: Why Attacks Are Viable¶
Random hallucinations would be difficult to exploit. An attacker would need extraordinary luck for their registered package name to match what an AI happens to generate for a specific developer. But hallucinations are not random—they're repeatable.
Spracklen's Repeatability Finding:
The research found a bimodal pattern in hallucination behavior:
- 43% of hallucinated packages were regenerated in all 10 iterations of the same prompt
- 58% appeared at least once more across 10 iterations
- 39% never reappeared at all
This bimodal pattern is significant: hallucinations are either highly stable and predictable or entirely unpredictable, with little middle ground.
Why Hallucinations Repeat:
-
Deterministic patterns: Given similar context, models produce similar outputs. "How do I connect to a PostgreSQL database in Python?" tends to generate similar import suggestions.
-
Common prompts: Developers ask similar questions. "Add logging to Flask" or "Parse JSON in Node" are near-universal tasks with common phrasings.
-
Naming convention convergence: Multiple models trained on similar data arrive at similar "plausible" names for packages that don't exist.
-
Temperature effects: At lower temperature settings (more deterministic), models are more likely to produce identical outputs across invocations.
Practical Implication:
An attacker can:
- Query AI models with common development questions
- Collect hallucinated package names from responses
- Filter for names that appear repeatedly
- Register those names with high confidence they'll be requested by real developers
This transforms a random-seeming vulnerability into a systematic attack.
The Attack Lifecycle¶
Slopsquatting attacks follow a predictable lifecycle:
Phase 1: Reconnaissance
Attackers systematically query AI coding assistants:
- Common development tasks across languages
- Popular frameworks and their typical integrations
- Security and authentication patterns
- Database and API integration scenarios
They collect every package name mentioned, checking each against actual registries.
Phase 2: Name Harvesting
Attackers filter collected names:
- Remove packages that actually exist
- Track which non-existent names appear repeatedly
- Prioritize by estimated frequency and value of target contexts
- Consider cross-model consistency (names hallucinated by multiple AI tools)
Phase 3: Registration
Attackers register high-value hallucinated names:
- Create packages on PyPI, npm, and other registries
- Include legitimate-seeming metadata and descriptions
- Add just enough functionality to avoid immediate suspicion
- Optionally include malicious payloads or credential harvesting
Phase 4: Passive Waiting
Unlike active attacks requiring victim targeting, slopsquatting is passive:
- Attackers wait for developers to receive AI suggestions
- Developers install packages without verification
- Malicious code executes on developer machines or in CI/CD
- Credentials, source code, or access are exfiltrated
Phase 5: Exploitation
Harvested credentials enable further attacks:
- Access to private repositories
- Publishing rights to legitimate packages
- Cloud infrastructure access
- Lateral movement through development systems
Comparison to Traditional Typosquatting¶
Slopsquatting and typosquatting exploit different error sources but share operational similarities:
| Aspect | Typosquatting | Slopsquatting |
|---|---|---|
| Error source | Human typing mistakes | AI hallucination |
| Prediction method | Keyboard proximity, common typos | AI model queries, frequency analysis |
| Target selection | Misspellings of popular packages | Names AI models commonly generate |
| Detection | Edit distance from known packages | No relation to existing packages |
| Scale | Limited by human error rates | Scales with AI adoption |
Critical Distinction:
Typosquatting detection relies on proximity to known packages—lodahs is close to lodash. Slopsquatting involves names that may have no relationship to existing packages. A hallucinated flask-auth-middleware isn't a typo of anything—it's a plausible name that happens not to exist.
This distinction defeats many typosquatting defenses. Edit distance checking, keyboard proximity analysis, and visual similarity detection don't help when the attack targets novel names rather than variations of existing ones.
Defense Strategies¶
Defending against slopsquatting requires interventions at multiple points:
Real-Time Validation:
The most effective defense is validating packages before installation:
# Before: Install whatever AI suggested
pip install flask-auth-middleware
# After: Verify package exists and matches expectations
pip index versions flask-auth-middleware # Check if it exists
pip install flask-auth-middleware==1.2.3 # Pin specific verified version
Tools can automate this:
- Pre-installation hooks: Scripts that verify package existence and metadata before allowing installation
- IDE plugins: Real-time checking of import statements against registries
- CI/CD gates: Pipeline steps that fail if unverified packages are introduced
Curated Package Lists:
Organizations can maintain approved package lists:
- Allowlists: Only packages on the approved list can be installed
- Internal registries: Proxy registries that only mirror vetted packages
- Package governance: Formal process for adding new dependencies
This is operationally expensive but effective for high-security environments.
AI Tool Integration:
AI assistants themselves could help:
- Real-time registry checking: Validate suggested packages against live registries before presenting to users
- Confidence indicators: Flag suggestions the model is uncertain about
- Warning annotations: Explicitly note when suggested packages couldn't be verified
Some AI providers are exploring these capabilities, though implementation remains limited.
Developer Training:
Awareness is a critical layer:
- Train developers to verify AI-suggested packages
- Emphasize that AI suggestions are not endorsements
- Build verification habits into development culture
Monitoring:
Detection after the fact:
- Monitor for installation failures (package doesn't exist)
- Alert on newly-created packages with suspicious characteristics
- Track dependencies against known-hallucinated name lists
Implementation Challenges¶
Several factors complicate defense:
Verification Friction:
Every verification step slows development. The appeal of AI assistants is speed—adding verification undermines that value proposition. Organizations must balance security with productivity.
Dynamic Package Ecosystem:
- New legitimate packages are created constantly
- A package that doesn't exist today might exist tomorrow
- False positives (blocking legitimate new packages) frustrate developers
Tooling Gaps:
Current development tools weren't designed for this threat:
- IDEs don't validate imports in real-time against registries
- Package managers don't distinguish "package doesn't exist" from "network error"
- CI/CD systems don't have standard slopsquatting detection
AI Tool Opacity:
Developers often can't configure how AI assistants generate suggestions. The model's behavior is largely fixed by the provider.
The Convenience-Security Tension¶
Slopsquatting exemplifies a broader tension in AI-assisted development: the features that make AI useful also make it dangerous.
Convenience Drivers:
- AI suggestions reduce typing and research time
- Developers accept suggestions without deep investigation
- Productivity metrics reward speed over verification
Security Requirements:
- Every dependency is a trust decision
- Verification requires interrupting flow
- Caution conflicts with AI tool value proposition
Possible Resolutions:
- Invisible verification: Tools that validate without user intervention
- Progressive trust: Reduced verification for well-established packages, full verification for unknowns
- Shared intelligence: Community databases of known hallucinations
- Provider responsibility: AI tool providers taking responsibility for suggestion validity
Agentic Dependency Injection: When AI Installs Without Asking¶
The threat landscape shifts dramatically when AI moves from suggesting packages to installing them. AI coding agents—autonomous systems that can execute commands, modify files, and run build processes—amplify slopsquatting risk by removing the human verification step entirely.
The Agentic Difference:
| Capability | AI Assistant | AI Agent |
|---|---|---|
| Package suggestion | Yes | Yes |
| Installation execution | No (human runs command) | Yes (agent runs command) |
| Human review point | Before installation | After installation (if at all) |
| Blast radius | Single decision | Entire development session |
| Verification opportunity | Each package | None without explicit controls |
How agentic injection works:
- User requests: "Set up a Flask app with authentication"
- Agent generates code including
from flask_auth_helper import AuthManager - Agent encounters ImportError when running code
- Agent "fixes" the error by running
pip install flask-auth-helper - Malicious package (registered by attacker) executes on installation
- Agent continues, unaware that compromise occurred
The feedback loop vulnerability:
AI agents are designed to iterate until tasks complete. When an import fails:
- Agent assumes it needs to install a dependency
- Agent has credentials/access to run
pip installornpm install - Installation succeeds (malicious package exists)
- Agent proceeds, treating the task as successful
- Malicious code has executed with agent's privileges
This creates a self-reinforcing attack path: the hallucination creates an error, the error triggers installation, installation executes malicious code.
Scale implications:
- Assistants: Attacker waits for individual developers to accept suggestions
- Agents: Attacker's package is installed automatically across all agent sessions that hallucinate that name
With TrendMicro research documenting how AI agents are increasingly deployed in automated development pipelines, the scale of potential impact grows exponentially.
Compounding factors:
- Agent privileges: Agents often run with developer credentials, npm tokens, or cloud access
- Autonomous operation: No human reviews each command the agent executes
- Retry behavior: Agents will repeatedly try to "fix" import errors
- Memory persistence: Some agents learn from sessions, potentially spreading hallucinations
- CI/CD integration: Agents in pipelines can affect production builds
The "vibe coding" amplification:
When developers embrace "vibe coding"—rapidly accepting AI-generated code without deep review—they're implicitly trusting not just the code but every dependency the AI suggests. When agents execute autonomously, even this minimal review disappears.
Agentic-specific defenses:
# Example: Agent configuration with dependency controls
agent_config:
permissions:
package_install:
mode: "allowlist" # Only pre-approved packages
allowlist_source: "approved-packages.txt"
behaviors:
on_import_error: "halt_and_report" # Don't auto-install
on_new_dependency: "require_approval"
sandbox:
network: "internal_only"
filesystem: "workspace_only"
Critical controls for agentic development:
- Disable auto-installation: Agents should not have permission to install packages without approval
- Pre-approved package lists: Agents can only use packages from vetted allowlists
- Sandbox execution: Agent package operations occur in isolated environments
- Audit logging: Every package installation attempt is logged for review
- Human checkpoints: Package additions require human approval before continuing
The emerging threat:
Industry analysis indicates that slopsquatting attacks are increasingly targeting agentic workflows specifically. Attackers recognize that:
- Agents remove human verification
- Agents have persistent credentials
- Agents can be systematically probed for hallucination patterns
- Compromised agent sessions provide rich access
As organizations deploy AI agents for development tasks, slopsquatting transitions from a nuisance to a critical vulnerability requiring architectural controls.
Recommendations¶
For Developers:
-
Never install AI-suggested packages blindly. Verify existence before installation by searching the official registry.
-
Check package metadata. Legitimate packages have histories—downloads, GitHub stars, maintainer information. Be suspicious of empty or very new packages.
-
Use lockfiles religiously. Pin exact versions of verified packages to prevent substitution.
-
Build verification habits. Make package verification as automatic as code review.
For Organizations:
-
Implement pre-installation checks. Add verification steps to development environments and CI/CD pipelines.
-
Consider package allowlisting. For high-security environments, maintain curated lists of approved packages.
-
Monitor for slopsquatting indicators. Alert on installation failures, newly-introduced unusual packages, or patterns matching known hallucinations.
-
Train developers. Ensure teams understand that AI suggestions require verification.
-
Evaluate AI tools on security features. Prefer tools that validate suggestions or provide confidence indicators.
For AI Tool Providers:
-
Validate suggestions against registries. Before presenting package suggestions, verify they exist.
-
Provide confidence signals. Indicate when package names might be uncertain.
-
Enable grounding. Allow users to configure authoritative package lists.
-
Share research. Publish findings on hallucination patterns to help the community defend.
For Registry Operators:
-
Flag newly-registered packages matching AI hallucination patterns. Coordinate with researchers tracking hallucinated names.
-
Consider publication friction for suspicious names. Require additional verification for packages with characteristics of slopsquatting.
-
Provide verification APIs. Make it easy for tools to validate package existence and legitimacy.
Slopsquatting represents a supply chain attack vector that exists because of AI adoption. As AI-assisted development becomes standard practice, defenders must adapt—building verification into workflows, developing detection capabilities, and accepting that AI suggestions are not endorsements. The 19.7% hallucination rate and bimodal repeatability pattern (43% fully repeatable, 39% never repeated) documented by researchers make clear that this is not a theoretical concern but an active threat requiring systematic response.