簡體   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