DEV Community

naugtur
naugtur

Posted on • Edited on

9 1

Get safe and remain productive with can-i-ignore-scripts

So you've heard about supply chain security? You should no longer want to run npm ci without the --ignore-scripts flag. If you still need more convincing - I made a package that makes typescript compiler inject malicious code to your application. See this video for more

Now with the good advice to always run npm ci --ignore-scripts all that's left is to start using it. But wait! My app no longer works! Some of these scripts were necessary after all...

All you need to do is npm rebuild packagename. If you know which package needs its scripts to run.

That's the problem with security best practices - there's always convenience that you need to give away. But that's where I come in! I enjoy making security best practices easier to deploy in your team. (That's why I wrote npm-audit-resolver but that's a different story)

--ignore-scripts and no regrets

Figuring out which of your packages actually need their postinstall scripts is not a candidate to make the list of "top 5 moments at work". I bet you'll be happy to hear there's a tool to do it for you.

can-i-ignore-scripts is a commandline utility that scans your node_modules folder and lists all packages with scripts that run on installation. It then checks against a collection of known packages whose scripts were already evaluated before and lists suggestions.

You can use the suggestions to quickly choose which scripts to run after the installations have finished.

There's nothing wrong with accepting some scripts you're not sure about. The goal of having an allowlist is to avoid running a script that was maliciously added where it wasn't before. The sooner you introduce --ignore-scripts to your app the better. Even if that means adding exceptions for ALL existing packages.

Quick roll out in your project? Here you go:

npm ci --ignore-scripts
npx can-i-ignore-scripts


█▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀█
  ▄▄·  ▄▄▄·  ▐ ▄    ▄     ▪    ▄▄     ▐ ▄       ▄▄▄   ▄▄▄      ▄▄▄▄·
 ▐█ ▌ ▐█ ▀█ ·█▌▐█   ██    ██  ▐█ ▀    █▌▐█      ▐▄ █· █  ▀·  .▀  .█▌
 ██ ▄▄▄█▀▀█ ▐█▐▐▌   ▐█·   ▐█· ▄█ ▀█▄ ▐█▐▐▌ ▄█▀▄ ▐▀▀▄ ▐█▀      ▄█▀▀▀·
 ▐███▌▐█ ▪▐▌██▐█▌   ▐█▌   ▐█▌ ▐█▄ ▐█ ██▐█▌▐█▌.▐▌▐▄ █▌▐█▄▄▄▌   ▀
 ·▀▀▀  ▀  ▀ ▀▀ █▪   ▀▀▀   ▀▀▀ ·▀▀▀▀  ▀▀ █▪ ▀█▄▀▪.▀  ▀ ▀▀▀     ▀

▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
Found following packages with scripts:
[ ignore ] 'pre-commit' has scripts but they can be ignored 
             reason: used in local development
[ ignore ] 'core-js' has scripts but they can be ignored 
             reason: funding
[  keep  ] 'grpc' needs its scripts to run
[  keep  ] 'bignum' needs its scripts to run

▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
What now? Run rebuild after 'npm ci --ignore-scripts' to trigger scripts you need to keep. 

A suggestion to get you started:
npm rebuild bignum grpc
█▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄█
Enter fullscreen mode Exit fullscreen mode

Then take a look at the recommendations and add the rebuild line after the installation in CI

npm rebuild bignum grpc
Enter fullscreen mode Exit fullscreen mode

Go hack yourself

Don't know if postinstall scripts actually run in your CI? Or maybe you need to convince someone it'a an issue? Hack yourself by installing @naugtur/pentest-my-ci as your dev dependency. It contains a preinstall script that stops the CI and tries to scare the reader of the logs a bit.

JavaScript tools that help you ship faster

JavaScript tools that help you ship faster

Skip boilerplate and focus on features. Kinde handles auth, access control, and billing behind the scenes.

Get a free account

Top comments (0)

Tiger Data image

🐯 🚀 Timescale is now TigerData: Building the Modern PostgreSQL for the Analytical and Agentic Era

We’ve quietly evolved from a time-series database into the modern PostgreSQL for today’s and tomorrow’s computing, built for performance, scale, and the agentic future.

So we’re changing our name: from Timescale to TigerData. Not to change who we are, but to reflect who we’ve become. TigerData is bold, fast, and built to power the next era of software.

Read more

👋 Kindness is contagious

Dive into this compelling post celebrated by our lively DEV Community. Developers everywhere are invited to share insights and uplift our collective expertise.

A simple “thank you” can make someone’s day—drop your appreciation in the comments!

On DEV, sharing expertise sparks growth and tightens our community bonds. Found this helpful? A quick nod to the author goes a long way.

Get started