@preventive/triage 1.0.0-alpha.71 → 1.0.0-alpha.73

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 (145) hide show
  1. package/common/bundle-sources.d.ts +2 -0
  2. package/common/bundle-sources.js +19 -2
  3. package/common/finding-duplicates.js +5 -0
  4. package/common/finding-filters.js +45 -4
  5. package/common/line-diff.d.ts +2 -2
  6. package/common/line-diff.js +20 -8
  7. package/common/managed/finding-detail.js +1 -1
  8. package/common/managed/review-view-routes.js +23 -0
  9. package/common/managed/risk-routes.js +30 -0
  10. package/common/managed/routes.js +11 -6
  11. package/common/managed/team-app.d.ts +8 -6
  12. package/common/managed/team-app.js +15 -3
  13. package/common/stasis-files.d.ts +5 -0
  14. package/common/stasis-files.js +20 -0
  15. package/common/stasis-tgz.d.ts +6 -0
  16. package/common/stasis-tgz.js +27 -0
  17. package/common/workspace-app.js +20 -0
  18. package/out/activity.js +2 -4
  19. package/out/{chunk-2F3CYNPZ.js → chunk-2IHCVA3Y.js} +1 -1
  20. package/out/{chunk-HAZKDK4M.js → chunk-2V6Q4ZOZ.js} +1 -1
  21. package/out/{chunk-RRBIJL2B.js → chunk-4IW3VD3H.js} +1 -1
  22. package/out/chunk-4YZOXAXR.js +2 -0
  23. package/out/chunk-5NUDNLJA.js +6 -0
  24. package/out/chunk-5YP7P5KA.js +1 -0
  25. package/out/chunk-6VOB5WXX.js +2 -0
  26. package/out/chunk-74JZTGTO.js +11 -0
  27. package/out/chunk-7YFJUT2P.js +1 -0
  28. package/out/chunk-A4BBBHQU.js +1 -0
  29. package/out/{chunk-4ISO5F7Y.js → chunk-ADMVRLNP.js} +1 -1
  30. package/out/chunk-BIWPZT4E.js +52 -0
  31. package/out/{chunk-HETCVNO4.js → chunk-BLPHP4U3.js} +1 -1
  32. package/out/{chunk-Y5ZHFNBD.js → chunk-BXQCCW7F.js} +1 -1
  33. package/out/{chunk-JFL7WLRV.js → chunk-CU3FPB3P.js} +1 -1
  34. package/out/chunk-CXVBTUK3.js +4 -0
  35. package/out/{chunk-M37X4LCF.js → chunk-EXQQ5VQJ.js} +1 -1
  36. package/out/{chunk-NXATZJQ2.js → chunk-GSNSP3EF.js} +1 -1
  37. package/out/chunk-GV5U7OLP.js +40 -0
  38. package/out/{chunk-JOXLZXMI.js → chunk-GX5UXWT4.js} +1 -1
  39. package/out/{chunk-YF4M5OCI.js → chunk-I6UQQKAH.js} +1 -1
  40. package/out/{chunk-UCP3JRE2.js → chunk-LVW7WWV4.js} +1 -1
  41. package/out/chunk-NEFCZAIY.js +11 -0
  42. package/out/{chunk-TOXMUOIW.js → chunk-OQS2USZM.js} +1 -1
  43. package/out/chunk-PQPKHAI5.js +15 -0
  44. package/out/chunk-PX5DLFSD.js +1 -0
  45. package/out/chunk-QBFT2DDR.js +1 -0
  46. package/out/{chunk-GZ46SSTF.js → chunk-RGNF4OH6.js} +1 -1
  47. package/out/chunk-RLLA4GHZ.js +1 -0
  48. package/out/chunk-ROHACH7V.js +3 -0
  49. package/out/chunk-ROWUGPID.js +1 -0
  50. package/out/{chunk-QMVOEBWC.js → chunk-S2ZIKK45.js} +1 -1
  51. package/out/chunk-SSPDMA6D.js +1 -0
  52. package/out/chunk-TD7RVUTT.js +2 -0
  53. package/out/{chunk-4XIH7YTX.js → chunk-UH6NZBSU.js} +1 -1
  54. package/out/{chunk-M3Y6SLC4.js → chunk-UJP4PQJA.js} +1 -1
  55. package/out/{chunk-PAD2JHZT.js → chunk-UJSJMXEG.js} +1 -1
  56. package/out/{chunk-E3LFSUDJ.js → chunk-VYYUMYVW.js} +1 -1
  57. package/out/{chunk-VRX2APNA.js → chunk-XVKDPU4D.js} +1 -1
  58. package/out/chunk-Y2XGV3RC.js +8 -0
  59. package/out/chunk-YCEMQETS.js +255 -0
  60. package/out/client-managed-import.js +3 -3
  61. package/out/client-managed.js +30 -27
  62. package/out/compare.js +16 -25
  63. package/out/dialogs.js +38 -38
  64. package/out/graph.js +14 -14
  65. package/out/index.html +32 -52
  66. package/out/scan.js +2 -2
  67. package/out/terminal.js +239 -240
  68. package/out/view.css +1 -1
  69. package/out/view.js +15 -15
  70. package/package.json +21 -13
  71. package/risk/actions/resolve.js +56 -19
  72. package/risk/actions/uses.js +27 -9
  73. package/risk/actions/workflow.js +75 -2
  74. package/risk/files/characters.js +11 -0
  75. package/risk/files/hosts.js +4 -4
  76. package/risk/files/readability.js +5 -2
  77. package/risk/files/runs-code.js +12 -25
  78. package/risk/files/script.js +141 -19
  79. package/risk/files/source-map-link.js +43 -0
  80. package/risk/index.d.ts +24 -7
  81. package/risk/index.js +1 -0
  82. package/risk/npm/assess.js +1 -1
  83. package/risk/npm/compare.js +9 -30
  84. package/risk/npm/diff.js +1 -1
  85. package/risk/npm/held.js +6 -2
  86. package/risk/npm/registry.js +5 -2
  87. package/risk/npm/tarball.js +79 -22
  88. package/server-managed/bundle-advisories.ts +12 -36
  89. package/server-managed/bundle-build-worker.js +29 -11
  90. package/server-managed/bundle-build.ts +4 -3
  91. package/server-managed/bundle-cache.ts +53 -13
  92. package/server-managed/bundle.ts +45 -13
  93. package/server-managed/comparison.ts +3 -3
  94. package/server-managed/db-methods.ts +54 -17
  95. package/server-managed/db-neon.ts +9 -2
  96. package/server-managed/db-schema.ts +12 -2
  97. package/server-managed/db.ts +4 -1
  98. package/server-managed/dependency-reviews.ts +8 -5
  99. package/server-managed/github-errors.ts +22 -0
  100. package/server-managed/github-requests.ts +4 -2
  101. package/server-managed/http.ts +336 -76
  102. package/server-managed/management.ts +10 -4
  103. package/server-managed/npm-delta.ts +17 -2
  104. package/server-managed/npm-kept.ts +6 -0
  105. package/server-managed/npm-packages.ts +30 -29
  106. package/server-managed/npm-patch.ts +9 -9
  107. package/server-managed/package-entry-points.ts +54 -6
  108. package/server-managed/pull-dependencies.ts +4 -1
  109. package/server-managed/rename-diff-worker.js +11 -7
  110. package/server-managed/rename-diff.ts +5 -4
  111. package/server-managed/report-opening.ts +77 -0
  112. package/server-managed/report-sources.ts +2 -1
  113. package/server-managed/repository-aliases.ts +34 -0
  114. package/server-managed/repository-browser.ts +87 -32
  115. package/server-managed/repository-code.ts +6 -3
  116. package/server-managed/repository-dependency-reviews.ts +21 -0
  117. package/server-managed/repository-issues.ts +15 -9
  118. package/server-managed/repository-risk-read.ts +50 -4
  119. package/server-managed/repository-risk.ts +9 -2
  120. package/server-managed/stasis-config.ts +97 -5
  121. package/server-managed/stasis-dependencies.ts +7 -4
  122. package/server-managed/team-reports.ts +5 -3
  123. package/server-managed/type-only-proof.js +26 -4
  124. package/server-managed/upstream-cache.ts +18 -12
  125. package/server-managed/workspace-shares.ts +4 -4
  126. package/out/chunk-2HGKTQ5Q.js +0 -52
  127. package/out/chunk-45HTR22K.js +0 -4
  128. package/out/chunk-4RS3NDOG.js +0 -1
  129. package/out/chunk-7GJNLCXF.js +0 -15
  130. package/out/chunk-CGRIZ5M5.js +0 -1
  131. package/out/chunk-CQIT5UNK.js +0 -1
  132. package/out/chunk-D6NVKGGG.js +0 -10
  133. package/out/chunk-DTAR2BB6.js +0 -247
  134. package/out/chunk-MPXVKVPF.js +0 -3
  135. package/out/chunk-NB4JBTKK.js +0 -2
  136. package/out/chunk-QPGM3DTI.js +0 -42
  137. package/out/chunk-VFWMQ3LG.js +0 -2
  138. package/out/chunk-VG5RH6KN.js +0 -2
  139. package/out/chunk-WFDXHPT2.js +0 -12
  140. package/out/chunk-WMFDQS2L.js +0 -11
  141. package/out/chunk-WREU7JAG.js +0 -1
  142. /package/out/{chunk-JMG5DXII.js → chunk-7HP2VYIC.js} +0 -0
  143. /package/out/{chunk-HOB3YPS5.js → chunk-K4XJHM2T.js} +0 -0
  144. /package/out/{chunk-4EMM6UCD.js → chunk-QI66YA4F.js} +0 -0
  145. /package/out/{chunk-G3LY2FV4.js → chunk-WCDYSIKG.js} +0 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@preventive/triage",
