osv-scanner-mcp 0.4.2 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -13
- package/dist/index.js +10 -8
- package/dist/osv/affectedVersions.js +72 -30
- package/dist/osv/scanReport.js +2 -2
- package/dist/osv/suggestFix.js +99 -102
- package/dist/osv/versionScheme.js +121 -11
- package/dist/tools/scanProject.js +5 -8
- package/dist/tools/suggestFix.js +13 -12
- package/dist/utils/pep440Version.js +100 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
|
|
8
8
|
Google製 [OSV-Scanner](https://github.com/google/osv-scanner) をラップするMCPサーバーです。Claude等のMCPクライアントから「このプロジェクトの脆弱性をチェックして」と自然言語で依頼するだけで、依存ライブラリの既知の脆弱性(CVE / GHSA)を深刻度順のレポートで取得できます。
|
|
9
9
|
|
|
10
|
-
> **ステータス**: [npmで公開中](https://www.npmjs.com/package/osv-scanner-mcp)(`npx -y osv-scanner-mcp`)。Java(Maven / Gradle)、JavaScript(npm / yarn / pnpm / bun)、Python(Poetry / uv / Pipenv / PDM / requirements.txt)、Go のlockfileに対応しています(
|
|
10
|
+
> **ステータス**: [npmで公開中](https://www.npmjs.com/package/osv-scanner-mcp)(`npx -y osv-scanner-mcp`)。Java(Maven / Gradle)、JavaScript(npm / yarn / pnpm / bun)、Python(Poetry / uv / Pipenv / PDM / requirements.txt)、Go のlockfileに対応しています(修正版の推奨も4言語に対応)。MCPクライアントは Claude Code / Claude Desktop / Codex CLI / Antigravity / VS Code(GitHub Copilot)での利用手順を用意しています。
|
|
11
11
|
|
|
12
12
|
## 特徴
|
|
13
13
|
|
|
@@ -142,7 +142,7 @@ osv-scanner v2.4.0 で接続先を実機確認した結果です(2026-10-07)。
|
|
|
142
142
|
| 操作 | 接続先 | 送られる情報 |
|
|
143
143
|
|---|---|---|
|
|
144
144
|
| `pom.xml` のスキャン(`scan_project` / `scan_java_project` / `suggest_fix`) | `api.osv.dev`、**`api.deps.dev`** | パッケージの名前とバージョン。deps.dev には推移的依存の解決のため、`pom.xml` に宣言された依存(社内パッケージを含む)の名前とバージョンが送られます |
|
|
145
|
-
| `requirements.txt` のスキャン(`scan_project`) | `api.osv.dev`、**`api.deps.dev`** | `pom.xml` と同じく、推移的依存の解決のため記載された依存の名前とバージョンが deps.dev に送られます。`--index-url` 等に書かれた取得先へは接続しません |
|
|
145
|
+
| `requirements.txt` のスキャン(`scan_project` / `suggest_fix`) | `api.osv.dev`、**`api.deps.dev`** | `pom.xml` と同じく、推移的依存の解決のため記載された依存の名前とバージョンが deps.dev に送られます。`--index-url` 等に書かれた取得先へは接続しません |
|
|
146
146
|
| lockfileのスキャン(`gradle.lockfile`、`package-lock.json` 等のnpm系、`poetry.lock` 等のPython系、`go.mod`) | `api.osv.dev` | パッケージの名前とバージョン(lockfileに全依存が記載済みのため、解決のための外部接続はしません) |
|
|
147
147
|
| `scan_java_artifact` / `scan_sbom` | `api.osv.dev` | 同定できたパッケージの名前とバージョン |
|
|
148
148
|
| `explain_vulnerability` | `api.osv.dev` | 指定した脆弱性ID |
|
|
@@ -212,7 +212,7 @@ api.osv.dev と deps.dev はどちらも Google が運営するサービスで
|
|
|
212
212
|
- 各一覧は200件までで、超えた分の件数を `omitted_items` に返します
|
|
213
213
|
- `ecosystem_breakdown` は、脆弱性0件のエコシステムも含めて「スキャンした」ことを示します
|
|
214
214
|
- `dependency_groups` はOSV-Scannerが付けた依存グループ(例: `dev`)の生の値です。lockfileの形式によって欠落・不正確なため(pnpmでは付かず、pdmでは `optional` になる等)、参考情報として扱ってください
|
|
215
|
-
- 修正版の推奨(`suggest_fix`)
|
|
215
|
+
- 修正版の推奨(`suggest_fix`)はJava・JavaScript・Python・Goに対応しています
|
|
216
216
|
|
|
217
217
|
### `scan_java_project`
|
|
218
218
|
|
|
@@ -277,7 +277,7 @@ OSV-Scannerは `pom.xml` の `<parent>` が参照する親POM(`<relativePath>`
|
|
|
277
277
|
```
|
|
278
278
|
|
|
279
279
|
- `packages` は最も深刻な脆弱性を持つ順、各 `vulnerabilities` は深刻度順(unknownは末尾)
|
|
280
|
-
- `fixed_versions` はOSVに記載された修正版です。MavenはMaven優先順位、npm・GoはSemantic Versioningの優先順位で昇順(SemVerとして解釈できない表記は末尾)、PyPI
|
|
280
|
+
- `fixed_versions` はOSVに記載された修正版です。MavenはMaven優先順位、npm・GoはSemantic Versioningの優先順位で昇順(SemVerとして解釈できない表記は末尾)、PyPIはPEP 440の優先順位で昇順(v0.5.0以前は記載順)、その他のエコシステムはOSVの記載順のまま(並び順は保証しません)。複数のリリース系統(例: 2.12系バックポートと2.15系)が混在することがあります。プレリリース版(`5.0.0-beta.3`)やGoの疑似バージョン(`0.0.0-20180925071336-cf3bd585ca2a`)が含まれることもあります。空配列は「OSVに修正版の記載がない」ことを意味します。v0.4.1以前はMaven以外のパッケージで常に空配列を返していました
|
|
281
281
|
- `severity_score` が取得できない脆弱性は `null` / `"unknown"` として扱います
|
|
282
282
|
|
|
283
283
|
### `scan_java_artifact`
|
|
@@ -333,23 +333,26 @@ OSV-Scanner 2.4.0の `java/archive` プラグインを使用し、ネストJAR
|
|
|
333
333
|
|
|
334
334
|
### `suggest_fix`
|
|
335
335
|
|
|
336
|
-
|
|
336
|
+
`scan_project` と同じ検出・スキャンを実行し、脆弱なパッケージごとに**推奨アップグレードバージョン**を提案します。推奨はJava(Maven / Gradle)・JavaScript(npm)・Python(PyPI)・Goに対応しています。単純な最大バージョンではなく、現在のバージョンに最も近いリリース系統の修正版を3段階フォールバックで選定します:
|
|
337
337
|
|
|
338
338
|
| Tier | 意味 |
|
|
339
339
|
|---|---|
|
|
340
|
-
| `same_minor` |
|
|
340
|
+
| `same_minor` | 現在と同じ系統内の修正版(最小の変更で済む) |
|
|
341
341
|
| `major_internal` | 同一メジャー内の修正版(マイナーバージョンアップが必要) |
|
|
342
342
|
| `cross_major` | メジャーアップグレードが必要(破壊的変更の可能性あり) |
|
|
343
343
|
|
|
344
|
-
|
|
344
|
+
npm・Go・PyPIの「同じ系統」は、npmの `^`(キャレット)が互換とみなす範囲です(PyPIには共通の互換規則がありませんが、0.x系でマイナー更新が破壊的変更になるパッケージがあるため同じ規則で扱います。epochが変わる更新も `cross_major`)。1.0.0以上は Maven と同じく major.minor 単位ですが、0.x では同じ `0.minor` 内だけを同じ系統とし、マイナー更新(`0.3` → `0.4`)は `cross_major`、0.0.x ではどの更新も `cross_major` として扱います(SemVerでは0.xの更新は互換を保証しないため)。
|
|
345
|
+
|
|
346
|
+
**入力**: `scan_project` と同じ(`project_path`)
|
|
345
347
|
|
|
346
348
|
**出力(成功時)**
|
|
347
349
|
|
|
348
350
|
```json
|
|
349
351
|
{
|
|
350
352
|
"project_dir": "/path/to/project",
|
|
351
|
-
"manifests": ["pom.xml"],
|
|
353
|
+
"manifests": ["pom.xml", "web/package-lock.json"],
|
|
352
354
|
"dependency_resolution": { "transitive_resolution": "enabled" },
|
|
355
|
+
"coverage": { "complete": true, "manifests": [ ... ], "lockfile_missing": [], "unpinned_requirements": [], "unscannable_requirements": [], "skipped_files": [] },
|
|
353
356
|
"vulnerable_package_count": 4,
|
|
354
357
|
"unfixed_vulnerability_count": 1,
|
|
355
358
|
"suggestions": [
|
|
@@ -370,10 +373,14 @@ OSV-Scanner 2.4.0の `java/archive` プラグインを使用し、ネストJAR
|
|
|
370
373
|
```
|
|
371
374
|
|
|
372
375
|
- `recommended_upgrade` は既知の修正版を候補に、修正対象の全CVEの影響範囲外と確認できたものを3段階Tier順・バージョン昇順で選びます。CVEごとの修正版の最大値を単純に採用せず、別系統で再び影響を受ける候補も除外します。全公開版の中での最小性や未検出の脆弱性がないことは保証しません。
|
|
373
|
-
-
|
|
376
|
+
- OSVの影響範囲(`introduced` / `fixed` / `last_affected` / 上限なし)を照合します。MavenはMavenの優先順位で `ECOSYSTEM` 範囲を、npm・GoはSemantic Versioningの優先順位で `SEMVER` / `ECOSYSTEM` 範囲を、PyPIはPEP 440の優先順位(`1.8c1` や `2.8.0-rc0` のような正規形でない表記も正規化)で `ECOSYSTEM` 範囲を使います。同じエントリに `ECOSYSTEM` 範囲があれば、コミット単位の `GIT` 範囲は無視します。`versions` に明示された影響も確認します(Gitのタグ名など版として解釈できない値は、解釈できる候補と一致しえないため無視します)。範囲欠落・不正・未対応形式(`GIT` 等)・`limit` による不完全な情報や、範囲の境界に解釈できない版(一部のGHSAに残る `19.03.9`、PyTorchの `2.6.0-cu124` のような表記)を含む場合は安全と推定せず、候補を検証できなければ `recommended_upgrade: null`、`verification: "no_verified_candidate"` を返します。
|
|
377
|
+
- プレリリース版(`5.0.0-beta.3`、`15.6.0-canary.61`、Goの疑似バージョン、PyPIの `rc`・`dev` 版。post版は正式版扱い)は、正式版の候補では全CVEを解消できない場合だけ推奨し、`recommended_is_prerelease: true` を付けます。同じTierのプレリリースより、上のTierの正式版を優先します。
|
|
374
378
|
- 推奨時は `verification: "verified"`、CVEごとの `recommended_status` は `affected` / `not_affected` / `unknown` です。推奨保留時は `not_evaluated` になります。`per_cve_detail.fixed_in` は各CVE単独の候補であり、最終推奨先の判定は `recommended_status` を参照してください。
|
|
375
|
-
- 現在より新しい修正版候補がないCVEは `tier: "unfixed"` として推奨の修正対象から除外します(情報欠落を含む場合があります)。除外したCVEも推奨先で判定し、その状態を表示します。全CVEがunfixedの場合も `recommended_upgrade` は `null`
|
|
376
|
-
-
|
|
379
|
+
- 現在より新しい修正版候補がないCVEは `tier: "unfixed"` として推奨の修正対象から除外します(情報欠落を含む場合があります)。除外したCVEも推奨先で判定し、その状態を表示します。全CVEがunfixedの場合も `recommended_upgrade` は `null` です。修正版の記載はあるがバージョンとして解釈できないCVE(SemVerでない `13.0` 等)は `tier: "unparseable_fix"` とし、修正版が無いとは扱わず修正対象に残すため、推奨は保留(`no_verified_candidate`)になります。
|
|
380
|
+
- npm・Go・PyPIの提案には更新方法の `update_hint` を付けます。PyPIでは、requirements.txtやpyproject.toml・Pipfileの指定を更新してlockfileを再生成し、推移的依存はpipの制約ファイル(`-c`)やuv・Poetryの上書き設定で版を指定します。推移的依存の場合、npmでは要求している直接依存の更新か、ルートの `package.json` の `overrides`(ルートのプロジェクトでのみ有効)で版を指定します。Goでは `go get <module>@<version>` で更新できます。Goのv2以上のメジャーは別のモジュールパス(`/v2` 等)としてOSV上も別パッケージになるため、新しいメジャー系列の修正版は候補に含まれません。現在の版が疑似バージョン(タグのないコミット)の場合は `upgrade_note` に示します。
|
|
381
|
+
- requirements.txtの `>=X` / `~=X` の行は、OSV-Scannerが下限Xを使用中の版とみなしてスキャンしています。この依存の提案には `version_is_lower_bound: true` を付け、推奨は「下限を推奨版以上に引き上げる」意味であること(実際にインストールされる版とは異なりうること)を `upgrade_note` に示します。
|
|
382
|
+
- 推奨に未対応のエコシステム(SBOM由来のRubyGems等)は `verification: "unsupported_ecosystem"`、現在の版をバージョンとして解釈できない場合(npmのgit・ローカルパス依存等)は `verification: "unparseable_version"` を返し、どちらもCVEごとの `tier: "unsupported"` として `unfixed` には数えません(修正版の有無は判定していないため。修正版は `scan_project` の `fixed_versions` や `explain_vulnerability` で確認できます)。
|
|
383
|
+
- 応答の `coverage` は `scan_project` と同じです。lockfileの無いマニフェストや外したファイルがあれば `complete: false` になり、それらの依存は提案に含まれません。v0.4.2以前の `skipped_manifests` / `scope_warning` は `coverage.skipped_files` / `coverage.warning` に統合しました。
|
|
377
384
|
|
|
378
385
|
### `explain_vulnerability`
|
|
379
386
|
|
|
@@ -458,8 +465,9 @@ npm run build # dist/ へビルド
|
|
|
458
465
|
- [x] Gradle対応(lockfile方式)
|
|
459
466
|
- [x] `scan_java_artifact` ツール: JAR/WAR実体スキャン(lockfileが無い・shaded/fat JARのみのプロジェクト向け)
|
|
460
467
|
- [x] `scan_project` ツール: Java / JavaScript / Python / Go のlockfileをまとめてスキャン
|
|
461
|
-
- [
|
|
462
|
-
- [
|
|
468
|
+
- [x] `suggest_fix` のJavaScript / Go対応(semver)
|
|
469
|
+
- [x] `suggest_fix` のPython対応(PEP 440)
|
|
470
|
+
- [ ] 直接/推移的依存の区別
|
|
463
471
|
|
|
464
472
|
## ライセンス
|
|
465
473
|
|
package/dist/index.js
CHANGED
|
@@ -36,7 +36,7 @@ if (startupWarning !== null) {
|
|
|
36
36
|
// NOTE: リリース時はpackage.jsonのversionと同じ値に更新すること
|
|
37
37
|
const server = new McpServer({
|
|
38
38
|
name: "osv-scanner-mcp",
|
|
39
|
-
version: "0.
|
|
39
|
+
version: "0.6.0",
|
|
40
40
|
});
|
|
41
41
|
server.registerTool("scan_project", {
|
|
42
42
|
title: "プロジェクトの依存の脆弱性スキャン(Java / JavaScript / Python / Go)",
|
|
@@ -45,7 +45,7 @@ server.registerTool("scan_project", {
|
|
|
45
45
|
"Python(poetry.lock / uv.lock / Pipfile.lock / pdm.lock / requirements.txt)、Go(go.mod)。" +
|
|
46
46
|
"パッケージマネージャーやビルドは実行しない。" +
|
|
47
47
|
"応答先頭のcoverageを必ず確認すること: lockfileが無いマニフェスト、バージョン未固定のrequirements行、スキャン対象から外したファイルを示す。" +
|
|
48
|
-
"coverage.complete=falseの場合は、検出0件でも安全とは判断しないこと。修正版の推奨(suggest_fix)
|
|
48
|
+
"coverage.complete=falseの場合は、検出0件でも安全とは判断しないこと。修正版の推奨(suggest_fix)はJava・JavaScript・Python・Goに対応。",
|
|
49
49
|
inputSchema: {
|
|
50
50
|
project_path: z
|
|
51
51
|
.string()
|
|
@@ -68,18 +68,20 @@ server.registerTool("scan_java_project", {
|
|
|
68
68
|
}, async ({ project_path }) => handleScanJavaProject({ project_path }, { allowedRoot: allowedRootFromEnv() }));
|
|
69
69
|
server.registerTool("suggest_fix", {
|
|
70
70
|
title: "脆弱性を解消する推奨アップグレードの提案",
|
|
71
|
-
description: "
|
|
71
|
+
description: "scan_projectと同じ検出でプロジェクトをスキャンし、脆弱な依存パッケージごとに推奨アップグレードバージョンを提案する。" +
|
|
72
|
+
"推奨はJava(Maven / Gradle)・JavaScript(npm)・Python(PyPI)・Goに対応。" +
|
|
72
73
|
"現在のバージョンに最も近いリリース系統の修正版を優先する3段階フォールバック" +
|
|
73
|
-
"(same_minor:
|
|
74
|
-
"推奨バージョン・アップグレード距離(upgrade_tier)・CVEごとの修正版を返す。" +
|
|
75
|
-
"候補を全修正対象CVE
|
|
76
|
-
"現在より新しい修正版候補のないCVEはunfixedとして別表示し、推奨先での判定も返す。
|
|
74
|
+
"(same_minor: 同一系統内 → major_internal: 同一メジャー内 → cross_major: メジャーアップグレード)で選定し、" +
|
|
75
|
+
"推奨バージョン・アップグレード距離(upgrade_tier)・CVEごとの修正版を返す。npm・Go・PyPIでは0.x系のマイナー更新もcross_major(破壊的変更の可能性)。requirements.txtの下限(>=)でスキャンした依存はversion_is_lower_boundを付け、推奨は下限の引き上げを意味する。" +
|
|
76
|
+
"候補を全修正対象CVEの影響範囲と照合し、情報不足の場合は推奨を保留する。プレリリース版は正式版で解消できない場合だけ推奨し、recommended_is_prereleaseを付ける。" +
|
|
77
|
+
"現在より新しい修正版候補のないCVEはunfixedとして別表示し、推奨先での判定も返す。" +
|
|
78
|
+
"応答のcoverageを必ず確認すること(complete=falseなら提案に含まれない依存がある)。" +
|
|
77
79
|
"dependency_resolution.warningがある場合は推移的依存の脆弱性が提案に含まれない。",
|
|
78
80
|
inputSchema: {
|
|
79
81
|
project_path: z
|
|
80
82
|
.string()
|
|
81
83
|
.min(1)
|
|
82
|
-
.describe("
|
|
84
|
+
.describe("スキャン対象のプロジェクトディレクトリ、または対応するlockfile・マニフェストの絶対パス"),
|
|
83
85
|
},
|
|
84
86
|
}, async ({ project_path }) => handleSuggestFix({ project_path }, { allowedRoot: allowedRootFromEnv() }));
|
|
85
87
|
server.registerTool("explain_vulnerability", {
|
|
@@ -1,8 +1,19 @@
|
|
|
1
|
-
import { compareMavenVersions } from "../utils/mavenVersion.js";
|
|
2
1
|
import { asArray, asRecord, asString } from "../utils/unknownJson.js";
|
|
3
|
-
|
|
2
|
+
import { samePackageName, versionRangeTypes, versionSchemeFor } from "./versionScheme.js";
|
|
3
|
+
/**
|
|
4
|
+
* Preserve uncertainty: unsupported or malformed ranges cannot prove a candidate safe.
|
|
5
|
+
* 比較・範囲の型はエコシステム別(versionScheme.ts)。比較器のないエコシステムは常に情報不足。
|
|
6
|
+
* 解釈できない版(SemVerでない値等)を含む範囲は判定に使わず、情報不足とする。
|
|
7
|
+
* - `versions[]`の解釈できない値(Gitのタグ名等)は無視する。`versions[]`は等価判定にだけ使い、
|
|
8
|
+
* 解釈できない文字列は解釈できる候補と等しくなりえないため、無視しても候補の判定は変わらない
|
|
9
|
+
* - 同じaffectedエントリにECOSYSTEM等の版の範囲があれば、GIT範囲(コミット単位)は無視する。GIT範囲だけなら情報不足
|
|
10
|
+
*/
|
|
4
11
|
export function extractAffectedVersions(details, ids, name, ecosystem) {
|
|
5
|
-
const
|
|
12
|
+
const scheme = versionSchemeFor(ecosystem);
|
|
13
|
+
const rangeTypes = versionRangeTypes(ecosystem);
|
|
14
|
+
const evidence = { complete: scheme !== null, intervals: [], versions: [] };
|
|
15
|
+
const valid = (value) => scheme?.isValid(value) ?? false;
|
|
16
|
+
const compare = (a, b) => scheme?.compare(a, b) ?? 0;
|
|
6
17
|
if (ids.length === 0 || ids.some(id => !details.some(d => d.id === id)))
|
|
7
18
|
evidence.complete = false;
|
|
8
19
|
for (const detail of details) {
|
|
@@ -10,7 +21,8 @@ export function extractAffectedVersions(details, ids, name, ecosystem) {
|
|
|
10
21
|
for (const raw of asArray(detail.affected)) {
|
|
11
22
|
const affected = asRecord(raw);
|
|
12
23
|
const pkg = asRecord(affected?.package);
|
|
13
|
-
|
|
24
|
+
const affectedName = asString(pkg?.name);
|
|
25
|
+
if (affectedName === null || !samePackageName(affectedName, name, ecosystem) || asString(pkg?.ecosystem) !== ecosystem) {
|
|
14
26
|
if (!asString(pkg?.name) || !asString(pkg?.ecosystem))
|
|
15
27
|
evidence.complete = false;
|
|
16
28
|
continue;
|
|
@@ -22,25 +34,30 @@ export function extractAffectedVersions(details, ids, name, ecosystem) {
|
|
|
22
34
|
const value = asString(version);
|
|
23
35
|
if (!value)
|
|
24
36
|
evidence.complete = false;
|
|
25
|
-
else
|
|
37
|
+
else if (valid(value))
|
|
26
38
|
evidence.versions.push(value);
|
|
27
39
|
}
|
|
28
40
|
const ranges = asArray(affected?.ranges);
|
|
29
41
|
// A versions-only list does not establish that unlisted releases are unaffected.
|
|
30
42
|
if (ranges.length === 0)
|
|
31
43
|
evidence.complete = false;
|
|
44
|
+
const hasVersionRange = ranges.some((r) => rangeTypes.has(asString(asRecord(r)?.type) ?? ""));
|
|
32
45
|
for (const rawRange of ranges) {
|
|
33
46
|
const range = asRecord(rawRange);
|
|
34
|
-
|
|
47
|
+
const type = asString(range?.type);
|
|
48
|
+
if (type === "GIT" && hasVersionRange)
|
|
49
|
+
continue;
|
|
50
|
+
if (range === null || type === null || !rangeTypes.has(type)) {
|
|
35
51
|
evidence.complete = false;
|
|
36
52
|
continue;
|
|
37
53
|
}
|
|
38
54
|
const events = asArray(range.events);
|
|
39
|
-
let start = null;
|
|
40
|
-
let previousEnd = null;
|
|
41
|
-
let previousInclusive = false;
|
|
42
55
|
if (events.length === 0)
|
|
43
56
|
evidence.complete = false;
|
|
57
|
+
// OSVの仕様では範囲内のeventsの並び順は保証されず、評価時に版の順に並べる(実データ: PYSECの
|
|
58
|
+
// `introduced 2.0.0 → fixed 2.0.6, introduced 0 → fixed 1.26.17`)。仕様どおり並べてから区間にする
|
|
59
|
+
const parsed = [];
|
|
60
|
+
let usable = true;
|
|
44
61
|
for (const rawEvent of events) {
|
|
45
62
|
const event = asRecord(rawEvent);
|
|
46
63
|
const keys = event ? Object.keys(event) : [];
|
|
@@ -50,31 +67,54 @@ export function extractAffectedVersions(details, ids, name, ecosystem) {
|
|
|
50
67
|
evidence.complete = false;
|
|
51
68
|
continue;
|
|
52
69
|
}
|
|
70
|
+
// 解釈できない境界を含む範囲は判定に使わない
|
|
71
|
+
if (!(kind === "introduced" && value === "0") && !valid(value)) {
|
|
72
|
+
usable = false;
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
// limit bounds known affected versions, but is not evidence of a fix beyond it.
|
|
76
|
+
if (kind === "limit")
|
|
77
|
+
evidence.complete = false;
|
|
78
|
+
parsed.push({ kind: kind, value });
|
|
79
|
+
}
|
|
80
|
+
if (!usable) {
|
|
81
|
+
evidence.complete = false;
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
// 同じ版では終点(fixed等)を始点(introduced)より前に置く(その版を影響ありとみなす安全側)
|
|
85
|
+
const rank = (kind) => (kind === "introduced" ? 1 : 0);
|
|
86
|
+
const isZero = (e) => e.kind === "introduced" && e.value === "0";
|
|
87
|
+
parsed.sort((a, b) => {
|
|
88
|
+
// introduced: "0" はすべての版より前
|
|
89
|
+
if (isZero(a) || isZero(b))
|
|
90
|
+
return isZero(a) === isZero(b) ? 0 : isZero(a) ? -1 : 1;
|
|
91
|
+
return compare(a.value, b.value) || rank(a.kind) - rank(b.kind);
|
|
92
|
+
});
|
|
93
|
+
// 並べた結果が「始点 → 終点」の交互にならない(始点の重複、始点のない終点)場合は解釈が曖昧なため使わない
|
|
94
|
+
const intervals = [];
|
|
95
|
+
let start = null;
|
|
96
|
+
for (const { kind, value } of parsed) {
|
|
53
97
|
if (kind === "introduced") {
|
|
54
|
-
if (start !== null
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
98
|
+
if (start !== null) {
|
|
99
|
+
usable = false;
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
58
102
|
start = value;
|
|
59
103
|
}
|
|
60
104
|
else {
|
|
61
105
|
if (start === null) {
|
|
62
|
-
|
|
63
|
-
|
|
106
|
+
usable = false;
|
|
107
|
+
break;
|
|
64
108
|
}
|
|
65
|
-
|
|
66
|
-
if (start !== "0" && (compareMavenVersions(start, value) > 0 ||
|
|
67
|
-
(!inclusive && compareMavenVersions(start, value) === 0)))
|
|
68
|
-
evidence.complete = false;
|
|
69
|
-
evidence.intervals.push({ introduced: start, end: value, inclusive });
|
|
70
|
-
// limit bounds known affected versions, but is not evidence of a fix beyond it.
|
|
71
|
-
if (kind === "limit")
|
|
72
|
-
evidence.complete = false;
|
|
73
|
-
previousEnd = value;
|
|
74
|
-
previousInclusive = inclusive;
|
|
109
|
+
intervals.push({ introduced: start, end: value, inclusive: kind === "last_affected" });
|
|
75
110
|
start = null;
|
|
76
111
|
}
|
|
77
112
|
}
|
|
113
|
+
if (!usable || parsed.every((e) => e.kind !== "introduced")) {
|
|
114
|
+
evidence.complete = false;
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
evidence.intervals.push(...intervals);
|
|
78
118
|
if (start !== null)
|
|
79
119
|
evidence.intervals.push({ introduced: start, end: null, inclusive: false });
|
|
80
120
|
}
|
|
@@ -86,12 +126,14 @@ export function extractAffectedVersions(details, ids, name, ecosystem) {
|
|
|
86
126
|
evidence.complete = false;
|
|
87
127
|
return evidence;
|
|
88
128
|
}
|
|
89
|
-
export function candidateStatus(evidence, version) {
|
|
90
|
-
|
|
129
|
+
export function candidateStatus(evidence, version, ecosystem) {
|
|
130
|
+
const scheme = versionSchemeFor(ecosystem);
|
|
131
|
+
if (!evidence || scheme === null || !scheme.isValid(version))
|
|
91
132
|
return "unknown";
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
133
|
+
const compare = scheme.compare;
|
|
134
|
+
if (evidence.versions.some(v => compare(v, version) === 0) || evidence.intervals.some(r => (r.introduced === "0" || compare(version, r.introduced) >= 0) &&
|
|
135
|
+
(r.end === null || compare(version, r.end) < 0 ||
|
|
136
|
+
(r.inclusive && compare(version, r.end) === 0))))
|
|
95
137
|
return "affected";
|
|
96
138
|
return evidence.complete ? "not_affected" : "unknown";
|
|
97
139
|
}
|
package/dist/osv/scanReport.js
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
*/
|
|
15
15
|
import { asArray, asRecord, asString, asStrings } from "../utils/unknownJson.js";
|
|
16
16
|
import { extractAffectedVersions } from "./affectedVersions.js";
|
|
17
|
-
import { sortVersions, versionRangeTypes } from "./versionScheme.js";
|
|
17
|
+
import { samePackageName, sortVersions, versionRangeTypes } from "./versionScheme.js";
|
|
18
18
|
/** 外部由来のsummaryをLLMに渡す際の長さ上限(プロンプト肥大・DoS対策) */
|
|
19
19
|
const MAX_SUMMARY_LENGTH = 500;
|
|
20
20
|
const CVSS_CRITICAL = 9.0;
|
|
@@ -77,7 +77,7 @@ function extractFixedVersions(vulnDetails, packageName, ecosystem) {
|
|
|
77
77
|
if (!affected)
|
|
78
78
|
continue;
|
|
79
79
|
const affectedName = asString(asRecord(affected.package)?.name);
|
|
80
|
-
if (affectedName
|
|
80
|
+
if (affectedName === null || !samePackageName(affectedName, packageName, ecosystem) || asString(asRecord(affected.package)?.ecosystem) !== ecosystem)
|
|
81
81
|
continue;
|
|
82
82
|
for (const rangeRaw of asArray(affected.ranges)) {
|
|
83
83
|
const type = asString(asRecord(rangeRaw)?.type);
|
package/dist/osv/suggestFix.js
CHANGED
|
@@ -6,32 +6,49 @@
|
|
|
6
6
|
* 最大バージョンではなく「現在のバージョンに最も近い系統の修正版」を優先して提案する。
|
|
7
7
|
*
|
|
8
8
|
* CVEごとの探索順:
|
|
9
|
-
* Tier 1 (same_minor):
|
|
9
|
+
* Tier 1 (same_minor): 現在と同じ系統内の修正版(最小の変更で済む)
|
|
10
10
|
* Tier 2 (major_internal): 同一メジャー内の最小の修正版(マイナーバージョンアップ)
|
|
11
11
|
* Tier 3 (cross_major): 全体最小の修正版(メジャーアップグレード、破壊的変更の可能性)
|
|
12
|
+
* 系統の判定はエコシステム別(versionScheme.ts)。npm・Goは0.xのマイナー更新もcross_major。
|
|
12
13
|
*
|
|
13
14
|
* パッケージ全体の候補を全修正対象CVEの影響範囲と照合して推奨する。
|
|
14
15
|
* 判定不能な候補は推奨しない。
|
|
15
16
|
* 現在より新しい修正版が存在しないCVEはunfixedとして明示し、推奨計算から除外する。
|
|
17
|
+
* プレリリース(canary版・Goの疑似バージョン等)は、正式版の候補で解消できない場合だけ推奨する。
|
|
16
18
|
*/
|
|
17
|
-
import { compareMavenVersions, mavenVersionSeries } from "../utils/mavenVersion.js";
|
|
18
19
|
import { candidateStatus } from "./affectedVersions.js";
|
|
19
|
-
|
|
20
|
-
const
|
|
20
|
+
import { versionSchemeFor } from "./versionScheme.js";
|
|
21
|
+
const TIER_ORDER = ["same_minor", "major_internal", "cross_major"];
|
|
22
|
+
const UPDATE_HINTS = {
|
|
23
|
+
npm: "直接依存ならpackage.jsonの指定を更新します。推移的依存の場合は、それを要求している直接依存の更新か、" +
|
|
24
|
+
"ルートのpackage.jsonのoverrides(ルートのプロジェクトでのみ有効)で版を指定します",
|
|
25
|
+
Go: "go get <module>@<version>で更新します(推移的依存もgo.modのrequireに追加されて更新されます)。" +
|
|
26
|
+
"Goではv2以上のメジャーは別のモジュールパス(/v2等)として別パッケージ扱いのため、新しいメジャー系列の修正版はここに含まれません",
|
|
27
|
+
PyPI: "requirements.txtの版の指定、またはpyproject.toml・Pipfileの指定を更新し、lockfile(poetry.lock・uv.lock等)を再生成します。" +
|
|
28
|
+
"推移的依存の場合は、pipの制約ファイル(-c)や、uv・Poetry等の上書き設定で版を指定します",
|
|
29
|
+
};
|
|
30
|
+
/** Goの疑似バージョン(タグのないコミット): 末尾がタイムスタンプ14桁-コミットハッシュ12桁 */
|
|
31
|
+
const GO_PSEUDO_VERSION = /(?:^|[.-])\d{14}-[0-9a-f]{12}(?:\+|$)/;
|
|
32
|
+
function hintFields(ecosystem) {
|
|
33
|
+
const hint = UPDATE_HINTS[ecosystem];
|
|
34
|
+
return hint === undefined ? {} : { update_hint: hint };
|
|
35
|
+
}
|
|
21
36
|
/**
|
|
22
|
-
*
|
|
23
|
-
* 修正版の抽出がMaven
|
|
24
|
-
*
|
|
37
|
+
* 未対応エコシステム・解釈できない現在の版は推奨を出さず、CVEをunfixedにも数えない。
|
|
38
|
+
* 修正版の抽出がMaven専用だった頃、そのまま処理すると修正版のある脆弱性を
|
|
39
|
+
* 「修正版なし」と誤表示した(v0.3.3で確認した不具合)。
|
|
25
40
|
*/
|
|
26
|
-
function
|
|
41
|
+
function notEvaluatedSuggestion(pkg, verification) {
|
|
27
42
|
return {
|
|
28
43
|
package: pkg.name,
|
|
29
44
|
current_version: pkg.version,
|
|
30
45
|
ecosystem: pkg.ecosystem,
|
|
31
46
|
recommended_upgrade: null,
|
|
32
47
|
upgrade_tier: null,
|
|
33
|
-
upgrade_note:
|
|
34
|
-
|
|
48
|
+
upgrade_note: (verification === "unsupported_ecosystem"
|
|
49
|
+
? `${pkg.ecosystem}の修正版推奨には未対応です(修正版の有無は判定していません)。`
|
|
50
|
+
: `現在の版(${pkg.version})をバージョンとして解釈できないため、修正版の推奨を判定していません(git・ローカルパス等の依存の可能性があります)。`) +
|
|
51
|
+
"各脆弱性の修正版はfixed_versions(scan_project)またはexplain_vulnerabilityで確認してください",
|
|
35
52
|
per_cve_detail: pkg.vulnerabilities.map((vuln) => ({
|
|
36
53
|
id: vuln.id,
|
|
37
54
|
cve: vuln.cve,
|
|
@@ -39,124 +56,104 @@ function unsupportedEcosystemSuggestion(pkg) {
|
|
|
39
56
|
fixed_in: null,
|
|
40
57
|
tier: "unsupported",
|
|
41
58
|
})),
|
|
42
|
-
verification
|
|
59
|
+
verification,
|
|
43
60
|
};
|
|
44
61
|
}
|
|
45
|
-
function classifyTier(currentSeries, candidate) {
|
|
46
|
-
const candidateSeries = mavenVersionSeries(candidate);
|
|
47
|
-
if (currentSeries === null || candidateSeries === null)
|
|
48
|
-
return "cross_major";
|
|
49
|
-
if (currentSeries.major !== candidateSeries.major)
|
|
50
|
-
return "cross_major";
|
|
51
|
-
if (currentSeries.minor !== candidateSeries.minor)
|
|
52
|
-
return "major_internal";
|
|
53
|
-
return "same_minor";
|
|
54
|
-
}
|
|
55
62
|
/**
|
|
56
|
-
*
|
|
57
|
-
*
|
|
63
|
+
* 現在より新しい候補を推奨の優先順に並べる: 正式版 → Tier順 → 版の昇順。
|
|
64
|
+
* (同じTierのプレリリースより、上のTierの正式版を優先する)
|
|
65
|
+
* 優先順位が等しい版(Goの`23.0.3`と`23.0.3+incompatible`)は、ビルドメタデータの有無が現在の版と同じものを先にする。
|
|
66
|
+
* `/vN`の無いモジュールパスでv2以上を使うには`+incompatible`付きの指定が必要なため。
|
|
58
67
|
*/
|
|
59
|
-
function
|
|
60
|
-
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
.
|
|
64
|
-
|
|
65
|
-
return null;
|
|
66
|
-
if (currentSeries !== null) {
|
|
67
|
-
for (const tier of ["same_minor", "major_internal"]) {
|
|
68
|
-
const found = candidates.find((v) => classifyTier(currentSeries, v) === tier);
|
|
69
|
-
if (found !== undefined)
|
|
70
|
-
return { version: found, tier };
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
const version = candidates[0];
|
|
74
|
-
return { version, tier: classifyTier(currentSeries, version) };
|
|
68
|
+
function rankCandidates(scheme, current, versions) {
|
|
69
|
+
const rank = (v) => (scheme.isPrerelease(v) ? TIER_ORDER.length : 0) + TIER_ORDER.indexOf(scheme.classify(current, v));
|
|
70
|
+
const buildMismatch = (v) => (v.includes("+") === current.includes("+") ? 0 : 1);
|
|
71
|
+
return [...new Set(versions)]
|
|
72
|
+
.filter((v) => scheme.isValid(v) && scheme.compare(v, current) > 0)
|
|
73
|
+
.sort((a, b) => rank(a) - rank(b) || scheme.compare(a, b) || buildMismatch(a) - buildMismatch(b));
|
|
75
74
|
}
|
|
76
|
-
function buildNote(
|
|
75
|
+
function buildNote(scheme, pkg, recommended, tier, fixableCount, unfixedCount) {
|
|
76
|
+
let note;
|
|
77
77
|
if (recommended === null) {
|
|
78
|
-
|
|
78
|
+
note = `全${unfixedCount}件のCVEに現在より新しい修正版候補がない(unfixed)。修正版情報の欠落を含む可能性があります`;
|
|
79
79
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
80
|
+
else {
|
|
81
|
+
const label = scheme.seriesLabel(pkg.version);
|
|
82
|
+
switch (tier) {
|
|
83
|
+
case "same_minor":
|
|
84
|
+
note = `現在の${label}系統内の候補${recommended}を推奨`;
|
|
85
|
+
break;
|
|
86
|
+
case "major_internal":
|
|
87
|
+
note = `同一メジャー(${label.split(".")[0]}.x)内の候補${recommended}を推奨`;
|
|
88
|
+
break;
|
|
89
|
+
default:
|
|
90
|
+
if (label === null)
|
|
91
|
+
note = `現在バージョンの系統を判定できないため、検証済み候補${recommended}を提示`;
|
|
92
|
+
else if (label.startsWith("0.") && scheme.seriesLabel(recommended)?.startsWith("0.")) {
|
|
93
|
+
note = `候補${recommended}への更新を推奨(0.x系のため、マイナー更新でも破壊的変更の可能性あり)`;
|
|
94
|
+
}
|
|
95
|
+
else
|
|
96
|
+
note = `候補${recommended}へのメジャーアップグレードを推奨(破壊的変更の可能性あり)`;
|
|
97
|
+
}
|
|
98
|
+
note += `。取得済みの影響範囲に基づき修正対象${fixableCount}件のCVEの範囲外と確認しました。全公開版の最小性や未検出の脆弱性がないことは保証しません`;
|
|
99
|
+
if (scheme.isPrerelease(recommended)) {
|
|
100
|
+
note += "。正式版の候補では全件を解消できないため、プレリリース版を推奨しています。正式版の公開を確認してください";
|
|
101
|
+
}
|
|
102
|
+
if (unfixedCount > 0)
|
|
103
|
+
note += `。残り${unfixedCount}件は現在より新しい修正版候補がなく、修正対象から除外しています。recommended_statusを確認してください`;
|
|
104
|
+
}
|
|
105
|
+
if (pkg.version_is_lower_bound) {
|
|
106
|
+
note += `。現在の版(${pkg.version})はrequirements.txtの下限(>=・~=)で、実際にインストールされる版とは異なる可能性があります。` +
|
|
107
|
+
"推奨は下限をその版以上に引き上げる意味です";
|
|
108
|
+
}
|
|
109
|
+
if (pkg.ecosystem === "Go" && GO_PSEUDO_VERSION.test(pkg.version)) {
|
|
110
|
+
note += "。現在の版は疑似バージョン(タグのないコミット)です";
|
|
94
111
|
}
|
|
95
|
-
note += `。取得済みの影響範囲に基づき修正対象${fixableCount}件のCVEの範囲外と確認しました。全公開版の最小性や未検出の脆弱性がないことは保証しません`;
|
|
96
|
-
if (unfixedCount > 0)
|
|
97
|
-
note += `。残り${unfixedCount}件は現在より新しい修正版候補がなく、修正対象から除外しています。recommended_statusを確認してください`;
|
|
98
112
|
return note;
|
|
99
113
|
}
|
|
100
114
|
/** 1パッケージ分のアップグレード提案を組み立てる。 */
|
|
101
115
|
export function suggestUpgradeForPackage(pkg) {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
if (pick === null) {
|
|
111
|
-
unfixedCount++;
|
|
112
|
-
details.push({
|
|
113
|
-
id: vuln.id,
|
|
114
|
-
cve: vuln.cve,
|
|
115
|
-
severity: vuln.severity,
|
|
116
|
-
fixed_in: null,
|
|
117
|
-
tier: "unfixed",
|
|
118
|
-
});
|
|
119
|
-
continue;
|
|
120
|
-
}
|
|
121
|
-
details.push({
|
|
116
|
+
const scheme = versionSchemeFor(pkg.ecosystem);
|
|
117
|
+
if (scheme === null)
|
|
118
|
+
return notEvaluatedSuggestion(pkg, "unsupported_ecosystem");
|
|
119
|
+
if (!scheme.isValid(pkg.version))
|
|
120
|
+
return { ...notEvaluatedSuggestion(pkg, "unparseable_version"), ...hintFields(pkg.ecosystem) };
|
|
121
|
+
const details = pkg.vulnerabilities.map((vuln) => {
|
|
122
|
+
const fix = rankCandidates(scheme, pkg.version, vuln.fixed_versions)[0];
|
|
123
|
+
return {
|
|
122
124
|
id: vuln.id,
|
|
123
125
|
cve: vuln.cve,
|
|
124
126
|
severity: vuln.severity,
|
|
125
|
-
fixed_in:
|
|
126
|
-
tier:
|
|
127
|
-
|
|
128
|
-
|
|
127
|
+
fixed_in: fix ?? null,
|
|
128
|
+
tier: fix !== undefined ? scheme.classify(pkg.version, fix)
|
|
129
|
+
: vuln.fixed_versions.some((v) => !scheme.isValid(v)) ? "unparseable_fix" : "unfixed",
|
|
130
|
+
};
|
|
131
|
+
});
|
|
132
|
+
const unfixedCount = details.filter((d) => d.tier === "unfixed").length;
|
|
133
|
+
const unparseableCount = details.filter((d) => d.tier === "unparseable_fix").length;
|
|
129
134
|
const fixableCount = details.length - unfixedCount;
|
|
130
|
-
|
|
131
|
-
const
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
const tierOrder = ["same_minor", "major_internal", "cross_major"];
|
|
135
|
-
recommended = null;
|
|
136
|
-
for (const tier of tierOrder) {
|
|
137
|
-
const candidate = candidates.find(v => classifyTier(currentSeries, v) === tier &&
|
|
138
|
-
targets.every(target => candidateStatus(target.affected_versions, v) === "not_affected"));
|
|
139
|
-
if (candidate !== undefined) {
|
|
140
|
-
recommended = candidate;
|
|
141
|
-
break;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
135
|
+
// 解釈できない修正版のCVEも修正対象に残す。その影響範囲は情報不足のため、どの候補も検証できず推奨を保留する
|
|
136
|
+
const targets = pkg.vulnerabilities.filter((_, i) => details[i].tier !== "unfixed");
|
|
137
|
+
const candidates = rankCandidates(scheme, pkg.version, targets.flatMap((v) => v.fixed_versions));
|
|
138
|
+
const recommended = candidates.find((v) => targets.every((target) => candidateStatus(target.affected_versions, v, pkg.ecosystem) === "not_affected")) ?? null;
|
|
144
139
|
for (let i = 0; i < details.length; i++) {
|
|
145
140
|
details[i].recommended_status = recommended === null ? "not_evaluated" :
|
|
146
|
-
candidateStatus(pkg.vulnerabilities[i].affected_versions, recommended);
|
|
141
|
+
candidateStatus(pkg.vulnerabilities[i].affected_versions, recommended, pkg.ecosystem);
|
|
147
142
|
}
|
|
148
|
-
|
|
149
|
-
// (per-CVEのTierの寄せ集めではなく、実際に行うアップグレードの距離を表す)
|
|
150
|
-
const upgradeTier = recommended !== null ? classifyTier(currentSeries, recommended) : null;
|
|
143
|
+
const upgradeTier = recommended !== null ? scheme.classify(pkg.version, recommended) : null;
|
|
151
144
|
return {
|
|
152
145
|
package: pkg.name,
|
|
153
146
|
current_version: pkg.version,
|
|
154
147
|
ecosystem: pkg.ecosystem,
|
|
155
148
|
recommended_upgrade: recommended,
|
|
156
149
|
upgrade_tier: upgradeTier,
|
|
150
|
+
...(recommended !== null && scheme.isPrerelease(recommended) ? { recommended_is_prerelease: true } : {}),
|
|
157
151
|
upgrade_note: recommended === null && fixableCount > 0
|
|
158
|
-
? "既知の修正版候補から、全修正対象CVEの影響範囲外と確認できる版が見つかりません。情報不足・未対応の範囲形式を含む場合も推奨を保留します。"
|
|
159
|
-
|
|
152
|
+
? "既知の修正版候補から、全修正対象CVEの影響範囲外と確認できる版が見つかりません。情報不足・未対応の範囲形式を含む場合も推奨を保留します。" +
|
|
153
|
+
(unparseableCount > 0 ? `${unparseableCount}件のCVEは修正版の記載をバージョンとして解釈できないため(tier: unparseable_fix)、推奨を保留しています。` : "")
|
|
154
|
+
: buildNote(scheme, pkg, recommended, upgradeTier, fixableCount, unfixedCount),
|
|
155
|
+
...hintFields(pkg.ecosystem),
|
|
156
|
+
...(pkg.version_is_lower_bound ? { version_is_lower_bound: true } : {}),
|
|
160
157
|
per_cve_detail: details,
|
|
161
158
|
verification: recommended === null ? "no_verified_candidate" : "verified",
|
|
162
159
|
};
|
|
@@ -3,13 +3,125 @@
|
|
|
3
3
|
*
|
|
4
4
|
* - Maven: Mavenの優先順位(mavenVersion.ts)。範囲はECOSYSTEM型
|
|
5
5
|
* - npm・Go: SemVer 2.0.0の優先順位(semverVersion.ts)。範囲はSEMVER型とECOSYSTEM型
|
|
6
|
-
* -
|
|
6
|
+
* - PyPI: PEP 440の優先順位(pep440Version.ts)。範囲はECOSYSTEM型。名前はPEP 503で正規化して照合する
|
|
7
|
+
* - その他: 比較器なし。範囲はECOSYSTEM型
|
|
7
8
|
*
|
|
8
9
|
* GIT型の範囲(コミットハッシュ)はリリース版と無関係のため、どのエコシステムでも版として扱わない。
|
|
9
10
|
*/
|
|
10
|
-
import { compareMavenVersions } from "../utils/mavenVersion.js";
|
|
11
|
+
import { compareMavenVersions, mavenVersionSeries } from "../utils/mavenVersion.js";
|
|
12
|
+
import { compareParsedPep440, parsePep440 } from "../utils/pep440Version.js";
|
|
11
13
|
import { compareParsedSemver, parseSemver } from "../utils/semverVersion.js";
|
|
12
14
|
const SEMVER_ECOSYSTEMS = new Set(["npm", "Go"]);
|
|
15
|
+
const MAVEN_SCHEME = {
|
|
16
|
+
isValid: () => true,
|
|
17
|
+
compare: compareMavenVersions,
|
|
18
|
+
isPrerelease: () => false,
|
|
19
|
+
classify(current, candidate) {
|
|
20
|
+
const a = mavenVersionSeries(current);
|
|
21
|
+
const b = mavenVersionSeries(candidate);
|
|
22
|
+
if (a === null || b === null || a.major !== b.major)
|
|
23
|
+
return "cross_major";
|
|
24
|
+
return a.minor === b.minor ? "same_minor" : "major_internal";
|
|
25
|
+
},
|
|
26
|
+
seriesLabel(version) {
|
|
27
|
+
const series = mavenVersionSeries(version);
|
|
28
|
+
return series === null ? null : `${series.major}.${series.minor}`;
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* npm・Go: npmの^(キャレット)が互換とみなす範囲を同じ系統とする。
|
|
33
|
+
* 1.0.0以上はmajor.minor、0.xは0.minor内だけが互換(マイナー更新は破壊的変更の可能性)、0.0.xはどの変更も非互換。
|
|
34
|
+
*/
|
|
35
|
+
const SEMVER_SCHEME = {
|
|
36
|
+
isValid: (version) => parseSemver(version) !== null,
|
|
37
|
+
compare(a, b) {
|
|
38
|
+
const x = parseSemver(a);
|
|
39
|
+
const y = parseSemver(b);
|
|
40
|
+
return x === null || y === null ? 0 : compareParsedSemver(x, y);
|
|
41
|
+
},
|
|
42
|
+
isPrerelease: (version) => (parseSemver(version)?.prerelease.length ?? 0) > 0,
|
|
43
|
+
classify(current, candidate) {
|
|
44
|
+
const a = parseSemver(current);
|
|
45
|
+
const b = parseSemver(candidate);
|
|
46
|
+
if (a === null || b === null)
|
|
47
|
+
return "cross_major";
|
|
48
|
+
return classifyCaret(a, b);
|
|
49
|
+
},
|
|
50
|
+
seriesLabel(version) {
|
|
51
|
+
const v = parseSemver(version);
|
|
52
|
+
if (v === null)
|
|
53
|
+
return null;
|
|
54
|
+
return v.major === 0 && v.minor === 0 ? `0.0.${v.patch}` : `${v.major}.${v.minor}`;
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
/** major・minor・patchから、npmのキャレットに合わせた距離を判定する(npm・Go・PyPI共通) */
|
|
58
|
+
function classifyCaret(a, b) {
|
|
59
|
+
if (a.major !== b.major)
|
|
60
|
+
return "cross_major";
|
|
61
|
+
if (Number(a.major) === 0) {
|
|
62
|
+
if (a.minor !== b.minor)
|
|
63
|
+
return "cross_major";
|
|
64
|
+
if (Number(a.minor) === 0 && a.patch !== b.patch)
|
|
65
|
+
return "cross_major";
|
|
66
|
+
return "same_minor";
|
|
67
|
+
}
|
|
68
|
+
return a.minor === b.minor ? "same_minor" : "major_internal";
|
|
69
|
+
}
|
|
70
|
+
function pep440Parts(v) {
|
|
71
|
+
return { major: v.release[0] ?? 0n, minor: v.release[1] ?? 0n, patch: v.release[2] ?? 0n };
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* PyPI: PEP 440。共通の互換規則はないが、0.x系でマイナー更新が破壊的変更になるパッケージが実在する(FastAPI等)ため、
|
|
75
|
+
* npm・Goと同じ規則で破壊的変更の可能性を少なく見積もらない側に倒す。epochが変わる更新もcross_major。
|
|
76
|
+
* プレリリースはプレリリース・dev版(post版は正式版扱い)。
|
|
77
|
+
*/
|
|
78
|
+
const PEP440_SCHEME = {
|
|
79
|
+
isValid: (version) => parsePep440(version) !== null,
|
|
80
|
+
compare(a, b) {
|
|
81
|
+
const x = parsePep440(a);
|
|
82
|
+
const y = parsePep440(b);
|
|
83
|
+
return x === null || y === null ? 0 : compareParsedPep440(x, y);
|
|
84
|
+
},
|
|
85
|
+
isPrerelease(version) {
|
|
86
|
+
const v = parsePep440(version);
|
|
87
|
+
return v !== null && (v.pre !== null || v.dev !== null);
|
|
88
|
+
},
|
|
89
|
+
classify(current, candidate) {
|
|
90
|
+
const a = parsePep440(current);
|
|
91
|
+
const b = parsePep440(candidate);
|
|
92
|
+
if (a === null || b === null || a.epoch !== b.epoch)
|
|
93
|
+
return "cross_major";
|
|
94
|
+
return classifyCaret(pep440Parts(a), pep440Parts(b));
|
|
95
|
+
},
|
|
96
|
+
seriesLabel(version) {
|
|
97
|
+
const v = parsePep440(version);
|
|
98
|
+
if (v === null)
|
|
99
|
+
return null;
|
|
100
|
+
const { major, minor, patch } = pep440Parts(v);
|
|
101
|
+
const epoch = v.epoch === 0n ? "" : `${v.epoch}!`;
|
|
102
|
+
return major === 0n && minor === 0n ? `${epoch}0.0.${patch}` : `${epoch}${major}.${minor}`;
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
/** 修正版の推奨に対応するエコシステムの操作。未対応ならnull */
|
|
106
|
+
export function versionSchemeFor(ecosystem) {
|
|
107
|
+
if (ecosystem === "Maven")
|
|
108
|
+
return MAVEN_SCHEME;
|
|
109
|
+
if (SEMVER_ECOSYSTEMS.has(ecosystem))
|
|
110
|
+
return SEMVER_SCHEME;
|
|
111
|
+
if (ecosystem === "PyPI")
|
|
112
|
+
return PEP440_SCHEME;
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* OSVレコードのパッケージ名とスキャン結果の名前が同じパッケージか。
|
|
117
|
+
* PyPIはPEP 503の正規化(小文字化、`-` `_` `.`の連続を`-`)で比較する(実データでは完全一致したが防御的に)
|
|
118
|
+
*/
|
|
119
|
+
export function samePackageName(a, b, ecosystem) {
|
|
120
|
+
if (ecosystem !== "PyPI")
|
|
121
|
+
return a === b;
|
|
122
|
+
const normalize = (name) => name.toLowerCase().replace(/[-_.]+/g, "-");
|
|
123
|
+
return normalize(a) === normalize(b);
|
|
124
|
+
}
|
|
13
125
|
/** 修正版・影響範囲として読み取るOSVの範囲の型 */
|
|
14
126
|
export function versionRangeTypes(ecosystem) {
|
|
15
127
|
if (ecosystem === "Maven")
|
|
@@ -20,20 +132,18 @@ export function versionRangeTypes(ecosystem) {
|
|
|
20
132
|
}
|
|
21
133
|
/**
|
|
22
134
|
* 重複を除いた版の一覧を、エコシステムの優先順位で昇順に並べる。
|
|
23
|
-
* - npm・Go:
|
|
135
|
+
* - npm・Go・PyPI: 解釈できない版は末尾に記載順のまま置く
|
|
24
136
|
* - 比較器のないエコシステム: 記載順のまま返す
|
|
25
137
|
*/
|
|
26
138
|
export function sortVersions(versions, ecosystem) {
|
|
27
139
|
const unique = [...new Set(versions)];
|
|
28
140
|
if (ecosystem === "Maven")
|
|
29
141
|
return unique.sort(compareMavenVersions);
|
|
30
|
-
|
|
142
|
+
const scheme = versionSchemeFor(ecosystem);
|
|
143
|
+
if (scheme === null)
|
|
31
144
|
return unique;
|
|
32
|
-
const parsed = unique.
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
const unparsed = unique.filter((raw) => parseSemver(raw) === null);
|
|
37
|
-
parsed.sort((a, b) => compareParsedSemver(a.version, b.version) || (a.raw < b.raw ? -1 : a.raw > b.raw ? 1 : 0));
|
|
38
|
-
return [...parsed.map((p) => p.raw), ...unparsed];
|
|
145
|
+
const parsed = unique.filter((raw) => scheme.isValid(raw));
|
|
146
|
+
const unparsed = unique.filter((raw) => !scheme.isValid(raw));
|
|
147
|
+
parsed.sort((a, b) => scheme.compare(a, b) || (a < b ? -1 : a > b ? 1 : 0));
|
|
148
|
+
return [...parsed, ...unparsed];
|
|
39
149
|
}
|
|
@@ -14,7 +14,7 @@ import { normalizePypiName } from "../utils/requirementsFile.js";
|
|
|
14
14
|
import { ScanSnapshot, snapshotManifests } from "../utils/scanSnapshot.js";
|
|
15
15
|
import { dependencyResolution, withScopeNotes } from "./scanJavaProject.js";
|
|
16
16
|
import { errorResult, jsonResult } from "./toolResult.js";
|
|
17
|
-
const TRANSITIVE_OMITTED_WARNING = "OSV_MCP_NO_REMOTE_RESOLUTIONの設定により、マニフェスト(pom.xml / requirements.txt)からの推移的依存の解決を省略しています。" +
|
|
17
|
+
export const TRANSITIVE_OMITTED_WARNING = "OSV_MCP_NO_REMOTE_RESOLUTIONの設定により、マニフェスト(pom.xml / requirements.txt)からの推移的依存の解決を省略しています。" +
|
|
18
18
|
"lockfile(package-lock.json / poetry.lock / go.mod / gradle.lockfile等)に記録された依存は対象ですが、" +
|
|
19
19
|
"pom.xml・requirements.txtに直接記載された依存の先にある推移的依存の脆弱性は含まれません。" +
|
|
20
20
|
"検出0件でも推移的依存の安全性は確認できていません";
|
|
@@ -26,7 +26,7 @@ const MAX_COVERAGE_ITEMS = 200;
|
|
|
26
26
|
function capped(items) {
|
|
27
27
|
return { items: items.slice(0, MAX_COVERAGE_ITEMS), omitted: Math.max(0, items.length - MAX_COVERAGE_ITEMS) };
|
|
28
28
|
}
|
|
29
|
-
function buildCoverage(project) {
|
|
29
|
+
export function buildCoverage(project) {
|
|
30
30
|
const rel = (file) => sanitizeExternalText(path.relative(project.projectDir, file));
|
|
31
31
|
const lockfileMissing = capped(project.lockfileMissing);
|
|
32
32
|
const unpinned = capped(project.requirementIssues);
|
|
@@ -78,22 +78,19 @@ function ecosystemBreakdown(project, packages) {
|
|
|
78
78
|
return breakdown;
|
|
79
79
|
}
|
|
80
80
|
/** `>=X` / `~=X` の行は、osv-scannerが下限Xを使用中の版とみなしてスキャンしている */
|
|
81
|
-
function markLowerBounds(project, packages) {
|
|
81
|
+
export function markLowerBounds(project, packages) {
|
|
82
82
|
return packages.map((pkg) => pkg.ecosystem === "PyPI" &&
|
|
83
83
|
project.lowerBounds.some((lb) => lb.name === normalizePypiName(pkg.name) && lb.version === pkg.version)
|
|
84
84
|
? { ...pkg, version_is_lower_bound: true }
|
|
85
85
|
: pkg);
|
|
86
86
|
}
|
|
87
|
-
/**
|
|
88
|
-
* requirements.txtは検証済みの正規化行だけを専用の一時ディレクトリに書いてスキャンする
|
|
89
|
-
* (元ファイルの取り込み指定をosv-scannerにたどらせない)。成功・失敗とも削除する。
|
|
90
|
-
*/
|
|
91
87
|
/**
|
|
92
88
|
* 元のファイルはosv-scannerに渡さず、スナップショット(scanSnapshot.ts)のコピーをスキャンする。
|
|
93
89
|
* lockfile・pom.xml(親POMの連鎖を含む)は安全に読んだ内容のコピー、requirements.txtは
|
|
94
90
|
* 検証済みの正規化行だけを書いたコピー。コピーできず外したファイルはskippedFilesに記録する。
|
|
91
|
+
* スナップショットは成功・失敗とも削除する。suggest_fixも同じスキャンを使う。
|
|
95
92
|
*/
|
|
96
|
-
async function scanFromSnapshot(project, options) {
|
|
93
|
+
export async function scanFromSnapshot(project, options) {
|
|
97
94
|
const snapshot = await ScanSnapshot.create();
|
|
98
95
|
try {
|
|
99
96
|
const { targets, skipped, incomplete } = await snapshotManifests(snapshot, project.targets, {
|
package/dist/tools/suggestFix.js
CHANGED
|
@@ -1,27 +1,28 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* `suggest_fix`ツールのハンドラ。
|
|
3
|
-
*
|
|
4
|
-
* 推奨アップグレードバージョン(3段階Tier)
|
|
3
|
+
* scan_projectと同じ検出・スキャンを実行し、脆弱なパッケージごとの
|
|
4
|
+
* 推奨アップグレードバージョン(3段階Tier)を返す。推奨はMaven・npm・Go・PyPIに対応し、
|
|
5
|
+
* それ以外はunsupported_ecosystemとして返す。
|
|
5
6
|
*/
|
|
6
7
|
import { isRemoteResolutionDisabled } from "../osv/runner.js";
|
|
7
8
|
import { suggestUpgrades } from "../osv/suggestFix.js";
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
9
|
+
import { sanitizeExternalText } from "../utils/externalText.js";
|
|
10
|
+
import { detectProject } from "../utils/manifestDetector.js";
|
|
11
|
+
import { dependencyResolution, } from "./scanJavaProject.js";
|
|
12
|
+
import { buildCoverage, markLowerBounds, scanFromSnapshot, TRANSITIVE_OMITTED_WARNING } from "./scanProject.js";
|
|
10
13
|
import { errorResult, jsonResult } from "./toolResult.js";
|
|
11
14
|
export async function handleSuggestFix(args, options = {}) {
|
|
12
15
|
try {
|
|
13
|
-
const project = await
|
|
14
|
-
allowedRoot: options.allowedRoot,
|
|
15
|
-
});
|
|
16
|
+
const project = await detectProject(args.project_path, { allowedRoot: options.allowedRoot });
|
|
16
17
|
const noRemoteResolution = isRemoteResolutionDisabled(options);
|
|
17
|
-
const
|
|
18
|
-
const suggestions = suggestUpgrades(report.packages);
|
|
18
|
+
const report = await scanFromSnapshot(project, { ...options, noRemoteResolution });
|
|
19
|
+
const suggestions = suggestUpgrades(markLowerBounds(project, report.packages));
|
|
19
20
|
const unfixedVulnerabilities = suggestions.reduce((sum, s) => sum + s.per_cve_detail.filter((d) => d.tier === "unfixed").length, 0);
|
|
20
21
|
return jsonResult({
|
|
21
22
|
project_dir: project.projectDir,
|
|
22
|
-
manifests,
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
manifests: project.manifests.map((m) => sanitizeExternalText(m.path)),
|
|
24
|
+
dependency_resolution: dependencyResolution(noRemoteResolution, TRANSITIVE_OMITTED_WARNING),
|
|
25
|
+
coverage: buildCoverage(project),
|
|
25
26
|
vulnerable_package_count: suggestions.length,
|
|
26
27
|
unfixed_vulnerability_count: unfixedVulnerabilities,
|
|
27
28
|
suggestions,
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PEP 440(Pythonパッケージのバージョン)の解釈と優先順位比較。
|
|
3
|
+
*
|
|
4
|
+
* 正規化と比較はPyPA `packaging`(packaging.version)と同じ規則に従う:
|
|
5
|
+
* - 大文字小文字を無視し、先頭の`v`と前後の空白を許す
|
|
6
|
+
* - プレリリースの別表記: alpha→a、beta→b、c/pre/preview→rc。区切り文字(`.` `-` `_`)と番号は省略可(番号の省略は0)
|
|
7
|
+
* - post版: `.post1`、`-1`、`rev1`、`r1`。dev版: `.dev1`
|
|
8
|
+
* - ローカル版: `+`以降。区切りは`.` `-` `_`
|
|
9
|
+
* - 比較: epoch → リリース番号(末尾のゼロは無視: `1.0` = `1.0.0`) → プレリリース → post → dev → ローカル版
|
|
10
|
+
* `1.0.dev1` < `1.0a1.dev1` < `1.0a1` < `1.0a1.post1` < `1.0b1` < `1.0rc1` < `1.0` < `1.0.post1.dev1` < `1.0.post1` < `1.0.post1+local`
|
|
11
|
+
*
|
|
12
|
+
* 実データ(docs/DESIGN_TODO.md「suggest_fix PyPI対応」)では、Djangoの`1.8c1`、TensorFlowの`2.8.0-rc0`のような
|
|
13
|
+
* 正規形でない値と、PyTorchの`2.6.0-cu124`のようなPEP 440でない値がある。後者はnullを返す。
|
|
14
|
+
* 数値はBigIntで保持する(2^53を超える値を丸めない)。
|
|
15
|
+
*/
|
|
16
|
+
const PEP440_RE = new RegExp("^\\s*v?" +
|
|
17
|
+
"(?:(?<epoch>[0-9]+)!)?" +
|
|
18
|
+
"(?<release>[0-9]+(?:\\.[0-9]+)*)" +
|
|
19
|
+
"(?<pre>[-_.]?(?<pre_l>alpha|a|beta|b|preview|pre|c|rc)[-_.]?(?<pre_n>[0-9]+)?)?" +
|
|
20
|
+
"(?<post>(?:-(?<post_n1>[0-9]+))|(?:[-_.]?(?<post_l>post|rev|r)[-_.]?(?<post_n2>[0-9]+)?))?" +
|
|
21
|
+
"(?<dev>[-_.]?(?<dev_l>dev)[-_.]?(?<dev_n>[0-9]+)?)?" +
|
|
22
|
+
"(?:\\+(?<local>[a-z0-9]+(?:[-_.][a-z0-9]+)*))?" +
|
|
23
|
+
"\\s*$", "i");
|
|
24
|
+
const PRE_KINDS = {
|
|
25
|
+
a: "a", alpha: "a", b: "b", beta: "b", c: "rc", pre: "rc", preview: "rc", rc: "rc",
|
|
26
|
+
};
|
|
27
|
+
/** PEP 440として解釈する。解釈できなければnull */
|
|
28
|
+
export function parsePep440(version) {
|
|
29
|
+
const m = PEP440_RE.exec(version);
|
|
30
|
+
const g = m?.groups;
|
|
31
|
+
if (!g)
|
|
32
|
+
return null;
|
|
33
|
+
const preLetter = g.pre_l?.toLowerCase();
|
|
34
|
+
return {
|
|
35
|
+
epoch: BigInt(g.epoch ?? "0"),
|
|
36
|
+
release: g.release.split(".").map(BigInt),
|
|
37
|
+
pre: preLetter === undefined ? null : { kind: PRE_KINDS[preLetter], number: BigInt(g.pre_n ?? "0") },
|
|
38
|
+
post: g.post === undefined ? null : BigInt(g.post_n1 ?? g.post_n2 ?? "0"),
|
|
39
|
+
dev: g.dev === undefined ? null : BigInt(g.dev_n ?? "0"),
|
|
40
|
+
local: g.local === undefined ? null : g.local.toLowerCase().split(/[-_.]/).map((s) => (/^\d+$/.test(s) ? BigInt(s) : s)),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
function cmp(a, b) {
|
|
44
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
45
|
+
}
|
|
46
|
+
const PRE_ORDER = { a: 0, b: 1, rc: 2 };
|
|
47
|
+
/** プレリリースの順位: dev版だけの版はすべてのプレリリースより前、プレリリースのない版は後 */
|
|
48
|
+
function preKey(v) {
|
|
49
|
+
if (v.pre === null)
|
|
50
|
+
return v.post === null && v.dev !== null ? [0, 0, 0n] : [2, 0, 0n];
|
|
51
|
+
return [1, PRE_ORDER[v.pre.kind], v.pre.number];
|
|
52
|
+
}
|
|
53
|
+
function compareRelease(a, b) {
|
|
54
|
+
// 末尾のゼロは比較に影響しない(1.0 = 1.0.0)
|
|
55
|
+
const length = Math.max(a.length, b.length);
|
|
56
|
+
for (let i = 0; i < length; i++) {
|
|
57
|
+
const result = cmp(a[i] ?? 0n, b[i] ?? 0n);
|
|
58
|
+
if (result !== 0)
|
|
59
|
+
return result;
|
|
60
|
+
}
|
|
61
|
+
return 0;
|
|
62
|
+
}
|
|
63
|
+
function compareLocal(a, b) {
|
|
64
|
+
if (a === null || b === null)
|
|
65
|
+
return a === b ? 0 : a === null ? -1 : 1;
|
|
66
|
+
const length = Math.min(a.length, b.length);
|
|
67
|
+
for (let i = 0; i < length; i++) {
|
|
68
|
+
const x = a[i];
|
|
69
|
+
const y = b[i];
|
|
70
|
+
if (typeof x === typeof y) {
|
|
71
|
+
const result = cmp(x, y);
|
|
72
|
+
if (result !== 0)
|
|
73
|
+
return result;
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
// 数値の区切りは英字の区切りより大きい
|
|
77
|
+
return typeof x === "bigint" ? 1 : -1;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return cmp(a.length, b.length);
|
|
81
|
+
}
|
|
82
|
+
/** 解釈済みの2つの版をPEP 440の優先順位で比較する */
|
|
83
|
+
export function compareParsedPep440(a, b) {
|
|
84
|
+
const [ap0, ap1, ap2] = preKey(a);
|
|
85
|
+
const [bp0, bp1, bp2] = preKey(b);
|
|
86
|
+
return (cmp(a.epoch, b.epoch) ||
|
|
87
|
+
compareRelease(a.release, b.release) ||
|
|
88
|
+
cmp(ap0, bp0) || cmp(ap1, bp1) || cmp(ap2, bp2) ||
|
|
89
|
+
// post版のない版はpost版より前
|
|
90
|
+
cmp(a.post === null ? -1n : a.post, b.post === null ? -1n : b.post) ||
|
|
91
|
+
// dev版でない版はdev版より後
|
|
92
|
+
(a.dev === b.dev ? 0 : a.dev === null ? 1 : b.dev === null ? -1 : cmp(a.dev, b.dev)) ||
|
|
93
|
+
compareLocal(a.local, b.local));
|
|
94
|
+
}
|
|
95
|
+
/** 2つの版をPEP 440の優先順位で比較する。どちらかを解釈できなければnull */
|
|
96
|
+
export function comparePep440(a, b) {
|
|
97
|
+
const x = parsePep440(a);
|
|
98
|
+
const y = parsePep440(b);
|
|
99
|
+
return x === null || y === null ? null : compareParsedPep440(x, y);
|
|
100
|
+
}
|