Skip to main content

Common Misconfigurations

  1. Running untrusted packages’ scripts automatically
  2. Using unsafe script commands
  3. Not validating environment variables in scripts
  4. Missing script injection protection
  5. Excessive permissions in scripts

Vulnerable Example

Secure Solution

Key Commands for Managing Scripts

Understanding these commands is critical for implementing the security practices described above.

1. Running a Script

This is the standard command to execute a script defined in your package.json scripts object.
Note: Some common scripts like test, start, stop, and restart have shortcuts and can be run without the run keyword (e.g., npm test).

2. Disabling All Lifecycle Scripts

This is the most important command for securing your environment, as shown in the .npmrc solution. It prevents potentially malicious preinstall, install, and postinstall scripts from running automatically when you add packages.
After running this, all package scripts must be triggered manually, giving you a chance to vet them.

3. Running npm install Safely (One Time)

If you don’t want to set ignore-scripts permanently but want to install a new, untrusted package safely, you can use the --ignore-scripts flag for a single command:
This will install the package without running any of its lifecycle scripts.

4. Listing All Available Scripts

To see all scripts defined in the package.json, simply run:
This will list all available script names, which is useful for knowing what you can run manually.

5. Passing Arguments to Scripts

This is essential for the secure pattern of using Node.js scripts. To pass arguments (like --validate in the example), you must use -- to separate the npm command from the arguments you want to pass to your script.
Example from “Secure Solution”:
Any arguments before the -- are for npm itself; any arguments after are for your script.

Best Practices

  • Set ignore-scripts=true in .npmrc.
  • Validate all environment variables.
  • Use dedicated script files instead of inline commands.
  • Avoid shell command execution when possible.
  • Implement proper input validation.
  • Never use eval() or Function() with user input.