Skip to content
mentoor

Topic 3 of 5 · 4 min read

Hallucinated and malicious AI-suggested packages

Goal: Check an AI-suggested dependency for invented, typosquatted or compromised packages before installing it.

An AI assistant will sometimes name a package that does not exist, and an attacker can register that name first, so every suggested dependency needs a check before it is installed.

Check yourself · 1 of 3

In supply chain security, what does the term slopsquatting describe?

Choose one answer

Why it matters

Code models predict plausible text, and a package name is plausible text. A study of 16 code models and 576,000 generated samples found that at least 5.2% of suggested packages for commercial models and 21.7% for open-source models did not exist, with over 200,000 unique invented names. Many invented names repeat across runs. An attacker can ask a model for suggestions, find the names that do not exist, and publish malicious packages under them. This is called slopsquatting. The next developer who runs pip install or npm install on the assistant's suggestion pulls the attacker's code, and install scripts can run immediately.

Related attacks use the same weak spot: typosquats (reqeusts), dependency confusion (a public package with the same name as your private one) and compromised releases (a real package whose maintainer account was taken over).

How to check a suggested dependency

  1. Does it exist, and is it the right one? Open the registry page. Check the exact spelling and the project's home page and repository link.
  2. Look for age and usage signals. A package created last week with few downloads and no linked source is suspect. Real popularity can be faked, so use this as a hint, not proof.
  3. Do you need it? Many suggestions can be a few lines of standard library code.
  4. Install with guards. Use a lockfile with integrity hashes, install from the lockfile in CI (npm ci), and consider blocking install scripts for new packages.
  5. Review the lockfile diff. A new package pulling in 40 transitive dependencies is a review item.
  6. Reserve your private names on the public registry, or scope them to your organisation.

Before and after

--- a/requirements.txt
+++ b/requirements.txt
@@ -1,2 +1,3 @@
 requests==2.32.3
+flask-jwt-helper-utils

The assistant added an unpinned name that nobody on the team has heard of. After checking, the team finds it is a week old with one release. They replace it with the maintained library they already use and pin versions with hashes:

 requests==2.32.3
+PyJWT==2.9.0 --hash=sha256:<hash-from-lockfile>

What goes wrong

  • Copy, paste, install. The command in a chat answer is run without a look at the registry.
  • Unpinned ranges. A floating version picks up a compromised release the day it ships.
  • Trusting a good README. Attackers copy real READMEs.
  • Skipping the lockfile diff. The risk often arrives as a transitive dependency.

Examples

The invented helper

You ask an assistant how to parse a vendor's log format. It writes import logparse_pro and says pip install logparse-pro. The registry shows the name was created nine days ago, has one release and links to no source. The assistant made up a plausible name, and someone else filled it. Use the standard library or a library with a known maintainer.

One-letter difference

-"axios": "^1.7.0"
+"axois": "^1.7.0"

A tired review reads this as the same package. The lockfile diff shows an unfamiliar publisher. Spelling checks belong in the review checklist for every new or changed dependency line.

Key points

  • Code models invent package names, and attackers can register those names, an attack called slopsquatting.
  • A 2024 study found at least 5.2% of suggested packages were invented for commercial models and 21.7% for open-source models.
  • Check the exact spelling, registry page, linked source repository and age before installing any suggestion.
  • Lockfiles with integrity hashes, installs from the lockfile in CI and reviews of lockfile diffs keep unexpected packages out.
  • Typosquats, dependency confusion and compromised releases use the same weakness: installing a name without verifying it.

Common mistakes

  • Running an install command from a chat answer without opening the registry page.
  • Using floating version ranges and no lockfile in CI.
  • Judging a package by its README or download count alone.
  • Looking at the direct dependency line and ignoring the transitive packages it pulls in.

Check yourself · 2 of 3

An assistant suggests installing a package you have never heard of. What should you do before installing?

Choose one answer

Check yourself · 3 of 3

Which practice best keeps a compromised new release out of your CI builds?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic