@preventive/upstream 1.8.0 → 1.9.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/advisories.d.ts CHANGED
@@ -63,7 +63,9 @@ export interface Advisory {
63
63
  cvss?: number
64
64
  cvssVector?: string
65
65
  cwe: string[]
66
- // The affected range as its source wrote it, where it has one.
66
+ // The affected range as its source wrote it, where it has one. OSV's as
67
+ // GitHub writes one, `>= 9.5.0, < 15.5.13 || >= 16.0.0-beta.0, < 16.1.7`:
68
+ // the ranges holding the versions it reports, else all of them.
67
69
  range?: string
68
70
  // RustSec's `unmaintained`, `unsound` or `notice`: not a vulnerability as
69
71
  // such. A yanked crate isn't an advisory: see cargo.js's yankedVersions.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@preventive/upstream",
3
- "version": "1.8.0",
3
+ "version": "1.9.0",
4
4
  "description": "Minimal npm, crates.io, RubyGems, Soldeer, CocoaPods trunk, GitHub and git checkout clients",
5
5
  "type": "module",
6
6
  "exports": {
@@ -1,12 +1,14 @@
1
1
  import assert from 'node:assert/strict'
2
2
 
3
+ import { allows, normalize } from '@preventive/lockfile/composer-semver.js'
4
+
3
5
  import { assertCrateName, assertCrateVersion, assertGemName, assertGemVersion, assertion, isGhsa, isRepo, isStrings, matches, show } from '../args.js'
4
6
  import { readJSON, writeJSON } from '../cache.js'
5
7
  import { OSV_API, OSV_EXPORTS, buildUrl, request } from '../http.js'
6
8
  import { chunks, pool } from '../pool.js'
7
- import { assertPypiName, assertPypiVersion, boundRange as boundPypiRange, compareVersions as comparePypiVersions, normalName, readRange as readPypiRange } from '../pypi/version.js'
8
- import { boundRange as boundGemRange, compareVersions as compareGemVersions, readRange as readGemRange } from '../rubygems/version.js'
9
- import { isExactVersion } from '../semver.js'
9
+ import { assertPypiName, assertPypiVersion, boundRange as boundPypiRange, compareVersions as comparePypiVersions, isVersion as isPypiVersion, normalName, readRange as readPypiRange } from '../pypi/version.js'
10
+ import { boundRange as boundGemRange, compareVersions as compareGemVersions, isVersion as isGemVersion, readRange as readGemRange } from '../rubygems/version.js'
11
+ import { compareVersions as compareSemver, isExactVersion } from '../semver.js'
10
12
  import { advisoryUrl, detailsOf, inRange, isText, metrics, order } from './common.js'
11
13
  import { bySemver, withRepositories } from './github.js'
12
14
  import { packagistRows } from './packagist.js'
@@ -179,13 +181,88 @@ export async function getVuln(id) {
179
181
  return { ...record, aliases: (record.aliases ?? []).filter(isOsvId) }
180
182
  }
181
183
 
184
+ // Whether a version is `>=`, `<` or `<=` a bound, as an ecosystem orders
185
+ // them; undefined where it can't read either.
186
+ const MEETS = { '>=': (cmp) => cmp >= 0, '<': (cmp) => cmp < 0, '<=': (cmp) => cmp <= 0 }
187
+ const meetsBy = (isVersion, compare) => (version, operator, bound) => (isVersion(version) && isVersion(bound) ? MEETS[operator](compare(version, bound)) : undefined)
188
+ const semverMeets = meetsBy(isExactVersion, compareSemver)
189
+ // Composer's own order, as `version_compare` has it, of what it normalizes.
190
+ function composerMeets(version, operator, bound) {
191
+ const [normal, against] = [normalize(version), normalize(bound)]
192
+ return normal === undefined || against === undefined ? undefined : allows({ operator, version: against }, normal)
193
+ }
194
+ // OSV's ECOSYSTEM ranges, by ecosystem; its SEMVER ones are semver's.
195
+ const ORDERS = {
196
+ __proto__: null,
197
+ npm: semverMeets,
198
+ 'crates.io': semverMeets,
199
+ SwiftURL: semverMeets,
200
+ PyPI: meetsBy(isPypiVersion, comparePypiVersions),
201
+ RubyGems: meetsBy(isGemVersion, compareGemVersions),
202
+ Packagist: composerMeets,
203
+ }
204
+
205
+ // A bound as OSV writes one, nothing in it to garble a range.
206
+ const isBound = (text) => typeof text === 'string' && /^[^\s,|]{1,256}$/u.test(text)
207
+
208
+ // A SEMVER or ECOSYSTEM range's intervals, event by event: from the first
209
+ // `introduced` to the next `fixed` (out) or `last_affected` (in), else on.
210
+ // A GIT range is of commits, not versions.
211
+ function intervalsIn(range) {
212
+ if (!['SEMVER', 'ECOSYSTEM'].includes(range?.type) || !Array.isArray(range.events)) return []
213
+ const intervals = []
214
+ let from
215
+ for (const event of range.events) {
216
+ if (from === undefined) {
217
+ if (isBound(event?.introduced)) from = event.introduced
218
+ continue
219
+ }
220
+ const end = isBound(event?.fixed) ? ['<', event.fixed] : (isBound(event?.last_affected) ? ['<=', event.last_affected] : undefined)
221
+ if (end === undefined) continue
222
+ intervals.push({ type: range.type, from, end })
223
+ from = undefined
224
+ }
225
+ if (from !== undefined) intervals.push({ type: range.type, from })
226
+ return intervals
227
+ }
228
+
229
+ const intervalsOf = (entries) => entries.flatMap((entry) => (Array.isArray(entry.ranges) ? entry.ranges.flatMap(intervalsIn) : []))
230
+
231
+ // As GitHub writes a range, `>= 1.0.0, < 1.2.6`; `0` is the first version.
232
+ const written = ({ from, end }) => (end === undefined ? `>= ${from}` : [...(from === '0' ? [] : [`>= ${from}`]), end.join(' ')].join(', '))
233
+
234
+ // Whether an interval may hold a version: it does where its order can't
235
+ // tell.
236
+ function mayHold(ecosystem, version, { type, from, end }) {
237
+ const meets = type === 'SEMVER' ? semverMeets : ORDERS[ecosystem]
238
+ if (meets === undefined) return true
239
+ return (from === '0' || meets(version, '>=', from) !== false) && (end === undefined || meets(version, ...end) !== false)
240
+ }
241
+
242
+ // OSV's ranges of a package, those holding one of the versions it reports,
243
+ // else all of them.
244
+ function rangeOf(ecosystem, entries, versions) {
245
+ const intervals = intervalsOf(entries)
246
+ const holding = intervals.filter((interval) => versions.some((version) => mayHold(ecosystem, version, interval)))
247
+ const ranges = [...new Set((holding.length > 0 ? holding : intervals).map(written))]
248
+ return ranges.length > 0 ? { range: ranges.join(' || ') } : {}
249
+ }
250
+
251
+ // An entry names a package as OSV spells it; a Python one in any spelling
252
+ // PEP 503 normalizes alike, as it was asked.
253
+ const NAME_KEYS = { __proto__: null, PyPI: normalName }
254
+ const names = (ecosystem, name) => {
255
+ const key = NAME_KEYS[ecosystem] ?? ((each) => each)
256
+ return (entry) => entry?.package?.ecosystem === ecosystem && typeof entry.package.name === 'string' && key(entry.package.name) === key(name)
257
+ }
258
+
182
259
  export function toAdvisory(ecosystem, name, versions, record, details) {
183
260
  const { aliases } = record
184
261
  const ghsas = aliases.filter(isGhsa)
185
262
  const ghsa = isGhsa(record.id) ? record.id : (ghsas.length === 1 ? ghsas[0] : undefined)
186
263
  const vector = record.severity?.find?.((entry) => /^CVSS_V[34]$/u.test(entry?.type))?.score
187
- const affected = record.affected?.find?.((entry) => entry?.package?.ecosystem === ecosystem && entry.package.name === name)
188
- const informational = affected?.database_specific?.informational
264
+ const entries = Array.isArray(record.affected) ? record.affected.filter(names(ecosystem, name)) : []
265
+ const informational = entries[0]?.database_specific?.informational
189
266
  return {
190
267
  name,
191
268
  source: 'osv',
@@ -196,6 +273,7 @@ export function toAdvisory(ecosystem, name, versions, record, details) {
196
273
  ...(record.summary && { title: record.summary }),
197
274
  ...(details && detailsOf(record.details, record.id)),
198
275
  ...metrics({ severity: record.database_specific?.severity, vector, cwe: record.database_specific?.cwe_ids }),
276
+ ...rangeOf(ecosystem, entries, versions),
199
277
  ...(INFORMATIONAL.has(informational) && { informational }),
200
278
  versions,
201
279
  }