3
- "version": "1.0.0-alpha.71",
3
+ "version": "1.0.0-alpha.73",
4
4
  "description": "Client & relay server for triaging of automated reports",
5
5
  "license": "MIT OR Apache-2.0",
6
6
  "author": {
@@ -184,6 +184,7 @@
184
184
  "server-managed/queue.ts",
185
185
  "server-managed/repo-path.ts",
186
186
  "server-managed/report-migration.ts",
187
+ "server-managed/report-opening.ts",
187
188
  "server-managed/report-query.ts",
188
189
  "server-managed/report-response.ts",
189
190
  "server-managed/report-store.ts",
@@ -311,6 +312,8 @@
311
312
  "common/managed/report-content.ts",
312
313
  "common/managed/repository-alias.ts",
313
314
  "common/managed/report-filter.ts",
315
+ "common/managed/review-view-routes.js",
316
+ "common/managed/risk-routes.js",
314
317
  "common/managed/roles.ts",
315
318
  "common/managed/routes.d.ts",
316
319
  "common/managed/routes.js",
@@ -330,6 +333,10 @@
330
333
  "common/scan-server.ts",
331
334
  "common/security-links.d.ts",
332
335
  "common/security-links.js",
336
+ "common/stasis-files.d.ts",
337
+ "common/stasis-files.js",
338
+ "common/stasis-tgz.d.ts",
339
+ "common/stasis-tgz.js",
333
340
  "common/utf8.d.ts",
334
341
  "common/utf8.js",
335
342
  "common/workspace-app.js",
@@ -341,6 +348,7 @@
341
348
  "risk/files/readability.js",
342
349
  "risk/files/runs-code.js",
343
350
  "risk/files/script.js",
351
+ "risk/files/source-map-link.js",
344
352
  "risk/files/stamps.js",
345
353
  "risk/npm/assess.js",
346
354
  "risk/npm/combined.js",
@@ -412,41 +420,41 @@
412
420
  "test": "node --js-base-64 --require=./tests/_resource-loader.js --experimental-test-module-mocks --test-timeout=120000 --test-concurrency=$(node -p 'Math.ceil(os.availableParallelism() * 1.5)') --test ./tests/*.test.js"
413
421
  },
