@preventive/triage 1.0.0-alpha.53 → 1.0.0-alpha.55

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.
Files changed (114) hide show
  1. package/common/bundle-compare-diff.js +28 -9
  2. package/common/bundle-compare-inputs.d.ts +1 -0
  3. package/common/bundle-compare-inputs.js +91 -15
  4. package/common/bundle-comparison.js +14 -7
  5. package/common/default-scan-models.ts +3 -0
  6. package/common/dependency-reviews.d.ts +1 -1
  7. package/common/dependency-reviews.js +1 -1
  8. package/common/managed/finding-detail.d.ts +1 -1
  9. package/common/managed/finding-detail.js +7 -7
  10. package/common/managed/routes.js +6 -2
  11. package/common/managed/team-app.d.ts +7 -0
  12. package/common/managed/team-app.js +31 -7
  13. package/common/managed/team-filters.js +26 -0
  14. package/common/workspace-app.js +19 -7
  15. package/out/activity.js +4 -4
  16. package/out/{chunk-NE2UGKRJ.js → chunk-3TFOB75M.js} +4 -3
  17. package/out/chunk-4EMM6UCD.js +1 -0
  18. package/out/chunk-4ISO5F7Y.js +7 -0
  19. package/out/chunk-4XIH7YTX.js +1 -0
  20. package/out/chunk-5PKS6FVK.js +2 -0
  21. package/out/chunk-6GAPL5ZA.js +42 -0
  22. package/out/chunk-6W5BMATD.js +1 -0
  23. package/out/{chunk-WFYLMF6Z.js → chunk-74AEKRES.js} +1 -1
  24. package/out/{chunk-YA4ETCFU.js → chunk-ATTKQVZH.js} +1 -1
  25. package/out/chunk-CGKPGHST.js +6 -0
  26. package/out/chunk-DEVAORP2.js +1 -0
  27. package/out/chunk-E3LFSUDJ.js +9 -0
  28. package/out/chunk-G3LY2FV4.js +1 -0
  29. package/out/chunk-HAZKDK4M.js +4 -0
  30. package/out/chunk-HOB3YPS5.js +1 -0
  31. package/out/{chunk-2K6UN3PC.js → chunk-IZ6MYRYB.js} +1 -1
  32. package/out/{chunk-KOGNQDSJ.js → chunk-J2FWBXFV.js} +1 -1
  33. package/out/{chunk-MYGJBYYJ.js → chunk-J4CLTPOR.js} +1 -1
  34. package/out/chunk-JFL7WLRV.js +1 -0
  35. package/out/chunk-JMG5DXII.js +2 -0
  36. package/out/chunk-JOXLZXMI.js +1 -0
  37. package/out/chunk-M37X4LCF.js +14 -0
  38. package/out/chunk-M3Y6SLC4.js +1 -0
  39. package/out/chunk-NXATZJQ2.js +1 -0
  40. package/out/chunk-PAD2JHZT.js +1 -0
  41. package/out/chunk-PKQBEPZU.js +244 -0
  42. package/out/chunk-PTFQKLKO.js +1 -0
  43. package/out/chunk-QMVOEBWC.js +13 -0
  44. package/out/{chunk-SXV2T5C4.js → chunk-QRKFPLA4.js} +1 -1
  45. package/out/chunk-RRBIJL2B.js +1 -0
  46. package/out/{chunk-GDPCLSOS.js → chunk-SYXT3SFP.js} +1 -1
  47. package/out/chunk-TOXMUOIW.js +5 -0
  48. package/out/chunk-UCP3JRE2.js +1 -0
  49. package/out/chunk-VRX2APNA.js +9 -0
  50. package/out/chunk-WMFDQS2L.js +11 -0
  51. package/out/chunk-WREU7JAG.js +1 -0
  52. package/out/chunk-WST3LUUL.js +1 -0
  53. package/out/{chunk-JEFZTVNX.js → chunk-X655SAHV.js} +1 -1
  54. package/out/chunk-Y5ZHFNBD.js +2 -0
  55. package/out/chunk-YF4M5OCI.js +1 -0
  56. package/out/chunk-Z667U2UJ.js +1 -0
  57. package/out/chunk-ZFXCWNXC.js +52 -0
  58. package/out/chunk-ZQHLECTI.js +1 -0
  59. package/out/client-managed-import.js +1 -1
  60. package/out/client-managed.js +27 -26
  61. package/out/compare.js +14 -14
  62. package/out/dialogs.js +36 -51
  63. package/out/graph.js +17 -15
  64. package/out/index.html +28 -12
  65. package/out/prism.js +1 -1
  66. package/out/scan.js +2 -2
  67. package/out/terminal.js +232 -232
  68. package/out/view.css +1 -1
  69. package/out/view.js +15 -15
  70. package/package.json +12 -7
  71. package/risk/actions/resolve.js +36 -5
  72. package/risk/index.d.ts +20 -2
  73. package/risk/npm/malware.js +6 -4
  74. package/risk/npm/registry.js +8 -6
  75. package/server-managed/bundle-advisories.ts +56 -74
  76. package/server-managed/bundle-build-worker.js +85 -21
  77. package/server-managed/bundle-build.ts +13 -4
  78. package/server-managed/bundle-dependency-reviews.ts +74 -0
  79. package/server-managed/bundle-validation.ts +32 -0
  80. package/server-managed/db-methods.ts +51 -16
  81. package/server-managed/db-neon.ts +18 -2
  82. package/server-managed/db.ts +11 -2
  83. package/server-managed/github-issue-links.ts +151 -47
  84. package/server-managed/github-pulls.ts +34 -15
  85. package/server-managed/http.ts +227 -12
  86. package/server-managed/index.ts +13 -1
  87. package/server-managed/managed-issues.ts +29 -15
  88. package/server-managed/npm-insights.ts +4 -3
  89. package/server-managed/npm-kept.ts +4 -0
  90. package/server-managed/package-entry-points.ts +37 -9
  91. package/server-managed/pretty-print.ts +7 -5
  92. package/server-managed/public-workspace.ts +2 -2
  93. package/server-managed/repository-actions.ts +7 -4
  94. package/server-managed/repository-browser.ts +11 -4
  95. package/server-managed/repository-risk-audit.ts +4 -5
  96. package/server-managed/repository-risk.ts +4 -4
  97. package/server-managed/sql.ts +2 -1
  98. package/server-managed/stasis-config.ts +55 -0
  99. package/server-managed/team-app-summaries.ts +54 -0
  100. package/server-managed/team-app.ts +52 -31
  101. package/server-managed/team-reports.ts +27 -7
  102. package/server-managed/upstream-cache.ts +41 -32
  103. package/server-managed/workspace-shares.ts +3 -1
  104. package/out/chunk-2ZDZWLFY.js +0 -1
  105. package/out/chunk-7VAWINQF.js +0 -6
  106. package/out/chunk-FMXWXDR4.js +0 -36
  107. package/out/chunk-HT2SSA3V.js +0 -1
  108. package/out/chunk-IZ7POWI3.js +0 -18
  109. package/out/chunk-LJ7XRXFU.js +0 -52
  110. package/out/chunk-QB4CBOX2.js +0 -1
  111. package/out/chunk-TQJDYHVE.js +0 -2
  112. package/out/chunk-TSIFIFBX.js +0 -232
  113. package/out/chunk-VH4XMZIW.js +0 -2
  114. package/out/chunk-WYLQWYLA.js +0 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@preventive/triage",
