@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
@@ -132,6 +132,10 @@ export interface ReportRecord {
132
132
  analyzer: string | null
133
133
  visible: boolean
134
134
  bundleId: string | null
135
+ // Whether it may be switched to open on Confirmed (null: not read yet), and
136
+ // whether it is (report-opening.ts).
137
+ confirmedOffered: boolean | null
138
+ confirmedDefault: boolean
135
139
  }
136
140
 
137
141
  // One transaction snapshots all metadata needed to authorize, filter and
@@ -143,6 +147,8 @@ export interface ReportAccessRecord {
143
147
  sha256: string
144
148
  repo: { github: string | null; directory: string }
145
149
  permissions: TeamUserPermissions
150
+ // Set where a team snapshot reads a report switched to open on Confirmed.
151
+ confirmedDefault?: true
146
152
  }
147
153
  export interface TeamReportAccessSnapshot extends ReportAccessSnapshot {
148
154
  teamId: string | null
@@ -179,6 +185,8 @@ export interface ReportRecordInput {
179
185
  visible: boolean
180
186
  bundleId: string | null
181
187
  bundleIntegrity: string | null
188
+ // Read of its content as it is uploaded (report-opening.ts); none: not yet.
189
+ confirmedOffered?: boolean | null
182
190
  }
183
191
 
184
192
  // A report row for the "Manage reports" list — adds display joins: uploader
@@ -202,6 +210,8 @@ export interface AdminReport {
202
210
  bundleFilename: string | null
203
211
  bundleIntegrity: string | null
204
212
  uploadedAt: number
213
+ confirmedOffered: boolean | null
214
+ confirmedDefault: boolean
205
215
  }
206
216
 
207
217
  export interface RepoDataItem {
@@ -471,6 +481,12 @@ export interface ManagedDb extends DependencyReviewStore, RepositoryDependencyRe
471
481
  // directory path.
472
482
  setReportRepo(id: string, repoId: number | null, repoDirectory?: string): Promise<boolean>
473
483
  setReportVisible(id: string, visible: boolean): Promise<boolean>
484
+ // A manager's switch of a report to open on Confirmed, resolving true iff it
485
+ // changed; and, of those not read for it yet, whether each may be
486
+ // (report-opening.ts), all in one write, any another read already told
487
+ // left as it is.
488
+ setReportConfirmedDefault(id: string, on: boolean): Promise<boolean>
489
+ setReportsConfirmedOffered(read: readonly { id: string; offered: boolean }[]): Promise<void>
474
490
  // Per-finding triage annotations, keyed by finding id alone (shared by every
475
491
  // report carrying the finding). listTriage reads the rows for a set of ids —
476
492
  // the endpoint passes a viewer's visible findings of one report; a cleared
@@ -611,7 +627,7 @@ const USER_TEAM_SCOPES_SQL = (user: string) => `SELECT tr.team_id AS teamId, tr.
611
627
  // shows under every team whose repo it's attached to).
612
628
  const USER_TEAM_REPORTS_SQL = (user: string) => `SELECT DISTINCT tr.team_id AS teamId, r.id AS id, r.slug AS slug, r.filename AS filename, r.analyzer, r.visible, r.uploaded_at,
613
629
  r.sha256 AS sha256, r.bundle_id AS bundleId, r.repo_id AS repoId, r.repo_directory AS repoDirectory, sr.full_name AS repoFullName,
614
- tu.view_dependencies AS dependencies, tu.view_security AS security,
630
+ r.confirmed_default AS confirmedDefault, tu.view_dependencies AS dependencies, tu.view_security AS security,
615
631
  (SELECT role FROM managed_user WHERE id = tu.user_id) AS role
616
632
  FROM managed_team_user tu
617
633
  JOIN managed_team t ON t.id = tu.team_id AND t.hidden = 0
@@ -644,18 +660,20 @@ type UserTeamRows = {
644
660
  scopes: { teamId: string; repoId: number; path: string; fullName: string }[]
645
661
  reports: {
646
662
  teamId: string; id: string; slug: string; filename: string; analyzer: string | null; sha256: string; bundleId: string | null; repoId: number
647
- repoDirectory: string; repoFullName: string | null; visible: number; dependencies: number; security: number; role: string
663
+ repoDirectory: string; repoFullName: string | null; visible: number; confirmedDefault: number; dependencies: number; security: number; role: string
648
664
  }[]
649
665
  bundles: (Omit<UserTeamBundle, 'visible'> & { teamId: string; visible: number })[]
650
666
  }
651
667
 
652
- // A catalog report's content key: its content, where it is, and what its
653
- // reader may see of it (their grants and role) under the link reports'
654
- // revision `links`. A team snapshot names it of each report it reads.
655
- type CatalogReportRow = Pick<UserTeamRows['reports'][number], 'sha256' | 'bundleId' | 'filename' | 'repoId' | 'repoDirectory' | 'repoFullName' | 'dependencies' | 'security'>
668
+ // A catalog report's content key: its content, where it is, whether it opens
669
+ // on Confirmed, and what its reader may see of it (their grants and role)
670
+ // under the link reports' revision `links`. A team snapshot names it of each
671
+ // report it reads.
672
+ type CatalogReportRow = Pick<UserTeamRows['reports'][number], 'sha256' | 'bundleId' | 'filename' | 'repoId' | 'repoDirectory' | 'repoFullName' | 'confirmedDefault' | 'dependencies' | 'security'>
656
673
  function reportCatalogKey(r: CatalogReportRow, role: string, links: string): string {
657
674
  return createHash('sha256').update(JSON.stringify([
658
675
  r.sha256, r.bundleId, r.filename, r.repoId, r.repoDirectory, r.repoFullName, r.dependencies, r.security, role, links,
676
+ ...r.confirmedDefault === 1 ? ['confirmed'] : [],
659
677
  ])).digest('base64url')
660
678
  }
661
679
 
@@ -845,9 +863,9 @@ function prepareStatements(db: ManagedSql) {
845
863
  selectAllReposStmt: db.prepare(`SELECT ${REPO_COLUMNS} FROM managed_selected_repo ORDER BY full_name ASC`),
846
864
  insertReportStmt: db.prepare(
847
865
  `WITH candidate(id, slug) AS (VALUES (?, ?))
848
- INSERT INTO managed_report (id, slug, filename, content_type, byte_size, sha256, uploaded_by, uploaded_by_login, repo_id, repo_directory, repo_embedded, analyzer, visible, bundle_id, bundle_integrity, uploaded_at, data_key, storage_encrypted)
866
+ INSERT INTO managed_report (id, slug, filename, content_type, byte_size, sha256, uploaded_by, uploaded_by_login, repo_id, repo_directory, repo_embedded, analyzer, visible, bundle_id, bundle_integrity, uploaded_at, data_key, storage_encrypted, confirmed_offered)
849
867
  SELECT id, CASE WHEN EXISTS (SELECT 1 FROM managed_report WHERE slug = candidate.slug) THEN id ELSE slug END,
850
- ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? FROM candidate`,
868
+ ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? FROM candidate`,
851
869
  ),
852
870
  // LEFT JOINs so a report whose uploader / repo / bundle was removed (the FK
853
871
  // nulled) still lists, with null display fields.
@@ -858,7 +876,7 @@ function prepareStatements(db: ManagedSql) {
858
876
  r.repo_directory AS repoDirectory, r.repo_embedded AS repoEmbedded,
859
877
  r.analyzer AS analyzer, r.visible AS visible,
860
878
  r.bundle_id AS bundleId, b.filename AS bundleFilename, r.bundle_integrity AS bundleIntegrity,
861
- r.uploaded_at AS uploadedAt
879
+ r.uploaded_at AS uploadedAt, r.confirmed_offered AS confirmedOffered, r.confirmed_default AS confirmedDefault
862
880
  FROM managed_report r
863
881
  LEFT JOIN managed_user u ON u.id = r.uploaded_by
864
882
  LEFT JOIN managed_selected_repo sr ON sr.repo_id = r.repo_id
@@ -871,7 +889,8 @@ function prepareStatements(db: ManagedSql) {
871
889
  `SELECT id, slug, filename, content_type AS contentType, byte_size AS byteSize,
872
890
  sha256, uploaded_by AS uploadedBy, uploaded_at AS uploadedAt,
873
891
  repo_id AS repoId, repo_directory AS repoDirectory, repo_embedded AS repoEmbedded,
874
- analyzer AS analyzer, visible AS visible, bundle_id AS bundleId
892
+ analyzer AS analyzer, visible AS visible, bundle_id AS bundleId,
893
+ confirmed_offered AS confirmedOffered, confirmed_default AS confirmedDefault
875
894
  FROM managed_report WHERE id = ?`,
876
895
  ),
877
896
  selectReportByHashStmt: db.prepare(
@@ -917,9 +936,10 @@ function prepareStatements(db: ManagedSql) {
917
936
  WHERE tu.user_id = v.uid AND tu.team_id = ? AND t.hidden = 0) AS member,
918
937
  (SELECT json_group_array(json_object('id', x.id, 'filename', x.filename, 'byteSize', x.byteSize, 'sha256', x.sha256,
919
938
  'visible', x.visible, 'bundleId', x.bundleId, 'repoId', x.repoId, 'repoDirectory', x.repoDirectory, 'repoFullName', x.repoFullName,
920
- 'dependencies', x.dependencies, 'security', x.security) ORDER BY x.id)
939
+ 'confirmedDefault', x.confirmedDefault, 'dependencies', x.dependencies, 'security', x.security) ORDER BY x.id)
921
940
  FROM (SELECT DISTINCT r.id AS id, r.filename AS filename, r.byte_size AS byteSize, r.sha256 AS sha256, r.visible AS visible,
922
941
  r.bundle_id AS bundleId, r.repo_id AS repoId, r.repo_directory AS repoDirectory, sr.full_name AS repoFullName,
942
+ r.confirmed_default AS confirmedDefault,
923
943
  tu.view_dependencies AS dependencies, tu.view_security AS security
924
944
  FROM managed_team_user tu
925
945
  JOIN managed_team t ON t.id = tu.team_id AND t.hidden = 0
@@ -943,6 +963,9 @@ function prepareStatements(db: ManagedSql) {
943
963
  deleteReportStmt: db.prepare(`DELETE FROM managed_report WHERE id = ?`),
944
964
  setReportRepoStmt: db.prepare(`UPDATE managed_report SET repo_id = ?, repo_directory = ? WHERE id = ?`),
945
965
  setReportVisibleStmt: db.prepare(`UPDATE managed_report SET visible = ? WHERE id = ?`),
966
+ setReportConfirmedDefaultStmt: db.prepare(`UPDATE managed_report SET confirmed_default = ? WHERE id = ? AND confirmed_default <> ?`),
967
+ setReportsConfirmedOfferedStmt: db.prepare(`UPDATE managed_report SET confirmed_offered = ?
968
+ WHERE confirmed_offered IS NULL AND id IN (SELECT value FROM json_each(?))`),
946
969
  upsertTriageStmt: db.prepare(
947
970
  `INSERT INTO managed_finding_triage (finding_id, color, triage, comment, fix, flagged, updated_by, updated_by_login, updated_at)
948
971
  VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
@@ -1117,7 +1140,7 @@ function prepareStatements(db: ManagedSql) {
1117
1140
  ${ENABLED_LINKS_SQL} AS links,
1118
1141
  (SELECT json_group_array(json_object('teamId', x.teamId, 'id', x.id, 'slug', x.slug, 'filename', x.filename, 'analyzer', x.analyzer,
1119
1142
  'visible', x.visible, 'sha256', x.sha256, 'bundleId', x.bundleId, 'repoId', x.repoId, 'repoDirectory', x.repoDirectory,
1120
- 'repoFullName', x.repoFullName, 'dependencies', x.dependencies, 'security', x.security, 'role', x.role)
1143
+ 'repoFullName', x.repoFullName, 'confirmedDefault', x.confirmedDefault, 'dependencies', x.dependencies, 'security', x.security, 'role', x.role)
1121
1144
  ORDER BY x.uploaded_at DESC, x.filename ASC, x.id ASC)
1122
1145
  FROM (${USER_TEAM_REPORTS_SQL('v.id')}) AS x) AS reports,
1123
1146
  (SELECT json_group_array(json_object('teamId', x.teamId, 'id', x.id, 'slug', x.slug, 'integrity', x.integrity, 'filename', x.filename,
@@ -1265,24 +1288,28 @@ type ReportListRow = {
1265
1288
  repoId: number | null; repoFullName: string | null
1266
1289
  repoDirectory: string; repoEmbedded: number; analyzer: string | null; visible: number
1267
1290
  bundleId: string | null; bundleFilename: string | null; bundleIntegrity: string | null
1268
- uploadedAt: number
1291
+ uploadedAt: number; confirmedOffered: number | null; confirmedDefault: number
1269
1292
  }
1270
1293
  type ReportRow = {
1271
1294
  id: string; slug: string; filename: string; contentType: string; byteSize: number
1272
1295
  sha256: string; uploadedBy: string | null; uploadedAt: number
1273
1296
  repoId: number | null; repoDirectory: string; repoEmbedded: number; analyzer: string | null; visible: number
1274
- bundleId: string | null
1297
+ bundleId: string | null; confirmedOffered: number | null; confirmedDefault: number
1275
1298
  }
1299
+ // Its opening's columns as a report gives them.
1300
+ const reportOpening = (row: { confirmedOffered: number | null; confirmedDefault: number }) =>
1301
+ ({ confirmedOffered: row.confirmedOffered == null ? null : row.confirmedOffered === 1, confirmedDefault: row.confirmedDefault === 1 })
1276
1302
 
1277
1303
  // A report as an access snapshot read it, and as the snapshot gives it: with
1278
1304
  // every permission where the reader's role sees `whole` reports.
1279
1305
  type AccessRow = Pick<ReportAccessRecord, 'id' | 'filename' | 'byteSize' | 'sha256'> & {
1280
- repoDirectory: string; repoFullName: string | null; dependencies: number; security: number
1306
+ repoDirectory: string; repoFullName: string | null; dependencies: number; security: number; confirmedDefault?: number
1281
1307
  }
1282
1308
  const accessRecord = (row: AccessRow, whole: boolean): ReportAccessRecord => ({
1283
1309
  id: row.id, filename: row.filename, byteSize: row.byteSize, sha256: row.sha256,
1284
1310
  repo: { github: row.repoFullName, directory: row.repoDirectory },
1285
1311
  permissions: { dependencies: whole || row.dependencies === 1, security: whole || row.security === 1 },
1312
+ ...row.confirmedDefault === 1 ? { confirmedDefault: true as const } : {},
1286
1313
  })
1287
1314
 
1288
1315
  // Byte storage may outlast a logout or grant change. HTTP uploads reauthorize
@@ -1304,7 +1331,7 @@ function reportMethods(stmts: ReturnType<typeof prepareStatements>, db: ManagedS
1304
1331
  async function getReport(id: string): Promise<ReportRecord | null> {
1305
1332
  const row = (await selectReportStmt.get(id)) as ReportRow | undefined
1306
1333
  if (row == null) return null
1307
- return { ...row, repoEmbedded: row.repoEmbedded === 1, visible: row.visible === 1 }
1334
+ return { ...row, repoEmbedded: row.repoEmbedded === 1, visible: row.visible === 1, ...reportOpening(row) }
1308
1335
  }
1309
1336
  async function getReportByHash(sha256: string, analyzer: string | null): Promise<ReportRecord | null> {
1310
1337
  const row = await stmts.selectReportByHashStmt.get(sha256, analyzer) as { id: string } | undefined
@@ -1317,6 +1344,7 @@ function reportMethods(stmts: ReturnType<typeof prepareStatements>, db: ManagedS
1317
1344
  report.sha256, report.uploadedBy, report.uploadedByLogin ?? null, report.repoId,
1318
1345
  report.repoDirectory ?? '', report.repoEmbedded ? 1 : 0, report.analyzer ?? null, report.visible ? 1 : 0, report.bundleId ?? null,
1319
1346
  report.bundleIntegrity ?? null, now, report.dataKey ?? null, report.dataKey ? 1 : 0,
1347
+ report.confirmedOffered == null ? null : report.confirmedOffered ? 1 : 0,
1320
1348
  )
1321
1349
  }
1322
1350
  return {
@@ -1335,7 +1363,7 @@ function reportMethods(stmts: ReturnType<typeof prepareStatements>, db: ManagedS
1335
1363
  },
1336
1364
  async listReports(userId?: string): Promise<AdminReport[]> {
1337
1365
  const rows = (await selectReportsStmt.all(...asManager(userId))) as ReportListRow[]
1338
- return rows.map(r => ({ ...r, repoEmbedded: r.repoEmbedded === 1, visible: r.visible === 1 }))
1366
+ return rows.map(r => ({ ...r, repoEmbedded: r.repoEmbedded === 1, visible: r.visible === 1, ...reportOpening(r) }))
1339
1367
  },
1340
1368
  async listFindingCatalogReports(after: string, limit: number): Promise<Pick<ReportRecord, 'id' | 'byteSize'>[]> {
1341
1369
  return await stmts.findingCatalogReportsStmt.all(after, limit) as Pick<ReportRecord, 'id' | 'byteSize'>[]
@@ -1387,6 +1415,15 @@ function reportMethods(stmts: ReturnType<typeof prepareStatements>, db: ManagedS
1387
1415
  async setReportVisible(id: string, visible: boolean): Promise<boolean> {
1388
1416
  return anyChanged(await setReportVisibleStmt.run(visible ? 1 : 0, id))
1389
1417
  },
1418
+ async setReportConfirmedDefault(id: string, on: boolean): Promise<boolean> {
1419
+ return anyChanged(await stmts.setReportConfirmedDefaultStmt.run(on ? 1 : 0, id, on ? 1 : 0))
1420
+ },
1421
+ async setReportsConfirmedOffered(read: readonly { id: string; offered: boolean }[]): Promise<void> {
1422
+ for (const offered of [true, false]) {
1423
+ const ids = read.filter(entry => entry.offered === offered).map(entry => entry.id)
1424
+ if (ids.length > 0) await stmts.setReportsConfirmedOfferedStmt.run(offered ? 1 : 0, JSON.stringify(ids))
1425
+ }
1426
+ },
1390
1427
  }
1391
1428
  }
1392
1429
 
@@ -8,7 +8,7 @@ import { AsyncLocalStorage } from 'node:async_hooks'
8
8
  import { WebSocket } from 'ws'
9
9
  import { type ManagedDb, type ManagedDbOptions, createManagedMethods } from './db-methods.ts'
10
10
  import { revisionSchema } from './revisions.ts'
11
- import { MANAGED_SCHEMA, REPO_UPLOAD_INDEXES, VIEW_SESSION_INDEX } from './db-schema.ts'
11
+ import { MANAGED_SCHEMA, REPORT_OPENING_COLUMNS, REPO_UPLOAD_INDEXES, VIEW_SESSION_INDEX } from './db-schema.ts'
12
12
  import { STORAGE_SCHEMA } from './storage-db.ts'
13
13
  import { GITHUB_COMMIT_SCHEMA, GITHUB_METADATA_SCHEMA, GITHUB_STATE_REASON_COLUMN } from './github-metadata.ts'
14
14
  import { MANAGED_ISSUE_SCHEMA } from './managed-issues.ts'
@@ -40,7 +40,7 @@ const LOCK = 'SELECT pg_advisory_xact_lock(1937006964, 1835101793)'
40
40
  // Whether every version up to the latest is recorded, in one round trip at
41
41
  // a cold start: a database with no versions table yet (undefined_table) has
42
42
  // none. Outside a transaction, so the failed read aborts nothing.
43
- const SCHEMA_VERSIONS = 36
43
+ const SCHEMA_VERSIONS = 38
44
44
  async function currentSchema(db: PgConnection): Promise<boolean> {
45
45
  try {
46
46
  const { rows } = await db.query(`SELECT count(*) AS n FROM managed_schema_version WHERE version BETWEEN 1 AND ${SCHEMA_VERSIONS}`)
@@ -139,6 +139,13 @@ async function migrateFrom12(db: PgConnection): Promise<void> {
139
139
  // The patch a dependency review's base is of, none of those written
140
140
  // before; each author's one draft of a comparison held to it too.
141
141
  await migration(db, 36, `ALTER TABLE managed_dependency_review ${addColumns(DEPENDENCY_REVIEW_COLUMNS)}`, DEPENDENCY_REVIEW_INDEXES)
142
+ // Whether a report may open on Confirmed, existing ones read for it as the
143
+ // reports page lists them (report-opening.ts), and none switched yet.
144
+ await migration(db, 37, `ALTER TABLE managed_report ${addColumns(REPORT_OPENING_COLUMNS)}`)
145
+ // When a dependency review was last published to a team, none of those
146
+ // published before: a time in milliseconds, so BIGINT, as postgresSchema
147
+ // makes SQLite's INTEGER.
148
+ await migration(db, 38, postgresSchema(`ALTER TABLE managed_dependency_review ${addColumns(DEPENDENCY_REVIEW_COLUMNS)}`))
142
149
  }
143
150
 
144
151
  async function initialize(db: PgConnection): Promise<void> {
@@ -27,6 +27,9 @@ export const inUserTeamScope = (user: string, row: string) => `EXISTS (SELECT 1
27
27
  JOIN managed_team t ON t.id = tu.team_id AND t.hidden = 0
28
28
  WHERE tu.user_id = ${user} AND tr.repo_id = ${row}.repo_id AND ${withinTeamPath(`${row}.repo_directory`)})`
29
29
 
30
+ // A report's opening (report-opening.ts), added to databases created before it.
31
+ export const REPORT_OPENING_COLUMNS = [['confirmed_offered', 'INTEGER'], ['confirmed_default', 'INTEGER NOT NULL DEFAULT 0']] as const
32
+
30
33
  export const MANAGED_SCHEMA = `
31
34
  CREATE TABLE IF NOT EXISTS managed_user (
32
35
  id TEXT PRIMARY KEY,
@@ -133,7 +136,12 @@ CREATE INDEX IF NOT EXISTS managed_bundle_uploaded_at_idx ON managed_bundle(uplo
133
136
  -- links, nulled (not cascaded) when the user / selected repo goes away. bundle_id
134
137
  -- is the (nullable) auto-resolved link to a stored bundle; bundle_integrity is
135
138
  -- the report's declared primary bundle (from its bundleHashes), kept so a later
136
- -- bundle upload of that integrity can re-link.
139
+ -- bundle upload of that integrity can re-link. confirmed_offered is whether
140
+ -- the report, alone, may be switched to open on Confirmed (Manage → Reports,
141
+ -- report-opening.ts): 1 where its pass leaves Confirmed rows but it is no App
142
+ -- view on its own, 0 where the switch would change nothing, NULL where it is
143
+ -- yet to be read (one uploaded before the column).
144
+ -- confirmed_default is that switch, as a manager set it.
137
145
  CREATE TABLE IF NOT EXISTS managed_report (
138
146
  id TEXT PRIMARY KEY,
139
147
  slug TEXT NOT NULL,
@@ -151,7 +159,9 @@ CREATE TABLE IF NOT EXISTS managed_report (
151
159
  visible INTEGER NOT NULL DEFAULT 0,
152
160
  bundle_id TEXT REFERENCES managed_bundle(id) ON DELETE SET NULL,
153
161
  bundle_integrity TEXT,
154
- uploaded_at INTEGER NOT NULL
162
+ uploaded_at INTEGER NOT NULL,
163
+ confirmed_offered INTEGER,
164
+ confirmed_default INTEGER NOT NULL DEFAULT 0
155
165
  ) STRICT;
156
166
 
157
167
  CREATE INDEX IF NOT EXISTS managed_report_uploaded_at_idx ON managed_report(uploaded_at);
@@ -7,7 +7,7 @@ import { migrateReportLocations } from './report-migration.ts'
7
7
  import { initCommentMethods } from './comments.ts'
8
8
  import { initActivityMethods } from './activity.ts'
9
9
  import { revisionSchema } from './revisions.ts'
10
- import { MANAGED_SCHEMA, REPO_UPLOAD_INDEXES, VIEW_SESSION_INDEX } from './db-schema.ts'
10
+ import { MANAGED_SCHEMA, REPORT_OPENING_COLUMNS, REPO_UPLOAD_INDEXES, VIEW_SESSION_INDEX } from './db-schema.ts'
11
11
  import { GITHUB_LINK_USER_COLUMNS } from './github-links.ts'
12
12
  import { REPOSITORY_DEPENDENCY_REVIEW_COLUMNS } from './repository-dependency-reviews.ts'
13
13
  import { DEPENDENCY_REVIEW_COLUMNS, DEPENDENCY_REVIEW_INDEXES } from './dependency-reviews.ts'
@@ -108,6 +108,9 @@ export function openSqliteManagedDb(path: string, options: ManagedDbOptions = {}
108
108
  ensureColumn(db, 'managed_report', 'repo_embedded', 'INTEGER NOT NULL DEFAULT 0')
109
109
  migrateReportLocations(db, join(dirname(path), 'reports'))
110
110
  ensureColumn(db, 'managed_report', 'analyzer', 'TEXT')
111
+ // Existing reports are read for it as the reports page lists them
112
+ // (report-opening.ts), and none was switched yet.
113
+ for (const [column, type] of REPORT_OPENING_COLUMNS) ensureColumn(db, 'managed_report', column, type)
111
114
  // Existing rows predate publication controls and were already visible.
112
115
  // Backfill only when adding the column; newly inserted rows still use the
113
116
  // hidden default and must be explicitly published.
@@ -69,7 +69,7 @@ CREATE TABLE IF NOT EXISTS managed_dependency_review (
69
69
  time INTEGER NOT NULL, revision INTEGER NOT NULL, published_at INTEGER, revoked_at INTEGER, files TEXT,
70
70
  status TEXT CHECK (status IN ('accept', 'accept-with-nits', 'request-changes', 'reject')), comment TEXT NOT NULL DEFAULT '',
71
71
  shasum TEXT NOT NULL DEFAULT '', base_shasum TEXT NOT NULL DEFAULT '', patch_hash TEXT NOT NULL DEFAULT '', patch_source TEXT,
72
- base_patch_hash TEXT NOT NULL DEFAULT ''
72
+ base_patch_hash TEXT NOT NULL DEFAULT '', shared_at INTEGER
73
73
  ) STRICT;
74
74
  CREATE INDEX IF NOT EXISTS managed_dependency_review_context_idx
75
75
  ON managed_dependency_review(ecosystem, package, version, integrity, base_package, base_version, base_integrity, time);
@@ -88,8 +88,11 @@ CREATE TABLE IF NOT EXISTS managed_dependency_review_team (
88
88
  CREATE INDEX IF NOT EXISTS managed_dependency_review_team_idx ON managed_dependency_review_team(team_id, review_id);
89
89
  `
90
90
  // The columns added since the table was, for a database that has it already.
91
+ // `shared_at`: when it was last published to a team, its first or one more,
92
+ // as a team's Dependencies tab asks what changed since (repository-dependency-reviews.ts
93
+ // changedReviewVersions); none of one published before it was kept.
91
94
  export const DEPENDENCY_REVIEW_COLUMNS = [['shasum', "TEXT NOT NULL DEFAULT ''"], ['base_shasum', "TEXT NOT NULL DEFAULT ''"],
92
- ['patch_hash', "TEXT NOT NULL DEFAULT ''"], ['patch_source', 'TEXT'], ['base_patch_hash', "TEXT NOT NULL DEFAULT ''"]] as const
95
+ ['patch_hash', "TEXT NOT NULL DEFAULT ''"], ['patch_source', 'TEXT'], ['base_patch_hash', "TEXT NOT NULL DEFAULT ''"], ['shared_at', 'INTEGER']] as const
93
96
  // Each author's one draft of a comparison, its patches' too, made once those
94
97
  // columns are: those held to it before patches, then its base's, were reviewed gone.
95
98
  export const DEPENDENCY_REVIEW_INDEXES = `
@@ -202,12 +205,12 @@ async function publish(db: ManagedSql, current: DependencyReview | undefined, us
202
205
  if (!summarizeReview(entries, change.files, change.nonTextFiles).checkedFiles) throw new ManagedMutationError(400, 'no-reviewed-files')
203
206
  if (!change.final || !allowedReviewVerdicts(entries.filter(entry => !change.nonTextFiles.includes(entry.file))).includes(change.final.status)) throw new ManagedMutationError(400, 'bad-final-verdict')
204
207
  if (needsPackageJsonReview(entries, change.files, change.nonTextFiles)) throw new ManagedMutationError(400, 'package-json-not-reviewed')
205
- await db.prepare(`UPDATE managed_dependency_review SET published_at = ?, files = ?, status = ?, comment = ?, shasum = ?, base_shasum = ?, patch_source = ?, revision = revision + 1
206
- WHERE id = ?`).run(now, JSON.stringify({ paths: [...new Set(change.files)].toSorted(), nonTextFiles: [...new Set(change.nonTextFiles)].toSorted() }),
208
+ await db.prepare(`UPDATE managed_dependency_review SET published_at = ?, shared_at = ?, files = ?, status = ?, comment = ?, shasum = ?, base_shasum = ?, patch_source = ?,
209
+ revision = revision + 1 WHERE id = ?`).run(now, now, JSON.stringify({ paths: [...new Set(change.files)].toSorted(), nonTextFiles: [...new Set(change.nonTextFiles)].toSorted() }),
207
210
  change.final.status, change.final.comment, ...shasums(tarballs), current.id)
208
211
  } else {
209
212
  if (change.final) return conflict()
210
- await db.prepare('UPDATE managed_dependency_review SET revision = revision + 1 WHERE id = ?').run(current.id)
213
+ await db.prepare('UPDATE managed_dependency_review SET shared_at = ?, revision = revision + 1 WHERE id = ?').run(now, current.id)
211
214
  }
212
215
  for (const team of selected) await db.prepare('INSERT INTO managed_dependency_review_team (review_id, team_id) VALUES (?, ?) ON CONFLICT DO NOTHING').run(current.id, team)
213
216
  }
@@ -7,6 +7,9 @@ export class GithubApiError extends Error {
7
7
  retryAfter: number | null
8
8
  // What failed, where GitHub wasn't reached (githubUnreachable).
9
9
  detail: string | null
10
+ // The repository GitHub's redirect sends one asked of to, by its id, where
11
+ // it moved (githubMoved).
12
+ movedTo: number | null = null
10
13
  constructor(status: number, message: string, retryAfter: number | null = null, detail: string | null = null) {
11
14
  super(message)
12
15
  this.name = 'GithubApiError'
@@ -31,7 +34,25 @@ export function githubUnreachable(error: unknown, started: number): GithubApiErr
31
34
  return new GithubApiError(502, 'github-unreachable', null, `${failed}, after ${((Date.now() - started) / 1000).toFixed(1)}s`)
32
35
  }
33
36
 
37
+ // The statuses of a redirect, and where GitHub's API sends a repository
38
+ // renamed or transferred: to that repository by its id, then the rest of
39
+ // what was asked of it.
40
+ const REDIRECTS = new Set([301, 302, 303, 307, 308])
41
+ const REPOSITORY_BY_ID = /^https:\/\/api\.github\.com\/repositories\/([1-9]\d{0,14})(?:[/?]|$)/u
42
+
43
+ // A redirect GitHub answered, never followed, as it answers of a repository
44
+ // renamed or transferred since it was named so: `github-moved`, with the id of
45
+ // the repository it sends it to (`movedTo`), null where it names none.
46
+ function githubMoved(location: string | null | undefined): GithubApiError {
47
+ const error = new GithubApiError(502, 'github-moved')
48
+ const id = REPOSITORY_BY_ID.exec(location ?? '')?.[1]
49
+ error.movedTo = id === undefined ? null : Number(id)
50
+ return error
51
+ }
52
+ export const isGithubMoved = (error: unknown): error is GithubApiError => error instanceof GithubApiError && error.message === 'github-moved'
53
+
34
54
  export async function throwGithubResponseError(res: Response): Promise<never> {
55
+ if (REDIRECTS.has(res.status)) throw githubMoved(res.headers?.get('location'))
35
56
  if (res.status === 403 || res.status === 429) {
36
57
  const body = await res.json().catch(() => null) as { message?: unknown } | null
37
58
  if (res.status === 429 || res.headers.get('x-ratelimit-remaining') === '0' || res.headers.has('retry-after')
@@ -57,6 +78,7 @@ export async function upstreamGithub<T>(request: () => Promise<T>): Promise<T> {
57
78
  // status handling, including rate limits, without exposing that text.
58
79
  if (error.status === 401) throw new GithubApiError(401, 'github-unauthorized')
59
80
  if (error.status === 404) throw new GithubApiError(404, 'github-not-found')
81
+ if (REDIRECTS.has(error.status)) throw githubMoved((error as { headers?: Headers }).headers?.get('location'))
60
82
  if (error.status === 429 || (error.status === 403 && /rate limit|abuse detection/iu.test(error.message))) {
61
83
  throw new GithubApiError(429, 'github-rate-limited', 60)
62
84
  }
@@ -134,7 +134,9 @@ export const repoApi = (fullName: string) => `${GITHUB_API}/repos/${encodedPath(
134
134
  // share an outstanding read through its body; one GitHub answered with an
135
135
  // ETag is asked again If-None-Match, its body kept answering a 304
136
136
  // (taggedRead). POSTs only opt in for read-only GraphQL
137
- // queries (repository-history.ts), never for mutations or token mints.
137
+ // queries (repository-history.ts), never for mutations or token mints. A
138
+ // redirect is never followed: it fails as `github-moved` (github-errors.ts),
139
+ // as GitHub answers of a repository renamed or transferred.
138
140
  export function githubJson(url: string, token: string | null, fetchImpl: typeof fetch, method: 'GET' | 'POST' = 'GET', body?: unknown,
139
141
  readOnly = method === 'GET'): Promise<unknown> {
140
142
  if (readOnly) return shareGithubRead(fetchImpl, JSON.stringify([url, token, method, body]), () => githubJson(url, token, fetchImpl, method, body, false))
@@ -149,7 +151,7 @@ export function githubJson(url: string, token: string | null, fetchImpl: typeof
149
151
  res = await fetchImpl(url, {
150
152
  method,
151
153
  signal: AbortSignal.timeout(body === undefined ? 10_000 : 20_000),
152
- redirect: 'error',
154
+ redirect: 'manual',
153
155
  headers: {
154
156
  ...githubHeaders(token), ...(body === undefined ? {} : { 'content-type': 'application/json' }),
155
157
  ...(tagged ? { 'if-none-match': tagged.etag } : {}),