npm@6 安全审核

npm@6 引入了自动安全审核功能,在安装或更新依赖时会生成漏洞报告。通过报告提示,大部分漏洞可以自动修复,对于剩余问题,可手动执行 'npm audit fix' 查看并解决依赖包的安全问题。此特性增强了前端开发中的安全性,但面对复杂的依赖关系,npm 仍需优化以实现更轻量级的安全解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

run npm audit fix to fix them, or npm audit for details

npm 升级到 @6 以后, 在项目中更新或者下载新的依赖包, 控制台会自动运行 npm audit, 对项目依赖包进行安全审核,并生成漏洞报告在控制台中显示:

added 1864 packages from 947 contributors and audited 29947 packages in 218.903s
found 395 vulnerabilities (350 low, 42 moderate, 3 high)run `npm audit fix` to fix them, or `npm audit` for details 

我们可以通过漏洞报告的提示信息来修补程序漏洞:

# 执行 npm audit fix
# 此命令将自动更新有安全漏洞的依赖项
npm audit fix

# 运行结果
# 加载了30个依赖包
# 删除了7个依赖包
# 更新了38个依赖包
# 移动了1个依赖包
# 修正了29947个扫描包中395个漏洞中的298个

+ babel-plugin-transform-es2015-modules-commonjs@6.26.2
+ babel-plugin-dynamic-import-node@1.2.0
+ express@4.16.3
+ antd@3.6.3
+ babel-cli@6.26.0
+ babel-preset-env@1.7.0
+ babel-core@6.26.3
+ compression@1.7.2
+ lodash@4.17.10
added 30 packages from 49 contributors, removed 7 packages, updated 38 packages and moved 1 package in 29.292s
fixed 298 of 395 vulnerabilities in 29947 scanned packages7 package updates for 97 vulns involved breaking changes(use `npm audit fix --force` to install breaking changes; or do it by hand) 

通过提示系统自动修补了大部分漏洞, 那剩下的怎么办呢?

# 继续执行控制台给的提示命令
npm audit fix --force

# 运行结果
# 加载了33依赖包
# 删除了17个依赖包
# 更新了8个依赖包
# 修正了29771个扫描包中16个漏洞

added 33 packages from 68 contributors, removed 17 packages and updated 8 packages in 171.584s
fixed 16 of 16 vulnerabilities in 29771 scanned packages4 package updates for 14 vulns involved breaking changes(installed due to `--force` option) 

目前为止我们执行了2个命令来修复漏洞, 那手动执行 npm audit 看看目前依赖包还有什么问题;

...
...
┌───────────────┬──────────────────────────────────────────────────────────────┐
│ Moderate│ Memory Exposure│
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Package │ tunnel-agent │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Patched in│ >=0.6.0│
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Dependency of │ image-webpack-loader [dev] │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Path│ image-webpack-loader > imagemin-webp > cwebp-bin > │
│ │ bin-wrapper > download > caw > tunnel-agent│
├───────────────┼──────────────────────────────────────────────────────────────┤
│ More info │ https://nodesecurity.io/advisories/598 │
└───────────────┴──────────────────────────────────────────────────────────────┘
found 9 moderate severity vulnerabilities in 32545 scanned packages9 vulnerabilities require manual review. See the full report for details.

# 在32545个扫描包中发现了9个中度严重缺陷
# 漏洞需要手动检查. 详情请参阅完整报告. 

上面详细信息表述了 tunnel-agent 出现的问题,并且 指出了依赖路径

image-webpack-loader > imagemin-webp > cwebp-bin >bin-wrapper > download > caw > tunnel-agent 

访问提示链接https://nodesecurity.io/advisories/598 里面给出了解决方案

Remediation

tunnel-agent Update to version 0.6.0 or later. 

结语

现在安全问题是不容忽视的, npm 安装的依赖包错中复杂. 往往安装一个包, 会自动安装一大堆相关联的依赖包, 这个时候开发者是不易察觉的; 就算开发者有安全意识, 但是也缺乏解决安全漏洞的手段. npm@6自带的 npm audit 也是帮助开发者解决基本的安全漏洞问题; 但是最终希望的是 npm 能不能瘦瘦身!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值