Photo by Erol Ahmed on Unsplash
node package manager - Vì sao nên định kỳ chạy npm audit? Một lý do khiến ta phải lưu package lock file trong source code với git
Table of contents
Phát hiện vấn đề
Một hôm đẹp trời, khi nhìn vào log CICD, chỗ output của npm i
:
xxx packages are looking for funding
run `npm fund` for details
x vulnerabilities (x moderate, x high)
To address all issues, run:
npm audit fix
Run `npm audit` for details.
Oh shit! Rõ ràng 1 tháng trước mình vừa chạy npm audit
và đã fix hết các lỗ hổng bảo mật rồi mà hôm nay lại lòi ra? Đó là vì dự án không thường xuyên chạy npm audit
. Ta thử chạy lại xem sao:
npm audit
Output:
# npm audit report
engine.io 5.1.0 - 6.4.1
Severity: moderate
engine.io Uncaught Exception vulnerability - https://github.com/advisories/GHSA-q9mw-68c2-j6m5
fix available via `npm audit fix`
node_modules/engine.io
socket.io 4.1.0 - 4.6.0-alpha1
Depends on vulnerable versions of engine.io
node_modules/socket.io
yaml 2.0.0-5 - 2.2.1
Severity: high
Uncaught Exception in yaml - https://github.com/advisories/GHSA-f9xv-q969-pqx4
fix available via `npm audit fix`
node_modules/yaml
3 vulnerabilities (2 moderate, 1 high)
To address all issues, run:
npm audit fix
Đơn giản là chạy:
npm audit fix
Ta thấy package-lock.json
đã update với git diff
. Xác nhận lại bằng cách chạy npm audit
1 lần nữa, vấn đề được giải quyết! Đến đây có thể xác định rằng npm audit
nên được chạy định kỳ 1 khoảng thời gian nhất định.
Expected output:
found 0 vulnerabilities
Tìm hiểu sâu hơn
Ta thấy package-lock.json
đã được update mà không phải package.json
là do ta hay viết devDependencies
or dependencies
:
{
"devDependencies": {
"xxx-package-name": "^1.1.1"
}
}
"^1.1.1"
chấp nhận "^1.1.x"
với x >= 1
, thế nên package-lock.json
rất quan trọng và cần phải lưu lại trong source code với git.
Tìm hiểu thêm npm audit
output thì ta thấy:
https://github.com/advisories/GHSA-q9mw-68c2-j6m5
Dò theo git diff
thì ta thấy yaml
package được update là dependencies requires của lint-staged
.
Vào releases note của https://github.com/okonet/lint-staged/releases/tag/v13.2.2:
Như vậy ta có thể set luôn "lint-staged": "^13.2.2"
trong package.json
rồi chạy:
npm i
# fix other vulnerabilities
npm audit fix
# expected output: found 0 vulnerabilities
Tổng kết
Các lỗ hổng bảo mật có thể chưa được phát hiện, nó có thể được phát hiện vào tương lai, vậy nên để an toàn chúng ta cần thiết nên audit định kỳ, điều mà các github bot vẫn làm thay chúng ta.
Việc đặc tả version chính xác cũng quan trọng không kém, đó là lý do tại sao
package-lock.json
(đối với npm) ra đời.