414
422
  "devDependencies": {
415
- "@electric-sql/pglite": "^0.5.15",
423
+ "@electric-sql/pglite": "^0.5.16",
416
424
  "@noble/ciphers": "^2.4.0",
417
425
  "@preventive/terminal": "2.0.0-beta.3",
418
426
  "@rray/frontend": "^1.0.0",
419
427
  "@stylistic/stylelint-plugin": "^6.0.1",
420
428
  "@types/node": "^26.6.6",
421
429
  "@types/ws": "^8.18.2",
422
- "@vercel/functions": "^3.11.0",
430
+ "@vercel/functions": "^3.11.3",
423
431
  "brotli": "^1.3.3",
424
432
  "esbuild": "^0.28.2",
425
433
  "lit": "^3.3.3",
426
434
  "minify-html-literals": "^1.3.5",
427
- "oxlint": "^1.92.0",
435
+ "oxlint": "^1.93.0",
428
436
  "prismjs": "^1.30.0",
429
437
  "stylelint": "^18.0.0",
430
438
  "stylelint-config-recommended": "^19.0.0",
431
439
  "typescript": "^7.0.2"
432
440
  },
433
441
  "dependencies": {
434
- "@exodus/stasis": "1.0.0-rc.6",
435
- "@exodus/stasis-core": "1.0.0-rc.6",
442
+ "@exodus/stasis": "1.0.0-rc.9",
443
+ "@exodus/stasis-core": "1.0.0-rc.9",
436
444
  "@preventive/archive": "^1.4.1",
437
- "@preventive/compare": "^1.2.13",
445
+ "@preventive/compare": "^1.2.14",
438
446
  "@preventive/diff": "^1.1.1",
439
- "@preventive/lockfile": "^1.3.0",
447
+ "@preventive/lockfile": "^1.3.1",
440
448
  "@preventive/publishers": "^1.1.0",
441
449
  "@preventive/report": "^1.0.1",
442
450
  "@preventive/sourcemap": "1.0.0-alpha.1",
443
- "@preventive/upstream": "^1.8.0",
451
+ "@preventive/upstream": "^1.9.1",
444
452
  "css-tree": "^3.2.1",
445
- "oxc-minify": "^0.157.0",
453
+ "oxc-minify": "^0.158.0",
446
454
  "oxc-parser": "^0.152.0",
447
- "oxfmt": "^0.77.0",
455
+ "oxfmt": "^0.78.0",
448
456
  "semver": "^7.8.6",
449
- "ws": "^8.22.1"
457
+ "ws": "^8.23.0"
450
458
  },
451
459
  "peerDependencies": {
452
460
  "@neondatabase/serverless": "^1.0.2",
@@ -464,5 +472,5 @@
464
472
  "optional": true
465
473
  }
466
474
  },
467
- "packageManager": "pnpm@12.18.0+sha512.9d7882d5dc82147ecfc4e062aa34834e0ad0296710a1837a1115ae012127d36b7109e321ac1598a1dd0a65597681b1e99917e12ed0d749eae91187943363beb3"
475
+ "packageManager": "pnpm@12.21.0+sha512.66fd8278ee28935ce951792d34367ff3af5c432a2756445823029533835b8da529bbe0be5130afd56ed0713fb8789e47bad82d93bd5ae2fe54b15562d6276ef0"
468
476
  }
@@ -5,7 +5,7 @@ import { YamlError } from '@preventive/lockfile/yaml.js'
5
5
  import { readWorkflow } from './workflow.js'
6
6
  import { workflowFindings, workflowUses } from './findings.js'
7
7
  import { imageFinding, scriptFindings } from './scripts/grade.js'
8
- import { isWorkflowPath, ownerKind, readImage, readUses, usesKey } from './uses.js'
8
+ import { isVersion, isWorkflowPath, ownerKind, readImage, readUses, usesKey } from './uses.js'
9
9
 
10
10
  // An action's tags as versions: a release's (`v4.2.1`), and a major (`v4`) or
11
11
  // minor one (`v4.2`) moved to its line's latest. `[major, minor, patch]`,
@@ -136,30 +136,55 @@ function tagFindings(ref, commit, tags) {
136
136
  return findings
137
137
  }
138
138
 
139
- // A ref not a commit, resolved as GitHub resolves it, a tag before a branch.
139
+ // The ref a reference names, and whether a branch or a tag alone (`only`):
140
+ // HEAD, as a checkout with no ref names one, its default branch; a
141
+ // checkout's `refs/heads/…` and `refs/tags/…`.
142
+ function refNamed(uses, info) {
143
+ if (uses.ref === 'HEAD') return { ref: info.defaultBranch, only: 'branch' }
144
+ const qualified = /^refs\/(heads|tags)\/(.+)$/u.exec(uses.ref)
145
+ return qualified ? { ref: qualified[2], only: qualified[1] === 'heads' ? 'branch' : 'tag' } : { ref: uses.ref, only: null }
146
+ }
147
+
148
+ // A ref not a commit, resolved as GitHub resolves it, a tag before a branch,
149
+ // or as a checkout does (`checkout`), a branch before a tag; one named a
150
+ // branch or a tag alone as that alone.
140
151
  async function refOf(uses, info, source, tags) {
141
- const name = info.fullName
142
- const [tagged, branched] = await Promise.all([tags ? tags.find(({ tag }) => tag === uses.ref)?.oid ?? null : source.tag(name, uses.ref), source.branch(name, uses.ref)])
152
+ const name = info.fullName, { ref, only } = refNamed(uses, info)
153
+ const [tagged, branched] = await Promise.all([only === 'branch' ? null : tags ? tags.find(each => each.tag === ref)?.oid ?? null : source.tag(name, ref),
154
+ only === 'tag' ? null : source.branch(name, ref)])
143
155
  if (!tagged && !branched) return { commit: null, at: null, findings: [{ id: 'ref-missing', severity: 'moderate' }] }
144
- if (!tagged) return { commit: branched, at: 'branch', findings: [{ id: 'branch-ref', severity: 'high' }] }
145
- const findings = branched ? [{ id: 'ambiguous-ref', severity: 'high', branch: branched }] : []
146
- findings.push(...tagFindings(uses.ref, tagged, tags))
156
+ const ambiguous = tagged && branched ? [{ id: 'ambiguous-ref', severity: 'high', branch: branched }] : []
157
+ if (!tagged || (branched && uses.checkout)) return { commit: branched, at: 'branch', findings: [...ambiguous, { id: 'branch-ref', severity: 'high' }] }
158
+ const findings = [...ambiguous, ...tagFindings(ref, tagged, tags)]
147
159
  const where = await reachable(source, info, tagged, tags)
148
160
  if (where === 'none') findings.push({ id: 'tag-off-branch', severity: 'low' })
149
161
  return { commit: tagged, at: 'tag', findings }
150
162
  }
