审计包依赖是否存在安全漏洞
See Details
目录
关于安全审计
¥About security audits
安全审计是对安全漏洞的包依赖的评估。安全审计使你能够发现并修复依赖中可能导致数据丢失、服务中断、未经授权访问敏感信息或其他问题的已知漏洞,从而帮助你保护包的用户。
¥A security audit is an assessment of package dependencies for security vulnerabilities. Security audits help you protect your package's users by enabling you to find and fix known vulnerabilities in dependencies that could cause data loss, service outages, unauthorized access to sensitive information, or other issues.
使用 npm audit
运行安全审计
¥Running a security audit with npm audit
注意:npm audit
命令在 npm@6 中可用。要升级,请运行 npm install npm@latest -g
。
¥Note: The npm audit
command is available in npm@6. To upgrade, run npm install npm@latest -g
.
npm audit
命令 将你的包中配置的依赖的描述提交到你的默认注册表,并要求报告已知漏洞。npm audit
检查直接 dependencies、devDependencies、bundledDependencies 和 optionalDependencies,但不检查 peerDependencies。
¥The npm audit
command submits a description of the dependencies configured in your package to your default registry and asks for a report of known vulnerabilities. npm audit
checks direct dependencies, devDependencies, bundledDependencies, and optionalDependencies, but does not check peerDependencies.
当你使用 npm install
安装包时,npm audit
会自动运行。你还可以在你的 本地安装的包 上手动运行 npm audit
以对包进行安全审计,并生成依赖漏洞报告,如果可用,建议补丁。
¥npm audit
automatically runs when you install a package with npm install
. You can also run npm audit
manually on your locally installed packages to conduct a security audit of the package and produce a report of dependency vulnerabilities and, if available, suggested patches.
-
在命令行上,输入
cd path/to/your-package-name
并按 Enter 键导航到包目录。¥On the command line, navigate to your package directory by typing
cd path/to/your-package-name
and pressing Enter. -
确保你的包中包含
package.json
和package-lock.json
文件。¥Ensure your package contains
package.json
andpackage-lock.json
files. -
输入
npm audit
并按 Enter 键。¥Type
npm audit
and press Enter. -
查看审计报告并运行推荐的命令或在需要时进一步调查。
¥Review the audit report and run recommended commands or investigate further if needed.
解决 EAUDITNOPJSON
和 EAUDITNOLOCK
错误
¥Resolving EAUDITNOPJSON
and EAUDITNOLOCK
errors
npm audit
要求包有 package.json
和 package-lock.json
文件。
¥npm audit
requires packages to have package.json
and package-lock.json
files.
-
如果出现
EAUDITNOPJSON
错误,请按照“创建 package.json 文件”中的步骤创建package.json
文件。¥If you get an
EAUDITNOPJSON
error, create apackage.json
file by following the steps in "Creating a package.json file". -
如果遇到
EAUDITNOLOCK
错误,请确保你的包有package.json
文件,然后通过运行npm i --package-lock-only
创建包锁定文件。¥If you get an
EAUDITNOLOCK
error, make sure your package has apackage.json
file, then create the package lock file by runningnpm i --package-lock-only
.
审查和处理安全审计报告
¥Reviewing and acting on the security audit report
运行 npm audit
将生成一份安全漏洞报告,其中包含受影响的包名称、漏洞严重性和描述、路径和其他信息,以及(如果可用)应用补丁来解决漏洞的命令。审计报告中各字段的详细信息,请参见“关于审计报告”
¥Running npm audit
will produce a report of security vulnerabilities with the affected package name, vulnerability severity and description, path, and other information, and, if available, commands to apply patches to resolve vulnerabilities. For more information on the fields in the audit report, see "About audit reports"
发现建议更新的安全漏洞
¥Security vulnerabilities found with suggested updates
如果发现安全漏洞并且有可用更新,你可以:
¥If security vulnerabilities are found and updates are available, you can either:
-
运行
npm audit fix
子命令以自动为易受攻击的依赖安装兼容更新。¥Run the
npm audit fix
subcommand to automatically install compatible updates to vulnerable dependencies. -
单独运行推荐的命令以安装易受攻击的依赖的更新。(某些更新可能是 semver 破坏性更改;有关详细信息,请参阅“SEMVER 警告”。)
¥Run the recommended commands individually to install updates to vulnerable dependencies. (Some updates may be semver-breaking changes; for more information, see "SEMVER warnings".)

SEMVER 警告
¥SEMVER warnings
如果建议的操作是潜在的重大更改(语义版本重大更改),则后面会出现 SEMVER WARNING
,显示“SEMVER 警告:建议采取的行动是一项潜在的重大改变”。如果存在漏洞的包更改了其 API,你可能需要对包的代码进行额外的更改。
¥If the recommended action is a potential breaking change (semantic version major change), it will be followed by a SEMVER WARNING
that says "SEMVER WARNING: Recommended action is a potentially breaking change". If the package with the vulnerability has changed its API, you may need to make additional changes to your package's code.
发现需要人工审核的安全漏洞
¥Security vulnerabilities found requiring manual review
如果发现安全漏洞,但没有可用的补丁,审核报告将提供有关漏洞的信息,以便你进一步调查。
¥If security vulnerabilities are found, but no patches are available, the audit report will provide information about the vulnerability so you can investigate further.

要解决该漏洞,你可以
¥To address the vulnerability, you can