Fixing security vulnerabilities in npm dependencies in less than 3 mins (2024)

Vivek Nayyar

Posted on

Fixing security vulnerabilities in npm dependencies in less than 3mins (3) Fixing security vulnerabilities in npm dependencies in less than 3mins (4) Fixing security vulnerabilities in npm dependencies in less than 3mins (5) Fixing security vulnerabilities in npm dependencies in less than 3mins (6) Fixing security vulnerabilities in npm dependencies in less than 3mins (7)

#javascript #security #vulnerabilities #npm

Hola people!!! 🥑

It’s been a while since I have written a blog and now since most of us are working from home we sort of have a considerable amount of time at hand and I thought why not write about my recent experience of fixing a security vulnerability.

So if any of you in the recent time have seen something like this image below and have no clue how to fix it then this article is for you. When I saw it, I had no clue either but with some research I could fix this.

🔬 Problem:

Fixing security vulnerabilities in npm dependencies in less than 3mins (8)github security vulnerability bot alert

So what this means is one of the dependencies in your package.json has some security implications which can be exploited by an attacker and can cause problems for you, your product or the company you work for.

For example: https://snyk.io/vuln/npm:eslint:20180222

This vulnerability could have caused a Regular Expression Denial of Service

💡 Finding:

In order to find potential vulnerabilities in your repo, you can either do

  1. npm audit — which should show you an output like the following image:

Fixing security vulnerabilities in npm dependencies in less than 3mins (9)npm audit log

2) Github security policy can also notify you — something like the following image:

Fixing security vulnerabilities in npm dependencies in less than 3mins (10)github security alert

Today when I started working I had to deal with this error where acorn and minimist were being reported as security vulnerabilities.

🎉 Solution

Solution to this problem is in steps:-

📦 npm update

1) This is the first thing you should do and it's the simplest one too.

  • Run npm update — https://docs.npmjs.com/cli-commands/update.html

  • Delete your package-lock.json file or for yarn users, delete your yarn.lock file. In an ideal world this would work, but there might be some dependency which does not follow semver and might get updated too.

  • So a better solution here would be to only delete the lines corresponding to the vulnerable package in your package-lock.json(or yarn.lock) file.

  • Run npm install again

In an ideal scenario, this should have upgraded your dependencies to the next semver version and those libraries might have already fixed the version of there transitive dependencies.

🔭 npm audit

2) But if that did not fix your issue, which for minimist did not fix for me, then follow the below mentioned steps:

2.1) To fix any dependency, you need to first know which npm package depends on that.

npm audit

This will tell you the packages which are vulernable.

Fixing security vulnerabilities in npm dependencies in less than 3mins (11)npm audit log

This tells me that minimist is required by mkdirp and that is required by mocha

A quick glance into package-lock.json can give you more information around the affected version.

In my case mocha(7.1.0) -> mkdirp(0.5.1) -> minimist(0.0.8) — the vulnerable version.

🔑 Resolutions key

3) And finally the fix was:

3.1) First npm install the non-vulnerable version, which in my case was 1.2.5

npm install minimist --save-dev

For yarn and npm users

3.2) Add a resolutions key in your package.json file

{ "resolutions": { "minimist": "^1.2.5" }}

For npm users, we need one more step for that resolutions key to work.

3.3) Use npm-force-resolutions (https://www.npmjs.com/package/npm-force-resolutions)

"scripts": { "preinstall": "npx npm-force-resolutions"}

3.4) After that run

 npm install

That’s it. That solves the dependency issues which can not be updated using either npm update or by uninstalling and reinstalling a new dependency.

To check if the dependency works correctly

npm ls minimist

This should give you an output like the image below

Fixing security vulnerabilities in npm dependencies in less than 3mins (12)npm ls command showing results of a dependency tree

🔥 Disclaimer

If some packages are only compatible with an older version, then this change might break your app. So be careful while resolving to a particular version and test your app before releasing this change.

👯 Share this article if you found it helpful!

You can follow me on twitter @VivekNayyar09 for more updates.

Also please don’t forget to maintain social distance to prevent the virus from spreading and wash your hands regularly. Stay safe and stay at home.

Top comments (5)

Subscribe

Phil Ashby

Phil Ashby

30+ years of tech, retired from an identity intelligence company, now part-time with an insurance broker.Dev community mod - mostly light gardening & weeding out spam :)

  • Location

    Felixstowe, UK

  • Education

    M.Eng (hons) Electronics, MIET, MBCS, AMCIISec, CEH

  • Pronouns

    he/him

  • Work

    Willis Towers Watson (aka WTW) part-time.

  • Joined

Mar 19 '20

  • Copy link

Well timed Sir :)

We've had this exact report this week for a demo we host, passing on your step-by-step advice to our team

Namaste!

Vivek Nayyar

Vivek Nayyar

