Securing the Account That Can Publish Your Package

Many well-known package compromises did not start with clever code. They started with a stolen password, a leaked token or an old maintainer account nobody remembered. Your publish credentials are a key to every machine that installs your package, so they deserve habits, not good intentions. Each habit below comes with the mistake it prevents.

Habit: phishing-resistant two-factor authentication

Turn on two-factor authentication on every registry and on the code host. Prefer a hardware security key or passkey over SMS or one-time codes, since keys bound to the site's origin resist phishing pages. npm, PyPI and GitHub all support security keys. PyPI requires two-factor authentication for all accounts that publish.

Mistake it prevents: a convincing fake login page that captures a password and a one-time code in real time.

Habit: scoped, expiring tokens

Mistake it prevents: one leaked, account-wide, never-expiring token giving access to every package you own.

Habit: publish from CI with short-lived credentials

The strongest option is to have no long-lived publish token at all. PyPI's Trusted Publishing and npm's equivalent workflow let a CI job exchange an OpenID Connect identity token for a short-lived upload credential. Restrict the trust to one repository, one workflow file and, ideally, one protected environment that requires approval.

# GitHub Actions job permissions for OIDC publishing
permissions:
  id-token: write
  contents: read

Mistake it prevents: a token copied out of CI secrets by a malicious pull request or a compromised third-party action.

Habit: lock down the CI workflow itself

Pin third-party actions to a full commit SHA rather than a moving tag, set default workflow permissions to read-only, and do not run publish steps on events triggered by forks. The OpenSSF Scorecard checks several of these automatically.

Mistake it prevents: an upstream action being changed under the same tag and running with your permissions.

Habit: review who can publish

Every few months, list the owners and maintainers of each package on the registry and the admins of the repository. Remove people who have stepped away, with thanks. Check that the email addresses on those accounts are on domains you still control; an expired domain can let someone else receive a password reset.

Mistake it prevents: a forgotten co-owner account with a weak password becoming the way in.

Habit: plan recovery before you need it

Store recovery codes offline, register two security keys, and write down which accounts matter. Registry support teams have to be cautious about account recovery, so being locked out can take a long time to resolve.

Further reading: PyPI Trusted Publishing setup and who publishes supply-chain guidance.