@preventive/upstream 1.7.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/cocoapods.d.ts CHANGED
@@ -33,18 +33,29 @@ export { HttpError } from './npm.js'
33
33
  // getPodArchive's.
34
34
  export function getPodspec(name: string, version: string, checksum: string): Promise<Uint8Array>
35
35
 
36
- // An archive a podspec's `source` names by :http, from GitHub alone, whole,
37
- // in memory: `url` is https://github.com/<owner>/<repo>/ and a path under
38
- // it, such as GitHub's archive of a tag, `archive/refs/tags/<tag>.tar.gz`,
39
- // or a release's asset, `releases/download/<tag>/<file>`, as written, with
40
- // no query, fragment, credentials or port, and each segment of the path
41
- // letters, digits, `_`, `.`, `~`, `@`, `-` or a %-escape in capitals. The
42
- // hash is the podspec's :sha256, in lowercase hex, as cocoapods-downloader
43
- // checks it; a sha1, which two archives made to collide share, is not
44
- // taken. GitHub answers with a
36
+ // Whether `url` is an archive getPodArchive fetches, as a podspec's
37
+ // `source` names one by :http, from GitHub or Meta's namespace on Maven
38
+ // Central alone: https://github.com/<owner>/<repo>/ and a path under it,
39
+ // such as GitHub's archive of a tag, `archive/refs/tags/<tag>.tar.gz`, or a
40
+ // release's asset, `releases/download/<tag>/<file>`; or
41
+ // https://repo1.maven.org/maven2/com/facebook/ and a path under it ending
42
+ // in `.tar.gz` or `.zip`, such as React Native's
43
+ // `react/react-native-artifacts/<version>/<file>.tar.gz`. Either exactly as
44
+ // the URL parser writes it back out, `new URL(url).toString() === url`, so
45
+ // that no `..`, `%2e%2e` or case takes it out from under the prefix it is
46
+ // written with; with no query, fragment (an empty `?` or `#` among them),
47
+ // credentials or port; and each segment of the path letters, digits, `_`,
48
+ // `.`, `~`, `@`, `-` or a %-escape in capitals.
49
+ export function isPodArchiveUrl(url: unknown): boolean
50
+
51
+ // An archive a podspec's `source` names by :http, whole, in memory: `url`
52
+ // is one isPodArchiveUrl takes. The hash is the podspec's :sha256, in
53
+ // lowercase hex, as cocoapods-downloader checks it; a sha1, which two
54
+ // archives made to collide share, is not taken. GitHub answers with a
45
55
  // redirect, to codeload.github.com or to where it keeps a release's files,
46
- // which is followed, as the bytes are held to the hash whoever answers,
47
- // downloaded (before they are cached) or read from setCacheDir's cache. They
48
- // are cached by the hash alone; one in the cache that does not match throws.
49
- // Each argument is asserted before any request.
56
+ // and Maven Central with one to repo.reactnative.dev for React Native's
57
+ // archives, which is followed, as the bytes are held to the hash whoever
58
+ // answers, downloaded (before they are cached) or read from setCacheDir's
59
+ // cache. They are cached by the hash alone; one in the cache that does not
60
+ // match throws. Each argument is asserted before any request.
50
61
  export function getPodArchive(url: string, sha256: string): Promise<Uint8Array>
package/cocoapods.js CHANGED
@@ -1,2 +1,2 @@
1
- export { getPodArchive, getPodspec } from './src/cocoapods.js'
1
+ export { getPodArchive, getPodspec, isPodArchiveUrl } from './src/cocoapods.js'
2
2
  export { HttpError } from './src/http.js'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@preventive/upstream",
3
- "version": "1.7.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
  }
package/src/cocoapods.js CHANGED
@@ -5,7 +5,7 @@ import { join, resolve } from 'node:path'
5
5
 
6
6
  import { assertPod, assertSha1, assertSha256, assertion, isRepo } from './args.js'
7
7
  import { verifiedDownload } from './download.js'
8
- import { COCOAPODS_CDN, GITHUB_WEB, buildUrl, encodeSegment } from './http.js'
8
+ import { COCOAPODS_CDN, GITHUB_WEB, MAVEN_CENTRAL, buildUrl, encodeSegment } from './http.js'
9
9
 
