7Bitrot/gatsby-ignore-scripts-test
GitHub: 7Bitrot/gatsby-ignore-scripts-test
该项目是一个基于 Gatsby 的最小示例,用于测试和演示如何通过 LavaMoat 白名单机制安全地忽略 NPM 安装脚本。
Stars: 0 | Forks: 0
# 使用 Gatsby 进行 NPM ignore-scripts 测试
本项目使用了一个遵循 [Gatsby 快速入门指南](https://www.gatsbyjs.com/docs/quick-start/) 的基础模板。
## 目的
本项目旨在测试 OWASP NPM 安全最佳实践,即通过在 Gatsby 中[忽略 run-scripts 来最小化攻击面](https://cheatsheetseries.owasp.org/cheatsheets/NPM_Security_Cheat_Sheet.html#3-minimize-attack-surfaces-by-ignoring-run-scripts),并作为 Gatsby 委员会上此[讨论](https://github.com/gatsbyjs/gatsby/discussions/39579)的一个示例。
## 测试说明
克隆此仓库后,运行 `npx can-i-ignore-scripts` 并按照说明操作。它会提示你安装 `@lavamoat/allow-scripts`,并使用其输出来填充你的允许列表。
```
npm i -g @lavamoat/allow-scripts
allow-scripts setup
allow-scripts auto
npm pkg set scripts.setup='npm ci && allow-scripts'
```
此时,你应该会在 `package.json` 文件中看到类似于以下内容的配置。请注意,确切的 `lavamoat.allowScripts` 条目取决于你已安装的包版本和插件集:
```
{
"lavamoat": {
"allowScripts": {
"@lavamoat/preinstall-always-fail#3.0.0": false,
"gatsby#5.16.1": false,
"gatsby-plugin-sharp>sharp#0.32.6": false,
"gatsby>@parcel/cache>lmdb#2.5.2": false,
"gatsby>@pmmmwh/react-refresh-webpack-plugin>core-js-pure#3.24.1": false,
"gatsby>core-js#3.48.0": false,
"gatsby>gatsby-cli#5.16.0": false,
"gatsby>lmdb#2.5.3": false,
"gatsby>lmdb>msgpackr>msgpackr-extract#3.0.3": false,
"gatsby>memoizee>es5-ext#0.10.64": false,
"sass>@parcel/watcher#2.5.6": false
}
}
}
```
```
ERROR UNKNOWN
Error in "/path/to/your/project/node_modules/gatsby-plugin-sharp/gatsby-node":
Something went wrong installing the "sharp" module
```
错误信息显示,必须允许 `sharp` 运行脚本才能正确安装,因此我们指示 `allow-scripts` 这样做:
```
{
"lavamoat": {
"allowScripts": {
...
- "gatsby-plugin-sharp>sharp#0.32.6": false,
+ "gatsby-plugin-sharp>sharp#0.32.6": true,
...
}
}
}
```
然后运行被允许的脚本:
`npm run setup`
最后,如果你再次运行该项目,它应该会正常工作。
## 附加测试
这是用户容易感到困惑的地方:
- 你如何知道哪些 post-install 脚本可以安全地忽略?
- 哪些应该被允许以确保 Gatsby 正常运行?
这时 `can-i-ignore-scripts` 就派上用场了。
再次运行 `npx can-i-ignore-scripts`,并在出现提示时接受建议。
```
{
"lavamoat": {
"allowScripts": {
"@lavamoat/preinstall-always-fail#3.0.0": false,
"gatsby#5.16.1": false,
"gatsby-plugin-sharp>sharp#0.32.6": false,
"gatsby>@parcel/cache>lmdb#2.5.2": false,
"gatsby>@pmmmwh/react-refresh-webpack-plugin>core-js-pure#3.24.1": false,
"gatsby>core-js#3.48.0": false,
"gatsby>gatsby-cli#5.16.0": false,
"gatsby>lmdb#2.5.3": false,
"gatsby>lmdb>msgpackr>msgpackr-extract#3.0.3": false,
"gatsby>memoizee>es5-ext#0.10.64": false,
"sass>@parcel/watcher#2.5.6": false,
+ "lmdb": true,
+ "@parcel/watcher": true,
+ "msgpackr-extract": true,
+ "sharp": true
}
}
}
```
由于 `@lavamoat/allow-scripts` 需要确切的版本才能正常工作,你需要手动在允许列表中进行一项更改:
```
{
"lavamoat": {
"allowScripts": {
"@lavamoat/preinstall-always-fail#3.0.0": false,
"gatsby#5.16.1": false,
+ "gatsby-plugin-sharp>sharp#0.32.6": true,
+ "gatsby>@parcel/cache>lmdb#2.5.2": true,
"gatsby>@pmmmwh/react-refresh-webpack-plugin>core-js-pure#3.24.1": false,
"gatsby>core-js#3.48.0": false,
"gatsby>gatsby-cli#5.16.0": false,
+ "gatsby>lmdb#2.5.3": true,
+ "gatsby>lmdb>msgpackr>msgpackr-extract#3.0.3": true,
"gatsby>memoizee>es5-ext#0.10.64": false,
+ "sass>@parcel/watcher#2.5.6": true,
- "lmdb": true,
- "@parcel/watcher": true,
- "msgpackr-extract": true,
- "sharp": true
}
}
}
```
之后,你的构建应该会_正常_运行。
`can-i-ignore-scripts` 会建议你检查其他包:
```
You have 4 packages identified as 'check' that are not yet allowed.
es5-ext,
gatsby,
gatsby-cli,
ljharb-monorepo-symlink-test
Please review these packages and update your
lavamoat allowlist in package.json as needed.
```
`es5-ext` 和 `ljharb-monorepo-symlink-test` 可以被忽略。我不确定是否需要允许 `gatsby` 和 `gatsby-cli`。
你可以在[这里](https://github.com/LavaMoat/LavaMoat/tree/main/packages/allow-scripts#readme)查看更多关于 `@lavamoat/allow-scripts` 工作原理的信息。
标签:Gatsby, MITM代理, Node包管理, 依赖安全, 暗色界面, 自定义脚本, 静态网站生成器