3
- "version": "1.0.0-alpha.53",
3
+ "version": "1.0.0-alpha.55",
4
4
  "description": "Client & relay server for triaging of automated reports",
5
5
  "license": "MIT OR Apache-2.0",
6
6
  "author": {
@@ -105,8 +105,10 @@
105
105
  "server-managed/bundle-build-diagnostics.d.ts",
106
106
  "server-managed/bundle-catalog.ts",
107
107
  "server-managed/bundle-commits.ts",
108
+ "server-managed/bundle-dependency-reviews.ts",
108
109
  "server-managed/bundle-store.ts",
109
110
  "server-managed/bundle-summary-cache.ts",
111
+ "server-managed/bundle-validation.ts",
110
112
  "server-managed/bundle.ts",
111
113
  "server-managed/cache-storage.ts",
112
114
  "server-managed/cli.js",
@@ -214,6 +216,7 @@
214
216
  "server-managed/solidity-entry-points.ts",
215
217
  "server-managed/sql-postgres.ts",
216
218
  "server-managed/sql.ts",
219
+ "server-managed/stasis-config.ts",
217
220
  "server-managed/static.ts",
218
221
  "server-managed/storage.ts",
219
222
  "server-managed/storage-command.ts",
@@ -226,6 +229,7 @@
226
229
  "server-managed/team-feed.ts",
227
230
  "server-managed/team-npm-scopes.ts",
228
231
  "server-managed/team-app.ts",
232
+ "server-managed/team-app-summaries.ts",
229
233
  "server-managed/team-reports.ts",
230
234
  "server-managed/triage-response.ts",
231
235
  "server-managed/type-only.ts",
@@ -305,6 +309,7 @@
305
309
  "common/managed/scan-models.ts",
306
310
  "common/managed/team-app.js",
307
311
  "common/managed/team-app.d.ts",
312
+ "common/managed/team-filters.js",
308
313
  "common/managed/triage.ts",
309
314
  "common/managed/upload-seal.ts",
310
315
  "common/save-error-reason.ts",
@@ -415,16 +420,16 @@
415
420
  "typescript": "^7.0.2"
416
421
  },
417
422
  "dependencies": {
418
- "@exodus/stasis": "1.0.0-beta.11",
419
- "@exodus/stasis-core": "1.0.0-beta.11",
420
- "@preventive/archive": "^1.2.0",
423
+ "@exodus/stasis": "1.0.0-beta.16",
424
+ "@exodus/stasis-core": "1.0.0-beta.16",
425
+ "@preventive/archive": "^1.4.0",
421
426
  "@preventive/compare": "^1.2.11",
422
427
  "@preventive/diff": "^1.1.0",
423
- "@preventive/lockfile": "^1.2.0",
428
+ "@preventive/lockfile": "^1.3.0",
424
429
  "@preventive/publishers": "^1.1.0",
425
- "@preventive/report": "^1.0.0",
430
+ "@preventive/report": "^1.0.1",
426
431
  "@preventive/sourcemap": "1.0.0-alpha.1",
427
- "@preventive/upstream": "^1.3.0",
432
+ "@preventive/upstream": "^1.7.0",
428
433
  "css-tree": "^3.2.1",
429
434
  "oxc-minify": "^0.152.0",
430
435
  "oxc-parser": "^0.152.0",
@@ -1,5 +1,6 @@
1
1
  // A reference resolved on GitHub (`source`, index.d.ts ActionsSource): its
2
- // repository, its ref's commit and its own code's findings (README.md).
2
+ // repository, its ref's commit, its advisories and its own code's findings
3
+ // (README.md).
3
4
  import { YamlError } from '@preventive/lockfile/yaml.js'
4
5
  import { readWorkflow } from './workflow.js'
5
6
  import { workflowFindings, workflowUses } from './findings.js'
@@ -168,7 +169,34 @@ async function pinnedOf(uses, info, source, tags) {
168
169
  else if (tags) findings.push({ id: 'untagged-commit', severity: 'moderate', on: where })
169
170
  }
170
171
  if (uses.comment && !commentHolds(uses.comment, named)) findings.push({ id: 'comment-mismatch', severity: 'high', comment: uses.comment, tags: named })
171
- return { commit, at: 'sha', findings }
172
+ return { commit, at: 'sha', named, findings }
173
+ }
174
+
175
+ // The versions a reference's advisories are asked of, as upstream's `github`
176
+ // packages take them: the releases on the commit it runs (`v4.2.1`, not the
177
+ // `v4` moving with it), else a commit by its tags, or as any release where
178
+ // none names it, and a tag or a branch as named (a moving tag its whole line).
179
+ function advisedVersions(at, ref, commit, named) {
180
+ const releases = named.filter(tag => isRelease(tagVersion(tag)))
181
+ if (releases.length > 0) return releases
182
+ if (at === 'sha') return named.length > 0 ? named : [commit]
183
+ return [ref]
184
+ }
185
+
186
+ const RATED = new Set(['critical', 'high', 'moderate', 'low'])
187
+
188
+ // A repository's advisories at `versions` (`source.advisories`), each a
189
+ // finding: malware red, else as its source rates it, one unrated yellow. One
190
+ // not known public is `internal`, asked of its own advisories alone, its name
191
+ // sent to no other source; why not (`advisories-unavailable`) where they
192
+ // couldn't be had, the rest resolved all the same.
193
+ async function advisoryFindings(source, info, versions) {
194
+ if (source.advisories === undefined) return []
195
+ try {
196
+ return (await source.advisories(info.fullName, versions, info.private ?? true)).map(({ id, title, url, severity, malware, versions: affected }) => ({
197
+ id: 'advisory', severity: malware ? 'critical' : RATED.has(severity) ? severity : 'moderate', advisory: id, ...title && { title }, ...url && { url },
198
+ ...malware && { malware: true }, versions: affected }))
199
+ } catch { return [{ id: 'advisories-unavailable', severity: 'notice' }] }
172
200
  }
