Using npm audit
Share:
The Node Package Manager (npm) is a powerful tool that not only allows developers to manage and maintain their application dependencies but also offers several utilities to ensure the code's integrity and security. One of these utilities is the npm audit command.
The npm audit is a sub-command of npm, which analyzes your dependency tree, examining what’s actually been installed based on your package-lock.json or npm-shrinkwrap.json file. It then compares that to the available advisory database and finds any known vulnerabilities. Think of it as having your own security expert review your project and its dependencies, providing you with valuable insights into potential issues and risks.
Let's imagine that your project, a movie catalog application called "Starbuster", relies on a number of external packages. These packages may contain some security vulnerabilities, which, if left unchecked, could lead to serious problems like data leakage or unauthorized access. The npm audit tool can help you identify such problems and even propose fixes for them.
When you run npm audit
in your project's directory, the following steps will take place:
- Collecting all necessary details about your project’s dependencies.
- Checking an advisory database for any known vulnerabilities.
- Conducting a deep analysis of the application dependency tree.
- Outputting an audit report.
The audit report outlines all the found vulnerabilities, ranks them by severity, and suggests necessary actions you can take to fix them.
npm audit
After running the npm audit
command, you might get a report like this:
=== npm audit security report ===
# Movie Handler (HIGH severity)
Your ‘movie-handler’ package is suffering from Cross-Site Scripting (XSS) vulnerability. It throws an error message, catching users' session tokens.
Package: Movie-Handler < 4.4.0
Path: ouput > movie-handler
More info: https://nodesecurity.io/advisories/123
# Users Handler (MODERATE severity)
Your ‘users-handler’ package is suffering from a Prototype pollution vulnerability in lodash.
Package: Users-Handler < 0.20.3
Path: authentication > users-handler > lodash
More info: https://nodesecurity.io/advisories/234
# Total: 6 vulnerabilities (1 high, 3 moderate, 2 low, 0 info)
In the report, you will see information about the package names, their versions, severity, and a link to more information about the vulnerabilities.
Once you have the report, you can now decide the course of automation to take in resolving the detected vulnerabilities. One way to fix these issues is by using the npm audit fix
command.
npm audit fix
This command tries to upgrade every vulnerable package possible to a safe, non-breaking version. In case npm cannot find such upgrades, it tries to apply patches to make the package secure.
Sometimes, you may need to interactively review and manually decide on actions for every vulnerability. To do that, add the --audit-level
flag, specifying which level of vulnerabilities to show.
npm audit fix --audit-level=moderate
The above command will display only Moderate, High, and Critical vulnerabilities.
In cases where npm audit fix
cannot automatically fix the packages, we may need to perform manual updates. Most of the manual methods would involve upgrading a vulnerable package to a non-vulnerable version, which lies outside of your defined version range.
For instance, let's imagine that the 'movie-handler' package version in your project is <4.4.0, determined from the audit, and the vulnerability only gets fixed in versions >=4.4.0.
Here is what you might need to do:
npm uninstall movie-handler
npm install movie-handler@latest
You can also use the npm update
command to update a package to the latest version:
npm update movie-handler
Managing software dependencies might seem like a tough job, especially when dealing with security vulnerabilities. However, npm provides valuable tools like npm audit, which eases the developer's process of identifying and fixing potential security issues.
While depending heavily on external packages is beneficial for faster and efficient development, it's essential to ensure the security of our applications. That's where npm audit comes into play, acting as a great tool to fortify the applications.
In summary, npm audit is a significant part in our journey of developing secure Node.js applications. By effectively using this command, you can eliminate a lot of risks that come from dependencies, ensuring your application, like our "Starbuster" movie catalog, stays secure and reliable.
Remember, regular auditing should be an integral part of your application development process, ensuring you don't overlook any dangerous vulnerability lurking in the depths of your dependencies.
0 Comment
Sign up or Log in to leave a comment