[BUG] `npm install-scripts approve` saves an absolute path for a local tarball dependency, so the approval breaks on other machines

Đang mở
#10,354 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 2 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
Nửa ngày
Mức phù hợp với người mới
35/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
javascript, nodejs
Lĩnh vực
tooling

Hướng nghiên cứu

Start with the npm install-scripts approve command and the allowScripts keys it writes to package.json, then reproduce with the script in the issue, which copies the project to a new folder and reinstalls. Draft PR #9794 is cited as already changing how these keys are written, so read it first to see what it does. Done means approved local tarball keys are project-relative, like file:../tgzdep-1.0.0.tgz, and both postinstall scripts still run in the moved copy.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Bug Needs Triage Triage Requested
Is there an existing issue for this?
  • I have searched the existing issues
This issue exists in the latest npm version
  • I am using the latest npm
This is not just a request to bump a dependency for a CVE
  • This is not solely a request to bump a dependency for a CVE
Current Behavior

When you approve the install script of a dependency installed from a local tarball, npm install-scripts approve saves the tarball's absolute path as the allowScripts key:

"allowScripts": {
  "file:../../dirdep": true,
  "file:C:\\Users\\<user>\\work\\tgzdep-1.0.0.tgz": true
}

That key only matches on the machine and in the folder where it was approved. After a clone, on CI, or when the project is moved, the approval no longer applies and the script is blocked again:

npm warn install-scripts 1 package had install scripts blocked because they are not covered by allowScripts:
npm warn install-scripts   [email protected] (postinstall: ...)

It also commits the approver's local username and folder layout into package.json.

A local directory dependency approved the same way keeps working after the move. Its key (file:../../dirdep) is relative, although it's relative to node_modules rather than to the project, unlike the file:../dirdep spec in dependencies.

Expected Behavior

npm install-scripts approve saves a key relative to the project, like the spec in dependencies (file:../tgzdep-1.0.0.tgz, file:../dirdep), so a committed approval keeps working on every machine.

I checked out draft PR #9794 and ran the same steps with it. It saves file:../dirdep and file:../tgzdep-1.0.0.tgz, and both scripts run in the moved copy. So #9794 appears to fix this, and this issue gives it a user-facing repro.

Steps To Reproduce
mkdir proj dirdep tgzsrc
echo '{"name":"dirdep","version":"1.0.0","scripts":{"postinstall":"node -e \"require(`fs`).writeFileSync(`ran`,``)\""}}' > dirdep/package.json
echo '{"name":"tgzdep","version":"1.0.0","scripts":{"postinstall":"node -e \"require(`fs`).writeFileSync(`ran`,``)\""}}' > tgzsrc/package.json
(cd tgzsrc && npm pack && mv tgzdep-1.0.0.tgz ..)

cd proj && echo '{"name":"proj","version":"1.0.0"}' > package.json
npm install ../dirdep ../tgzdep-1.0.0.tgz
npm install-scripts approve --all
cat package.json      # allowScripts has an absolute file:<...>/tgzdep-1.0.0.tgz key

# simulate a teammate's clone: copy the project somewhere else
cd .. && mkdir ../moved && cp -r proj dirdep tgzdep-1.0.0.tgz ../moved/
cd ../moved/proj && rm -rf node_modules && npm install
# -> tgzdep's postinstall is blocked again; dirdep's runs
Environment
  • npm: 12.2.3 (latest branch)
  • Node.js: 24.20.0
  • OS Name: Windows 11 (the absolute path is file:C:\\...; on POSIX I'd expect file:/home/... with the same effect, but I've only tested Windows)
  • Related: draft PR #9794 changes how these keys are written and fixes this in my test.
Ngôn ngữ chính
JavaScript
Star
10.3k
Fork
5k
Merge trung bình
4 ngày 11 giờ
Pull request đã merge (30 ngày)
26

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của npm/cli

Tất cả issue của npm/cli

Issue tương tự

Thêm issue về JavaScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.