npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jupiterone/npm-enforce-policy

v1.2.1

Published

Scan a user's NPM tokens and profile and fail with exit code 1 if any fail to comply with policy.

Downloads

251

Readme

npm-enforce-policy

This project is a superset of @jupiterone/npm-enforce-age.

This is a tool meant to help enforce good security hygiene around NPM tokens by reminding users when it is time to revoke tokens that are out-of-spec with corporate policy. If called with no arguments, it will list the NPM tokens for the currently-logged-in NPM user, and audit them according to the following default policy:

| Setting | Value | | --------------------- | ----- | | maxAgeInDays | 30 | | allowPublishTokens | false | | allowAutomationTokens | false | | allow2FADisabled | false | | allow2FAAuthOnly | true | | allow2FAAuthAndWrites | true | | bannedUserList | '' |

To use these defaults, simply do:

npx @jupiterone/npm-enforce-policy

The script will exit cleanly with exit code 0 upon success, or emit warning messages and exit with code 1 if one or more of your issued tokens is older than the maxAgeInDays limit, or has write permissions of publish or automation type. This makes it convenient to put this script in a package.json lifecycle hook, (say, prepublish), or some other local build script. A few examples:

 "scripts": {
    "prepublish": "npx @jupiterone/npm-enforce-policy",
 }

or:

#!/bin/bash
set -e
npx @jupiterone/npm-enforce-policy
...rest of build script...

or perhaps:

npx @jupiterone/npm-enforce-policy || exit 1
...rest of script...

For use inside a local NodeJS script, you might do:

const { validateTokenMaxAge, validateTokenPermissions, validateProfile2FASetting } = require('@jupiterone/npm-enforce-policy');

const policy = {
  maxAgeInDays: 15,
  allowPublishTokens: false,
  allowAutomationTokens: true
  allow2FADisabled: false,
  allow2FAAuthOnly: true,
  allow2FAAuthAndWrites: false,
};

if (!validateTokenMaxAge(policy) || !validateTokenPermissions(policy) || !validateProfile2FASetting(policy))) {
  return;
}
...rest of script...

The npx form (as well as the bin/npm-enforce-policy script) support the following command-line arguments:

  • --max-age <days>, e.g. --max-age 30 to override the default age policy. Set to -1 to disable age checking. Default if unset: 30
  • --allow-publish-tokens <true/false> will set the policy for allowing non-automation tokens with write permissions. Default if unset: false
  • --allow-automation-tokens <true/false> will set the policy for allowing automation tokens with write permissions. Default if unset: false
  • --allow-2fa-disabled <true/false> will set the policy for allowing 2FA to be disabled for the NPM profile. Default if unset: false
  • --allow-2fa-auth-only <true/false> will set the policy for 2FA to allow the value 'auth-only'. Default if unset: true
  • --allow-2fa-auth-and-writes <true/false> will set the policy for 2FA to allow the value 'auth-and-writes'. Default if unset: true
  • --banned-user-list <csv users> will set the policy to ban specific NPM users. Default if unset: '' (allow all users)

Examples:

  1. npx @jupiterone/npm-enforce-policy will prompt to revoke any tokens that are older than 30 days, and any tokens with publishing authority. Additionally, it will prompt the user to enable some flavor of 2FA if it is disabled in their profile.

  2. npx @jupiterone/npm-enforce-policy --max-age 90 --allow-automation-tokens true will prompt to revoke any tokens that are older than 90 days, as well as any publishing tokens that are not of type automation. Additionally, it will prompt the user to enable some flavor of 2FA if it is disabled in their profile.

  3. npx @jupiterone/npm-enforce-policy --max-age -1 --allow-publish-tokens true --allow-2fa-auth-only false will allow all tokens, regardless of age, EXCEPT automation tokens. Additionally, it will prompt the user if their 2FA profile setting is anything other than 'auth-and-writes'.

  4. npx @jupiterone/npm-enforce-policy --banned-user-list some-cicd-user As 1. above, but also halt if the cicd user is being used outside of CI/CD.