LoginSignup
2
4

More than 1 year has passed since last update.

npm auditについて調べたメモ

Last updated at Posted at 2022-03-21

概要

npm auditについて軽く調べたメモ。

オプション

npm audit --only=prod --audit-level=high
オプション 意味
--only prodにするとdependenciesのパッケージのみチェック
--audit-level 実行結果に影響。highにするとhigh以上の問題がなければ正常終了(0)を返す。 "info", "low", "moderate", "high", "critical"から選択
--json 結果をJSONとして出力

jqで出力

npm audit --only=prod --audit-level=high --json | jq '.vulnerabilities | to_entries '

jqで成型すると以下のようなJSONの配列が得られる。

  {
    "key": "node-fetch",
    "value": {
      "name": "node-fetch",
      "severity": "high",
      "isDirect": false,
      "via": [
        {
          "source": 1064646,
          "name": "node-fetch",
          "dependency": "node-fetch",
          "title": "node-fetch is vulnerable to Exposure of Sensitive Information to an Unauthorized Actor",
          "url": "https://github.com/advisories/GHSA-r683-j2x4-v87g",
          "severity": "high",
          "range": "<2.6.7"
        }
      ],
      "effects": [
        "@firebase/firestore",
        "@firebase/functions",
        "@firebase/storage"
      ],
      "range": "<2.6.7",
      "nodes": [
        "node_modules/node-fetch"
      ],
      "fixAvailable": true
    }
  }

一覧で出力

npm audit --only=prod --audit-level=high --json | jq '.vulnerabilities | to_entries | .[] | { package: .key, level: .value.severity} | [.package, .level]' | jq -r '@csv'

脆弱性の結果の読み方

プロパティ 意味
name パッケージ名
severity 重大度レベル
range パッケージの影響を受けるバージョン
fixAvailable 自動修正が可能か

viaは、詳細な脆弱性の原因を特定するための情報のリスト。
追加で脆弱性のタイプ(たとえば、不十分なエントロピー)と、問題の詳細な説明を含むページへのリンクが含まれる。

バージョンについて

npm outdatedで最新バージョンの有無を調査可能。

npm outdated
Package                           Current  Wanted  Latest  Location                                       Depended by
@supabase/supabase-js              1.23.0  1.31.1  1.31.1  node_modules/@supabase/supabase-js             aws-whitemap-client
@typescript-eslint/eslint-plugin   4.31.2  4.33.0  5.16.0  node_modules/@typescript-eslint/eslint-plugin  aws-whitemap-client
@typescript-eslint/parser          4.31.2  4.33.0  5.16.0  node_modules/@typescript-eslint/parser         aws-whitemap-client
@vitejs/plugin-vue                  1.9.2  1.10.2   2.2.4  node_modules/@vitejs/plugin-vue                aws-whitemap-client

詳細に出力

via内に詳細情報が含まれているので、これを表示できるようにする。
ただし、viaはstring型とobject型が入り混じっている。
また、同様にfixAvailableもbooleanとobjectの可能性がある。
if文でtypeを判定し、出しわけるようにする。

npm audit --only=prod --json| jq '.vulnerabilities | to_entries | .[] | { package: .key, range:.value.range, severity: .value.severity,  fix: (.value.fixAvailable | if type == "boolean" then (.|tostring) else .name+"|"+.version+"|"+(.isSemVerMajor|tostring) end), via: .value.via}|if(.via[] | type)=="string" then {package:.package, fix:.fix, name:.via[], severity: .severity, title: "-", range: .range, url:"-"} else  {package:.package, fix:.fix, name:.via[].name, severity: .via[].severity, title: .via[].title, range: .via[].range, url:.via[].url} end'  | jq -r '[.package,.fix,.name,.severity,.title,.range,.url]|@csv'
' > result.csv
package fixAvailable via_package severity title range url
@firebase/auth hoge_7.4.0_true node-fetch high - <=0.0.900-exp.f919db6a9
ansi-regex true ansi-regex moderate Inefficient Regular Expression Complexity in chalk/ansi-regex >2.1.1 <5.0.1 https://github.com/advisories/GHSA-93q8-gq69-wqmw

参考

公式 - npm audit
公式 - npm outdated
npm@v7でのnpm auditの出力結果について
I get an impression that a long time ago, enlightened sirs and ladies implemented the previous version; the new version, however, was overridden by a bunch of monkeys.とか言ってる
npmパッケージのvulnerability対応フロー

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4