151
163
 
152
- // Whether a version a comment names is a tag on the commit, or one's line.
164
+ // Whether the version or ref a comment names is a tag on the commit, a
165
+ // version with or without its `v`, or a major or minor tag's line.
153
166
  function commentHolds(comment, named) {
154
- const version = tagVersion(comment)
155
- return named.some(tag => tag === comment || tag.replace(/^v/u, '') === comment.replace(/^v/u, '') || (version && version[2] === null && ofLine(version, tagVersion(tag))))
167
+ const unprefixed = text => text.replace(/^v/u, ''), version = tagVersion(comment)
168
+ return named.some(tag => tag === comment || (isVersion(comment) && isVersion(tag) && unprefixed(tag) === unprefixed(comment))
169
+ || (version && version[2] === null && ofLine(version, tagVersion(tag))))
170
+ }
171
+
172
+ // Whether the branch a comment names has the commit in its history; null
173
+ // where it has no such branch, or GitHub no such commit.
174
+ async function onBranch(source, name, branch, commit) {
175
+ const head = await source.branch(name, branch)
176
+ return head === null ? null : head === commit || await source.contains(name, head, commit)
156
177
  }
157
178
 
158
- // A commit, looked for in the history where no tag names it, held to its comment.
179
+ // A commit, looked for in the history where no tag names it, held to its
180
+ // comment: by a tag on it of the version or ref the comment names, else by
181
+ // a branch so named with it in its history (`# main`, or a `# v4` that is
182
+ // a branch). A ref named alone that no tag nor branch is named is a note,
183
+ // held to nothing.
159
184
  async function pinnedOf(uses, info, source, tags) {
160
- const commit = uses.ref
161
- let named = tagsOn(tags, commit)
162
- if (!tags && uses.comment && await source.tag(info.fullName, uses.comment) === commit) named = [uses.comment]
185
+ const commit = uses.ref, { comment } = uses
186
+ const commented = !tags && comment ? await source.tag(info.fullName, comment) : null
187
+ const named = commented === commit ? [comment] : tagsOn(tags, commit)
163
188
  const findings = []
164
189
  if (named.length === 0) {
165
190
  const where = await reachable(source, info, commit, tags)
@@ -168,7 +193,13 @@ async function pinnedOf(uses, info, source, tags) {
168
193
  else if (where === 'unknown') findings.push({ id: 'unverified-commit', severity: 'high' })
169
194
  else if (tags) findings.push({ id: 'untagged-commit', severity: 'moderate', on: where })
170
195
  }
171
- if (uses.comment && !commentHolds(uses.comment, named)) findings.push({ id: 'comment-mismatch', severity: 'high', comment: uses.comment, tags: named })
196
+ if (comment && !commentHolds(comment, named)) {
197
+ const branch = await onBranch(source, info.fullName, comment, commit)
198
+ const tagged = commented !== null || Boolean(tags?.some(({ tag }) => tag === comment))
199
+ if (branch === false || (branch === null && (isVersion(comment) || tagged))) {
200
+ findings.push({ id: 'comment-mismatch', severity: 'high', comment, tags: named, ...branch === false && { branch: comment } })
201
+ }
202
+ }
172
203
  return { commit, at: 'sha', named, findings }
173
204
  }
174
205
 
@@ -189,13 +220,19 @@ const RATED = new Set(['critical', 'high', 'moderate', 'low'])
189
220
  // finding: malware red, else as its source rates it, one unrated yellow. One
190
221
  // not known public is `internal`, asked of its own advisories alone, its name
191
222
  // sent to no other source; why not (`advisories-unavailable`) where they
192
- // couldn't be had, the rest resolved all the same.
223
+ // couldn't be had, the rest resolved all the same. Each with what tells it
224
+ // apart, as its row's tooltip shows it: its source, other ids, CVSS score,
225
+ // CWEs, the range it affects, whether its source rates it (`unrated`) and,
226
+ // where it is no vulnerability as such, what it is (`informational`); not
227
+ // its text, which its link has.
193
228
  async function advisoryFindings(source, info, versions) {
194
229
  if (source.advisories === undefined) return []
195
230
  try {
196
- return (await source.advisories(info.fullName, versions, info.private ?? true)).map(({ id, title, url, severity, malware, versions: affected }) => ({
231
+ return (await source.advisories(info.fullName, versions, info.private ?? true)).map(({ id, title, url, severity, malware, versions: affected, source: from,
232
+ aliases, cvss, cwe, range, informational }) => ({
197
233
  id: 'advisory', severity: malware ? 'critical' : RATED.has(severity) ? severity : 'moderate', advisory: id, ...title && { title }, ...url && { url },
198
- ...malware && { malware: true }, versions: affected }))
234
+ ...malware && { malware: true }, ...!malware && !RATED.has(severity) && { unrated: true }, versions: affected, ...from && { source: from }, ...aliases?.length && { aliases }, ...Number.isFinite(cvss) && { cvss },
235
+ ...cwe?.length && { cwe }, ...range && { range }, ...informational && { informational } }))
199
236
  } catch { return [{ id: 'advisories-unavailable', severity: 'notice' }] }
200
237
  }
201
238
 
@@ -233,7 +270,7 @@ export async function resolveAction(uses, source, { owner = null } = {}) {
233
270
  const runs = ref.commit && !ref.findings.some(({ id }) => id === 'commit-missing')
234
271
  // What it runs, and its advisories at the versions that is.
235
272
  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, []]
273
+ advisoryFindings(source, info, advisedVersions(ref.at, refNamed(uses, info).ref, ref.commit, ref.named ?? tagsOn(tags, ref.commit)))]) : [null, []]
237
274
  return { ...at, repository: { fullName: info.fullName, private: Boolean(info.private) }, commit: ref.commit, at: ref.at, tags: tagsOn(tags, ref.commit),
238
275
  findings: [...findings, ...ref.findings, ...advised, ...code?.findings ?? []], uses: code?.uses ?? [] }