173
201
 
174
202
  // A `$/path` (the repository's own at the commit running) as a reference.
@@ -202,9 +230,12 @@ export async function resolveAction(uses, source, { owner = null } = {}) {
202
230
  if (!info) return { ...at, repository: null, commit: null, at: null, tags: [], findings, uses: [] }
203
231
  const tags = await source.tags(info.fullName)
204
232
  const ref = uses.pin === 'sha' ? await pinnedOf(uses, info, source, tags) : await refOf(uses, info, source, tags)
205
- const code = ref.commit && !ref.findings.some(({ id }) => id === 'commit-missing') ? await codeOf(uses, info, ref.commit, source) : null
233
+ const runs = ref.commit && !ref.findings.some(({ id }) => id === 'commit-missing')
234
+ // What it runs, and its advisories at the versions that is.
235
+ const [code, advised] = runs ? await Promise.all([codeOf(uses, info, ref.commit, source),
236
+ advisoryFindings(source, info, advisedVersions(ref.at, uses.ref, ref.commit, ref.named ?? tagsOn(tags, ref.commit)))]) : [null, []]
206
237
  return { ...at, repository: { fullName: info.fullName, private: Boolean(info.private) }, commit: ref.commit, at: ref.at, tags: tagsOn(tags, ref.commit),
207
- findings: [...findings, ...ref.findings, ...code?.findings ?? []], uses: code?.uses ?? [] }
238
+ findings: [...findings, ...ref.findings, ...advised, ...code?.findings ?? []], uses: code?.uses ?? [] }
208
239
  }
209
240
 
210
241
  // `source` with its answers kept, by what was asked, while held: a scan's.
@@ -215,5 +246,5 @@ export function keptSource(source) {
215
246
  if (!kept.has(key)) kept.set(key, source[method](...args).catch(error => { kept.delete(key); throw error }))
216
247
  return kept.get(key)
217
248
  }
218
- return Object.fromEntries(['repo', 'tags', 'tag', 'branch', 'branches', 'contains', 'file'].map(method => [method, keep(method)]))
249
+ return Object.fromEntries(['repo', 'tags', 'tag', 'branch', 'branches', 'contains', 'file', 'advisories'].filter(method => source[method]).map(method => [method, keep(method)]))
219
250
  }
