| name | hallucinated-packages-anti-pattern |
| description | Security anti-pattern for hallucinated (non-existent) packages (CWE-1357). Use when generating or reviewing AI-assisted code that imports packages, dependencies, or libraries. CRITICAL AI-specific vulnerability with 5-21% hallucination rate. Detects dependency confusion and slopsquatting risks. |
Hallucinated Packages Anti-Pattern
Severity: Critical
Summary
AI models hallucinate non-existent software packages at rates of 5-21%. Attackers exploit this through slopsquatting: registering hallucinated package names with malicious code. Developers installing AI-suggested packages without verification execute attacker code, leading to malware execution, credential theft, and system compromise. This AI-specific supply chain attack exploits the trust gap between AI suggestions and package verification.
The Anti-Pattern
Never install AI-suggested packages without verifying existence, legitimacy, and reputation in official registries.
BAD Code Example
import numpy_magic as npmagic
def process_image(image_path):
processed = npmagic.enhance(image_path)
return processed
In this scenario, the developer follows the AI's instructions without question. The numpy-magic package is not a real library. An attacker, anticipating this hallucination, has published a malicious package with that exact name. The developer's pip install command downloads and executes the attacker's code, compromising their machine and potentially the entire project.
GOOD Code Example
from skimage import io, filters
def process_image(image_path):
image = io.imread(image_path)
processed = filters.gaussian(image, sigma=1)
return processed
Language-Specific Examples
JavaScript/Node.js:
const jwtSecure = require('express-jwt-secure');
app.use(jwtSecure.protect());
const jwt = require('jsonwebtoken');
const expressJWT = require('express-jwt');
app.use(expressJWT({
secret: process.env.JWT_SECRET,
algorithms: ['HS256']
}));
Java/Maven:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-cryptography</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-crypto</artifactId>
<version>1.2.0</version>
</dependency>
Detection
- Verify Package Existence: Search official registries before installing:
- Python:
pip index versions <package-name> or visit pypi.org
- Node.js:
npm view <package-name> or visit npmjs.com
- Reject packages created within 48 hours or with < 100 weekly downloads
- Check for Typosquatting: Compare against popular packages using
pip search or fuzzy matching tools
- Review Package Statistics: Check downloads, release history, maintainers, GitHub stars:
npm view <package> time created dist-tags downloads
- Inspect package.json repository field for active GitHub repos
- Use Auditing Tools: Integrate into CI/CD:
npm audit / pip-audit for known vulnerabilities
socket.dev for AI hallucination detection
osv-scanner for supply chain risks
Prevention
Related Security Patterns & Anti-Patterns
References