当运行npm install 命令的时候带上ignore-scripts,会发生什么?

x33g5p2x  于2022-06-08 转载在 其他  
字(2.0k)|赞(0)|评价(0)|浏览(209)

**摘要:**运行npm install 命令的时候带上ignore-scripts, 可以避免一个恶意包里的病毒。

本文分享自华为云社区《运行npm install 命令的时候带上--ignore-scripts有什么好处吗?》,作者:gentle_zhou。

在npm依赖库里,当前有超过130多万的依赖包可以被下载使用(数据来源:https://en.wikipedia.org/wiki/Npm_(software) )。但其中会不会有一些是恶意依赖包呢,这真的说不定。

因此在Predicting the Future of the Web Development (2020 and 2025)这场演讲中,推荐我们设置:npm config set ignore-scripts true:

或则就如我们标题里说的,在安装依赖包时,确保添加–ignore-scripts后缀以禁止npm里第三方依赖包的预先安装脚本或则安装后脚本被执行;这样就可以避免一个恶意包里的病毒。

但是问题来了,有些依赖包就是需要这些预先安装脚本或则安装后脚本来配置环境;如果如果我们在下载的时候,命令里用了–ignore-scripts后缀,那么是可以减轻恶意代码的危害,但同时也会导致下载的依赖包没有正常发挥作用。

那么我们如何知道什么时候可以使用–ignore-scripts后缀享受它的好处呢?就是说我们怎么样可以预先了解哪些依赖包需要脚本文件,我们不能使用–ignore-scripts后缀呢?

我们可以预先先去下载can-i-ignore-scripts 这个依赖包:
A service and cli to analyze your dependencies and check what’ll break when you switch from npm ci to npm ci --ignore-scripts

can-i-ignore-scripts工具,可以帮助我们分析各个依赖包是否可以使用--ignore-scripts命令。该工具可以帮助我们确定哪些依赖包可以使用--ignore-scripts命令;实际上呢,一些我们已经运行了几个月的脚本是没啥问题的,而该工具可以帮助我们确定新加入的依赖包是否可以加入到没问题列表中。

如何下载这个工具呢?很简单,可以直接在工具官网下载或则通过npx命令行npx can-i-ignore-scripts (npx是什么?后面计划会出一篇文章介绍一下;现在可以就理解成是npm的一个包执行器,能让npm包中的命令行工具和其他可执行文件在使用上变得更加简单)。

如何使用这个工具呢?我们去安装了node_modules包的目录下运行can-i-ignore-scripts命令去查看(下面是官方给出的例子):

naugtur@localtoast:~/repo/ [main]$ can-i-ignore-scripts 
█▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀█
  ▄▄·  ▄▄▄·  ▐ ▄    ▄     ▪    ▄▄     ▐ ▄       ▄▄▄   ▄▄▄     ·▄▄▄▄•
 ▐█ ▌▪▐█ ▀█ •█▌▐█   ██    ██  ▐█ ▀    █▌▐█      ▐▄ █· █  ▀·  .▀· .█▌
 ██ ▄▄▄█▀▀█ ▐█▐▐▌   ▐█·   ▐█· ▄█ ▀█▄ ▐█▐▐▌ ▄█▀▄ ▐▀▀▄ ▐█▀      ▄█▀▀▀•
 ▐███▌▐█ ▪▐▌██▐█▌   ▐█▌   ▐█▌ ▐█▄ ▐█ ██▐█▌▐█▌.▐▌▐▄ █▌▐█▄▄▄▌   ▀
 ·▀▀▀  ▀  ▀ ▀▀ █▪   ▀▀▀   ▀▀▀ ·▀▀▀▀  ▀▀ █▪ ▀█▄▀▪.▀  ▀ ▀▀▀     ▀

▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
Found following packages with scripts:
[ ignore ] 'monorepo-symlink-test' has scripts but they can be ignored 
             reason: false positive
[ ignore ] 'ejs' has scripts but they can be ignored 
             reason: funding
[ ignore ] 'core-js' has scripts but they can be ignored 
             reason: funding

参考资料

  1. https://www.youtube.com/watch?v=24tQRwIRP_w&t=923s
  2. https://stackoverflow.com/questions/59471962/how-does-npm-behave-differently-with-ignore-scripts-set-to-true
  3. https://www.npmjs.com/package/can-i-ignore-scripts

点击关注,第一时间了解华为云新鲜技术~

相关文章

微信公众号

最新文章

更多