package/risk/index.d.ts CHANGED
@@ -68,7 +68,8 @@ export interface AssessNpmOptions {
68
68
  advisories?: boolean
69
69
  // Whether OSV's records of malware in the version are asked for too, where
70
70
  // `advisories` are: false for a private package, whose name OSV is never
71
- // to be sent. True unless given.
71
+ // to be sent, as upstream sends it none npm's registry doesn't serve
72
+ // anyone. True unless given.
72
73
  osv?: boolean
73
74
  // Whether a package is the caller's own, as its own scopes take: a new
74
75
  // dependency on one is told no tier nor age, nor is npm asked of it. None
@@ -178,7 +179,8 @@ export function npmReadyDownload(name: string, options: { cache: CacheStore; sig
178
179
  // Whether an advisory is of malware rather than a vulnerability: OSV's of a
179
180
  // malicious package (`MAL-…`), or one such a record names (its `aliases`),
180
181
  // CWE-506, embedded malicious code, whatever its title, or a title naming
181
- // malware.
182
+ // malware. The rule @preventive/upstream's advisories tell `malware` by, for
183
+ // those it doesn't tell, as Dependabot's alerts.
182
184
  export function npmMalwareAdvisory(advisory: { id?: string | undefined; aliases?: readonly string[] | undefined; cwe?: readonly string[] | undefined; title?: string | undefined }): boolean
183
185
 
184
186
  export function npmFileRisk(path: string, text: string | null, bytes?: number, script?: boolean): unknown
@@ -235,6 +237,22 @@ export interface ActionsSource {
235
237
  // commits, it holds for good.
236
238
  contains(name: string, base: string, sha: string): Promise<boolean | null>
237
239
  file(name: string, path: string, commit: string): Promise<string | null>
240
+ // A repository's advisories at `versions`, as upstream's `advisories`
241
+ // tells those of a `github` package: its own published ones, and but for
242
+ // one `internal` (not known public), OSV's of it as an Action. Without it,
243
+ // none are asked.
244
+ advisories?(name: string, versions: string[], internal: boolean): Promise<ActionAdvisory[]>
245
+ }
246
+
247
+ // An advisory of a repository, as upstream's `advisories` tells one.
248
+ export interface ActionAdvisory {
249
+ id: string
250
+ url?: string | undefined
251
+ title?: string | undefined
252
+ severity?: string | undefined
253
+ malware?: boolean | undefined
254
+ // The versions asked that it affects.
255
+ versions: string[]
238
256
  }
239
257
 
240
258
  // A reference resolved: its repository as GitHub has it now, its ref's
@@ -9,7 +9,9 @@
9
9
  // files its malware advisories as CWE-506, embedded malicious code, whatever
10
10
  // their title ("Critical severity vulnerability that affects event-stream
11
11
  // and flatmap-stream"); and its title may say so where its CWE doesn't
12
- // (@solana/web3.js's GHSA-jcxm-7wvp-g6p5, CWE-200 alone).
12
+ // (@solana/web3.js's GHSA-jcxm-7wvp-g6p5, CWE-200 alone). The rule upstream
13
+ // tells its own advisories' `malware` by, for those it doesn't tell, as
14
+ // Dependabot's alerts.
13
15
  export const npmMalwareAdvisory = ({ id, aliases, cwe, title }) => [id, ...aliases ?? []].some(name => (name ?? '').startsWith('MAL-'))
14
16
  || (cwe ?? []).includes('CWE-506') || /\bmalware\b/iu.test(title ?? '')
15
17
 
@@ -27,10 +29,10 @@ export function npmPlaceholder(json) {
27
29
 
28
30
  // The `malware` finding of a version, critical: its package taken down by npm
29
31
  // (`takenDown`, its latest version npm's placeholder), and the advisories of
30
- // malware among `advisories` (`{ id, url?, title?, cwe }`), each `{ id, url?,
31
- // title? }` once; null for neither.
32
+ // malware among `advisories` (`{ id, url?, title?, malware }`, as upstream
33
+ // tells it), each `{ id, url?, title? }` once; null for neither.
32
34
  export function npmMalwareFinding({ takenDown, advisories }) {
33
- const named = new Map(advisories.filter(npmMalwareAdvisory).map(({ id, url, title }) => [id, { id, ...url ? { url } : {}, ...title ? { title } : {} }]))
35
+ const named = new Map(advisories.filter(({ malware }) => malware).map(({ id, url, title }) => [id, { id, ...url ? { url } : {}, ...title ? { title } : {} }]))
34
36
  if (!takenDown && named.size === 0) return null
35
37
  return { id: 'malware', severity: 'critical', ...takenDown ? { takenDown: true } : {}, ...named.size > 0 ? { advisories: [...named.values()] } : {} }
36
38
  }
@@ -211,10 +211,12 @@ export async function npmPackageFacts(name, version, oldVersion, context) {
211
211
  // npm's advisories of `version` of a package, as `npm audit` asks them, and
212
212
  // where `osv`, OSV's records npm's registry hasn't, as the malicious
213
213
  // packages' of nx's versions are (MAL-2025-41443, of GHSA-cxm3-wv7p-598c,
214
- // which npm's registry hasn't): upstream's `advisories` and its `osv`, an
215
- // advisory of npm's an OSV record names taking the record's ids among its
216
- // `aliases`. Each `{ id, url?, title?, cwe, aliases }`, kept ADVISORIES_MS,
217
- // and given up on once `signal` aborts.
214
+ // which npm's registry hasn't): upstream's `advisories` and its `osv`, which
215
+ // sends OSV the name only where npm's registry serves it anyone. Each `{ id,
216
+ // url?, title?, malware }`, whether it is of malware as upstream tells it, an
217
+ // advisory of npm's an OSV record of malware names among them; kept
218
+ // ADVISORIES_MS, those kept with no `malware` asked again, and given up on
219
+ // once `signal` aborts.
218
220
  export async function npmVersionAdvisories(name, version, osv, context) {
219
221
  const what = `${name}@${version}`
220
222
  const { rows } = await kept(context, 'risk/npm/advisories', what, async () => {
@@ -222,8 +224,8 @@ export async function npmVersionAdvisories(name, version, osv, context) {
222
224
  const asked = await untilAborted(advisories([{ ecosystem: 'npm', name, versions: [version] }], { cache: false, osv }), context.signal).catch(error => {
223
225
  throw context.signal?.aborted ? error : new NpmRiskError(error?.status === 429 ? 'upstream-busy' : 'upstream-unavailable', `advisories of ${what} couldn't be had: ${error.message}`)
224
226
  })
225
- return { at: context.now, osv, rows: asked.map(({ id, url, title, cwe, aliases }) => ({ id, ...url ? { url } : {}, ...title ? { title } : {}, cwe, aliases })) }
226
- }, hit => context.now - hit.at < ADVISORIES_MS && hit.osv === osv)
227
+ return { at: context.now, osv, rows: asked.map(({ id, url, title, malware }) => ({ id, ...url ? { url } : {}, ...title ? { title } : {}, malware: malware === true })) }
228
+ }, hit => context.now - hit.at < ADVISORIES_MS && hit.osv === osv && hit.rows.every(row => typeof row.malware === 'boolean'))
227
229
  return rows
228
230
  }
229
231
 
@@ -1,83 +1,67 @@
1
1
  import { type Advisory, type CacheStore, type Package, advisories } from '@preventive/upstream/advisories.js'
2
2
  import { type Client, HttpError, createClient } from '@preventive/upstream/github.js'
3
- import { isNpmPackageName, npmPackageScope } from '../common/managed/npm-packages.js'
4
- import { NPM_REGISTRY, askUpstream } from './npm-packages.ts'
5
3
 
6
4
  export const ADVISORIES_TIMEOUT_MS = 30_000
7
- const PUBLIC_AT_ONCE = 16
8
- const order = (a: string, b: string) => Number(a > b) - Number(a < b)
9
-
10
- // Whether anyone may read a package, so that its name may go to a service
11
- // other than npm (OSV): an unscoped one always, npm's private packages
12
- // always being scoped; a scoped one where npm answers it anonymously, asked
13
- // by a HEAD, which reads nothing of it. One npm can't be asked of is taken
14
- // as private, and not kept; the rest are, for PUBLIC_MS, PUBLIC_NAMES at
15
- // most, as a package may be made private, or public. A name npm can't have
16
- // is asked of no one, upstream refusing it.
17
- const PUBLIC_MS = 60 * 60_000
18
- const PUBLIC_NAMES = 10_000
19
- const publicNames = new Map<string, { at: number; open: boolean }>()
20
- async function isPublicNpmPackage(name: string, signal: AbortSignal): Promise<boolean> {
21
- if (!isNpmPackageName(name)) return false
22
- if (npmPackageScope(name) === null) return true
23
- const kept = publicNames.get(name)
24
- if (kept && Date.now() - kept.at < PUBLIC_MS) return kept.open
25
- let open
26
- try { open = await askUpstream(`${NPM_REGISTRY}/${name}`, { method: 'HEAD', limit: Number.MAX_SAFE_INTEGER, signal, absent: [401, 403, 404] }) !== null } catch { return false }
27
- publicNames.delete(name)
28
- publicNames.set(name, { at: Date.now(), open })
29
- while (publicNames.size > PUBLIC_NAMES) publicNames.delete(publicNames.keys().next().value!)
30
- return open
31
- }
32
-
33
- // The npm packages anyone may read (isPublicNpmPackage), a few asked at a
34
- // time, within PUBLIC_BUDGET_MS, a third of the audit's: a scoped one npm
35
- // hasn't told of by then is taken as private, asked of npm alone, for the
36
- // audit to keep the rest of its time.
37
- const PUBLIC_BUDGET_MS = 10_000
38
- async function publicNpmNames(packages: Package[], signal: AbortSignal): Promise<Set<string>> {
39
- const names = [...new Set(packages.filter(pkg => pkg.ecosystem === 'npm').map(pkg => pkg.name))]
40
- const open = new Set<string>()
41
- const budget = new AbortController()
42
- const timer = setTimeout(() => { budget.abort(new Error('public-check-budget')) }, PUBLIC_BUDGET_MS)
43
- const asking = AbortSignal.any([signal, budget.signal])
44
- try {
45
- for (let at = 0; at < names.length; at += PUBLIC_AT_ONCE) {
46
- const chunk = names.slice(at, at + PUBLIC_AT_ONCE)
47
- const checked = await Promise.all(chunk.map(name => isPublicNpmPackage(name, asking)))
48
- for (const [i, name] of chunk.entries()) if (checked[i]) open.add(name)
49
- }
50
- } finally { clearTimeout(timer) }
51
- return open
52
- }
53
5
 
54
6
  function advisoryGithubClient(token: string | null, signal: AbortSignal): Client {
55
7
  const authenticated = createClient({ token, userAgent: 'deepview-triage' })
56
8
  let client = authenticated
9
+ // Published advisories, and whether a repository is public, remain
10
+ // readable after a token is revoked. Reuse anonymous access for subsequent
11
+ // repositories in this audit as well.
12
+ async function retried<T>(ask: (current: Client) => Promise<T>): Promise<T> {
13
+ const current = client
14
+ try { return await ask(current) } catch (error) {
15
+ if (token === null || current !== authenticated || !(error instanceof HttpError) || error.status !== 401) throw error
16
+ signal.throwIfAborted()
17
+ if (client === authenticated) client = createClient({ token: null, userAgent: 'deepview-triage' })
18
+ return await ask(client)
19
+ }
20
+ }
57
21
  return {
58
22
  ...authenticated,
59
- async listRepoAdvisories(options) {
60
- const current = client
61
- try { return await current.listRepoAdvisories(options) } catch (error) {
62
- if (token === null || current !== authenticated || !(error instanceof HttpError) || error.status !== 401) throw error
63
- signal.throwIfAborted()
64
- // Published advisories remain readable after a token is revoked. Reuse
65
- // anonymous access for subsequent repositories in this audit as well.
66
- if (client === authenticated) client = createClient({ token: null, userAgent: 'deepview-triage' })
67
- return await client.listRepoAdvisories(options)
68
- }
69
- },
23
+ listRepoAdvisories: options => retried(current => current.listRepoAdvisories(options)),
24
+ getRepo: options => retried(current => current.getRepo(options)),
25
+ }
26
+ }
27
+
28
+ // How many repositories GitHub is asked at once whether they are public, as
29
+ // upstream asks for their listings.
30
+ const REPOS_AT_ONCE = 4
31
+
32
+ // Whether GitHub says anyone may read `repo`: not one private, nor one it
33
+ // has none of for this client (gone, or not the caller's to read). Any other
34
+ // failure is thrown, as the audit then fails.
35
+ async function isPublicRepo(github: Client, repo: string): Promise<boolean> {
36
+ try { return (await github.getRepo({ repo, followRedirect: true }) as { private?: unknown } | null)?.private === false } catch (error) {
37
+ if (error instanceof HttpError && [404, 410, 451].includes(error.status)) return false
38
+ throw error
39
+ }
40
+ }
41
+
42
+ // The packages as upstream is asked of them. Upstream asks OSV of every
43
+ // `github` package by its name, as an Action, but of one `internal`: each is
44
+ // internal but where GitHub says its repository is public, so a private one,
45
+ // or one GitHub has none of, has its own published advisories read alone, as
46
+ // a private npm package's name never reaches OSV either. One that says
47
+ // (`internal`, as its caller knows) is taken as it says.
48
+ async function markInternal(packages: Package[], github: Client): Promise<Package[]> {
49
+ const internal = new Set(packages.flatMap(pkg => pkg.ecosystem === 'github' && pkg.internal === undefined ? [pkg.name] : []))
50
+ const repos = [...internal]
51
+ for (let at = 0; at < repos.length; at += REPOS_AT_ONCE) {
52
+ await Promise.all(repos.slice(at, at + REPOS_AT_ONCE).map(async repo => { if (await isPublicRepo(github, repo)) internal.delete(repo) }))
70
53
  }
54
+ return packages.map(pkg => pkg.ecosystem === 'github' && internal.has(pkg.name) ? { ...pkg, internal: true } : pkg)
71
55
  }
72
56
 
73
57
  // The managed caller supplies its user's token when available; public shares
74
58
  // remain anonymous. Upstream sends this credential only to GitHub and returns
75
59
  // published advisories, matching versions across npm, OSV and repository rows:
76
60
  // OSV's for npm packages anyone may read too, the malicious packages' records
77
- // among them, which npm's registry hasn't.
78
- // `cache` keeps each repository's listing (upstream-cache.ts) across audits;
79
- // `github`, a client of the caller's, asks GitHub in place of one with
80
- // `githubToken`.
61
+ // among them, which npm's registry hasn't, each row of malware told so
62
+ // (`malware`). `cache` keeps each repository's listing, and the npm packages
63
+ // anyone may read (upstream-cache.ts), across audits; `github`, a client of
64
+ // the caller's, asks GitHub in place of one with `githubToken`.
81
65
  export async function fetchBundleAdvisories(packages: Package[], signal: AbortSignal,
82
66
  { debug = false, repoAdvisories = false, details = false, githubToken = null, github, cache }: {
83
67
  debug?: boolean; repoAdvisories?: boolean; details?: boolean; githubToken?: string | null; github?: Client; cache?: CacheStore | undefined
@@ -95,17 +79,15 @@ export async function fetchBundleAdvisories(packages: Package[], signal: AbortSi
95
79
  signal.addEventListener('abort', onAbort, { once: true })
96
80
  })
97
81
  // npm packages anyone may read are asked of OSV too (upstream's `osv`),
98
- // for the malicious packages' records npm's registry hasn't; a private
99
- // one's name never leaves for OSV, as it never does for Socket. A package
100
- // is wholly in one ask or the other, so upstream's order holds.
101
- const open = await Promise.race([publicNpmNames(packages, signal), deadline])
102
- const toOsv = (pkg: Package) => pkg.ecosystem === 'npm' && open.has(pkg.name)
103
- // Kept in the caller's store, or without one in upstream's disk cache,
104
- // where the server set one (auditCache).
105
- const options = { github: github ?? advisoryGithubClient(githubToken, signal), repoAdvisories, details, ...(cache && { cache }) }
106
- const asked = [{ part: packages.filter(toOsv), osv: true }, { part: packages.filter(pkg => !toOsv(pkg)), osv: false }]
107
- const result = await Promise.race([Promise.all(asked.map(({ part, osv }) => part.length === 0 ? Promise.resolve([]) : advisories(part, { ...options, osv }))), deadline])
108
- return { status: 200, body: result.flat().toSorted((a, b) => order(a.ecosystem, b.ecosystem) || order(a.name, b.name)) }
82
+ // for the malicious packages' records npm's registry hasn't: upstream
83
+ // sends OSV only the names npm's registry serves without a token, so a
84
+ // private one's never leaves for OSV, as it never does for Socket; nor an
85
+ // internal `github` package's (markInternal). Kept
86
+ // in the caller's store, or without one in upstream's disk cache, where
87
+ // the server set one (auditCache).
88
+ const client = github ?? advisoryGithubClient(githubToken, signal)
89
+ const options = { github: client, repoAdvisories, details, osv: true, ...(cache && { cache }) }
90
+ return { status: 200, body: await Promise.race([markInternal(packages, client).then(asked => advisories(asked, options)), deadline]) }
109
91
  } catch (error) {
110
92
  // Answered as upstream's failure alone: logged, its message, or with
111
93
  // `debug` the whole error, but for a request its client gave up on. One
@@ -7,8 +7,62 @@ import { brotliOptions } from '@exodus/stasis-core/brotli'
7
7
  import { HttpError, createClient } from '@preventive/upstream/github.js'
8
8
  import { githubBundleFilename } from './bundle-build.ts'
9
9
  import { bundleBuildDiagnostic } from './bundle-build-diagnostics.js'
10
+ import { MAX_STASIS_CONFIG_BYTES, STASIS_CONFIG, namesLockfiles, parseStasisConfig } from './stasis-config.ts'
10
11
 
11
12
  const regularFile = entry => entry.type === 'blob' && ['100644', '100755'].includes(entry.mode)
13
+ const parentOf = path => posix.dirname(path).replace(/^\.$/u, '')
14
+
15
+ // The text of the stasis.config.json in a directory, null where there is none,
16
+ // each read once a build.
17
+ function configReader(input, github, client) {
18
+ const read = new Map()
19
+ const configText = async directory => {
20
+ const listing = await client.listRepoDir({ repo: github, sha: input.commit, directory: directory || undefined })
21
+ if (!listing.some(entry => entry.path === STASIS_CONFIG && regularFile(entry))) return null
22
+ return await client.getRepoFile({ repo: github, path: posix.join(directory, STASIS_CONFIG), ref: input.commit })
23
+ }
24
+ return directory => {
25
+ if (!read.has(directory)) read.set(directory, configText(directory))
26
+ return read.get(directory)
27
+ }
28
+ }
29
+
30
+ // The entries the stasis.config.json in `directory` names (parseStasisConfig's),
31
+ // null where it has none. One whose entries Stasis can't tell is config-invalid.
32
+ async function directoryConfig(configText, directory) {
33
+ const text = await configText(directory)
34
+ if (text === null) return null
35
+ const path = posix.join(directory, STASIS_CONFIG)
36
+ try {
37
+ if (Buffer.byteLength(text) > MAX_STASIS_CONFIG_BYTES) throw new Error(`${path} is larger than ${MAX_STASIS_CONFIG_BYTES} bytes`)
38
+ return parseStasisConfig(text, directory)
39
+ } catch (error) {
40
+ throw new Error('config-invalid', { cause: error })
41
+ }
42
+ }
43
+
44
+ // The directory of the stasis.config.json listing the selected entries: for
45
+ // each entry, the nearest one at or above it whose `entries` names it. null
46
+ // where none lists any. Stasis reads the one in the directory it builds from
47
+ // alone, and builds the entries it lists alone: with others, or with another
48
+ // file's, they are config-mixed.
49
+ async function listingConfig(input, configText) {
50
+ const found = await Promise.all(input.entries.map(async entry => {
51
+ for (let directory = parentOf(entry); ; directory = parentOf(directory)) {
52
+ const config = await directoryConfig(configText, directory)
53
+ if (config?.has(posix.relative(`/${directory}`, `/${entry}`))) return { entry, directory }
54
+ if (!directory) return { entry, directory: null }
55
+ }
56
+ }))
57
+ const listed = found.find(at => at.directory !== null)
58
+ if (!listed) return null
59
+ const other = found.find(at => at.directory !== listed.directory)
60
+ if (other) {
61
+ const where = at => at.directory === null ? 'none' : posix.join(at.directory, STASIS_CONFIG)
62
+ throw new Error('config-mixed', { cause: new Error(`${listed.entry} is listed in ${where(listed)}, ${other.entry} in ${where(other)}`) })
63
+ }
64
+ return listed.directory
65
+ }
12
66
 
13
67
  async function projectDirectory(input, github, client) {
14
68
  // Input validation permits either JS/TS entries or Solidity entries, never a mix.
@@ -32,21 +86,6 @@ async function packageName(input, github, project, bundle, client) {
32
86
  return typeof name === 'string' ? name : null
33
87
  }
34
88
 
35
- // Prisma Client is what `prisma generate` writes, not what the repo holds: for
36
- // a JS project whose package.json depends on a @prisma/ package, Stasis
37
- // generates it before the scan (generate: ['prisma']).
38
- async function prismaGenerate(input, github, project, client) {
39
- if (input.entries[0].endsWith('.sol')) return []
40
- const listing = await client.listRepoDir({ repo: github, sha: input.commit, directory: project || undefined })
41
- if (!listing.some(entry => entry.path === 'package.json' && regularFile(entry))) return []
42
- const text = await client.getRepoFile({ repo: github, path: posix.join(project, 'package.json'), ref: input.commit })
43
- let manifest
44
- try { manifest = JSON.parse(text) } catch { return [] }
45
- const named = ['dependencies', 'devDependencies', 'optionalDependencies', 'peerDependencies']
46
- .flatMap(field => manifest?.[field] !== null && typeof manifest?.[field] === 'object' ? Object.keys(manifest[field]) : [])
47
- return named.some(name => name.startsWith('@prisma/')) ? ['prisma'] : []
48
- }
49
-
50
89
  export async function buildStasisBundle({ input, github, token, maxBytes, scopes, cacheDir = null }, client = createClient({ token }), progress = () => {}) {
51
90
  const allowed = path => scopes.some(scope => !scope || path === scope || path.startsWith(scope + '/'))
52
91
  // Reuse these immutable listings when Stasis discovers the lockfile root.
@@ -61,13 +100,26 @@ export async function buildStasisBundle({ input, github, token, maxBytes, scopes
61
100
  // disk. This fresh worker inherits no application credentials but NPM_TOKEN.
62
101
  setCacheDir(cacheDir ?? false)
63
102
  progress('build')
64
- const project = await projectDirectory(input, github, buildClient)
103
+ // Entries stasis.config.json lists are built from its directory, where
104
+ // Stasis builds each with its options there, taking no conditions; others,
105
+ // from their project with the conditions chosen.
106
+ const configText = configReader(input, github, buildClient)
107
+ const configured = await listingConfig(input, configText)
108
+ if (configured !== null && input.conditions !== null) throw new Error('config-conditions')
109
+ if (configured === null && input.conditions === null && !input.entries[0].endsWith('.sol')) throw new Error('bad-conditions')
110
+ const project = configured ?? await projectDirectory(input, github, buildClient)
65
111
  if (!allowed(project)) throw new Error('build-scope')
66
- const generate = await prismaGenerate(input, github, project, buildClient)
112
+ // Stasis lays out the lockfiles the stasis.config.json it builds from names,
113
+ // refusing a package manager given beside them: Soldeer is named only for a
114
+ // Solidity build of a project whose config names none. Stasis detects the
115
+ // rest itself: TypeScript, and the Prisma Clients the entries reach, as the
116
+ // config's generatePrisma leaves it.
117
+ const { packageManager, ...options } = input.options
118
+ const named = packageManager !== undefined && !namesLockfiles(await configText(project) ?? '{}')
67
119
  const { bundle } = await buildGitHubBundle({
68
120
  github, sha: input.commit, directory: project || undefined,
69
121
  entries: input.entries.map(entry => posix.relative(project || '.', entry)),
70
- ...input.options, client: buildClient, ...(cacheDir === null && { cache: false }), ...(generate.length > 0 && { generate }),
122
+ ...options, ...(named && { packageManager }), client: buildClient, ...(cacheDir === null && { cache: false }),
71
123
  })
72
124
  const directory = bundle.repo?.directory ?? ''
73
125
  progress('scope')
@@ -86,10 +138,22 @@ export async function buildStasisBundle({ input, github, token, maxBytes, scopes
86
138
 
87
139
  // The code a failed build answers with. Never upstream bodies or paths outside
88
140
  // the caller's grants: those stay in the diagnostic. A missing or ambiguous
89
- // lockfile, or the named one missing, is the lockfile's.
141
+ // lockfile, or the named one missing, is the lockfile's. Stasis refuses a
142
+ // stasis.config.json entry setting an option that runs a program, whatever
143
+ // is given (nothing of the repo runs); a bundle holding a path or package its
144
+ // allowedPaths, disallowedPaths, allowedModules or disallowedModules refuses,
145
+ // the config's or an entry's, is the policy's; one it can't read, build with
146
+ // its options, or lay out the lockfiles it names of, is the config's. A build
147
+ // the repository's stasis.lock.json disagrees with, or that its lock mode
148
+ // holds to one that isn't there, is the stasis.lock.json's.
90
149
  export function buildErrorCode(error, message) {
91
- if (['build-scope', 'too-large'].includes(message)) return message
150
+ if (['build-scope', 'too-large', 'bad-conditions', 'config-invalid', 'config-mixed', 'config-conditions'].includes(message)) return message
92
151
  if (error instanceof HttpError) return error.status === 429 ? 'github-rate-limited' : 'github-build-failed'
152
+ if (/, which runs .+, and nothing of the project runs here$/u.test(message)) return 'config-runs-code'
153
+ if (message.endsWith(': bundle either the entries it names or others, not both')) return 'config-mixed'
154
+ if (/\bstasis\.config\.json(?:: entries .*?)?: (?:allowed|disallowed)(?:Paths|Modules) (?:refuses|does not allow|allows none of) /u.test(message)) return 'config-policy'
155
+ if (/\bstasis\.config\.json(?: is not valid JSON|: |'s lockfiles )/u.test(message)) return 'config-invalid'
156
+ if (/\bstasis\.lock\.json(?::| isn't there,| \(lock = )/u.test(message)) return 'stasis-lock'
93
157
  return /no packageManager given|lockfile installs|no (?:pnpm-lock\.yaml|yarn\.lock|package-lock\.json|soldeer\.lock) found in /u.test(message) ? 'build-lockfile' : 'build-failed'
94
158
  }
95
159
 
@@ -110,6 +174,6 @@ if (parentPort && workerData?.type === 'managed-bundle-build') {
110
174
  } catch (error) {
111
175
  const diagnostic = bundleBuildDiagnostic(error, workerData.token)
112
176
  const code = buildErrorCode(error, diagnostic.message)
113
- send({ error: code, status: code === 'too-large' ? 413 : code === 'build-scope' ? 403 : 422, diagnostic })
177
+ send({ error: code, status: code === 'too-large' ? 413 : code === 'build-scope' ? 403 : code === 'bad-conditions' ? 400 : 422, diagnostic })
114
178
  }
115
179
  }
@@ -18,7 +18,9 @@ export interface BundleBuildInput {
18
18
  entries: string[]
19
19
  directory: string
20
20
  options: { conditions?: string[]; mainFields?: string[]; metro?: boolean; platforms?: string[]; jsx?: boolean; packageManager?: 'soldeer' }
21
- // What the stored bundle records; null for Solidity, which takes no conditions.
21
+ // What the stored bundle records; null for Solidity, which takes no
22
+ // conditions, and for entries given none, which stasis.config.json must
23
+ // then list (the build checks: bundle-build-worker.js).
22
24
  conditions: BundleBuildConditions | null
23
25
  }
24
26
 
@@ -27,6 +29,13 @@ function path(value: unknown): value is string {
27
29
  && value.split('/').every(part => part !== '' && part !== '.' && part !== '..')
28
30
  }
29
31
 
32
+ const isScript = (entry: string) => /\.[cm]?[tj]sx?$/iu.test(entry)
33
+
34
+ // Whether a build takes `value` as an entry point, with others in its language.
35
+ export function buildableEntry(value: unknown): value is string {
36
+ return path(value) && (isScript(value) || value.endsWith('.sol'))
37
+ }
38
+
30
39
  export function parseBundleBuild(value: unknown): BundleBuildInput {
31
40
  const fail = (code = 'bad-request'): never => { throw new BundleBuildError(400, code) }
32
41
  if (value == null || typeof value !== 'object' || Array.isArray(value)) return fail()
@@ -35,7 +44,7 @@ export function parseBundleBuild(value: unknown): BundleBuildInput {
35
44
  || typeof commit !== 'string' || !/^[a-f\d]{40}$/u.test(commit)
36
45
  || !Array.isArray(entries) || entries.length === 0 || entries.length > 100 || !entries.every(path)) return fail()
37
46
  const selected = [...new Set(entries as string[])]
38
- const scripts = selected.every(entry => /\.[cm]?[tj]sx?$/iu.test(entry))
47
+ const scripts = selected.every(isScript)
39
48
  if (!scripts && !selected.every(entry => entry.endsWith('.sol'))) return fail('unsupported-entries')
40
49
  const parts = posix.dirname(selected[0]!).split('/').filter(part => part !== '.')
41
50
  for (const entry of selected) {
@@ -46,8 +55,8 @@ export function parseBundleBuild(value: unknown): BundleBuildInput {
46
55
  // above the project no longer leaves it unable to tell which one installs it.
47
56
  const options: BundleBuildInput['options'] = scripts ? {} : { packageManager: 'soldeer' }
48
57
  let recorded: BundleBuildConditions | null = null
49
- if (scripts) {
50
- if (conditions == null || typeof conditions !== 'object' || Array.isArray(conditions)) return fail('bad-conditions')
58
+ if (scripts && conditions != null) {
59
+ if (typeof conditions !== 'object' || Array.isArray(conditions)) return fail('bad-conditions')
51
60
  const { preset, conditions: names, platforms } = conditions as Record<string, unknown>
52
61
  if (typeof preset !== 'string' || !['node', 'browser', 'metro'].includes(preset) || !Array.isArray(names) || names.length > 16
53
62
  || !names.every(name => typeof name === 'string' && name.length > 0 && name.length <= 64