Rexact
Cheatsheet

Regex npm access token (npm_…)

Since September 2021, an npm access token starts with 'npm_' (the underscore replaces the hyphen so a double-click selects the whole token) and its last 6 characters are a CRC32 checksum encoded in Base62; entropy goes from 128 to 178 bits (announcement by GitHub, npm's owner). Older tokens were 36-character UUIDs. The announcement does not give the new format's length: the three detectors I read all take 36 characters after the prefix. They differ on the rest: gitleaks ignores case, secretlint accepts an underscore in the body, trufflehog accepts a 40-character body, and all three accept a token followed by a space. Here's an anchored pattern, compared against those tools.

/^npm_[A-Za-z0-9]{36}$/
passes 21/21 cases

Comparison of existing patterns

Implementation

const re = new RegExp("^npm_[A-Za-z0-9]{36}$", "");
re.test(input);

Limits

  • Valide seulement la FORME : ne vérifie ni la somme de contrôle CRC32 des 6 derniers caractères, ni que le jeton existe ou est actif.
  • La longueur de 36 caractères après le préfixe n'est pas écrite dans l'annonce de GitHub : elle vient des détecteurs et d'un recoupement avec l'entropie annoncée. Les anciens jetons UUID (8-4-4-4-12 hexadécimaux), encore possibles dans de vieux fichiers, sont rejetés.
  • Un fichier .npmrc contient le jeton après « _authToken= » : extrayez-le avant de valider (la ligne entière est rejetée). Ne collez jamais un vrai jeton dans un outil en ligne.

Related pages