239
276
  }
@@ -9,7 +9,15 @@ const SHA = /^[\da-f]{40}$/u
9
9
  const SHORT_SHA = /^[\da-f]{7,39}$/u
10
10
  // A version a comment beside a pin names, as Dependabot, Renovate, pinact
11
11
  // and ratchet write one (`# v4.1.1`, `# tag=v4.1.1`, `# ratchet:x@v4`).
12
- const VERSION_COMMENT = /^#\s*(?:tag\s*[=:]\s*|pin@|ratchet:[^\s@]+@)?(v?\d+(?:\.\d+){0,3}(?:[-+][\w.-]+)?)(?![\w.])/iu
12
+ const VERSION = String.raw`v?\d+(?:\.\d+){0,3}(?:[-+][\w.-]+)?`
13
+ const VERSION_COMMENT = new RegExp(String.raw`^#\s*(?:tag\s*[=:]\s*|pin@|ratchet:[^\s@]+@)?(${VERSION})(?![\w.])`, 'iu')
14
+ // Whether what a comment names is a version, not a ref named alone.
15
+ const VERSION_ALONE = new RegExp(`^${VERSION}$`, 'iu')
16
+ export const isVersion = text => VERSION_ALONE.test(text)
17
+ // Else a branch or a tag a comment names alone (`# main`, `# ratchet:x@main`),
18
+ // one git can name so.
19
+ const REF_COMMENT = /^#\s*(?:ratchet:[^\s@]+@)?([\w./-]{1,255})$/u
20
+ const BAD_REF = /\.\.|\/\/|^-|(?:^|\/)\.|\.lock(?:\/|$)|[./]$|^\//u
13
21
 
14
22
  // `sha`, `short-sha`, `ref` (a tag or a branch), or `expression`, which GitHub refuses.