10
10
  // No expiry, but kept by checksum as well: trunk refuses a version it
11
11
  // already has, yet rewrites a podspec it has, as `pod trunk deprecate`
@@ -39,29 +39,39 @@ async function localPaths(name, version) {
39
39
  // Where pod archives are kept: by their hash alone, which names them.
40
40
  const ARCHIVES = 'cocoapods/archives'
41
41
 
42
- // https://github.com/<owner>/<repo>/ and a path under it, as buildUrl
43
- // would write it: no query, fragment, credentials or port, and nothing in
44
- // a segment but what it lets through.
45
- function isGitHubUrl(value) {
42
+ // Meta's namespace on Maven Central, which React Native's archives are
43
+ // published under, and the two kinds of archive taken from it.
44
+ const MAVEN_FACEBOOK = `${MAVEN_CENTRAL}/maven2/com/facebook/`
45
+ const MAVEN_ARCHIVE = /\.(?:tar\.gz|zip)$/u
46
+
47
+ // As the URL parser writes it back out, so that what its text is checked
48
+ // for, a prefix above all, holds of what is fetched; and as buildUrl writes
49
+ // it, with no query, fragment, credentials or port, and nothing in a
50
+ // segment but what it lets through.
51
+ export function isPodArchiveUrl(value) {
46
52
  const parsed = typeof value === 'string' ? URL.parse(value) : null
47
- if (parsed?.origin !== GITHUB_WEB) return false
53
+ if (parsed === null || parsed.toString() !== value) return false
48
54
  const segments = parsed.pathname.slice(1).split('/')
49
- if (segments.length < 3 || !isRepo(`${segments[0]}/${segments[1]}`)) return false
55
+ const github = parsed.origin === GITHUB_WEB && segments.length >= 3 && isRepo(`${segments[0]}/${segments[1]}`)
56
+ const maven = value.startsWith(MAVEN_FACEBOOK) && MAVEN_ARCHIVE.test(value)
57
+ if (!github && !maven) return false
50
58
  try {
51
- return buildUrl(GITHUB_WEB, segments) === value
59
+ return buildUrl(parsed.origin, segments) === value
52
60
  } catch {
53
61
  return false
54
62
  }
55
63
  }
56
- const assertGitHubUrl = assertion('an https URL of a GitHub repository\'s, with a path under it', isGitHubUrl)
64
+ const assertPodArchiveUrl = assertion('an https URL of a GitHub repository\'s, with a path under it, or of a .tar.gz or a .zip under Maven Central\'s com/facebook/', isPodArchiveUrl)
57
65
 
58
66
  // An archive a podspec's source names by :http, held to the sha256 the
59
67
  // podspec gives of it: a sha1 alone, which two archives made to collide
60
68
  // share, is not taken. GitHub's answer for a tag's archive or a release's
61
69
  // asset is a redirect, to codeload.github.com or to where it keeps the
62
- // files, which is followed: the bytes are held to the hash whoever answers.
70
+ // files, and Maven Central's for React Native's is one to
71
+ // repo.reactnative.dev, which is followed: the bytes are held to the hash
72
+ // whoever answers.
63
73
  export async function getPodArchive(url, sha256) {
64
- assertGitHubUrl('getPodArchive', 'url', url)
74
+ assertPodArchiveUrl('getPodArchive', 'url', url)
65
75
  assertSha256('getPodArchive', 'sha256', sha256)
66
76
  return await verifiedDownload({
67
77
  method: 'getPodArchive', dir: ARCHIVES, what: url, entry: `sha256-${sha256}`, ext: 'archive', algorithm: 'sha256', expected: sha256,
package/src/http.js CHANGED
@@ -22,9 +22,10 @@ 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
+ export const MAVEN_CENTRAL = 'https://repo1.maven.org'
25
26
  export const MAX_BYTES = 512 * 1024 * 1024
26
27
 
27
- 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])
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])
28
29
  const isSegment = matches(/^(?!\.\.?$)(?:[\w.~@-]|%[\dA-F]{2})+$/u)
29
30
  const isQueryKey = matches(/^[a-z_]+(?:\[\])?$/u)
30
31
  const isQueryValue = (value) => (typeof value === 'string' && value !== '') || (Number.isSafeInteger(value) && value >= 0)