cve-lite-cli 1.0.5 → 1.1.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 +161 -16
- package/dist/advisory/local-advisory-source.js +21 -0
- package/dist/advisory/local-db.js +188 -0
- package/dist/advisory/osv-sync.js +165 -0
- package/dist/cli/args.js +38 -1
- package/dist/cli/help.js +9 -3
- package/dist/index.js +61 -14
- package/dist/osv/cache.js +12 -4
- package/dist/output/formatters.js +42 -13
- package/dist/output/printers.js +18 -3
- package/dist/output/spinner.js +1 -0
- package/dist/remediation/parent-upgrade.js +224 -0
- package/dist/scanner.js +114 -15
- package/package.json +10 -2
package/README.md
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
|
|
2
|
+
[](https://www.npmjs.com/package/cve-lite-cli)
|
|
3
|
+
[](https://www.npmjs.com/package/cve-lite-cli)
|
|
4
|
+
[](https://github.com/sonukapoor/cve-lite-cli/actions)
|
|
5
|
+
[](https://github.com/sonukapoor/cve-lite-cli/blob/main/LICENSE)
|
|
6
|
+
|
|
1
7
|
<div align="center">
|
|
2
8
|
<img src="https://github.com/sonukapoor/cve-lite-cli/blob/main/assets/logo-with-title.png" alt="CVE Lite CLI" width="320"/>
|
|
3
9
|
|
|
@@ -10,7 +16,7 @@
|
|
|
10
16
|
<a href="#quick-start">Quick Start</a> •
|
|
11
17
|
<a href="#features">Features</a> •
|
|
12
18
|
<a href="#usage">Usage</a> •
|
|
13
|
-
<a href="CONTRIBUTING.md">Contributing</a>
|
|
19
|
+
<a href="https://github.com/sonukapoor/cve-lite-cli/blob/main/src/docs/CONTRIBUTING.md">Contributing</a>
|
|
14
20
|
</p>
|
|
15
21
|
|
|
16
22
|
</div>
|
|
@@ -21,6 +27,29 @@
|
|
|
21
27
|
|
|
22
28
|
**CVE Lite CLI** helps developers scan their projects for known package vulnerabilities without signing up for an expensive platform. It is built for the moment right before release, when you want a clear answer, a practical fix plan, and a tool you can actually afford to use.
|
|
23
29
|
|
|
30
|
+
## New: Offline advisory DB support
|
|
31
|
+
|
|
32
|
+
CVE Lite CLI now supports a local advisory database workflow for teams that cannot allow runtime outbound advisory API calls.
|
|
33
|
+
|
|
34
|
+
That means you can:
|
|
35
|
+
|
|
36
|
+
- sync advisory data ahead of time
|
|
37
|
+
- scan projects fully offline
|
|
38
|
+
- use the tool in restricted, enterprise, or air-gapped environments more easily
|
|
39
|
+
|
|
40
|
+
Example offline workflow:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
# Build the local advisory database
|
|
44
|
+
cve-lite advisories sync
|
|
45
|
+
|
|
46
|
+
# Scan using the default local advisory DB
|
|
47
|
+
cve-lite /path/to/project --offline
|
|
48
|
+
|
|
49
|
+
# Or scan using a specific local advisory DB file
|
|
50
|
+
cve-lite /path/to/project --offline-db /path/to/advisories.db
|
|
51
|
+
```
|
|
52
|
+
|
|
24
53
|
## Quick start
|
|
25
54
|
|
|
26
55
|
Install globally:
|
|
@@ -68,13 +97,22 @@ cve-lite /path/to/project --sarif --output reports/scan.sarif
|
|
|
68
97
|
|
|
69
98
|
# Use a custom advisory endpoint
|
|
70
99
|
cve-lite /path/to/project --osv-url https://security.company.internal/osv
|
|
100
|
+
|
|
101
|
+
# Build the local advisory DB for offline scans
|
|
102
|
+
cve-lite advisories sync
|
|
103
|
+
|
|
104
|
+
# Scan with zero runtime advisory API calls
|
|
105
|
+
cve-lite /path/to/project --offline
|
|
106
|
+
|
|
107
|
+
# Use a specific local advisory DB file
|
|
108
|
+
cve-lite /path/to/project --offline-db /path/to/advisories.db
|
|
71
109
|
```
|
|
72
110
|
|
|
73
111
|
## What it looks like
|
|
74
112
|
|
|
75
113
|
CVE Lite CLI produces a clean, summary-first console view by default, designed for fast triage before release.
|
|
76
114
|
|
|
77
|
-
For deeper investigation, running with `--verbose` provides full details, including dependency paths, a complete fix plan, and a detailed table view.
|
|
115
|
+
For deeper investigation, running with `--verbose` provides full details, including dependency paths, parent upgrade guidance for transitive issues when available, a complete fix plan, and a detailed table view.
|
|
78
116
|
|
|
79
117
|
That is the core idea: install it, point it at your project, and immediately get a practical fix plan instead of a wall of raw advisories.
|
|
80
118
|
|
|
@@ -139,6 +177,7 @@ The project emphasizes:
|
|
|
139
177
|
- direct vs transitive visibility
|
|
140
178
|
- top-priority fixes and a suggested remediation plan
|
|
141
179
|
- JSON and SARIF output for automation
|
|
180
|
+
- offline scanning through a local advisory database
|
|
142
181
|
- configurable advisory endpoint support via `--osv-url` for internal proxies or mirrors
|
|
143
182
|
- a small, reviewable runtime footprint
|
|
144
183
|
|
|
@@ -160,19 +199,88 @@ This section is here to make the scope clear. CVE Lite CLI aims to complement th
|
|
|
160
199
|
|
|
161
200
|
## Network behavior and privacy
|
|
162
201
|
|
|
163
|
-
CVE Lite CLI is a local-first scanner. It parses dependency information locally, uses local caching to reduce repeated lookups, and in standard mode queries OSV for advisory data needed for matching.
|
|
202
|
+
CVE Lite CLI is a local-first scanner. It parses dependency information locally, uses local caching to reduce repeated lookups, and in standard mode queries OSV for advisory data needed for matching. It also now supports a local advisory database workflow for offline scans with zero runtime advisory API calls.
|
|
164
203
|
|
|
165
204
|
It does not require a hosted account, cloud dashboard, or source code upload.
|
|
166
205
|
|
|
167
|
-
For the full explanation, see [Network Behavior and Privacy](
|
|
206
|
+
For the full explanation, see [Network Behavior and Privacy](https://github.com/sonukapoor/cve-lite-cli/blob/main/src/docs/network-and-privacy.md).
|
|
207
|
+
|
|
208
|
+
### Offline workflow
|
|
209
|
+
|
|
210
|
+
For teams in controlled or restricted environments, the intended model is:
|
|
211
|
+
|
|
212
|
+
1. sync advisory data into a local database
|
|
213
|
+
2. distribute that database as needed
|
|
214
|
+
3. scan projects using the local DB instead of runtime OSV API calls
|
|
215
|
+
|
|
216
|
+
Sync the local advisory database:
|
|
217
|
+
|
|
218
|
+
```bash
|
|
219
|
+
cve-lite advisories sync
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
Write the advisory database to a specific path:
|
|
223
|
+
|
|
224
|
+
```bash
|
|
225
|
+
cve-lite advisories sync --output /path/to/advisories.db
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
Scan using the default local advisory DB:
|
|
229
|
+
|
|
230
|
+
```bash
|
|
231
|
+
cve-lite . --offline
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
Scan using an explicit local advisory DB file:
|
|
235
|
+
|
|
236
|
+
```bash
|
|
237
|
+
cve-lite . --offline-db /path/to/advisories.db
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
Keep the local advisory DB fresh with a scheduled job when needed. For example, teams can run a cron job, CI job, or other scheduler to refresh the advisory DB periodically:
|
|
241
|
+
|
|
242
|
+
```bash
|
|
243
|
+
cve-lite advisories sync --output /path/to/advisories.db
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
That helps ensure offline scans continue using current advisory data without requiring developers to remember to refresh the DB manually every time.
|
|
247
|
+
|
|
248
|
+
Standard online mode remains available when you want live OSV-backed scans:
|
|
249
|
+
|
|
250
|
+
```bash
|
|
251
|
+
cve-lite . --osv-url https://security.company.internal/osv
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
### Advisory DB freshness
|
|
255
|
+
|
|
256
|
+
The local advisory DB is only as current as the last successful sync.
|
|
257
|
+
|
|
258
|
+
For now, the recommended model is:
|
|
259
|
+
|
|
260
|
+
- sync the advisory DB on a schedule using cron, CI, or another automation system
|
|
261
|
+
- distribute the refreshed DB where needed
|
|
262
|
+
- run offline scans against that updated DB
|
|
263
|
+
|
|
264
|
+
A future improvement is to add built-in advisory DB freshness metadata, such as:
|
|
168
265
|
|
|
169
|
-
|
|
266
|
+
- last sync timestamp reporting
|
|
267
|
+
- a TTL-style warning when the DB is older than a recommended threshold
|
|
268
|
+
- clearer CLI guidance when a local advisory DB should be refreshed
|
|
170
269
|
|
|
171
|
-
|
|
270
|
+
## Detecting malicious package incidents
|
|
172
271
|
|
|
173
|
-
-
|
|
174
|
-
|
|
175
|
-
|
|
272
|
+
CVE Lite CLI can help detect malicious package incidents and supply-chain compromises **once the affected versions are represented in OSV or equivalent advisory data available through the configured advisory endpoint**.
|
|
273
|
+
|
|
274
|
+
In practice, that means the tool can catch incidents such as a compromised npm package **when the exact malicious version appears in your lockfile** and the advisory data has already been published and indexed. This makes the tool useful for real-world release checks after a malicious package disclosure becomes known.
|
|
275
|
+
|
|
276
|
+
It is important to be precise about the current scope:
|
|
277
|
+
|
|
278
|
+
- CVE Lite CLI is **advisory-driven**, not a behavioral malware detector
|
|
279
|
+
- it does **not** perform static malware analysis on package contents
|
|
280
|
+
- it does **not** detect a package compromise before advisory intelligence exists for it
|
|
281
|
+
- it works best with lockfiles because they capture the exact resolved version that was actually installed
|
|
282
|
+
|
|
283
|
+
So the practical model is: **local lockfile scan + advisory matching**. When malicious versions are published to OSV or mirrored through a compatible internal endpoint, CVE Lite CLI can flag those exact versions during a scan.
|
|
176
284
|
|
|
177
285
|
## How it works
|
|
178
286
|
|
|
@@ -188,6 +296,7 @@ CVE Lite CLI is designed as a **local-first, metadata-only** scanner. Unlike tra
|
|
|
188
296
|
* **Lockfile-Driven Accuracy:** By parsing `package-lock.json`, `pnpm-lock.yaml`, or `yarn.lock`, the tool avoids the "it works on my machine" discrepancy. It scans the *exact* dependency tree that will be deployed.
|
|
189
297
|
* **Intelligent Triage:** The Analysis engine utilizes the lockfile's graph structure to distinguish between dependencies you manage directly and those brought in by third-party packages (transitive). This allows for a "Fix the Root" strategy rather than chasing individual nested vulnerabilities.
|
|
190
298
|
* **Performance Optimization:** A local TTL (Time-To-Live) cache stores advisory results. This ensures that subsequent scans—common in iterative development or CI/CD retry loops—are near-instant and respect external API rate limits.
|
|
299
|
+
* **Offline-Capable Advisory Flow:** Advisory data can be synced into a local SQLite database and reused for offline scans with zero runtime advisory API calls.
|
|
191
300
|
* **Standards-Based Output:** Results are natively available in **SARIF (Static Analysis Results Interchange Format)** and **JSON**, ensuring compatibility with modern DevSecOps dashboards and IDE integrations.
|
|
192
301
|
|
|
193
302
|
## What makes it stand out
|
|
@@ -204,6 +313,8 @@ CVE Lite CLI is designed as a **local-first, metadata-only** scanner. Unlike tra
|
|
|
204
313
|
It is intended for developers and teams who want useful security checks without paying for a large commercial product.
|
|
205
314
|
- **local-first**
|
|
206
315
|
It reads your project locally and uses package/version matching against OSV advisories.
|
|
316
|
+
- **offline-capable**
|
|
317
|
+
It can sync and scan against a local advisory database for zero-runtime-network workflows.
|
|
207
318
|
- **release-focused**
|
|
208
319
|
It is especially useful before a release, in CI, or during final dependency cleanup.
|
|
209
320
|
|
|
@@ -229,6 +340,7 @@ Instead of only showing advisory IDs, the CLI reports:
|
|
|
229
340
|
- fixed-version hint when available
|
|
230
341
|
- advisory IDs
|
|
231
342
|
- dependency path hints
|
|
343
|
+
- recommended parent upgrade for transitive issues when available
|
|
232
344
|
|
|
233
345
|
By default, the CLI now presents a cleaner summary-first view, with `--verbose` available for the full detailed output.
|
|
234
346
|
|
|
@@ -247,7 +359,7 @@ CVE Lite CLI organizes likely remediation work into a practical sequence, such a
|
|
|
247
359
|
|
|
248
360
|
### 5. Parent package hints for transitive issues
|
|
249
361
|
|
|
250
|
-
For transitive vulnerabilities, the
|
|
362
|
+
For transitive vulnerabilities, CVE Lite CLI can show the dependency path and, when it can determine one reliably, recommend the parent package upgrade to make. That makes the output more actionable than simply saying a nested package is vulnerable.
|
|
251
363
|
|
|
252
364
|
### 6. JSON output
|
|
253
365
|
|
|
@@ -267,15 +379,26 @@ cve-lite /path/to/project --sarif --output reports/scan.sarif
|
|
|
267
379
|
|
|
268
380
|
The CLI can be used in CI/CD pipelines and can fail builds based on severity thresholds.
|
|
269
381
|
|
|
270
|
-
### 9.
|
|
382
|
+
### 9. Offline advisory DB support
|
|
383
|
+
|
|
384
|
+
The CLI can sync the official OSV npm advisory dump into a local SQLite advisory database and scan against it later with zero runtime advisory API calls.
|
|
385
|
+
|
|
386
|
+
That makes it more practical for:
|
|
387
|
+
|
|
388
|
+
- enterprise environments
|
|
389
|
+
- restricted CI systems
|
|
390
|
+
- air-gapped workflows
|
|
391
|
+
- teams that want explicit local advisory control
|
|
392
|
+
|
|
393
|
+
### 10. Local cache
|
|
271
394
|
|
|
272
395
|
It caches advisory detail results locally so repeated scans are faster and make fewer repeated requests.
|
|
273
396
|
|
|
274
|
-
###
|
|
397
|
+
### 11. Clear final scan status
|
|
275
398
|
|
|
276
399
|
At the end of each run, CVE Lite CLI prints a short final status line so users immediately know whether the scan was clean or whether they should start with the priority fixes above.
|
|
277
400
|
|
|
278
|
-
###
|
|
401
|
+
### 12. Small runtime footprint
|
|
279
402
|
|
|
280
403
|
This project intentionally keeps runtime dependencies minimal to reduce attack surface and keep the tool easier to review.
|
|
281
404
|
|
|
@@ -286,6 +409,8 @@ CVE Lite CLI is intentionally designed with a very small dependency surface.
|
|
|
286
409
|
**Runtime dependencies**
|
|
287
410
|
- `yaml`
|
|
288
411
|
- `yarn-lockfile`
|
|
412
|
+
- `better-sqlite3`
|
|
413
|
+
- `fflate`
|
|
289
414
|
|
|
290
415
|
**Development dependencies**
|
|
291
416
|
- `@types/node`
|
|
@@ -304,6 +429,7 @@ CVE Lite CLI is a good fit for:
|
|
|
304
429
|
- teams that want a lightweight dependency scan in CI
|
|
305
430
|
- developers who want a second opinion alongside other tools
|
|
306
431
|
- OSS maintainers who need a practical scan before publishing
|
|
432
|
+
- teams that need offline or controlled-network dependency scanning
|
|
307
433
|
|
|
308
434
|
## Supported workflows
|
|
309
435
|
|
|
@@ -311,9 +437,16 @@ CVE Lite CLI is a good fit for:
|
|
|
311
437
|
|
|
312
438
|
Run it before a release, during dependency cleanup, or after a major package upgrade.
|
|
313
439
|
|
|
440
|
+
For offline-capable local workflows:
|
|
441
|
+
|
|
442
|
+
```bash
|
|
443
|
+
cve-lite advisories sync
|
|
444
|
+
cve-lite . --offline
|
|
445
|
+
```
|
|
446
|
+
|
|
314
447
|
### CI/CD
|
|
315
448
|
|
|
316
|
-
This repository also uses CVE Lite CLI in its own GitHub Actions workflow to scan itself as part of CI. See [`self-scan.yml`](
|
|
449
|
+
This repository also uses CVE Lite CLI in its own GitHub Actions workflow to scan itself as part of CI. See [`self-scan.yml`](https://github.com/sonukapoor/cve-lite-cli/blob/main/.github/workflows/self-scan.yml).
|
|
317
450
|
|
|
318
451
|
For CI, we recommend using `--verbose` so build logs include the full fix plan, dependency paths, and detailed table output when a scan fails.
|
|
319
452
|
|
|
@@ -337,6 +470,13 @@ Use JSON output for custom reporting:
|
|
|
337
470
|
cve-lite . --json > cve-lite-report.json
|
|
338
471
|
```
|
|
339
472
|
|
|
473
|
+
Use an offline local advisory DB in controlled environments:
|
|
474
|
+
|
|
475
|
+
```bash
|
|
476
|
+
cve-lite advisories sync --output ./.cache/advisories.db
|
|
477
|
+
cve-lite . --offline-db ./.cache/advisories.db --json > cve-lite-report.json
|
|
478
|
+
```
|
|
479
|
+
|
|
340
480
|
## Comparison with other tools
|
|
341
481
|
|
|
342
482
|
CVE Lite CLI is not trying to be everything for everyone. It is designed to be one of the easiest and most actionable vulnerability scanners for JavaScript and TypeScript developers who want fast release-time checks without the cost and complexity of a full security platform.
|
|
@@ -354,6 +494,7 @@ Compared with other tools in this space, CVE Lite CLI focuses on:
|
|
|
354
494
|
- support for **npm**, **pnpm**, and **Yarn** lockfiles
|
|
355
495
|
- **SARIF** and **JSON** output for CI and automation
|
|
356
496
|
- a **lightweight**, **security-conscious** dependency footprint
|
|
497
|
+
- **offline local advisory DB support** for controlled environments
|
|
357
498
|
- a **developer-friendly** option for teams that want useful **CVE scanning without paying for a larger commercial product**
|
|
358
499
|
|
|
359
500
|
### At a glance
|
|
@@ -422,11 +563,14 @@ If your goal is a practical developer-first CVE scan right before release, CVE L
|
|
|
422
563
|
|
|
423
564
|
To keep the project honest, here is what it does **not** do in the current version:
|
|
424
565
|
|
|
566
|
+
- it does not detect malicious packages before they are represented in advisory data
|
|
567
|
+
- it does not perform behavioral malware detection or package content analysis
|
|
425
568
|
- it does not prove exploitability
|
|
426
569
|
- it does not verify runtime reachability
|
|
427
570
|
- it does not scan container images, binaries, secrets, or IaC
|
|
428
571
|
- it does not replace a full application security program
|
|
429
572
|
- it is currently focused on JS/TS dependency scanning
|
|
573
|
+
- local advisory sync performance will need continued optimization as the advisory dataset grows
|
|
430
574
|
|
|
431
575
|
## Positioning
|
|
432
576
|
|
|
@@ -447,6 +591,7 @@ CVE Lite CLI is evolving from a vulnerability scanner into a comprehensive remed
|
|
|
447
591
|
* **Official GitHub Action:** Create a dedicated Action for one-line setup in CI/CD pipelines.
|
|
448
592
|
* **Expanded Lockfile Support:** Introduce parsers for emerging JS/TS ecosystems, including `bun.lockb`.
|
|
449
593
|
* **IDE Integration:** Develop a lightweight extension to highlight vulnerable packages directly within the code editor.
|
|
594
|
+
* **Workflow Integration Guidance:** Expand official workflow patterns for local scripts, hooks, CI, and offline developer adoption.
|
|
450
595
|
|
|
451
596
|
### Phase 3: Maturity & Compliance (Long-Term)
|
|
452
597
|
* **Standardized SBOM Support:** Add the ability to export findings as an **SBOM (Software Bill of Materials)** in CycloneDX or SPDX formats.
|
|
@@ -456,7 +601,7 @@ CVE Lite CLI is evolving from a vulnerability scanner into a comprehensive remed
|
|
|
456
601
|
|
|
457
602
|
This project is being shaped around real-world developer usability. Feedback on output clarity, remediation guidance, ecosystem coverage, and CI usage is especially valuable.
|
|
458
603
|
|
|
459
|
-
To get started, see [CONTRIBUTING.md](
|
|
604
|
+
To get started, see [CONTRIBUTING.md](https://github.com/sonukapoor/cve-lite-cli/blob/main/src/docs/CONTRIBUTING.md).
|
|
460
605
|
|
|
461
606
|
## Community and support
|
|
462
607
|
|
|
@@ -475,7 +620,7 @@ Helpful feedback includes:
|
|
|
475
620
|
|
|
476
621
|
As the project grows, contribution and security guidance will be expanded further in dedicated project docs.
|
|
477
622
|
|
|
478
|
-
For bug reports and feature requests, please use [GitHub Issues](https://github.com/sonukapoor/cve-lite-cli/issues). For contribution guidance, see [CONTRIBUTING.md](
|
|
623
|
+
For bug reports and feature requests, please use [GitHub Issues](https://github.com/sonukapoor/cve-lite-cli/issues). For contribution guidance, see [CONTRIBUTING.md](https://github.com/sonukapoor/cve-lite-cli/blob/main/src/docs/CONTRIBUTING.md). For security-related reporting, see [SECURITY.md](https://github.com/sonukapoor/cve-lite-cli/blob/main/src/docs/SECURITY.md).
|
|
479
624
|
|
|
480
625
|
## License
|
|
481
626
|
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export class LocalAdvisorySource {
|
|
2
|
+
db;
|
|
3
|
+
constructor(db) {
|
|
4
|
+
this.db = db;
|
|
5
|
+
}
|
|
6
|
+
queryBatch(packages) {
|
|
7
|
+
const results = packages.map(pkg => ({
|
|
8
|
+
package: pkg.name,
|
|
9
|
+
version: pkg.version,
|
|
10
|
+
vulnerabilities: this.db.findMatchingVulnerabilityIds(pkg).map(id => ({ id })),
|
|
11
|
+
}));
|
|
12
|
+
return Promise.resolve(results);
|
|
13
|
+
}
|
|
14
|
+
getVuln(id) {
|
|
15
|
+
const vuln = this.db.getVulnerability(id);
|
|
16
|
+
if (!vuln) {
|
|
17
|
+
return Promise.reject(new Error(`Local advisory database lookup failed for ${id}`));
|
|
18
|
+
}
|
|
19
|
+
return Promise.resolve(vuln);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import Database from "better-sqlite3";
|
|
4
|
+
import { compareVersions, looksLikeVersion } from "../utils/version.js";
|
|
5
|
+
const SCHEMA_SQL = `
|
|
6
|
+
CREATE TABLE IF NOT EXISTS advisories (
|
|
7
|
+
id TEXT PRIMARY KEY,
|
|
8
|
+
modified_at TEXT,
|
|
9
|
+
aliases_json TEXT NOT NULL,
|
|
10
|
+
summary TEXT,
|
|
11
|
+
osv_json TEXT NOT NULL
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
CREATE TABLE IF NOT EXISTS advisory_packages (
|
|
15
|
+
advisory_id TEXT NOT NULL,
|
|
16
|
+
ecosystem TEXT NOT NULL,
|
|
17
|
+
package_name TEXT NOT NULL,
|
|
18
|
+
introduced TEXT,
|
|
19
|
+
fixed TEXT,
|
|
20
|
+
last_affected TEXT,
|
|
21
|
+
FOREIGN KEY (advisory_id) REFERENCES advisories(id) ON DELETE CASCADE
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
CREATE INDEX IF NOT EXISTS idx_advisory_packages_lookup
|
|
25
|
+
ON advisory_packages (ecosystem, package_name);
|
|
26
|
+
|
|
27
|
+
CREATE INDEX IF NOT EXISTS idx_advisory_packages_advisory
|
|
28
|
+
ON advisory_packages (advisory_id);
|
|
29
|
+
`;
|
|
30
|
+
export class LocalAdvisoryDatabase {
|
|
31
|
+
dbPath;
|
|
32
|
+
db;
|
|
33
|
+
constructor(dbPath, options) {
|
|
34
|
+
this.dbPath = dbPath;
|
|
35
|
+
const readonly = !!options?.readonly;
|
|
36
|
+
if (!readonly) {
|
|
37
|
+
fs.mkdirSync(path.dirname(dbPath), { recursive: true });
|
|
38
|
+
}
|
|
39
|
+
this.db = new Database(dbPath, readonly ? { readonly: true, fileMustExist: true } : undefined);
|
|
40
|
+
this.db.pragma("foreign_keys = ON");
|
|
41
|
+
this.db.exec(SCHEMA_SQL);
|
|
42
|
+
}
|
|
43
|
+
close() {
|
|
44
|
+
this.db.close();
|
|
45
|
+
}
|
|
46
|
+
upsertVulnerability(vuln) {
|
|
47
|
+
const advisoryRows = deriveAdvisoryPackageRows(vuln);
|
|
48
|
+
const advisoryJson = JSON.stringify(vuln);
|
|
49
|
+
const aliasesJson = JSON.stringify(vuln.aliases ?? []);
|
|
50
|
+
const upsertAdvisory = this.db.prepare(`
|
|
51
|
+
INSERT INTO advisories (id, modified_at, aliases_json, summary, osv_json)
|
|
52
|
+
VALUES (@id, @modified_at, @aliases_json, @summary, @osv_json)
|
|
53
|
+
ON CONFLICT(id) DO UPDATE SET
|
|
54
|
+
modified_at = excluded.modified_at,
|
|
55
|
+
aliases_json = excluded.aliases_json,
|
|
56
|
+
summary = excluded.summary,
|
|
57
|
+
osv_json = excluded.osv_json
|
|
58
|
+
`);
|
|
59
|
+
const deleteRanges = this.db.prepare(`
|
|
60
|
+
DELETE FROM advisory_packages
|
|
61
|
+
WHERE advisory_id = ?
|
|
62
|
+
`);
|
|
63
|
+
const insertRange = this.db.prepare(`
|
|
64
|
+
INSERT INTO advisory_packages (
|
|
65
|
+
advisory_id,
|
|
66
|
+
ecosystem,
|
|
67
|
+
package_name,
|
|
68
|
+
introduced,
|
|
69
|
+
fixed,
|
|
70
|
+
last_affected
|
|
71
|
+
) VALUES (
|
|
72
|
+
@advisory_id,
|
|
73
|
+
@ecosystem,
|
|
74
|
+
@package_name,
|
|
75
|
+
@introduced,
|
|
76
|
+
@fixed,
|
|
77
|
+
@last_affected
|
|
78
|
+
)
|
|
79
|
+
`);
|
|
80
|
+
const transaction = this.db.transaction(() => {
|
|
81
|
+
upsertAdvisory.run({
|
|
82
|
+
id: vuln.id,
|
|
83
|
+
modified_at: null,
|
|
84
|
+
aliases_json: aliasesJson,
|
|
85
|
+
summary: vuln.summary ?? null,
|
|
86
|
+
osv_json: advisoryJson,
|
|
87
|
+
});
|
|
88
|
+
deleteRanges.run(vuln.id);
|
|
89
|
+
for (const row of advisoryRows) {
|
|
90
|
+
insertRange.run(row);
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
transaction();
|
|
94
|
+
}
|
|
95
|
+
getVulnerability(id) {
|
|
96
|
+
const row = this.db.prepare(`
|
|
97
|
+
SELECT osv_json
|
|
98
|
+
FROM advisories
|
|
99
|
+
WHERE id = ?
|
|
100
|
+
`).get(id);
|
|
101
|
+
if (!row)
|
|
102
|
+
return null;
|
|
103
|
+
return JSON.parse(row.osv_json);
|
|
104
|
+
}
|
|
105
|
+
findMatchingVulnerabilityIds(pkg) {
|
|
106
|
+
if (!looksLikeVersion(pkg.version)) {
|
|
107
|
+
return [];
|
|
108
|
+
}
|
|
109
|
+
const rows = this.db.prepare(`
|
|
110
|
+
SELECT advisory_id, introduced, fixed, last_affected
|
|
111
|
+
FROM advisory_packages
|
|
112
|
+
WHERE ecosystem = ? AND package_name = ?
|
|
113
|
+
`).all(pkg.ecosystem, pkg.name);
|
|
114
|
+
const ids = new Set();
|
|
115
|
+
for (const row of rows) {
|
|
116
|
+
if (versionMatchesRange(pkg.version, row)) {
|
|
117
|
+
ids.add(row.advisory_id);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return [...ids];
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
function versionMatchesRange(version, row) {
|
|
124
|
+
const introduced = row.introduced;
|
|
125
|
+
const fixed = row.fixed;
|
|
126
|
+
const lastAffected = row.last_affected;
|
|
127
|
+
if (introduced && introduced !== "0" && compareVersions(version, introduced) < 0) {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
if (fixed && compareVersions(version, fixed) >= 0) {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
if (lastAffected && compareVersions(version, lastAffected) > 0) {
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
function deriveAdvisoryPackageRows(vuln) {
|
|
139
|
+
const rows = [];
|
|
140
|
+
for (const affected of vuln.affected ?? []) {
|
|
141
|
+
const ecosystem = affected.package?.ecosystem;
|
|
142
|
+
const packageName = affected.package?.name;
|
|
143
|
+
if (!ecosystem || !packageName) {
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
for (const range of affected.ranges ?? []) {
|
|
147
|
+
const events = range.events ?? [];
|
|
148
|
+
let introduced = null;
|
|
149
|
+
for (const event of events) {
|
|
150
|
+
if (event.introduced) {
|
|
151
|
+
introduced = event.introduced;
|
|
152
|
+
}
|
|
153
|
+
if (event.fixed || event.last_affected) {
|
|
154
|
+
rows.push({
|
|
155
|
+
advisory_id: vuln.id,
|
|
156
|
+
ecosystem,
|
|
157
|
+
package_name: packageName,
|
|
158
|
+
introduced,
|
|
159
|
+
fixed: event.fixed ?? null,
|
|
160
|
+
last_affected: event.last_affected ?? null,
|
|
161
|
+
});
|
|
162
|
+
introduced = null;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
if (events.length === 0) {
|
|
166
|
+
rows.push({
|
|
167
|
+
advisory_id: vuln.id,
|
|
168
|
+
ecosystem,
|
|
169
|
+
package_name: packageName,
|
|
170
|
+
introduced: null,
|
|
171
|
+
fixed: null,
|
|
172
|
+
last_affected: null,
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
else if (introduced !== null) {
|
|
176
|
+
rows.push({
|
|
177
|
+
advisory_id: vuln.id,
|
|
178
|
+
ecosystem,
|
|
179
|
+
package_name: packageName,
|
|
180
|
+
introduced,
|
|
181
|
+
fixed: null,
|
|
182
|
+
last_affected: null,
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return rows;
|
|
188
|
+
}
|