15
23
  function pinOf(ref) {
@@ -31,14 +39,21 @@ export function readImage(text) {
31
39
  return { image: tagged ? named.slice(0, colon) : named, tag: tagged ? named.slice(colon + 1) : null, digest }
32
40
  }
33
41
 
34
- const commentVersion = comment => typeof comment === 'string' ? VERSION_COMMENT.exec(comment.trim())?.[1] ?? null : null
42
+ // The ref a comment names alone, whole (`# v1/maintenance`), else the
43
+ // version it begins with (`# v4 is fine`); null for neither.
44
+ function commentNamed(comment) {
45
+ const text = typeof comment === 'string' ? comment.trim() : ''
46
+ const ref = REF_COMMENT.exec(text)?.[1]
47
+ return ref && !BAD_REF.test(ref) ? ref : VERSION_COMMENT.exec(text)?.[1] ?? null
48
+ }
35
49
 
36
50
  export const isWorkflowPath = path => /^\.github\/workflows\/[^/]+\.ya?ml$/u.test(path)
37
51
 
38
52
  // `{ kind, uses, … }`: `action`, or `workflow` at a job's level (`job`), with
39
- // its `repo`, `path`, `ref`, `pin` and `comment`'s version; `local`, `self`
40
- // at the commit running, `job` a reusable workflow's, at the calling
41
- // workflow's commit; `docker`; or `invalid`, which GitHub won't run.
53
+ // its `repo`, `path`, `ref`, `pin` and the version or ref its `comment`
54
+ // names; `local`, `self` at the commit running, `job` a reusable workflow's,
55
+ // at the calling workflow's commit; `docker`; or `invalid`, which GitHub
56
+ // won't run.
42
57
  export function readUses(text, { job = false, comment = null } = {}) {
43
58
  const uses = typeof text === 'string' ? text.trim() : ''
44
59
  if (uses.startsWith('./') || uses.startsWith('$/')) {
@@ -54,18 +69,21 @@ export function readUses(text, { job = false, comment = null } = {}) {
54
69
  if (!OWNER.test(owner) || !NAME.test(name) || ref === '' || path.some(part => !NAME.test(part)) || (job && !isWorkflowPath(path.join('/')))) {
55
70
  return { kind: 'invalid', uses }
56
71
  }
57
- return { kind: job ? 'workflow' : 'action', uses, repo: `${owner}/${name}`, path: path.join('/'), ref, pin: pinOf(ref), comment: commentVersion(comment) }
72
+ return { kind: job ? 'workflow' : 'action', uses, repo: `${owner}/${name}`, path: path.join('/'), ref, pin: pinOf(ref), comment: commentNamed(comment) }
58
73
  }
59
74
 
60
- // The key a reference is resolved once by, a commit's with its comment's version.
61
- export const usesKey = ({ repo, path, ref, pin, comment }) => `${repo.toLowerCase()}${path ? `/${path}` : ''}@${ref}${pin === 'sha' && comment ? ` # ${comment}` : ''}`
75
+ // The key a reference is resolved once by, a commit's with what its comment
76
+ // names, and one a checkout resolves a branch first by (`checkout`) so.
77
+ export const usesKey = ({ repo, path, ref, pin, comment, checkout }) => `${repo.toLowerCase()}${path ? `/${path}` : ''}@${ref}${
78
+ pin === 'sha' ? comment ? ` # ${comment}` : '' : checkout ? ' # checkout' : ''}`
62
79
 
63
80
  // A reference from its key, a reusable workflow's by its path's.
64
81
  export function usesOfKey(key) {
65
82
  const m = typeof key === 'string' ? /^([^@\s]+@\S+)(?: # (\S+))?$/u.exec(key) : null
66
83
  const job = Boolean(m && /^[^/]+\/[^/]+\/\.github\/workflows\/[^/@]+\.ya?ml@/u.test(m[1]))
67
84
  const read = m ? readUses(m[1], { job, comment: m[2] ? `# ${m[2]}` : null }) : null
68
- return read && (read.kind === 'action' || read.kind === 'workflow') && usesKey(read) === key ? read : null
85
+ const each = read?.kind === 'action' && read.pin !== 'sha' && m[2] === 'checkout' ? { ...read, comment: null, checkout: true } : read
86
+ return each && (each.kind === 'action' || each.kind === 'workflow') && usesKey(each) === key ? each : null
69
87
  }
70
88
 
71
89
  // GitHub's own, the workflow's owner's, or another's.
@@ -13,11 +13,79 @@ function lineOf(file, container, key, fallback) {
13
13
  return line === undefined ? fallback : line + 1
14
14
  }
15
15
 
16
+ // The comment a line ends with, from 1.
17
+ const commentOn = (file, line) => /(?:^|[ \t])(#.*)$/u.exec(file.lines[line - 1] ?? '')?.[1] ?? null
18
+
16
19
  // A step's or a job's `uses`, on its line, with the comment that line ends with.
17
20
  function usesOf(file, owner, fallback, job = false) {
18
21
  if (typeof owner.uses !== 'string') return null
19
22
  const line = lineOf(file, owner, 'uses', fallback)
20
- return { ...readUses(owner.uses, { job, comment: /(?:^|[ \t])(#.*)$/u.exec(file.lines[line - 1] ?? '')?.[1] ?? null }), line }
23
+ return { ...readUses(owner.uses, { job, comment: commentOn(file, line) }), line }
24
+ }
25
+
26
+ // A path's parts from the workspace, null for one out of it.
27
+ function partsOf(path) {
28
+ const parts = path.split('/').filter(part => part !== '' && part !== '.')
29
+ return parts.includes('..') ? null : parts
30
+ }
31
+
32
+ const hasExpression = text => /\$\{\{/u.test(text)
33
+ const OWN_REPOSITORY = /^\$\{\{\s*github\.repository\s*\}\}$/u
34
+ const inputText = value => value === undefined || value === null ? '' : String(value).trim()
35
+
36
+ // A checkout's ref as a reference names it: a commit in either case; HEAD,
37
+ // the default branch, for none; `refs/heads/…` and `refs/tags/…`, a branch
38
+ // or a tag alone; else one checkout looks for as a branch before a tag
39
+ // (`checkout`), not a tag first as a `uses:` is.
40
+ function checkoutRef(written) {
41
+ if (written === '') return { ref: 'HEAD' }
42
+ if (/^[\da-f]{40}$/iu.test(written)) return { ref: written.toLowerCase() }
43
+ const qualified = /^refs\/(heads|tags)\/(.+)$/iu.exec(written)
44
+ if (qualified) return { ref: `refs/${qualified[1].toLowerCase()}/${qualified[2]}` }
45
+ return /^refs\//iu.test(written) ? { ref: written } : { ref: written, branchFirst: true }
46
+ }
47
+
48
+ // What a step checks out (actions/checkout) and where (`at`, the path's
49
+ // parts): the repository's own (`own`); another's `repository` at its ref
50
+ // (checkoutRef), with the comment its ref's line ends with; or, where an
51
+ // expression names which, `unknown`. Null for no checkout, or one to a
52
+ // path an expression names.
53
+ function checkoutOf(file, step, uses) {
54
+ if (uses?.kind !== 'action' || uses.repo.toLowerCase() !== 'actions/checkout') return null
55
+ const inputs = isMap(step.with) ? step.with : {}
56
+ const path = inputText(inputs.path)
57
+ const at = hasExpression(path) ? null : partsOf(path)
58
+ if (!at) return null
59
+ const repository = inputText(inputs.repository), written = inputText(inputs.ref)
60
+ if (repository === '' || OWN_REPOSITORY.test(repository)) return { at, own: true }
61
+ if (hasExpression(repository) || hasExpression(written)) return { at, unknown: true }
62
+ return { at, repository, ...checkoutRef(written), comment: inputs.ref === undefined ? null : commentOn(file, lineOf(file, inputs, 'ref', null)) }
63
+ }
64
+
65
+ // A step's condition: null where it always runs (none, or true), false
66
+ // where it never does, else its expression's text.
67
+ function conditionOf(step) {
68
+ if (step.if === undefined || step.if === null || step.if === true) return null
69
+ const text = step.if === false ? 'false' : String(step.if).trim().replace(/^\$\{\{([^]*)\}\}$/u, '$1').trim()
70
+ if (text === '' || text === 'true') return null
71
+ return text === 'false' ? false : text
72
+ }
73
+
74
+ // A step's `./` action, where a checkout before it in its job is at its
75
+ // path, the latest's that runs whenever the step does (none's condition,
76
+ // or the step's own): another repository's, as a `uses:` naming it at its
77
+ // ref; the repository's own, from the path it is checked out to; as
78
+ // written where an expression names which.
79
+ function checkedOut(uses, checkouts, when) {
80
+ if (uses?.kind !== 'local' || uses.self) return uses
81
+ const dir = partsOf(uses.path)
82
+ const checkout = dir && checkouts.findLast(({ at, ...each }) => (each.when === null || each.when === when) && at.every((part, index) => dir[index] === part))
83
+ if (!checkout || checkout.unknown || (checkout.own && checkout.at.length === 0)) return uses
84
+ const rest = dir.slice(checkout.at.length).join('/')
85
+ if (checkout.own) return { ...uses, path: rest }
86
+ const read = readUses(`${checkout.repository}${rest ? `/${rest}` : ''}@${checkout.ref}`, { comment: checkout.comment })
87
+ if (read.kind !== 'action') return uses
88
+ return { ...read, ...checkout.branchFirst && read.pin !== 'sha' && { checkout: true }, line: uses.line }
21
89
  }
22
90
 
23
91
  function triggersOf(on) {
@@ -48,12 +116,17 @@ function inputsOf(file, owner, line) {
48
116
  return { with: inputs, withLines: inputs ? Object.fromEntries(Object.keys(inputs).map(key => [key, lineOf(file, inputs, key, line)])) : {} }
49
117
  }
50
118
 
119
+ // A job's steps, each `./` action as the checkouts before it that run have it.
51
120
  function stepsOf(file, steps, directory, env = {}) {
52
121
  if (!Array.isArray(steps)) return []
122
+ const checkouts = []
53
123
  return steps.flatMap((step, index) => {
54
124
  if (!isMap(step)) return []
55
125
  const line = lineOf(file, steps, index, null)
56
- return [{ line, uses: usesOf(file, step, line), run: runOf(file, step, directory, env), ...inputsOf(file, step, line) }]
126
+ const uses = usesOf(file, step, line), when = conditionOf(step)
127
+ const checkout = when === false ? null : checkoutOf(file, step, uses)
128
+ if (checkout) checkouts.push({ ...checkout, when })
129
+ return [{ line, uses: checkedOut(uses, checkouts, when), run: runOf(file, step, directory, env), ...inputsOf(file, step, line) }]
57
130
  })
58
131
  }
59
132
 
@@ -14,6 +14,17 @@
14
14
  export const CONTROLS = /(?![\t\n\r])\p{Cc}|[\u202A-\u202E\u2066-\u2069]/gu
15
15
  const NON_ASCII = /\P{ASCII}/u
16
16
 
17
+ // What shows as nothing, or as another character, where a viewer draws it as
18
+ // text: the controls but tab and line feed (C0, DEL, C1), the format
19
+ // characters (Cf: zero-width ones, the bidi controls, tags), lone surrogates
20
+ // (Cs; `u` reads a pair as the one character it spells), private use (Co) and
21
+ // unassigned (Cn) code points, the line and paragraph separators, spaces other
22
+ // than a space, the default-ignorable characters and the Braille blank. A code
23
+ // viewer boxes each (ui/view/control-chars.js), and a source map's listing
24
+ // writes a line holding one as JSON (common/sourcemap-listing.js).
25
+ // eslint-disable-next-line no-control-regex
26
+ export const UNSEEN = /[\u0000-\u0008\u000B-\u001F\u007F-\u009F\u00A0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u2800\u3000\p{Cf}\p{Cs}\p{Co}\p{Cn}\p{Default_Ignorable_Code_Point}]/u
27
+
17
28
  // A file's text: `kind` 'ascii' or 'utf8', by whether it holds anything past
18
29
  // ASCII, else 'binary' (which the server tells by its bytes, sending no
19
30
  // text); `controls` the control and invisible characters a text holds, how
@@ -6,14 +6,14 @@
6
6
  // IP lookup), as a file's stamps say (stamps.js).
7
7
  //
8
8
  // A quick read, not a parse, as runs-code.js reads: a host is a URL's in a
9
- // script's strings, regular expressions and template literals
10
- // (JS_STRING_OR_COMMENT), its comments aside, where a link to docs is no host
9
+ // script's strings, regular expressions and template literals (script.js
10
+ // jsMatchAll), its comments aside, where a link to docs is no host
11
11
  // the code calls; an exfiltration host is told in any text but prose and a
12
12
  // source map, wherever it is written. A host built as the code runs
13
13
  // (`'https://' + name`, `${host}`) goes unseen, so a version without these
14
14
  // labels is not one that sends nothing.
15
15
  import { NPM_PROSE, SOURCE_MAP } from './readability.js'
16
- import { JS_STRING_OR_COMMENT, NPM_MAX_FOUND, lineOf, npmIsScript } from './script.js'
16
+ import { NPM_MAX_FOUND, jsMatchAll, lineOf, npmIsScript } from './script.js'
17
17
 
18
18
  // How many hosts a file is told naming at most, the first it names.
19
19
  const NPM_MAX_HOSTS = 1000
@@ -86,7 +86,7 @@ function githubNamed(url) {
86
86
  export function npmHosts(path, text, script = false) {
87
87
  if (!npmIsScript(path, text, script) || !(text.includes('://') || text.includes(':\\/\\/'))) return null
88
88
  const hosts = new Map(), line = lineOf(text)
89
- for (const { 1: token, index } of text.matchAll(JS_STRING_OR_COMMENT)) {
89
+ for (const { 1: token, index } of jsMatchAll(text)) {
90
90
  if (hosts.size >= NPM_MAX_HOSTS) break
91
91
  // Read unescaped, its offsets as near: on the same line but in a template
92
92
  // across lines; a regular expression without its closing slash and flags,
@@ -58,8 +58,11 @@ export const npmLargeCode = (path, bytes, script = false) => typeof bytes === 'n
58
58
  // no one can read it in review, yet what reads it by its byte order mark can
59
59
  // run it, or type with it, as UTF-16 (a browser's classic script, Windows
60
60
  // Script Host, TypeScript), and node runs it decoded as UTF-8 whatever it
61
- // holds past its code (`//` and a byte no UTF-8 has).
62
- export const npmBinaryCode = (path, readability, script = false) => readability?.category === 'binary' && (script || CODE.test(path))
61
+ // holds past its code (`//` and a byte no UTF-8 has). `npmCodePath`: whether
62
+ // a path names JavaScript or TypeScript by its extension, by which Compare
63
+ // tells such a file that isn't text from a binary one.
64
+ export const npmCodePath = path => CODE.test(path)
65
+ export const npmBinaryCode = (path, readability, script = false) => readability?.category === 'binary' && (script || npmCodePath(path))
63
66
 
64
67
  // A file with a line over UNREADABLE_LINE characters, by how it reads
65
68
  // (`now`, npmFileReadability's), `path` its path: an inline source map's line
@@ -30,7 +30,7 @@
30
30
  // required by a name computed, a path or URL put together, the
31
31
  // registry's host read from config), so a file without a label is not one
32
32
  // without them.
33
- import { JS_STRING_OR_COMMENT, NPM_MAX_FOUND, lineOf, npmIsScript } from './script.js'
33
+ import { NPM_MAX_FOUND, jsMatchAll, lineOf, npmIsScript } from './script.js'
34
34
 
35
35
  // What may come between a call's parts: blanks and comments
36
36
  // (`Function/* note */(…)`), each comment ending at its first `*/`.
@@ -128,8 +128,9 @@ const TOKEN_NAME = String.raw`(?=NPM|GITHUB)(?<![\p{ID_Continue}$])(?<!\b(?:cons
128
128
  // object (`const { eval: run } = globalThis`, `({ a, eval: run } = window)`).
129
129
  const KEYED = String.raw`(?<=[{,;]${GAP}eval)\s*:(?!${GAP}(?:true|!0)(?![\p{ID_Continue}$]))`
130
130
  + String.raw`(?![^{};]{0,200}\}${GAP}=${GAP}(?:${GLOBAL})(?![\p{ID_Continue}$]))`
131
- // A script's strings, regular expressions and comments, as script.js
132
- // tells them, each taking its own text, and its code in none of them: eval,
131
+ // What a script's code does, read beside its strings, regular expressions and
132
+ // comments as script.js's jsMatchAll tells them, each taking its own text,
133
+ // and in none of them: eval,
133
134
  // not a property TypeScript types as optional (`eval?: T`, JavaScript's `?:`
134
135
  // none of its own) nor a key (KEYED), and Function called, but to find the
135
136
  // global object;
@@ -138,8 +139,8 @@ const KEYED = String.raw`(?<=[{,;]${GAP}eval)\s*:(?!${GAP}(?:true|!0)(?![\p{ID_C
138
139
  // = …`), not named in a type (`typeof Module.prototype._compile`); a token's
139
140
  // name, its value read; the environment read out whole; and a PUT request's
140
141
  // call (`axios.put(…)`).
141
- const READS = new RegExp(String.raw`${JS_STRING_OR_COMMENT.source}|(?<eval>${named('eval')}(?![\p{ID_Continue}$])(?!\s*\?:)(?!${KEYED}))|(?<function>${named('Function')}${FUNCTION_CALLED}${GLOBAL_FOUND})`
142
- + String.raw`|(?<compile>\.${GAP}_compile(?:${FUNCTION_CALLED}|${GAP}=(?![=>])))|(?<tokens>${TOKEN_NAME})|(?<env>${ENV_WHOLE})|(?<put>\.${GAP}put${GAP}\()`, 'gmu')
142
+ const READS = String.raw`(?<eval>${named('eval')}(?![\p{ID_Continue}$])(?!\s*\?:)(?!${KEYED}))|(?<function>${named('Function')}${FUNCTION_CALLED}${GLOBAL_FOUND})`
143
+ + String.raw`|(?<compile>\.${GAP}_compile(?:${FUNCTION_CALLED}|${GAP}=(?![=>])))|(?<tokens>${TOKEN_NAME})|(?<env>${ENV_WHOLE})|(?<put>\.${GAP}put${GAP}\()`
143
144
  // What a string holds that a kind is: a credential file's path, npm's, SSH's
144
145
  // or AWS's (`~/.npmrc`, `.ssh/id_rsa`, `.aws/credentials`, not `x.npmrc` or
145
146
  // `.sshd`); a token's name where it is read, the string the name alone (a
@@ -456,7 +457,7 @@ function read(code, base, at) {
456
457
  // ends; whether the strings since a program's name are its arguments, an
457
458
  // option or its value each, and whether the last is an option.
458
459
  let before = null, command = null, end = 0, option = false, program = false
459
- for (const { 0: token, 1: string, groups, index } of code.matchAll(READS)) {
460
+ for (const { 0: token, 1: string, groups, index } of jsMatchAll(code, READS)) {
460
461
  if (string === undefined) {
461
462
  for (const kind of CODE_KINDS) if (groups[kind]) at(kind, base + index)
462
463
  continue
@@ -552,29 +553,15 @@ function substitutions(template) {
552
553
  return found
553
554
  }
554
555
 
555
- // A string, regular expression or comment where it starts, told as
556
- // JS_STRING_OR_COMMENT tells them in a whole text.
557
- const TOKEN = new RegExp(JS_STRING_OR_COMMENT.source, 'muy')
558
-
559
556
  // Where the `}` closing code from `from` in a text is, or the text's end for
560
557
  // none: the first its braces leave unbalanced, those in its strings, regular
561
- // expressions and comments aside (`${/* } */ x}`, `${/}/.test(x)}`).
558
+ // expressions and comments aside (`${/* } */ x}`, `${/}/.test(x)}`), read as
559
+ // script.js reads them.
562
560
  function closing(text, from) {
563
561
  let depth = 0
564
- for (let at = from; at < text.length; at++) {
565
- const char = text[at]
566
- // Tried where a token can start: a quote, a slash, or a run of blanks, a
567
- // comment's leading blanks taken from where they start.
568
- if ('"\'`/ \t'.includes(char)) {
569
- TOKEN.lastIndex = at
570
- const token = TOKEN.exec(text)
571
- if (token) {
572
- at += token[0].length - 1
573
- continue
574
- }
575
- }
576
- if (char === '{') depth++
577
- else if (char === '}' && depth-- === 0) return at
562
+ for (const { 0: token, index } of jsMatchAll(text, '[{}]', from)) {
563
+ if (token === '{') depth++
564
+ else if (token === '}' && depth-- === 0) return index
578
565
  }
579
566
  return text.length
580
567
  }