繁体   English   中英

如何升级所有全局子模块中的 npm 包

[英]How to upgrade an npm package in all global submodules

作为最近漏洞 (CVE-2021-3807) 的一部分, npmansi-regex需要在 docker 镜像的所有层中高于5.0.16.0.1

升级全局npm包的系统方法是什么?

命令npx npm-force-resolutions不适用于全局包( npm i -g或全局预安装的包)。

这是一个旧线程,但它可能仍然对某些人有所帮助。 答案是您不在最终的容器映像中使用 NPM。

例如,使用 Docker 多阶段构建,它将是这样的

# Build stage
FROM node:16-alpine3.15 as build

# Install dependencies
WORKDIR /
COPY package-lock.json .
COPY package.json .
RUN npm ci --production

# Final stage
FROM alpine:3.15 as final

# Upgrade APK
RUN apk --no-cache add --upgrade nodejs~16

# Setup application
RUN mkdir -p /app/simple-server
WORKDIR /app/simple-server
COPY . .
COPY --from=build node_modules node_modules

# Run App
ENTRYPOINT ["node", "index.js"]

你可以看到最终的镜像没有 NPM,它仍然容易受到 CVE-2021-3807 的攻击。

以下脚本在alpine linux 中使用 nodejs 14.8.1

主脚本进行了深度升级:

#!/usr/bin/env bash
find /usr/lib \
| grep ansi-regex/pack \
| sed -e 's/node_modules\/ansi-regex\/package.json$//' \
| xargs -I {} sh /root/scripts/upgrade_ansi_regex_in_submodule.sh {}

其中脚本/root/scriptsupgrade_ansi_regex_in_submodule.sh修复了每个全局子模块:

#!/usr/bin/env bash
# Fixes Vulnerability CVE-2021-3807 in the given npm (global) package in given folder
# $1 = base directory for the submodule

set -e

printf "Fixing vulunerability of ansi-regex in $1 \n"
cd $1
ls ./node_modules/ansi-regex

printf "The Before package version:"
cat node_modules/ansi-regex/package.json | grep "version\":"

npm i -f ansi-regex@5.0.1 --save

printf "The After package version:"
cat node_modules/ansi-regex/package.json | grep  "version\":"

printf "... done $1\n-------\n\n"

暂无
暂无

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

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