简体   繁体   English

我们如何信任 npm 模块?

[英]How can we trust npm modules?

I'm using many Node.js modules through npm package manager.我通过 npm 包管理器使用了许多 Node.js 模块。 Since these modules are not developed by trusted organisations, are they trustworthy?由于这些模块不是由可信赖的组织开发的,它们是否值得信赖?

I don't know whether the npm team is doing any security checks for each module submitted by developers.不知道npm 团队有没有对开发者提交的每个模块做任何安全检查。

NPM is not doing any checks whatsoever. NPM没有做任何检查。 They are just a registry. 他们只是一个注册表。 The whole thing is built on the trust in the dev community and sharing. 整个事情建立在开发社区的信任和共享上。

Most node modules are open source and you can review their code in their repository (usually Github). 大多数节点模块都是开源的,您可以在其存储库(通常是Github)中查看其代码。 So that's the best way to 'trust' them. 这是“信任”他们的最佳方式。 Some node modules give you prebuilt native binaries, so that might be riskier in a way, but if it is popular (like ws for example) then I see no issue. 一些节点模块为您提供了预构建的本机二进制文件,因此在某种程度上可能会有风险,但如果它很受欢迎(例如ws ),那么我认为没有问题。 You can also check the NPM publisher user, which sometimes is a known company like Oracle. 您还可以检查NPM发布者用户,该用户有时是Oracle等知名公司。

The idea is to find the most popular npm modules. 我们的想法是找到最受欢迎的npm模块。 You can do this by checking the stars on each project. 你可以通过检查每个项目的星星来做到这一点。

Some tips: 一些技巧:

Use npm to manage dependencies in your dev environment, but not in your deployment scripts. 使用npm管理开发环境中的依赖项,但不能在部署脚本中管理依赖项。

Tools like npm are development tools. 像npm这样的工具是开发工具。 They're a convenient way to download and update modules. 它们是下载和更新模块的便捷方式。 They're not deployment tools, have never been deployment tools, and should not be used for deployment! 它们不是部署工具,从未使用过部署工具,也不应该用于部署!

Use npm shrinkwrap in the development repository and check in the result. 在开发存储库中使用npm shrinkwrap并检查结果。 This will lock your module versions in place, including sub-dependencies 这将锁定您的模块版本,包括子依赖项

More details here 更多细节在这里

There are a few programs, available from npm, that can run against your package.json and check for known vulnerabilities. npm中有一些程序可以针对你的package.json运行并检查已知的漏洞。 Not perfect, but a great start. 不完美,但是一个很好的开始。 The one I have used is called nsp but there are others. 我使用的那个叫做nsp,但还有其他的。

Update - June 2019 更新 - 2019年6月

In npm@6 security check is included. 在npm @ 6中包含安全检查。 You could run npm audit to recursively analyze your dependency trees to identify specifically what's insecure 您可以运行npm audit来递归分析您的依赖关系树,以明确识别哪些是不安全的

2016 version 2016版

You could use the nsp tool provided by Node Security Platform , which helps to audit all the modules from your package.json 您可以使用Node Security Platform提供的nsp工具,它有助于审核package.json中的所有模块

npm install nsp --global
nsp check

More info is here: https://nodesecurity.io/opensource 更多信息请访问: https//nodesecurity.io/opensource

Yes ! 是的! Almost all node modules are open source so you can actually view code snippets running behind module. 几乎所有节点模块都是开源的,因此您可以实际查看在模块后面运行的代码片段。 this might help you to build your trust on package you are willing to use in your application 这可能有助于您建立对您愿意在应用程序中使用的包的信任

It is not much secure because these modules are not developed by any organizations like what php/apache have, However it is good technology and you can also use nsp modules to check the security issues in you node modles. 它不是很安全,因为这些模块不是像php / apache那样的任何组织开发的,但它是很好的技术,你也可以使用nsp模块检查你的节点模块中的安全问题。

More info 更多信息

Actually I don't use to much packages: 其实我不用太多包裹:

1) express 1)表达
2) body & cookie-parser (sometimes I'm lazy to write middleware), 2)body&cookie-parser(有时候我懒得写中间件),
3) mongoose, 3)猫鼬,
4) pug, 4)哈巴狗,
5) request, 5)请求,
6) async, 6)异步,
7) lodash, 7)lodash,
8) string 8)字符串

all other stuff I write myself and put in "components" folder. 我写的所有其他东西,并放在“组件”文件夹中。

let's say most of people so lazy that do: 让我们说大多数人如此懒惰:

  const md5 = require('md5');
  let data = 'something';
  data = md5(data);

but I do it with crypto (it's by default included in all nodejs versions): 但是我使用crypto (默认情况下包含在所有nodejs版本中):

  const crypto = require('crypto');
  let data = 'something';
  data = crypto
           .createHash('md5')
           .update(data.toString())
           .digest('hex');

I keep logic to not to use package: 我保持逻辑不使用包:

1) if package is small (I always read package files if it's unknown for me package) 1)如果包很小(我总是读包文件,如果我的包不知道)
2) version is not above 1.0.0 (no warranty that will go further) 2)版本不高于1.0.0(不保证会更进一步)
3) no recent iterations (commits) in repository 3)存储库中没有最近的迭代(提交)

btw nsp check of my applications says: (+) No known vulnerabilities found (: btw我的应用程序的nsp check说: (+) No known vulnerabilities found (:

I've made node-safe , which allows you to use the native macOS sandbox when using node , npm and yarn :我制作了node-safe ,它允许您在使用nodenpmyarn时使用本机 macOS 沙箱:

# Allow reading files, but only in the current folder
node --enable-sandbox --allow-read="./**" myscript.js

# Run npm with sandbox (can only write to `./node_modules` by default)
npm --enable-sandbox install got 

When using the sandboxed package managers rogue dependencies are not able to compromise your system anymore through postinstall scripts and other means.使用沙盒包管理器时,恶意依赖项无法再通过postinstall脚本和其他方式危害您的系统。

If you are installing a package that you do not trust, you can avoid this vulnerability by running 如果要安装不信任的软件包,则可以通过运行来避免此漏洞

npm install --ignore-scripts

for more details check here 有关详细信息,请点击此处

Here is an awesome blog which can give you clear picture blog 这是一个很棒的博客,可以给你清晰的图片博客

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM