ВопросHow to protect a small Python project from bad packages on PyPI?

TP Tomasz P. Участник Регистрация: 30 июн. 2026 г.
Сообщений: 84
#1

Hello. I read more and more about malicious packages in npm and PyPI. For a small Python project (a few scripts, a small Flask app), what are the basic steps? I do not have a security team, only myself.

1. Is a requirements.txt with versions enough?
2. What about CI, does it need something special?

YS Yury Slinkin Сотрудник · админ Регистрация: 6 июн. 2026 г.
Сообщений: 94
#2

1. Versions alone are not enough: lock the hashes too, then pip refuses any file that differs from what you reviewed.

pip install pip-tools
pip-compile --generate-hashes requirements.in -o requirements.txt
pip install --require-hashes --only-binary :all: -r requirements.txt

--only-binary :all: installs wheels only, so no package runs a setup.py on your machine during install. Some packages have no wheel for your platform; then allow source builds for those names only.

PyPI itself has tightened things this year: since 22.07.2026 no new files can be added to releases older than 14 days, and since 16.10.2026 new accounts must verify their email (https://blog.pypi.org/). That helps, but it does not stop a malicious new version or a typosquatted name, so updates should be a deliberate step, not something that happens on every build.

2. In CI: install only from the lock file, give the job no secrets it does not need, and if you publish your own package, use PyPI trusted publishing (OIDC) instead of a long-lived API token stored in the repository.

SL Sara L. Участник Регистрация: 17 июл. 2026 г.
Сообщений: 80
#3

Doesn't that make updates painful though? If every version and hash is locked, how do you get security fixes?

TP Tomasz P. Участник Регистрация: 30 июн. 2026 г.
Сообщений: 84
#4

Also I do not understand: --require-hashes checks only the packages I wrote, or also the packages they need?

IS Irakli S. Участник Регистрация: 18 сент. 2026 г.
Сообщений: 44
#5

>>4 All of them. In hash-checking mode every package that gets installed, dependencies of dependencies included, must be pinned with == and have a hash in the file, otherwise pip stops with an error. That's why you generate the file with pip-compile or uv pip compile --generate-hashes instead of writing it by hand.

>>3 You don't edit the lock file by hand, you regenerate it: pip-compile --upgrade-package requests --generate-hashes requirements.in, then read the diff, run the tests, commit. Dependabot or Renovate can open such pull requests for you, so updates stay regular but reviewed.

Быстрый ответ

Писать могут только вошедшие участники. Войти