👋 Engineering Manager with 8 years' of experience building products for numerous domains like real estate, video-streaming, Fin-Tech, e-commerce and now Insurance Tech.

  • Location

    Berlin, Germany

  • Education

    B.E

  • Work

    Senior Software Engineer at Zalando

  • Joined

Mar 22 '20

  • Copy link

Thank you Phil. I hope it will help the team fix those security vulnerabilities

Carolyn Stransky

Carolyn Stransky

Software developer and sometimes still a journalist (she/her)

  • Email

    hello@workwithcarolyn.com

  • Location

    Berlin, Germany

  • Joined

Apr 21 '20

  • Copy link

Forgot to mention this earlier, but I've used this post multiple times to fix security vulnerabilities that pop up in my company's repos. Thank you so much for writing and sharing 🙏

Vivek Nayyar

Vivek Nayyar

👋 Engineering Manager with 8 years' of experience building products for numerous domains like real estate, video-streaming, Fin-Tech, e-commerce and now Insurance Tech.

  • Location

    Berlin, Germany

  • Education

    B.E

  • Work

    Senior Software Engineer at Zalando

  • Joined

Apr 21 '21

  • Copy link

I don't know how I missed this but thank you so much Carolyn.

Ibrahim Ezzy

Ibrahim Ezzy

  • Work

    Product Designer

  • Joined

Dec 20 '20

  • Copy link

If I do npm install on a fresh codebase without package-lock.json or node_modules it throws an error:

Local package.json exists, but node_modules missing, did you mean to install?

For further actions, you may consider blocking this person and/or reporting abuse

Fixing security vulnerabilities in npm dependencies in less than 3 mins (2024)
Top Articles
Cryptocurrency and Sustainability: Navigating the Green Frontier
Diversifying Your Property Investment Portfolio
Rabbits Foot Osrs
oklahoma city for sale "new tulsa" - craigslist
Craigslist Parsippany Nj Rooms For Rent
Acts 16 Nkjv
Owatc Canvas
CA Kapil 🇦🇪 Talreja Dubai on LinkedIn: #businessethics #audit #pwc #evergrande #talrejaandtalreja #businesssetup…
AB Solutions Portal | Login
Dark Souls 2 Soft Cap
Select Truck Greensboro
Transformers Movie Wiki
Ree Marie Centerfold
Indiana Immediate Care.webpay.md
Wgu Admissions Login
Non Sequitur
Michael Shaara Books In Order - Books In Order
Driving Directions To Bed Bath & Beyond
Toy Story 3 Animation Screencaps
Northeastern Nupath
Everything you need to know about Costco Travel (and why I love it) - The Points Guy
My Homework Lesson 11 Volume Of Composite Figures Answer Key
Www.publicsurplus.com Motor Pool
Unforeseen Drama: The Tower of Terror’s Mysterious Closure at Walt Disney World
Pirates Of The Caribbean 1 123Movies
C&T Wok Menu - Morrisville, NC Restaurant
Wics News Springfield Il
Weve Got You Surrounded Meme
Democrat And Chronicle Obituaries For This Week
Jail Roster Independence Ks
Ewg Eucerin
Everything You Need to Know About Ñ in Spanish | FluentU Spanish Blog
+18886727547
Craigs List Tallahassee
R3Vlimited Forum
Word Trip Level 359
Los Amigos Taquería Kalona Menu
Covalen hiring Ai Annotator - Dutch , Finnish, Japanese , Polish , Swedish in Dublin, County Dublin, Ireland | LinkedIn
42 Manufacturing jobs in Grayling
Skyrim:Elder Knowledge - The Unofficial Elder Scrolls Pages (UESP)
Wisconsin Women's Volleyball Team Leaked Pictures
Gateway Bible Passage Lookup
Complete List of Orange County Cities + Map (2024) — Orange County Insiders | Tips for locals & visitors
Craigslist en Santa Cruz, California: Tu Guía Definitiva para Comprar, Vender e Intercambiar - First Republic Craigslist
The Attleboro Sun Chronicle Obituaries
Luciane Buchanan Bio, Wiki, Age, Husband, Net Worth, Actress
Random Animal Hybrid Generator Wheel
Ucla Basketball Bruinzone
What is 'Breaking Bad' star Aaron Paul's Net Worth?
The Blackening Showtimes Near Ncg Cinema - Grand Blanc Trillium
Maplestar Kemono
Ratchet And Clank Tools Of Destruction Rpcs3 Freeze
Latest Posts
Article information

Author: Barbera Armstrong

Last Updated:

Views: 6794

Rating: 4.9 / 5 (79 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Barbera Armstrong

Birthday: 1992-09-12

Address: Suite 993 99852 Daugherty Causeway, Ritchiehaven, VT 49630

Phone: +5026838435397

Job: National Engineer

Hobby: Listening to music, Board games, Photography, Ice skating, LARPing, Kite flying, Rugby

Introduction: My name is Barbera Armstrong, I am a lovely, delightful, cooperative, funny, enchanting, vivacious, tender person who loves writing and wants to share my knowledge and understanding with you.