@preventive/upstream 1.6.0 → 1.8.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
@@ -19,6 +19,10 @@ export interface Package {
19
19
  // Bundler writes it (`3.3.6p108`). A `github` package takes a tag (`v4` its
20
20
  // whole line), a branch or a commit; an unversioned Swift pin is refused.
21
21
  versions: string[]
22
+ // A `github` package: true to ask its repository alone, its name sent to
23
+ // no other source (OSV), as for one not everyone may read. Any spelling
24
+ // saying so holds for all. Not for other ecosystems.
25
+ internal?: boolean
22
26
  }
23
27
 
24
28
  export interface AdvisoryOptions {
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.6.0",
3
+ "version": "1.8.0",
4
4
  "description": "Minimal npm, crates.io, RubyGems, Soldeer, CocoaPods trunk, GitHub and git checkout clients",
5
5
  "type": "module",
6
6
  "exports": {
@@ -62,14 +62,19 @@ async function actionRows(asked, details) {
62
62
  }
63
63
 
64
64
  // GitHub repositories as dependencies: OSV's records of them as Actions,
65
- // and every range their own advisories list, whichever package it names.
65
+ // but of those `internal`, and every range their own advisories list,
66
+ // whichever package it names.
66
67
  export const GITHUB = {
67
68
  repositoryOnly: true,
68
69
  assertName: assertRepo,
69
70
  nameKey: (repo) => repo.toLowerCase(),
70
71
  assertVersion: assertion('a version or a branch name', isRefName),
71
- advisories: async (asked, { github, details, cache }) => (await Promise.all([
72
- actionRows(asked, details),
73
- repositoryAdvisories(github, asked, { repoOf: (repo) => repo, versionsIn: bySemver(coversPlaceholder), details, cache }),
74
- ])).flat(),
72
+ advisories: async (asked, { github, details, cache, internal }) => {
73
+ const external = new Map(asked)
74
+ for (const name of internal) external.delete(name)
75
+ return (await Promise.all([
76
+ actionRows(external, details),
77
+ repositoryAdvisories(github, asked, { repoOf: (repo) => repo, versionsIn: bySemver(coversPlaceholder), details, cache }),
78
+ ])).flat()
79
+ },
75
80
  }
@@ -14,17 +14,19 @@ const byNumbers = new Intl.Collator('en', { numeric: true }).compare
14
14
  const assertEcosystem = assertion(`one of ${Object.keys(ECOSYSTEMS).join(', ')}`, (value) => typeof value === 'string' && Object.hasOwn(ECOSYSTEMS, value))
15
15
  const assertVersions = assertion('a non-empty array', (value) => Array.isArray(value) && value.length > 0)
16
16
 
17
- // Ecosystem -> name -> { versions, github? }, every package checked first.
17
+ // Ecosystem -> name -> { versions, github?, internal? }, every package
18
+ // checked first. A name is internal where any of its spellings is.
18
19
  function collect(packages) {
19
20
  assert.ok(isIterable(packages), 'advisories: packages must be an iterable of { ecosystem, name, github?, versions }')
20
21
  const byEcosystem = new Map()
21
22
  const spelled = new Map()
22
23
  for (const pkg of packages) {
23
- assertArgs('advisories', pkg, { ecosystem: assertEcosystem, name: null, github: optional(assertRepo), versions: assertVersions }, 'package')
24
+ assertArgs('advisories', pkg, { ecosystem: assertEcosystem, name: null, github: optional(assertRepo), versions: assertVersions, internal: optional(assertBoolean) }, 'package')
24
25
  const { assertName, assertVersion, nameKey } = ECOSYSTEMS[pkg.ecosystem]
25
26
  assertName('advisories', 'package.name', pkg.name)
26
27
  for (const version of pkg.versions) assertVersion('advisories', 'package.versions', version)
27
28
  assert.ok(pkg.ecosystem !== 'github' || pkg.github === undefined, 'advisories: a github package is its own repository, and takes no package.github')
29
+ assert.ok(pkg.ecosystem === 'github' || pkg.internal === undefined, 'advisories: only a github package takes package.internal')
28
30
  const named = byEcosystem.get(pkg.ecosystem) ?? byEcosystem.set(pkg.ecosystem, new Map()).get(pkg.ecosystem)
29
31
  // A repository's name in any case, a Python package's as PEP 503
30
32
  // normalizes it: the first spelling stands.
@@ -33,6 +35,7 @@ function collect(packages) {
33
35
  const entry = named.get(name) ?? named.set(name, { versions: new Set() }).get(name)
34
36
  assert.ok(pkg.github === undefined || entry.github === undefined || sameName(entry.github, pkg.github), `advisories: ${pkg.name} is given two repositories`)
35
37
  entry.github ??= pkg.github
38
+ entry.internal ||= pkg.internal === true
36
39
  for (const version of pkg.versions) entry.versions.add(version)
37
40
  }
38
41
  return byEcosystem
@@ -90,7 +93,8 @@ export async function advisories(packages, options = {}) {
90
93
  const names = [...named.keys()].toSorted()
91
94
  const asked = new Map(names.map((name) => [name, [...named.get(name).versions].toSorted(compare)]))
92
95
  const known = new Map(names.filter((name) => named.get(name).github).map((name) => [name, named.get(name).github]))
93
- const rows = await find(asked, { github: options.github, repoAdvisories: options.repoAdvisories === true, details: options.details === true, osv: options.osv === true, known, cache: options.cache })
96
+ const internal = new Set(names.filter((name) => named.get(name).internal))
97
+ const rows = await find(asked, { github: options.github, repoAdvisories: options.repoAdvisories === true, details: options.details === true, osv: options.osv === true, known, internal, cache: options.cache })
94
98
  return oneRowPerAdvisory(rows.filter((row) => row.versions.length > 0), asked).map((row) => ({ ecosystem, ...row, ...(isMalware(row) && { malware: true }) }))
95
99
  }))
96
100
  return found.flat().toSorted((a, b) => order(a.ecosystem, b.ecosystem) || order(a.name, b.name))
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)