@preventive/upstream 1.9.1 → 1.11.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
@@ -60,6 +60,11 @@ export interface Advisory {
60
60
  url?: string
61
61
  // Its other ids.
62
62
  aliases: string[]
63
+ // Those of other advisories OSV joins with a GHSA, its GitHub record telling
64
+ // them apart: another GHSA, and its CVE, as GHSA-37ch-88jc-xwx2 (of
65
+ // CVE-2026-4867 alone) has GHSA-9wv6-86v2-598j and its CVE-2024-45296.
66
+ // Each is an advisory of its own, which these versions may not be of.
67
+ related?: string[]
63
68
  title?: string
64
69
  // With `details`: its full text, in Markdown.
65
70
  details?: string
package/electron.d.ts ADDED
@@ -0,0 +1,29 @@
1
+ // The version is asserted to be an Electron one (`45.5.0`,
2
+ // `45.0.0-alpha.16`, `36.0.0-nightly.20250101`), the platform and arch
3
+ // ones Electron builds for, and the checksum 64 lowercase hex digits,
4
+ // before any request or cache path. The cache is setCacheDir's (npm.js).
5
+
6
+ import type { CacheOptions } from './npm.js'
7
+
8
+ export { HttpError } from './npm.js'
9
+
10
+ export type ElectronPlatform = 'darwin' | 'linux' | 'mas' | 'win32'
11
+ export type ElectronArch = 'arm64' | 'armv7l' | 'ia32' | 'x64'
12
+
13
+ // `electron-v<version>-<platform>-<arch>.zip`, whole, in memory, from the
14
+ // GitHub release `v<version>` of electron/electron (electron/nightlies
15
+ // for a version naming a nightly), through GitHub's redirect to its asset
16
+ // host. The release says nothing a lockfile holds of what the zip hashes
17
+ // to, so `checksum` is required: its sha256, as the `electron` package's
18
+ // checksums.json gives it. The bytes are checked against it, whether
19
+ // downloaded (before they are kept) or read from upstream's or stasis's
20
+ // default cache, setCacheDir's, or a `cache` store in its place, where one
21
+ // that does not match throws. A kept zip that matches is answered with no
22
+ // request at all.
23
+ //
24
+ // @electron/get's cache is read first, and never written: the zip under
25
+ // $electron_config_cache where install.js is told to keep it there, else
26
+ // under the platform's cache directory for electron (~/.cache/electron on
27
+ // Linux), in the directory named for the sha256 of the release's URL. A
28
+ // file there that does not match is passed over.
29
+ export function getElectronZip(version: string, platform: ElectronPlatform, arch: ElectronArch, checksum: string, options?: CacheOptions): Promise<Uint8Array>
package/electron.js ADDED
@@ -0,0 +1,2 @@
1
+ export { HttpError } from './src/http.js'
2
+ export { getElectronZip } from './src/electron.js'
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@preventive/upstream",
3
- "version": "1.9.1",
4
- "description": "Minimal npm, crates.io, RubyGems, Soldeer, CocoaPods trunk, GitHub and git checkout clients",
3
+ "version": "1.11.0",
4
+ "description": "Minimal npm, crates.io, RubyGems, Soldeer, CocoaPods trunk, GitHub, Electron release and git checkout clients",
5
5
  "type": "module",
6
6
  "exports": {
7
7
  "./advisories.js": {
@@ -16,6 +16,10 @@
16
16
  "types": "./cocoapods.d.ts",
17
17
  "default": "./cocoapods.js"
18
18
  },
19
+ "./electron.js": {
20
+ "types": "./electron.d.ts",
21
+ "default": "./electron.js"
22
+ },
19
23
  "./github.js": {
20
24
  "types": "./github.d.ts",
21
25
  "default": "./github.js"
@@ -62,6 +66,8 @@
62
66
  "cargo.js",
63
67
  "cocoapods.d.ts",
64
68
  "cocoapods.js",
69
+ "electron.d.ts",
70
+ "electron.js",
65
71
  "github.d.ts",
66
72
  "github.js",
67
73
  "git.d.ts",
@@ -97,6 +103,7 @@
97
103
  "src/cargo/repos.js",
98
104
  "src/cocoapods.js",
99
105
  "src/download.js",
106
+ "src/electron.js",
100
107
  "src/git.js",
101
108
  "src/github/client.js",
102
109
  "src/github/commit.js",
@@ -56,9 +56,11 @@ function joinRows(rows, asked) {
56
56
  const joined = {
57
57
  ...head,
58
58
  aliases: [...new Set(all.flatMap((row) => row.aliases))].filter((id) => id !== head.id),
59
+ related: [...new Set(all.flatMap((row) => row.related ?? []))].filter((id) => id !== head.id && !all.some((row) => row.aliases.includes(id))),
59
60
  cwe: [...new Set(all.flatMap((row) => row.cwe))],
60
61
  versions: asked.filter((version) => versions.has(version)),
61
62
  }
63
+ if (joined.related.length === 0) delete joined.related
62
64
  if (rest.some((row) => row.informational !== head.informational)) delete joined.informational
63
65
  for (const key of ['title', 'details']) {
64
66
  const from = joined[key] === undefined && rest.find((row) => row[key] !== undefined)
@@ -65,7 +65,7 @@ async function withDetails(rows) {
65
65
 
66
66
  // With `osv`, OSV's records too, malware's among them. A version a registry
67
67
  // row reports under the same advisory is left to that row, which takes the
68
- // record's ids as aliases.
68
+ // record's ids as aliases, and those it is related to (osv.js apart).
69
69
  function withOsv(registry, osv) {
70
70
  const rows = registry.map((row) => ({ ...row }))
71
71
  const added = []
@@ -74,6 +74,8 @@ function withOsv(registry, osv) {
74
74
  const same = rows.filter((row) => row.name === record.name && ids.includes(row.id) && row.versions.some((version) => record.versions.includes(version)))
75
75
  for (const row of same) {
76
76
  row.aliases = [...new Set([...row.aliases, ...ids])].filter((id) => id !== row.id)
77
+ const related = [...new Set([...row.related ?? [], ...record.related ?? []])].filter((id) => id !== row.id && !row.aliases.includes(id))
78
+ if (related.length > 0) row.related = related
77
79
  if (record.id === row.id && record.details !== undefined) row.details = record.details
78
80
  }
79
81
  const versions = record.versions.filter((version) => !same.some((row) => row.versions.includes(version)))
@@ -4,7 +4,7 @@ import { allows, normalize } from '@preventive/lockfile/composer-semver.js'
4
4
 
5
5
  import { assertCrateName, assertCrateVersion, assertGemName, assertGemVersion, assertion, isGhsa, isRepo, isStrings, matches, show } from '../args.js'
6
6
  import { readJSON, writeJSON } from '../cache.js'
7
- import { OSV_API, OSV_EXPORTS, buildUrl, request } from '../http.js'
7
+ import { GITHUB_RAW, OSV_API, OSV_EXPORTS, buildUrl, isNotFound, recover, request } from '../http.js'
8
8
  import { chunks, pool } from '../pool.js'
9
9
  import { assertPypiName, assertPypiVersion, compareVersions as comparePypiVersions, isVersion as isPypiVersion, normalName, readRange as readPypiRange } from '../pypi/version.js'
10
10
  import { compareVersions as compareGemVersions, isVersion as isGemVersion, readRange as readGemRange } from '../rubygems/version.js'
@@ -28,6 +28,12 @@ const INFORMATIONAL = new Set(['unmaintained', 'unsound', 'notice'])
28
28
  const rank = (id) => (id.startsWith('RUSTSEC-') ? 0 : (isGhsa(id) ? 1 : 2))
29
29
  const first = (a, b) => rank(a.id) - rank(b.id) || order(a.id, b.id)
30
30
  const named = (a, b) => a.aliases.includes(b.id) || b.aliases.includes(a.id)
31
+ const isCve = matches(/^CVE-\d{4}-\d{4,}$/u)
32
+ // GitHub's advisory database, its records by whether GitHub reviewed them,
33
+ // then the year and month each was published:
34
+ // advisories/github-reviewed/2026/03/GHSA-37ch-88jc-xwx2/GHSA-37ch-88jc-xwx2.json.
35
+ const GITHUB_ADVISORIES = ['github', 'advisory-database', 'main', 'advisories']
36
+ const PUBLISHED = /^(?<year>\d{4})-(?<month>\d{2})-\d{2}T/u
31
37
 
32
38
  export const CARGO = {
33
39
  osv: 'crates.io',
@@ -182,6 +188,41 @@ export async function getVuln(id) {
182
188
  return { ...record, aliases: (record.aliases ?? []).filter(isOsvId) }
183
189
  }
184
190
 
191
+ // The ids GitHub's own record of a GHSA names, as its advisory database
192
+ // keeps it, where OSV's record of it says where that is (`published`,
193
+ // `database_specific.github_reviewed`): kept by the record's `modified`;
194
+ // undefined where GitHub has no such file.
195
+ async function githubAliases(record, cache) {
196
+ const published = PUBLISHED.exec(record.published ?? '')?.groups
197
+ if (!published || typeof record.modified !== 'string') return undefined
198
+ const kept = await readJSON('osv/github-aliases', record.id, cache)
199
+ if (kept?.modified === record.modified && isStrings(kept.aliases)) return kept.aliases
200
+ const reviewed = record.database_specific?.github_reviewed === true ? 'github-reviewed' : 'unreviewed'
201
+ const url = buildUrl(GITHUB_RAW, [...GITHUB_ADVISORIES, reviewed, published.year, published.month, record.id, `${record.id}.json`])
202
+ const github = await request(url, { as: 'json' }).catch(recover(isNotFound, undefined))
203
+ if (github === undefined) return undefined
204
+ assert.ok(github?.id === record.id && (github.aliases === undefined || isStrings(github.aliases)), `advisories: malformed GitHub record of ${record.id}`)
205
+ const aliases = github.aliases ?? []
206
+ await writeJSON('osv/github-aliases', record.id, { modified: record.modified, aliases }, cache)
207
+ return aliases
208
+ }
209
+
210
+ // A GHSA's record as OSV tells it, its ids those of every record OSV joins
211
+ // it with: another GHSA's among them, and that one's CVE, as
212
+ // GHSA-37ch-88jc-xwx2 (CVE-2026-4867 on GitHub) has GHSA-9wv6-86v2-598j's
213
+ // CVE-2024-45296. Where it names another GHSA, the GHSAs and CVEs GitHub's
214
+ // own record of it doesn't name are those it is `related` to, not its
215
+ // aliases; any other id (MAL-, PYSEC-...) stays one. As OSV tells it where
216
+ // GitHub has no record of it.
217
+ async function apart(record, cache) {
218
+ if (!isGhsa(record.id) || !record.aliases.some((id) => isGhsa(id))) return record
219
+ const own = await githubAliases(record, cache)
220
+ if (own === undefined) return record
221
+ const isRelated = (id) => (isGhsa(id) || isCve(id)) && !own.includes(id)
222
+ const related = record.aliases.filter(isRelated)
223
+ return related.length > 0 ? { ...record, aliases: record.aliases.filter((id) => !isRelated(id)), related } : record
224
+ }
225
+
185
226
  // Whether a version is `>=`, `<` or `<=` a bound, as an ecosystem orders
186
227
  // them; undefined where it can't read either.
187
228
  const MEETS = { '>=': (cmp) => cmp >= 0, '<': (cmp) => cmp < 0, '<=': (cmp) => cmp <= 0 }
@@ -271,6 +312,7 @@ export function toAdvisory(ecosystem, name, versions, record, details) {
271
312
  ...(ghsa && { ghsa }),
272
313
  url: ghsa ? advisoryUrl(ghsa) : osvPage(record.id),
273
314
  aliases,
315
+ ...(record.related && { related: record.related }),
274
316
  ...(record.summary && { title: record.summary }),
275
317
  ...(details && detailsOf(record.details, record.id)),
276
318
  ...metrics({ severity: record.database_specific?.severity, vector, cwe: record.database_specific?.cwe_ids }),
@@ -280,10 +322,11 @@ export function toAdvisory(ecosystem, name, versions, record, details) {
280
322
  }
281
323
  }
282
324
 
283
- // OSV's batch query answers ids only, each record fetched after. Records
284
- // of one advisory, naming each other as aliases, are one group: each
285
- // version is reported under its first, the rest its aliases.
286
- export async function osvRows({ osv }, asked, { details }) {
325
+ // OSV's batch query answers ids only, each record fetched after, a GHSA's
326
+ // told apart from those OSV joins it with (apart). Records of one advisory,
327
+ // naming each other as aliases, are one group: each version is reported
328
+ // under its first, the rest its aliases.
329
+ export async function osvRows({ osv }, asked, { details, cache }) {
287
330
  const list = [...asked].flatMap(([name, versions]) => versions.map((version) => ({ name, version })))
288
331
  const hits = new Map() // id -> name -> versions, in `list` order
289
332
  const found = await idsOf(list.map(({ name, version }) => ({ package: { name, ecosystem: osv }, version })), (j) => `${list[j].name}@${list[j].version}`)
@@ -294,7 +337,8 @@ export async function osvRows({ osv }, asked, { details }) {
294
337
  byName.set(name, (byName.get(name) ?? new Set()).add(version))
295
338
  }
296
339
  }
297
- const records = (await pool([...hits.keys()], RECORDS_AT_ONCE, getVuln)).filter((record) => !record.withdrawn)
340
+ const fetched = (await pool([...hits.keys()], RECORDS_AT_ONCE, getVuln)).filter((record) => !record.withdrawn)
341
+ const records = await pool(fetched, RECORDS_AT_ONCE, (record) => apart(record, cache))
298
342
  const groups = new Map()
299
343
  for (const record of records) {
300
344
  if (groups.has(record)) continue
package/src/cache.js CHANGED
@@ -10,7 +10,7 @@ import { assertCacheName, assertDirectoryPath, assertion, isRepo } from './args.
10
10
  import { MAX_BYTES, decode } from './http.js'
11
11
  import { pool } from './pool.js'
12
12
 
13
- const DIRS = new Set(['npm/repos', 'npm/public', 'npm/tarballs', 'npm/versions', 'cargo/repos', 'cargo/crates', 'cargo/index', 'composer/repos', 'soldeer/repos', 'soldeer/zips', 'rubygems/repos', 'rubygems/gems', 'pypi/repos', 'cocoapods/podspecs', 'cocoapods/archives', 'cocoapods/repos', 'github/trees', 'github/advisories', 'github/public', 'osv/names', 'rubysec/index', 'rubysec/advisories'])
13
+ const DIRS = new Set(['npm/repos', 'npm/public', 'npm/tarballs', 'npm/versions', 'cargo/repos', 'cargo/crates', 'cargo/index', 'composer/repos', 'soldeer/repos', 'soldeer/zips', 'electron/zips', 'rubygems/repos', 'rubygems/gems', 'pypi/repos', 'cocoapods/podspecs', 'cocoapods/archives', 'cocoapods/repos', 'github/trees', 'github/advisories', 'github/public', 'osv/names', 'osv/github-aliases', 'rubysec/index', 'rubysec/advisories'])
14
14
  const RECORD_TTL_MS = 30 * 24 * 60 * 60 * 1000 // A link only moves on a transfer or rename, and GitHub redirects those.
15
15
  const RECORDS_AT_ONCE = 8
16
16
  // JSON filed gzipped, as `<name>.json.gz`: a listing carries each
@@ -0,0 +1,42 @@
1
+ import { createHash } from 'node:crypto'
2
+ import { join, posix } from 'node:path'
3
+
4
+ import { assertArgs, assertSha256, assertion, matches, optional } from './args.js'
5
+ import { absolute, assertCache, cacheDirFor } from './cache.js'
6
+ import { verifiedDownload } from './download.js'
7
+ import { GITHUB_WEB, buildUrl } from './http.js'
8
+
9
+ const DIR = 'electron/zips' // No expiry: a zip is held to its checksum.
10
+
11
+ // As @electron/get names the builds Electron publishes.
12
+ const PLATFORMS = new Set(['darwin', 'linux', 'mas', 'win32'])
13
+ const ARCHES = new Set(['arm64', 'armv7l', 'ia32', 'x64'])
14
+ // No build metadata: the version is a URL segment and a file name.
15
+ const assertElectronVersion = assertion('an Electron version', matches(/^(?=.{1,128}$)\d+\.\d+\.\d+(?:-[\dA-Za-z][\dA-Za-z.-]*)?$/u))
16
+ const assertOneOf = (set, must) => assertion(must, (value) => set.has(value))
17
+ const assertPlatform = assertOneOf(PLATFORMS, `one of ${[...PLATFORMS].join(', ')}`)
18
+ const assertArch = assertOneOf(ARCHES, `one of ${[...ARCHES].join(', ')}`)
19
+
20
+ // Where @electron/get keeps `url`: <root>/<sha256 of its directory's URL>/<file>,
21
+ // the root install.js's electron_config_cache, else the platform's cache for electron.
22
+ function electronGetPaths(url) {
23
+ const parsed = new URL(url)
24
+ parsed.pathname = posix.dirname(parsed.pathname)
25
+ const dir = createHash('sha256').update(parsed.toString()).digest('hex')
26
+ return [absolute(process.env.electron_config_cache), cacheDirFor('electron')].filter(Boolean).map((root) => join(root, dir, posix.basename(url)))
27
+ }
28
+
29
+ // As @electron/get downloads it for install.js. The redirect to GitHub's asset
30
+ // host is followed: the bytes are held to `checksum` wherever they come from.
31
+ export async function getElectronZip(version, platform, arch, checksum, options = {}) {
32
+ const method = 'getElectronZip'
33
+ assertElectronVersion(method, 'version', version)
34
+ assertPlatform(method, 'platform', platform)
35
+ assertArch(method, 'arch', arch)
36
+ assertSha256(method, 'checksum', checksum)
37
+ assertArgs(method, options, { cache: optional(assertCache) })
38
+ const name = `electron-v${version}-${platform}-${arch}`
39
+ const repo = version.includes('nightly') ? 'nightlies' : 'electron'
40
+ const url = buildUrl(GITHUB_WEB, ['electron', repo, 'releases', 'download', `v${version}`, `${name}.zip`])
41
+ return await verifiedDownload({ method, dir: DIR, what: `${name}.zip`, entry: name, ext: 'zip', algorithm: 'sha256', expected: checksum, local: electronGetPaths(url), ours: true, cache: options.cache, locate: () => url, options: { redirect: 'follow' } })
42
+ }
package/src/http.js CHANGED
@@ -22,10 +22,12 @@ export const RUBYGEMS = 'https://rubygems.org'
22
22
  export const RUBYGEMS_INDEX = 'https://index.rubygems.org'
23
23
  export const PYPI = 'https://pypi.org'
24
24
  export const GITHUB_WEB = 'https://github.com'
25
+ // GitHub's repositories' files as committed: its advisory database's records.
26
+ export const GITHUB_RAW = 'https://raw.githubusercontent.com'
25
27
  export const MAVEN_CENTRAL = 'https://repo1.maven.org'
26
28
  export const MAX_BYTES = 512 * 1024 * 1024
27
29
 
28
- const ORIGINS = new Set([NPM_REGISTRY, GITHUB_API, OSV_API, OSV_EXPORTS, CRATES_API, PACKAGIST_REPO, PACKAGIST, CRATES_INDEX, CRATES_STATIC, SOLDEER_API, SOLDEER_REVISIONS, COCOAPODS_CDN, RUBYGEMS, RUBYGEMS_INDEX, PYPI, GITHUB_WEB, MAVEN_CENTRAL])
30
+ const ORIGINS = new Set([NPM_REGISTRY, GITHUB_API, OSV_API, OSV_EXPORTS, CRATES_API, PACKAGIST_REPO, PACKAGIST, CRATES_INDEX, CRATES_STATIC, SOLDEER_API, SOLDEER_REVISIONS, COCOAPODS_CDN, RUBYGEMS, RUBYGEMS_INDEX, PYPI, GITHUB_WEB, GITHUB_RAW, MAVEN_CENTRAL])
29
31
  const isSegment = matches(/^(?!\.\.?$)(?:[\w.~@-]|%[\dA-F]{2})+$/u)
30
32
  const isQueryKey = matches(/^[a-z_]+(?:\[\])?$/u)
31
33
  const isQueryValue = (value) => (typeof value === 'string' && value !== '') || (Number.isSafeInteger(value) && value >= 0)