@preventive/triage 1.0.0-alpha.8 → 1.0.0-alpha.80

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 (442) hide show
  1. package/LICENSE-APACHE +202 -0
  2. package/README.md +16 -0
  3. package/api/reap.ts +16 -78
  4. package/cli.js +6 -0
  5. package/client/dependency-paths.d.ts +3 -0
  6. package/client/dependency-paths.js +36 -0
  7. package/client/finding-link.js +305 -0
  8. package/client/linked-findings.d.ts +1 -0
  9. package/client/linked-findings.js +115 -0
  10. package/common/bundle-commit.d.ts +1 -0
  11. package/common/bundle-commit.js +4 -0
  12. package/common/bundle-compare-diff.d.ts +6 -0
  13. package/common/bundle-compare-diff.js +377 -0
  14. package/common/bundle-compare-inputs.d.ts +3 -0
  15. package/common/bundle-compare-inputs.js +141 -0
  16. package/common/bundle-compare-versions.js +172 -0
  17. package/common/bundle-comparison.d.ts +8 -0
  18. package/common/bundle-comparison.js +69 -0
  19. package/common/bundle-imports.js +20 -0
  20. package/common/bundle-metadata.d.ts +15 -0
  21. package/common/bundle-metadata.js +226 -0
  22. package/common/bundle-pkg-of.d.ts +1 -0
  23. package/common/bundle-pkg-of.js +90 -0
  24. package/common/bundle-reasons.d.ts +6 -0
  25. package/common/bundle-reasons.js +58 -0
  26. package/common/bundle-source-links.d.ts +6 -0
  27. package/common/bundle-source-links.js +216 -0
  28. package/common/bundle-sourcemap.d.ts +10 -0
  29. package/common/bundle-sourcemap.js +100 -0
  30. package/common/bundle-sources.d.ts +10 -0
  31. package/common/bundle-sources.js +374 -0
  32. package/common/bundle-stats.js +41 -0
  33. package/common/bundle-tabs.js +1 -0
  34. package/common/code-language.js +66 -0
  35. package/common/default-scan-models.ts +36 -0
  36. package/common/dependency-reviews.d.ts +18 -0
  37. package/common/dependency-reviews.js +64 -0
  38. package/common/finding-duplicates.js +63 -0
  39. package/common/finding-filters.js +313 -0
  40. package/common/finding-id.js +47 -0
  41. package/common/finding-repo.d.ts +5 -0
  42. package/common/finding-repo.js +67 -0
  43. package/common/github-issue-labels.d.ts +1 -0
  44. package/common/github-issue-labels.js +11 -0
  45. package/common/github-pr.ts +56 -0
  46. package/common/gzip.js +59 -0
  47. package/common/line-diff.d.ts +14 -0
  48. package/common/line-diff.js +181 -0
  49. package/common/linked-workspace-groups.js +63 -0
  50. package/common/managed/agent-tasks.ts +292 -0
  51. package/common/managed/comments.ts +43 -0
  52. package/common/managed/dependency-routes.js +25 -0
  53. package/common/managed/directory-packages.ts +28 -0
  54. package/common/managed/finding-detail.d.ts +5 -0
  55. package/common/managed/finding-detail.js +75 -0
  56. package/common/managed/finding-values.d.ts +2 -0
  57. package/common/managed/finding-values.js +138 -0
  58. package/common/managed/github-agents.ts +114 -0
  59. package/common/managed/github-logins.ts +49 -0
  60. package/common/managed/lite-name.d.ts +2 -0
  61. package/common/managed/lite-name.js +20 -0
  62. package/common/managed/npm-packages.d.ts +9 -0
  63. package/common/managed/npm-packages.js +63 -0
  64. package/common/managed/permissions.ts +31 -0
  65. package/common/managed/report-content.ts +72 -0
  66. package/common/managed/report-filter.ts +120 -0
  67. package/common/managed/repository-alias.ts +65 -0
  68. package/common/managed/review-view-routes.js +23 -0
  69. package/common/managed/risk-routes.js +30 -0
  70. package/common/managed/roles.ts +41 -0
  71. package/common/managed/routes.d.ts +2 -0
  72. package/common/managed/routes.js +385 -0
  73. package/common/managed/scan-models.ts +6 -0
  74. package/common/managed/stasis-config.ts +7 -0
  75. package/common/managed/team-app.d.ts +19 -0
  76. package/common/managed/team-app.js +129 -0
  77. package/common/managed/team-filters.js +26 -0
  78. package/common/managed/triage.ts +83 -0
  79. package/common/managed/upload-seal.ts +126 -0
  80. package/common/pretty-json.js +98 -0
  81. package/common/pretty-kinds.js +20 -0
  82. package/common/pretty-print.d.ts +6 -0
  83. package/common/pretty-print.js +36 -0
  84. package/common/pretty-sourcemap.js +70 -0
  85. package/common/repo-commit.d.ts +4 -0
  86. package/common/repo-commit.js +30 -0
  87. package/common/revalidation-conflicts.js +48 -0
  88. package/common/revalidation-input-groups.js +56 -0
  89. package/common/save-error-reason.ts +20 -7
  90. package/common/scan-server.ts +13 -0
  91. package/common/security-links.d.ts +5 -0
  92. package/common/security-links.js +33 -0
  93. package/common/server-info.ts +46 -0
  94. package/common/sourcemap-listing.js +78 -0
  95. package/common/sourcemap-mappings.js +136 -0
  96. package/common/sourcemap-unlist.js +85 -0
  97. package/common/stasis-files.d.ts +5 -0
  98. package/common/stasis-files.js +20 -0
  99. package/common/stasis-tgz.d.ts +6 -0
  100. package/common/stasis-tgz.js +27 -0
  101. package/common/utf8.d.ts +3 -0
  102. package/common/utf8.js +45 -0
  103. package/common/whitespace-insensitive.js +19 -0
  104. package/common/workspace-app.js +91 -0
  105. package/common/workspace-groups.js +176 -0
  106. package/out/activity.js +2 -0
  107. package/out/brotli-fallback.js +3 -3
  108. package/out/chunk-2XUEZHIP.js +28 -0
  109. package/out/chunk-55YIWY3H.js +5 -0
  110. package/out/chunk-5AVGKFMR.js +1 -0
  111. package/out/chunk-5CJKUV53.js +1 -0
  112. package/out/chunk-5EAVJAD2.js +3 -0
  113. package/out/chunk-5M2X3QPK.js +6 -0
  114. package/out/chunk-5QWPKTCE.js +40 -0
  115. package/out/chunk-6CCSTJWB.js +1 -0
  116. package/out/chunk-6VOB5WXX.js +2 -0
  117. package/out/chunk-6Y6PZ2BU.js +9 -0
  118. package/out/chunk-74JZTGTO.js +11 -0
  119. package/out/chunk-7BKKYVWR.js +7 -0
  120. package/out/chunk-7C7DWL7I.js +1 -0
  121. package/out/chunk-7HP2VYIC.js +2 -0
  122. package/out/chunk-7YFJUT2P.js +1 -0
  123. package/out/chunk-7YO4QV5P.js +1 -0
  124. package/out/chunk-A4BBBHQU.js +1 -0
  125. package/out/chunk-B64CTMVU.js +4 -0
  126. package/out/chunk-BEKYZCPX.js +14 -0
  127. package/out/chunk-C4Z6ZBXV.js +10 -0
  128. package/out/chunk-CU3FPB3P.js +1 -0
  129. package/out/chunk-DEVAORP2.js +1 -0
  130. package/out/chunk-DUASGS2O.js +8 -0
  131. package/out/chunk-DUS5ARE2.js +53 -0
  132. package/out/chunk-EEJA7JBI.js +13 -0
  133. package/out/chunk-FSU2PLJL.js +1 -0
  134. package/out/chunk-GBMAXAYK.js +3 -0
  135. package/out/chunk-GO7FT4OD.js +1 -0
  136. package/out/chunk-GX5UXWT4.js +1 -0
  137. package/out/chunk-ITUKKCCK.js +10 -0
  138. package/out/chunk-IYPY47MC.js +1 -0
  139. package/out/chunk-JXBRWLMA.js +4 -0
  140. package/out/chunk-K4XJHM2T.js +1 -0
  141. package/out/chunk-LBD3M3J6.js +1 -0
  142. package/out/chunk-LVSLM7HB.js +1 -0
  143. package/out/chunk-LVW7WWV4.js +1 -0
  144. package/out/chunk-MV66NL3G.js +3 -0
  145. package/out/chunk-N57OWFKY.js +15 -0
  146. package/out/chunk-NRPC74AM.js +1 -0
  147. package/out/chunk-OOJM4CTU.js +1 -0
  148. package/out/chunk-OWSEQOL5.js +1 -0
  149. package/out/chunk-PKLVXUEI.js +11 -0
  150. package/out/chunk-PLGH63UC.js +2 -0
  151. package/out/chunk-PNHBZGDD.js +1 -0
  152. package/out/chunk-PTFYDJUP.js +1 -0
  153. package/out/chunk-PWFUOLZK.js +17 -0
  154. package/out/chunk-PXK7EU4V.js +1 -0
  155. package/out/chunk-QD3IBSF4.js +7 -0
  156. package/out/chunk-QI66YA4F.js +1 -0
  157. package/out/chunk-QRKFPLA4.js +80 -0
  158. package/out/chunk-R3NZFXVR.js +1 -0
  159. package/out/chunk-R5O6FAW4.js +2 -0
  160. package/out/chunk-RPNO56H2.js +1 -0
  161. package/out/chunk-SSPDMA6D.js +1 -0
  162. package/out/chunk-SZWPYLPC.js +1 -0
  163. package/out/chunk-TUII34K2.js +1 -0
  164. package/out/chunk-UJP4PQJA.js +1 -0
  165. package/out/chunk-UJSJMXEG.js +1 -0
  166. package/out/chunk-UR4WYMIP.js +1 -0
  167. package/out/chunk-VHEXSGGP.js +1 -0
  168. package/out/chunk-VYYUMYVW.js +9 -0
  169. package/out/chunk-VZPPX6MV.js +1 -0
  170. package/out/chunk-WCDYSIKG.js +1 -0
  171. package/out/chunk-XM4RLC4A.js +2 -0
  172. package/out/chunk-YKCRCNYZ.js +266 -0
  173. package/out/chunk-ZQHLECTI.js +1 -0
  174. package/out/client-managed-import.js +4 -0
  175. package/out/client-managed.js +37 -0
  176. package/out/client-sync.js +11 -17
  177. package/out/compare.js +26 -0
  178. package/out/dialogs.js +40 -0
  179. package/out/github-issue-authorized.html +3 -0
  180. package/out/github-issue-authorized.js +1 -0
  181. package/out/graph.js +19 -4
  182. package/out/index.html +104 -24
  183. package/out/prism.js +14 -10
  184. package/out/scan.js +2 -0
  185. package/out/stasis.svg +20 -0
  186. package/out/terminal.js +367 -39
  187. package/out/view.css +1 -1
  188. package/out/view.js +19 -147
  189. package/package.json +409 -64
  190. package/risk/actions/findings.js +161 -0
  191. package/risk/actions/resolve.js +287 -0
  192. package/risk/actions/scripts/commands.js +182 -0
  193. package/risk/actions/scripts/grade.js +113 -0
  194. package/risk/actions/scripts/packages.js +123 -0
  195. package/risk/actions/scripts/shell.js +169 -0
  196. package/risk/actions/uses.js +94 -0
  197. package/risk/actions/workflow.js +183 -0
  198. package/risk/files/characters.js +341 -0
  199. package/risk/files/hosts.js +206 -0
  200. package/risk/files/readability.js +185 -0
  201. package/risk/files/runs-code.js +563 -0
  202. package/risk/files/script.js +402 -0
  203. package/risk/files/source-map-link.js +43 -0
  204. package/risk/files/stamps.js +47 -0
  205. package/risk/index.d.ts +294 -0
  206. package/risk/index.js +12 -0
  207. package/risk/npm/assess.js +285 -0
  208. package/risk/npm/combined.js +68 -0
  209. package/risk/npm/compare.js +236 -0
  210. package/risk/npm/dependencies.js +172 -0
  211. package/risk/npm/diff.js +79 -0
  212. package/risk/npm/downloads.js +84 -0
  213. package/risk/npm/files.js +445 -0
  214. package/risk/npm/held.js +67 -0
  215. package/risk/npm/install-scripts.js +34 -0
  216. package/risk/npm/malware.js +38 -0
  217. package/risk/npm/manifest.js +65 -0
  218. package/risk/npm/new-dependency.js +60 -0
  219. package/risk/npm/old-version.js +108 -0
  220. package/risk/npm/registry.js +393 -0
  221. package/risk/npm/release.js +85 -0
  222. package/risk/npm/score.js +59 -0
  223. package/risk/npm/specs.js +124 -0
  224. package/risk/npm/tarball.js +169 -0
  225. package/server-common/database-config.ts +16 -0
  226. package/server-common/initialize.ts +18 -0
  227. package/server-common/npm-advisories.ts +101 -0
  228. package/{server → server-common}/origin.ts +5 -5
  229. package/server-common/reap.ts +48 -0
  230. package/server-common/scan-config.ts +19 -0
  231. package/server-common/standalone.ts +29 -0
  232. package/server-common/storage-crypto.ts +182 -0
  233. package/server-common/storage-log.ts +38 -0
  234. package/server-common/vercel-blob.ts +103 -0
  235. package/server-e2e/app.ts +487 -0
  236. package/{server → server-e2e}/bus-receiver.ts +2 -1
  237. package/{server → server-e2e}/cli.js +9 -4
  238. package/{server → server-e2e}/config.ts +50 -42
  239. package/{server → server-e2e}/db-neon.ts +2 -2
  240. package/{server → server-e2e}/db-revision-sql.ts +1 -1
  241. package/{server → server-e2e}/db-stmt.ts +2 -2
  242. package/{server → server-e2e}/db.ts +7 -7
  243. package/{server → server-e2e}/http.ts +44 -13
  244. package/{server → server-e2e}/hub.ts +40 -9
  245. package/server-e2e/index.ts +17 -0
  246. package/server-e2e/lifecycle.ts +95 -0
  247. package/{server → server-e2e}/neon-driver.ts +2 -2
  248. package/{server → server-e2e}/npm-proxy.ts +12 -145
  249. package/{server → server-e2e}/objstore/blob-vercel.ts +10 -114
  250. package/{server → server-e2e}/objstore/blob.ts +1 -1
  251. package/{server → server-e2e}/objstore/fetch-mint-guard.ts +2 -2
  252. package/{server → server-e2e}/objstore/handlers.ts +9 -8
  253. package/{server → server-e2e}/objstore/init.ts +19 -27
  254. package/{server → server-e2e}/objstore/reaper.ts +1 -1
  255. package/{server → server-e2e}/objstore/rest-deny.ts +1 -1
  256. package/{server → server-e2e}/objstore/rest-mint.ts +3 -3
  257. package/{server → server-e2e}/objstore/rest.ts +3 -3
  258. package/{server → server-e2e}/objstore/store-neon.ts +6 -10
  259. package/{server → server-e2e}/objstore/store.ts +7 -7
  260. package/{server → server-e2e}/objstore/tokens.ts +2 -2
  261. package/{server → server-e2e}/pubsub.ts +12 -9
  262. package/{server → server-e2e}/sign.ts +4 -4
  263. package/{server → server-e2e}/sse-server.ts +60 -33
  264. package/{server → server-e2e}/static.ts +18 -17
  265. package/{server → server-e2e}/sync-handlers.ts +64 -30
  266. package/{server → server-e2e}/ws-server.ts +20 -9
  267. package/server-managed/activity.ts +247 -0
  268. package/server-managed/agent-merges.ts +109 -0
  269. package/server-managed/app-agents.ts +23 -0
  270. package/server-managed/avatar-store.ts +13 -0
  271. package/server-managed/blob-store.ts +26 -0
  272. package/server-managed/brotli.ts +10 -0
  273. package/server-managed/bundle-advisories.ts +79 -0
  274. package/server-managed/bundle-advisory-inventory.ts +85 -0
  275. package/server-managed/bundle-build-diagnostics.d.ts +10 -0
  276. package/server-managed/bundle-build-diagnostics.js +23 -0
  277. package/server-managed/bundle-build-leases.ts +41 -0
  278. package/server-managed/bundle-build-worker.js +380 -0
  279. package/server-managed/bundle-build.ts +326 -0
  280. package/server-managed/bundle-cache.ts +261 -0
  281. package/server-managed/bundle-catalog.ts +29 -0
  282. package/server-managed/bundle-commits.ts +133 -0
  283. package/server-managed/bundle-dependency-reviews.ts +91 -0
  284. package/server-managed/bundle-store.ts +24 -0
  285. package/server-managed/bundle-summary-cache.ts +139 -0
  286. package/server-managed/bundle-validation.ts +33 -0
  287. package/server-managed/bundle.ts +151 -0
  288. package/server-managed/cache-storage.ts +20 -0
  289. package/server-managed/cli.js +21 -0
  290. package/server-managed/combined.ts +43 -0
  291. package/server-managed/comments.ts +138 -0
  292. package/server-managed/commit-coauthors.ts +105 -0
  293. package/server-managed/commit-lockfiles.ts +78 -0
  294. package/server-managed/comparison.ts +51 -0
  295. package/server-managed/config.ts +163 -0
  296. package/server-managed/content-access.ts +15 -0
  297. package/server-managed/crypto.ts +25 -0
  298. package/server-managed/db-methods.ts +1914 -0
  299. package/server-managed/db-neon.ts +398 -0
  300. package/server-managed/db-schema.ts +263 -0
  301. package/server-managed/db-table-names.ts +22 -0
  302. package/server-managed/db.ts +149 -0
  303. package/server-managed/dependabot-access.ts +61 -0
  304. package/server-managed/dependabot-alerts.ts +180 -0
  305. package/server-managed/dependency-review-comments.ts +143 -0
  306. package/server-managed/dependency-reviews.ts +369 -0
  307. package/server-managed/github-accounts.ts +59 -0
  308. package/server-managed/github-actions.ts +71 -0
  309. package/server-managed/github-app.ts +347 -0
  310. package/server-managed/github-errors.ts +92 -0
  311. package/server-managed/github-graphql.ts +42 -0
  312. package/server-managed/github-issue-links.ts +264 -0
  313. package/server-managed/github-issue-oauth.ts +30 -0
  314. package/server-managed/github-issues.ts +157 -0
  315. package/server-managed/github-link-oauth.ts +56 -0
  316. package/server-managed/github-links.ts +204 -0
  317. package/server-managed/github-metadata.ts +213 -0
  318. package/server-managed/github-oauth.ts +264 -0
  319. package/server-managed/github-pulls.ts +257 -0
  320. package/server-managed/github-requests.ts +181 -0
  321. package/server-managed/github-users.ts +30 -0
  322. package/server-managed/github-values.ts +26 -0
  323. package/server-managed/history-windows.ts +172 -0
  324. package/server-managed/http-response.ts +76 -0
  325. package/server-managed/http.ts +6324 -0
  326. package/server-managed/import-triage.ts +51 -0
  327. package/server-managed/index.ts +204 -0
  328. package/server-managed/kept-briefly.ts +37 -0
  329. package/server-managed/kept.ts +28 -0
  330. package/server-managed/link-reports.ts +149 -0
  331. package/server-managed/listed-activity.ts +39 -0
  332. package/server-managed/locked-packages.ts +234 -0
  333. package/server-managed/lockfile-scope.ts +58 -0
  334. package/server-managed/lru.ts +30 -0
  335. package/server-managed/managed-issues.ts +92 -0
  336. package/server-managed/management-catalog.ts +45 -0
  337. package/server-managed/management.ts +116 -0
  338. package/server-managed/npm-delta-worker.js +142 -0
  339. package/server-managed/npm-delta.ts +277 -0
  340. package/server-managed/npm-insights.ts +455 -0
  341. package/server-managed/npm-kept.ts +265 -0
  342. package/server-managed/npm-loads.ts +259 -0
  343. package/server-managed/npm-manifest.ts +117 -0
  344. package/server-managed/npm-packages.ts +469 -0
  345. package/server-managed/npm-patch-paths.ts +86 -0
  346. package/server-managed/npm-patch.ts +455 -0
  347. package/server-managed/npm-provenance.ts +127 -0
  348. package/server-managed/npm-risk.ts +287 -0
  349. package/server-managed/object-storage-disk.ts +147 -0
  350. package/server-managed/object-storage-vercel.ts +71 -0
  351. package/server-managed/object-storage.ts +82 -0
  352. package/server-managed/package-entry-points.ts +127 -0
  353. package/server-managed/pretty-copies.ts +22 -0
  354. package/server-managed/pretty-format.d.ts +3 -0
  355. package/server-managed/pretty-format.js +13 -0
  356. package/server-managed/pretty-print.ts +442 -0
  357. package/server-managed/public-workspace.ts +243 -0
  358. package/server-managed/pull-comment-reviews.ts +161 -0
  359. package/server-managed/pull-dependencies.ts +370 -0
  360. package/server-managed/pull-dependency-reviews.ts +407 -0
  361. package/server-managed/pull-reviews.ts +202 -0
  362. package/server-managed/queue.ts +54 -0
  363. package/server-managed/release-commits.ts +85 -0
  364. package/server-managed/rename-diff-worker.js +30 -0
  365. package/server-managed/rename-diff.ts +237 -0
  366. package/server-managed/repo-path.ts +21 -0
  367. package/server-managed/report-migration.ts +32 -0
  368. package/server-managed/report-opening.ts +77 -0
  369. package/server-managed/report-query.ts +10 -0
  370. package/server-managed/report-response.ts +16 -0
  371. package/server-managed/report-sources.ts +185 -0
  372. package/server-managed/report-store.ts +77 -0
  373. package/server-managed/repository-actions.ts +220 -0
  374. package/server-managed/repository-agents.ts +422 -0
  375. package/server-managed/repository-aliases.ts +119 -0
  376. package/server-managed/repository-browser.ts +361 -0
  377. package/server-managed/repository-changes.ts +280 -0
  378. package/server-managed/repository-code.ts +222 -0
  379. package/server-managed/repository-dependency-reviews.ts +182 -0
  380. package/server-managed/repository-discovery.ts +83 -0
  381. package/server-managed/repository-history.ts +428 -0
  382. package/server-managed/repository-issues.ts +293 -0
  383. package/server-managed/repository-policy.ts +27 -0
  384. package/server-managed/repository-pulls.ts +207 -0
  385. package/server-managed/repository-pulse.ts +477 -0
  386. package/server-managed/repository-risk-assess.ts +108 -0
  387. package/server-managed/repository-risk-audit.ts +78 -0
  388. package/server-managed/repository-risk-caches.ts +37 -0
  389. package/server-managed/repository-risk-read.ts +260 -0
  390. package/server-managed/repository-risk-stasis.ts +61 -0
  391. package/server-managed/repository-risk-turn.ts +97 -0
  392. package/server-managed/repository-risk.ts +516 -0
  393. package/server-managed/repository-squashes.ts +160 -0
  394. package/server-managed/repository-stamp.ts +41 -0
  395. package/server-managed/request-timing.ts +63 -0
  396. package/server-managed/reviewed-dependencies.ts +170 -0
  397. package/server-managed/revisions.ts +35 -0
  398. package/server-managed/scan-downloads.ts +121 -0
  399. package/server-managed/session.ts +132 -0
  400. package/server-managed/slugs.ts +38 -0
  401. package/server-managed/solidity-entry-points.ts +71 -0
  402. package/server-managed/sql-postgres.ts +34 -0
  403. package/server-managed/sql.ts +107 -0
  404. package/server-managed/stasis-config.ts +147 -0
  405. package/server-managed/stasis-dependencies.ts +221 -0
  406. package/server-managed/static.ts +28 -0
  407. package/server-managed/storage-command.ts +16 -0
  408. package/server-managed/storage-db.ts +183 -0
  409. package/server-managed/storage-encryption.ts +288 -0
  410. package/server-managed/storage-maintenance.ts +259 -0
  411. package/server-managed/storage-payload.ts +121 -0
  412. package/server-managed/storage-stores.ts +91 -0
  413. package/server-managed/storage.ts +82 -0
  414. package/server-managed/team-app-summaries.ts +56 -0
  415. package/server-managed/team-app.ts +156 -0
  416. package/server-managed/team-catalog.ts +7 -0
  417. package/server-managed/team-feed.ts +180 -0
  418. package/server-managed/team-npm-scopes.ts +103 -0
  419. package/server-managed/team-reports.ts +368 -0
  420. package/server-managed/triage-response.ts +16 -0
  421. package/server-managed/type-only-proof.js +161 -0
  422. package/server-managed/type-only-worker.js +31 -0
  423. package/server-managed/type-only.ts +135 -0
  424. package/server-managed/uploads.ts +106 -0
  425. package/server-managed/upstream-cache.ts +256 -0
  426. package/server-managed/view-as.ts +58 -0
  427. package/server-managed/worker-turns.ts +76 -0
  428. package/server-managed/workspace-shares.ts +170 -0
  429. package/server.ts +70 -0
  430. package/strip-types-loader.js +13 -10
  431. package/server/index.ts +0 -480
  432. package/server/lifecycle.ts +0 -208
  433. /package/{LICENSE → LICENSE-MIT} +0 -0
  434. /package/{server → server-e2e}/auth.ts +0 -0
  435. /package/{server → server-e2e}/config.example.json +0 -0
  436. /package/{server → server-e2e}/objstore/blob-fs.ts +0 -0
  437. /package/{server → server-e2e}/objstore/fs.ts +0 -0
  438. /package/{server → server-e2e}/objstore/sign.ts +0 -0
  439. /package/{server → server-e2e}/peer.ts +0 -0
  440. /package/{server → server-e2e}/sse-session.ts +0 -0
  441. /package/{server → server-e2e}/util.ts +0 -0
  442. /package/{server → server-e2e}/validation.ts +0 -0
@@ -0,0 +1,294 @@
1
+ // Hand-written against index.js; a change to either belongs with the other.
2
+ import type { CacheStore } from '@preventive/upstream/npm.js'
3
+ import type { Entry } from '@preventive/archive/tar.js'
4
+
5
+ // How severe a stamp or a finding is, the most first: `critical` red, `high`
6
+ // a warning's orange, `moderate` yellow, `low`, and a `notice`.
7
+ export type Severity = 'critical' | 'high' | 'moderate' | 'low' | 'notice'
8
+
9
+ // The severities, the most severe first.
10
+ export const SEVERITIES: readonly Severity[]
11
+
12
+ // How old a version must be to be trusted unless said: two weeks, in ms.
13
+ export const DEFAULT_MINIMUM_RELEASE_AGE: number
14
+
15
+ // npm has no such package or version (`package-not-found`,
16
+ // `version-not-found`), a tarball unpacks past risk's bounds
17
+ // (`package-too-large`) or would install other than one set of files
18
+ // (`tarball-conflict`, npmTarLayout), npm asked to be asked later
19
+ // (`upstream-busy`, a 429, for a caller to wait on and ask again), or npm
20
+ // couldn't be asked (`upstream-unavailable`), answered past them
21
+ // (`upstream-too-large`) or answered what can't be read (`upstream-invalid`).
22
+ // `message` tells which request and why.
23
+ export class NpmRiskError extends Error {
24
+ code: string
25
+ }
26
+
27
+ export interface AssessNpmOptions {
28
+ // The version to compare with; true for the one of the package's versions
29
+ // most likely free of what an attack injects, as risk picks it by npm's
30
+ // document (risk/npm/old-version.js), none where none is below it; without
31
+ // one, everything of the version is new.
32
+ oldVersion?: string | true | null
33
+ // Compare all files with an empty package, for an initial dependency review.
34
+ fromEmpty?: boolean
35
+ // Younger than this, in ms, a version is `too-new`; 0 for none, as a
36
+ // caller's own scopes take. Two weeks unless given.
37
+ minimumReleaseAge?: number
38
+ // Whether the package is flagged as few download it (`popularity`); false
39
+ // for none, as a caller's own scopes take, its downloads then not asked.
40
+ // True unless given.
41
+ popularity?: boolean
42
+ // Where what is read is kept, by type (risk/npm/registry.js and assess.js
43
+ // name each): `npm/tarballs`, `risk/npm/versions`, `risk/npm/packages`,
44
+ // `risk/npm/old-versions`, `risk/npm/public`, `risk/npm/downloads`,
45
+ // `risk/npm/advisories` and `risk/npm/assessments`. One that reads records
46
+ // of any types at once (`readEach`, each null where none is kept) is read
47
+ // so where risk reads several together.
48
+ cache?: (CacheStore & { readEach?(entries: [type: string, key: string][]): Promise<unknown[]> }) | false
49
+ // Asks for a package's whole document in risk's stead, as the managed
50
+ // server does within its bounds: null where npm has none for this asker.
51
+ // `authorized` whether it may be asked with the caller's npm token: the
52
+ // package assessed, not its dependencies.
53
+ packument?: (name: string, options: { authorized: boolean; signal: AbortSignal | undefined }) => Promise<Record<string, unknown> | null>
54
+ // Reads a version's tarball, gzipped, in place of upstream's getTarball, as
55
+ // the managed server does within its bounds: `version` the one assessed or
56
+ // `oldVersion`, that one only where its files could change what is found.
57
+ // What it throws is thrown as it is.
58
+ tarball?: (name: string, version: string, options: { signal: AbortSignal | undefined }) => Promise<Uint8Array>
59
+ now?: number
60
+ signal?: AbortSignal
61
+ // Whether npm's downloads are asked for: the package's popularity, and its
62
+ // new dependencies' tiers and ages. True unless given.
63
+ downloads?: boolean
64
+ // Whether its new dependencies' tiers and ages are asked for, a request or
65
+ // two of npm each, where `downloads` is. True unless given.
66
+ dependencies?: boolean
67
+ // Whether npm's advisories of the version are asked for, an advisory of
68
+ // malware naming it a `malware` finding, as its package taken down by npm
69
+ // is. True unless given.
70
+ advisories?: boolean
71
+ // Whether OSV's records of malware in the version are asked for too, where
72
+ // `advisories` are: false for a private package, whose name OSV is never
73
+ // to be sent, as upstream sends it none npm's registry doesn't serve
74
+ // anyone. True unless given.
75
+ osv?: boolean
76
+ // Whether a package is the caller's own, as its own scopes take: a new
77
+ // dependency on one is told no tier nor age, nor is npm asked of it. None
78
+ // unless given.
79
+ owned?: (name: string) => boolean | Promise<boolean>
80
+ }
81
+
82
+ // What calls for a look, by `id`, with what it found beside.
83
+ export interface Finding {
84
+ id: string
85
+ severity: Severity
86
+ [found: string]: unknown
87
+ }
88
+
89
+ // A stamp on a file, as a list of files tags it.
90
+ export interface Stamp {
91
+ name: string
92
+ severity: Severity
93
+ }
94
+
95
+ export interface FileRisk {
96
+ stamps: Stamp[]
97
+ readability: Record<string, unknown>
98
+ runsCode: Record<string, number[]> | null
99
+ exfiltration: Record<string, number[]> | null
100
+ hosts: Record<string, number> | null
101
+ }
102
+
103
+ export interface DependencyRisk {
104
+ key: string
105
+ name: string
106
+ kind: 'peer' | 'optional' | null
107
+ range?: string
108
+ from?: string
109
+ to?: string
110
+ opens: string | null
111
+ flags: { check: string; label: string; tooltip: string }[]
112
+ tier?: number | null
113
+ level?: string
114
+ // Its last whole month's downloads, by what its tier goes by: none where
115
+ // npm counts none of them.
116
+ month?: { total: number; from: string }
117
+ // Whether npm counts none of its downloads, as of a package published
118
+ // the day before; unpopular all the same.
119
+ uncounted?: true
120
+ created?: string
121
+ gap?: number
122
+ // For one nothing imports, the version its spec installs (registry.js
123
+ // npmPublicFacts): its package's latest beside it, when it was published,
124
+ // whether it has install scripts, and whether that was since the version
125
+ // compared with (`new`).
126
+ installs?: { version: string; latest: string | null; published: string | null; scripts: boolean; new: boolean }
127
+ // Where its package was looked up and npm has none of it for anyone.
128
+ missing?: true
129
+ }
130
+
131
+ // A version's risk as one number (risk/npm/score.js): what its findings call
132
+ // for (`raised`, each it counts), each by its severity, malware all of it,
133
+ // lowered by its exposure (`lowered`): how long it has been out (`days`)
134
+ // among how many download its package (`tier`, its rank, null past the last
135
+ // or where unknown), `by` the share taken off. Its `level` is named as the
136
+ // findings' severities are: from 75 critical, 50 high, 25 moderate, else low.
137
+ export interface RiskScore {
138
+ score: number
139
+ level: 'critical' | 'high' | 'moderate' | 'low'
140
+ raised: { id: string; severity: Severity }[]
141
+ lowered?: { days: number; tier: number | null; by: number }
142
+ }
143
+
144
+ export interface NpmAssessment {
145
+ name: string
146
+ version: string
147
+ // The version compared with, as given or as risk picked it; null for none.
148
+ oldVersion: string | null
149
+ published: string | null
150
+ // A version npm removed, as it removes every one it finds malware in, told
151
+ // of by its `malware` finding alone, with no files nor dependencies.
152
+ removed?: true
153
+ findings: Finding[]
154
+ files: Record<string, FileRisk>
155
+ // Against `oldVersion`, each it holds too, flagged against none (`held`).
156
+ dependencies: { added: DependencyRisk[]; changed: DependencyRisk[]; held?: DependencyRisk[] }
157
+ score: RiskScore
158
+ }
159
+
160
+ export function assessNpm(name: string, version: string, options?: AssessNpmOptions): Promise<NpmAssessment>
161
+
162
+ // Readies, at once, the downloads assessNpm asks of each of these packages
163
+ // that can be, kept in `cache` as it keeps them, so that their assessments
164
+ // ask npm none: those kept within a week, and unscoped ones, asked of npm 128
165
+ // at a time. `ready` is told each once its downloads are kept; the rest (scoped
166
+ // ones, which npm answers only one by one) are left for npmReadyDownload.
167
+ // npm asking to be asked later is thrown as an NpmRiskError
168
+ // (`upstream-busy`), those readied by then told.
169
+ // A cache that reads and writes many at once too (`readMany`, `writeMany`)
170
+ // is read once and written once a lookup.
171
+ export function npmReadyDownloads(names: string[], ready: (name: string) => void, options: { cache: CacheStore & {
172
+ readMany?(type: string, keys: string[]): Promise<Map<string, unknown>>
173
+ writeMany?(type: string, entries: [key: string, value: unknown][]): Promise<void>
174
+ }; signal?: AbortSignal; now?: number }): Promise<void>
175
+
176
+ // Readies the downloads assessNpm asks of a package, asked of npm on their
177
+ // own where they aren't kept; npm asking to be asked later thrown as an
178
+ // NpmRiskError (`upstream-busy`).
179
+ export function npmReadyDownload(name: string, options: { cache: CacheStore; signal?: AbortSignal; now?: number }): Promise<void>
180
+
181
+ // Whether an advisory is of malware rather than a vulnerability: OSV's of a
182
+ // malicious package (`MAL-…`), or one such a record names (its `aliases`),
183
+ // CWE-506, embedded malicious code, whatever its title, or a title naming
184
+ // malware. The rule @preventive/upstream's advisories tell `malware` by, for
185
+ // those it doesn't tell, as Dependabot's alerts.
186
+ export function npmMalwareAdvisory(advisory: { id?: string | undefined; aliases?: readonly string[] | undefined; cwe?: readonly string[] | undefined; title?: string | undefined }): boolean
187
+
188
+ export function npmFileRisk(path: string, text: string | null, bytes?: number, script?: boolean): unknown
189
+
190
+ // How many levels up a package's symlinks may point: any (npm/tarball.js).
191
+ export const NPM_SYMLINKS_ABOVE: number
192
+
193
+ // The folds macOS or Windows may take two paths as one under (npm/tarball.js).
194
+ export const NPM_PATH_FOLDS: readonly ((path: string) => string)[]
195
+
196
+ // Every file a tar archive's entries hold, by package path, as npm and pnpm
197
+ // lay them out (npm/tarball.js): each entry's name as stored past its first
198
+ // component, whatever that is, and a file under no directory at its name, as
199
+ // pnpm installs it; a hard link is the file it names again, a symlink its
200
+ // target as stored (`link`), a file with an execute bit `executable`. `refuse` is called, and throws, with
201
+ // `tarball-conflict` where the files conflict (two at one path that differ,
202
+ // or at two paths one on macOS or Windows unless `foldPaths` is false, a
203
+ // file within another), and with `package-too-large` past `maxFiles` files or
204
+ // `maxBytes` bytes.
205
+ export function npmTarLayout(entries: Iterable<Pick<Entry, 'name' | 'storedName' | 'type' | 'data' | 'linkname' | 'mode'>>,
206
+ reading: { maxFiles: number; maxBytes: number; foldPaths?: boolean },
207
+ refuse: (code: 'tarball-conflict' | 'package-too-large') => never): Map<string, { bytes: Uint8Array; link?: string; executable?: true }>
208
+
209
+ // GitHub Actions (actions/), a workflow GitHub can't run thrown as
210
+ // @preventive/lockfile's YamlError. A finding of a workflow's, its `line` from 1; of a reference's own code,
211
+ // with the `file` it is in.
212
+ export interface ActionsFinding extends Finding {
213
+ line?: number | null
214
+ file?: string
215
+ }
216
+
217
+ // What a `uses:` names: an action's or a reusable workflow's, by its `key`,
218
+ // or the repository's own. `checkout`, a ref a checkout put a `./` action
219
+ // at, looked for as a branch before a tag.
220
+ export type ActionUses = { kind: 'action' | 'workflow'; uses: string; repo: string; path: string; ref: string; pin: 'sha' | 'short-sha' | 'ref' | 'expression'; comment: string | null; checkout?: true; key?: string; line?: number | null }
221
+ export type Uses = ActionUses | { kind: 'local'; uses: string; path: string; self?: true; job?: true; line?: number | null }
222
+
223
+ export interface ScanOptions {
224
+ // Whose repository it is, as an unpinned `uses:` of its owner's is yellow.
225
+ owner?: string | null
226
+ // Its lockfiles' paths, the packages each locks, as npx runs those, by
227
+ // name and by `name@version`, and those only devDependencies need.
228
+ lockfiles?: { path: string; names: ReadonlySet<string>; dev?: ReadonlySet<string> }[]
229
+ // Its own actions' metadata by their directories, and its workflows by
230
+ // their paths: for where its actions install its packages, else each
231
+ // counts as installing them anywhere; and for the inputs its actions and
232
+ // reusable workflows write into their scripts, else each of a reusable
233
+ // workflow's counts as one.
234
+ actions?: ReadonlyMap<string, string> | null
235
+ }
236
+
237
+ export type WorkflowScan = { uses: Uses[]; findings: ActionsFinding[] } | { error: 'workflow-unreadable'; detail: string }
238
+
239
+ export function scanWorkflow(kind: 'workflow' | 'action', text: string, options?: ScanOptions): WorkflowScan
240
+ // The actions of the repository's own a workflow or an action uses, by
241
+ // their directories, and a Docker action's Dockerfile, from its own.
242
+ export function ownFilesOf(kind: 'workflow' | 'action', text: string): { actions: string[]; dockerfile: string | null }
243
+ // A Dockerfile's findings, each on its line.
244
+ export function dockerfileFindings(text: string): ActionsFinding[]
245
+ // A reference by its key, as a scan names it; null for none.
246
+ export function usesOfKey(key: string): ActionUses | null
247
+
248
+ // GitHub as the asker can read it; null for none.
249
+ export interface ActionsSource {
250
+ // Its name now, where it moved, and the repository it forks.
251
+ repo(name: string): Promise<{ fullName: string; defaultBranch: string; fork?: string | null; archived?: boolean; private?: boolean } | null>
252
+ // Every tag's commit; null where it has too many to list.
253
+ tags(name: string): Promise<{ tag: string; oid: string }[] | null>
254
+ tag(name: string, tag: string): Promise<string | null>
255
+ branch(name: string, branch: string): Promise<string | null>
256
+ // Its branches, and whether those are all.
257
+ branches(name: string): Promise<{ branches: { branch: string; oid: string }[]; complete: boolean }>
258
+ // Whether `sha` is `base` or in its history; null for no such commit. Of two
259
+ // commits, it holds for good.
260
+ contains(name: string, base: string, sha: string): Promise<boolean | null>
261
+ file(name: string, path: string, commit: string): Promise<string | null>
262
+ // A repository's advisories at `versions`, as upstream's `advisories`
263
+ // tells those of a `github` package: its own published ones, and but for
264
+ // one `internal` (not known public), OSV's of it as an Action. Without it,
265
+ // none are asked.
266
+ advisories?(name: string, versions: string[], internal: boolean): Promise<ActionAdvisory[]>
267
+ }
268
+
269
+ // An advisory of a repository, as upstream's `advisories` tells one.
270
+ export interface ActionAdvisory {
271
+ id: string
272
+ url?: string | undefined
273
+ title?: string | undefined
274
+ severity?: string | undefined
275
+ malware?: boolean | undefined
276
+ // The versions asked that it affects.
277
+ versions: string[]
278
+ }
279
+
280
+ // A reference resolved: its repository as GitHub has it now, its ref's
281
+ // commit, what the ref is, the tags on the commit, and the keys of the
282
+ // references its code makes; null for none.
283
+ export interface ResolvedAction extends Pick<ActionUses, 'kind' | 'repo' | 'path' | 'ref'> {
284
+ key: string
285
+ repository: { fullName: string; private: boolean } | null
286
+ commit: string | null
287
+ at: 'sha' | 'tag' | 'branch' | null
288
+ tags: string[]
289
+ findings: ActionsFinding[]
290
+ uses: string[]
291
+ }
292
+
293
+ export function resolveAction(uses: ActionUses, source: ActionsSource, options?: { owner?: string | null }): Promise<ResolvedAction>
294
+ export function keptSource(source: ActionsSource): ActionsSource
package/risk/index.js ADDED
@@ -0,0 +1,12 @@
1
+ // What calls for a look at a package version, or a repository's GitHub
2
+ // Actions, for a reviewer: risk's heuristics, apart from any view of them.
3
+ // See README.md.
4
+ export { assessNpm } from './npm/assess.js'
5
+ export { npmMalwareAdvisory } from './npm/malware.js'
6
+ export { DEFAULT_MINIMUM_RELEASE_AGE } from './npm/release.js'
7
+ export { NpmRiskError, npmReadyDownload, npmReadyDownloads } from './npm/registry.js'
8
+ export { NPM_PATH_FOLDS, NPM_SYMLINKS_ABOVE, npmTarLayout } from './npm/tarball.js'
9
+ export { SEVERITIES, npmFileRisk } from './files/stamps.js'
10
+ export { ownFilesOf, scanWorkflow } from './actions/findings.js'
11
+ export { dockerfileFindings, keptSource, resolveAction } from './actions/resolve.js'
12
+ export { usesOfKey } from './actions/uses.js'
@@ -0,0 +1,285 @@
1
+ // A package version's risk, as the managed server asks it: what calls for a
2
+ // look at it, against the version before it where one is given or risk picks
3
+ // one (old-version.js), each file's stamps, and its dependencies', from npm's
4
+ // registry, kept in the caller's cache (registry.js) as it goes.
5
+ import { npmBaseFilesTell, npmCompareFindings } from './compare.js'
6
+ import { npmCombinedFindings, npmHeldDependencyFindings } from './combined.js'
7
+ import { npmHeldFindings, npmVersionFindings } from './held.js'
8
+ import { npmVersionFileRisk } from './files.js'
9
+ import { npmDependencyChanges, npmFlagDependencyChanges, npmUnusedDependencyCounted } from './dependencies.js'
10
+ import { npmDownloadMonths, npmDownloadTier, npmSettledDownloads, npmTierLevel } from './downloads.js'
11
+ import { NpmRiskError, npmDownloads, npmPackageFacts, npmPackageReleases, npmPublicFacts, npmVersionAdvisories, npmVersionFiles } from './registry.js'
12
+ import { npmMalwareFinding } from './malware.js'
13
+ import { npmKnownScope } from './manifest.js'
14
+ import { DEFAULT_MINIMUM_RELEASE_AGE, npmAccountStanding } from './release.js'
15
+ import { npmAddedDependency, npmUnusedDependencySeverity } from './new-dependency.js'
16
+ import { SEVERITIES } from '../files/stamps.js'
17
+ import { npmRiskScore } from './score.js'
18
+
19
+ // Which reading of a version an assessment kept (`risk/npm/assessments`) is:
20
+ // one more whenever a heuristic changes what it finds, so one kept by an
21
+ // older reading is read anew.
22
+ const RISK_VERSION = 22
23
+
24
+ // How long an assessment kept may be as JSON, and what risk reads of either
25
+ // version's files: a package past it, whose files name thousands of hosts or
26
+ // do what they run on thousands of lines each, is refused as too large as its
27
+ // files are read, before more of them is held (filesRisk), and the assessment
28
+ // once told, rather than kept and answered. A package's files tell a few
29
+ // hundred characters each, a few MiB for the most files npm's viewer reads.
30
+ const MAX_ASSESSMENT_LENGTH = 16 * 1024 * 1024
31
+ const tooLarge = what => new NpmRiskError('package-too-large', `what risk tells of ${what} is past ${MAX_ASSESSMENT_LENGTH} characters`)
32
+
33
+ // A value as JSON holds it: a Map as an object, its keys as strings.
34
+ const plain = value => value instanceof Map ? Object.fromEntries([...value].map(([key, item]) => [key, plain(item)]))
35
+ : Array.isArray(value) ? value.map(plain)
36
+ : value && typeof value === 'object' ? Object.fromEntries(Object.entries(value).map(([key, item]) => [key, plain(item)])) : value
37
+
38
+ // A tier among npm's packages as an assessment says it: `{ tier, level }`, the
39
+ // rank a package is among (null past the last, unpopular) and its tint
40
+ // (downloads.js npmTierLevel).
41
+ const tierOf = tier => ({ tier: Number.isFinite(tier) ? tier : null, level: npmTierLevel(tier) })
42
+
43
+ // Each of a version's files' stamps and what risk read of it (stamps.js
44
+ // npmFileRisk), by path, as JSON holds them: read a file at a time and given
45
+ // up on, as too large, once they are past MAX_ASSESSMENT_LENGTH as JSON
46
+ // altogether, so that no more of them is held than that.
47
+ function filesRisk(version, what) {
48
+ let length = 0
49
+ return Object.fromEntries(version.json.sources.map(path => {
50
+ const { readability, runsCode, exfiltration, hosts, stamps } = npmVersionFileRisk(version, path)
51
+ const risk = plain({ stamps, readability, runsCode, exfiltration, hosts })
52
+ length += path.length + JSON.stringify(risk).length
53
+ if (length > MAX_ASSESSMENT_LENGTH) throw tooLarge(what)
54
+ return [path, risk]
55
+ }))
56
+ }
57
+
58
+ // What of an assessment holds whatever the time, of the open version
59
+ // (`other`) against the one compared with (`base`), or alone without one,
60
+ // each as compare.js takes a version, as JSON holds it: what it holds,
61
+ // always (held.js npmHeldFindings, and its dependencies against none,
62
+ // combined.js npmHeldDependencyFindings), and against `base` what its files,
63
+ // dependencies, install scripts, publisher and provenance call for anew
64
+ // (compare.js npmCompareFindings), folded into what it holds, never lowering
65
+ // it (combined.js npmCombinedFindings); each file's stamps and what risk read
66
+ // of it (filesRisk); and its dependencies added and changed, each with its
67
+ // flags (dependencies.js), against `base` each it holds too (`held`). Its own findings are told fresh
68
+ // (npmAssessmentOf). The managed server's assessments keep it (`lasting`),
69
+ // and a test can tell what one says of versions it holds. Each publisher's
70
+ // standing on the package is told where `standing` is given (release.js
71
+ // npmAccountStanding, of the open version).
72
+ export const npmLastingRisk = (other, base = null, standing = null) => lastingOf(other, base, filesRisk(other, `${other.npm.name}@${other.npm.version}`), standing)
73
+
74
+ // npmLastingRisk's, the open version's files read within bounds (`files`,
75
+ // filesRisk's), as the one compared with's are where `base` has them, before
76
+ // the findings read them all.
77
+ function lastingOf(other, base, files, standing) {
78
+ if (base?.json) filesRisk(base, `${base.npm.name}@${base.npm.version}`)
79
+ const all = npmFlagDependencyChanges(npmDependencyChanges(undefined, other.npm.manifest), null, other)
80
+ const held = [...npmHeldFindings(other), ...npmHeldDependencyFindings(all)]
81
+ const { added, changed } = base ? npmFlagDependencyChanges(npmDependencyChanges(base.npm.manifest, other.npm.manifest), base, other) : all
82
+ const findings = base ? npmCombinedFindings(held, npmCompareFindings(base, other, { added, changed }, standing)) : held
83
+ const kept = { findings: plain(findings), files, dependencies: plain({ added, changed, ...base ? { held: all.added } : {} }) }
84
+ if (JSON.stringify(kept).length > MAX_ASSESSMENT_LENGTH) throw tooLarge(`${other.npm.name}@${other.npm.version}`)
85
+ return kept
86
+ }
87
+
88
+ // What of an assessment holds whatever the time (npmLastingRisk), kept once a
89
+ // reading and pair, its versions' files read from npm only where it isn't:
90
+ // the open version's first, then the one compared with's only where they
91
+ // could change what is found (compare.js npmBaseFilesTell), that one else
92
+ // told by what npm's document of it says alone; and where another account
93
+ // published it, each one's standing on the package (release.js
94
+ // npmAccountStanding), by its versions as `facts` read them, or as its
95
+ // document is read again where they were kept. A standing changes as npm
96
+ // removes a version, its publisher then unknown, and never else, so such a
97
+ // pair is kept by how many npm has removed (`~<removals>`), read anew as it
98
+ // removes another.
99
+ async function lasting(name, version, oldVersion, facts, context, fromEmpty) {
100
+ const old = oldVersion === null ? null : facts.versions.get(oldVersion)
101
+ const publishers = [old, facts.versions.get(version)].map(fact => fact?.manifest.publisher)
102
+ const changed = publishers.every(Boolean) && publishers[0] !== publishers[1]
103
+ const key = `${RISK_VERSION}/${name}@${version}${fromEmpty ? '..(empty)' : oldVersion === null ? '' : `..${oldVersion}`}${changed ? `~${facts.removals}` : ''}`
104
+ const hit = context.cache ? await context.cache.read('risk/npm/assessments', key) : null
105
+ if (hit) return hit
106
+ const other = await npmVersionFiles(name, version, facts.versions.get(version), context)
107
+ const files = filesRisk(other, `${name}@${version}`)
108
+ const base = fromEmpty ? { npm: { name, version: '', manifest: {} }, json: { sources: [], sourcesContent: [] } } : old === null ? null : npmBaseFilesTell(other, old.manifest) ? await npmVersionFiles(name, oldVersion, old, context)
109
+ : { npm: { name, version: oldVersion, manifest: old.manifest, published: old.published }, json: null }
110
+ const standing = changed ? npmAccountStanding(facts.releases ?? await npmPackageReleases(name, context), other.npm.published) : null
111
+ const kept = lastingOf(other, base, files, standing)
112
+ if (context.cache) await context.cache.write('risk/npm/assessments', key, kept)
113
+ return kept
114
+ }
115
+
116
+ // How many dependencies added are told of at most, however many a version
117
+ // adds, as each costs npm a request or two: the rest are left as they are,
118
+ // their packages unknown.
119
+ const MAX_TOLD_DEPENDENCIES = 100
120
+
121
+ // Each dependency `added` that npm installs from its registry, up to
122
+ // MAX_TOLD_DEPENDENCIES of them, but for the caller's own packages (`owned`,
123
+ // as its own scopes take, few downloading them, as new as its releases),
124
+ // with what risk tells of its package
125
+ // (new-dependency.js npmAddedDependency): its tier, its last whole month's
126
+ // downloads by what its tier goes by where it has one (`month`, `{ total,
127
+ // from }`), `uncounted` where npm counts none of them, `created` and `gap`
128
+ // where it is new since the version compared with (`published`, when that
129
+ // and the open version were), and for one nothing imports (flagged so, or
130
+ // among `unusedKeys`) that risk counts (dependencies.js
131
+ // npmUnusedDependencyCounted), the version its spec installs (`installs`,
132
+ // registry.js npmPublicFacts); `missing` where
133
+ // its package was looked up and npm has none of it for anyone, as once
134
+ // unpublished or taken down; four asked at a time, whether each is the
135
+ // caller's own among them, none more once one fails or MAX_TOLD_DEPENDENCIES
136
+ // not the caller's own were taken, however many a version adds.
137
+ async function addedDependencies(added, published, context, owned, unusedKeys = new Set()) {
138
+ const told = [...added]
139
+ const asked = told.flatMap((row, at) => row.opens ? [at] : [])
140
+ let next = 0, taken = 0
141
+ const tell = async row => {
142
+ const downloads = await npmDownloads(row.opens, context)
143
+ const unused = (row.flags.some(flag => flag.check === 'unimported') || unusedKeys.has(keyOf(row))) && npmUnusedDependencyCounted(row)
144
+ let facts = npmAddedDependency({ downloads, published, unused }, context.now), missing = false
145
+ if (facts.wanted) {
146
+ const listed = await npmPublicFacts(row.opens, context, unused ? row.range : null)
147
+ missing = listed === null
148
+ facts = npmAddedDependency({ downloads, listed: listed && { ...listed, private: false }, published, unused }, context.now)
149
+ }
150
+ const settled = npmSettledDownloads(downloads, context.now)
151
+ const month = npmDownloadMonths(settled).at(-1)
152
+ return { ...row, ...facts.tier === null ? {} : { ...tierOf(facts.tier), ...settled ? month && { month: { total: month.total, from: month.from.toISOString().slice(0, 10) } } : { uncounted: true } },
153
+ ...facts.created ? { created: facts.created, gap: facts.gap } : {}, ...facts.installs ? { installs: facts.installs } : {}, ...missing ? { missing } : {} }
154
+ }
155
+ await Promise.all(Array.from({ length: 4 }, async () => {
156
+ while (next < asked.length && taken < MAX_TOLD_DEPENDENCIES) {
157
+ const at = asked[next++]
158
+ try {
159
+ if (await owned(told[at].opens)) continue
160
+ if (taken >= MAX_TOLD_DEPENDENCIES) break
161
+ taken++
162
+ told[at] = await tell(told[at])
163
+ } catch (error) { next = asked.length; throw error }
164
+ }
165
+ }))
166
+ return told
167
+ }
168
+
169
+ // What calls for a look at `version` of package `name`, as JSON holds it:
170
+ // malware first (malware.js npmMalwareFinding: its package taken down by
171
+ // npm, and npm's advisories of malware naming it where `advisories`, true
172
+ // unless given false, has them asked, OSV's too where `osv` is, true unless
173
+ // given false, as a private package's name is never sent it); against `oldVersion` where given,
174
+ // true for the one of its versions most likely free of what an attack
175
+ // injects, as risk picks it by npm's document (old-version.js npmOldVersion;
176
+ // none where none is below it), the one it picked said as `oldVersion`
177
+ // (compare.js npmCompareFindings's order), else what it holds (held.js
178
+ // npmHeldFindings), a deprecation first, and last
179
+ // one younger than `minimumReleaseAge` (two weeks unless given; 0 for none, as
180
+ // a caller's own scopes take), and the package's tier past the top 20,000
181
+ // (`popularity`, held.js npmVersionFindings, unless `popularity` is given
182
+ // false, as a caller's own scopes take); `files`, each file's stamps and what risk read of it; and
183
+ // `dependencies`, those added and changed against it, each with its flags,
184
+ // an added one with its package's tier and age where `downloads` (true unless
185
+ // given false) has them asked, and `dependencies` (true unless given false)
186
+ // too, but where `owned` says its package is the caller's own, as its own scopes
187
+ // take (none unless given). `cache` keeps what is read (registry.js),
188
+ // `packument` asks for a package's whole document and `tarball` for a
189
+ // version's tarball in risk's stead (registry.js), `now` is when it is asked, `signal` gives up on what is
190
+ // still asked of npm. A version npm removed, as it removes every one it finds
191
+ // malware in, is told of by its malware alone (`removed`), nothing of it left
192
+ // to read. Throws an NpmRiskError (registry.js), its `code` saying why, where
193
+ // npm has no such package or version (nor malware explaining one removed),
194
+ // answers past risk's bounds, or can't be asked.
195
+ export async function assessNpm(name, version, { oldVersion = null, fromEmpty = false, minimumReleaseAge = DEFAULT_MINIMUM_RELEASE_AGE, popularity = true, cache, packument, tarball, now = Date.now(), signal, downloads = true, dependencies = true, advisories = true, osv = true, owned = () => false } = {}) {
196
+ const context = { cache, packument, tarball, now, signal }
197
+ const facts = await npmPackageFacts(name, version, oldVersion, context)
198
+ const old = facts.oldVersion
199
+ if (old !== null && !facts.versions.has(old)) throw new NpmRiskError('version-not-found', `npm has no ${name}@${old}`)
200
+ // Its advisories, its lasting risk and its downloads need none of each
201
+ // other, so all are asked at once, and waited on in this order, so that a
202
+ // failure fails it as the first of them failing would.
203
+ const listed = facts.versions.has(version)
204
+ const advised = settledLater(advisories ? npmVersionAdvisories(name, version, osv, context) : Promise.resolve([]))
205
+ const keeping = listed ? settledLater(lasting(name, version, old, facts, context, fromEmpty)) : null
206
+ const counting = listed && downloads && popularity ? settledLater(npmDownloads(name, context)) : null
207
+ const malware = npmMalwareFinding({ takenDown: facts.takenDown, advisories: await advised })
208
+ if (!listed) {
209
+ if (!malware) throw new NpmRiskError('version-not-found', `npm has no ${name}@${version}`)
210
+ return { name, version, oldVersion: null, published: facts.removed.get(version), removed: true, findings: [malware], files: {}, dependencies: { added: [], changed: [] },
211
+ score: npmRiskScore([malware]) }
212
+ }
213
+ const { published, manifest } = facts.versions.get(version)
214
+ const kept = await keeping
215
+ const counted = await counting
216
+ const own = npmVersionFindings({ deprecated: facts.deprecated[version], published, minimumReleaseAge, now, known: Boolean(manifest.knownAccounts), scope: npmKnownScope(name),
217
+ provenance: Boolean(manifest.provenance), downloads: counted })
218
+ const was = old === null ? null : facts.versions.get(old).published
219
+ // Those it holds that nothing imports by another spec than the version
220
+ // compared with's, told by their new spec as an added one nothing imports
221
+ // is (`respecified`), and an added one nothing imports however it was
222
+ // flagged, as one moved from another kind of dependency is.
223
+ const unusedKeys = unusedKeysOf(kept)
224
+ const respecified = kept.dependencies.changed.filter(row => unusedKeys.has(keyOf(row))).map(row => ({ ...row, range: row.to }))
225
+ const told = downloads && dependencies ? await addedDependencies([...kept.dependencies.added, ...respecified], [was, published], context, owned, unusedKeys)
226
+ : [...kept.dependencies.added, ...respecified]
227
+ const added = told.slice(0, kept.dependencies.added.length)
228
+ const assessment = npmAssessmentOf(kept, malware ? [malware, ...own] : own, added, told.slice(added.length))
229
+ return { name, version, oldVersion: old, published, ...assessment, score: npmRiskScore(assessment.findings, { published, now, tier: counted ? npmDownloadTier(counted) : null }) }
230
+ }
231
+
232
+ // A dependency's key among a version's, by its kind and name.
233
+ const keyOf = row => `${row.kind ?? ''}\0${row.name}`
234
+
235
+ // `promise`, waited on later: its failure, if it fails before then, not
236
+ // thrown as unhandled meanwhile.
237
+ const settledLater = promise => { promise.catch(() => {}); return promise }
238
+
239
+ // The keys of the dependencies a version holds that nothing of it imports
240
+ // (`kept`, npmLastingRisk's: its `held-unused-deps`), those risk counts
241
+ // (dependencies.js npmUnusedDependencyCounted).
242
+ const unusedKeysOf = kept => new Set((kept.findings.find(({ id }) => id === 'held-unused-deps')?.dependencies ?? []).filter(npmUnusedDependencyCounted).map(keyOf))
243
+
244
+ // The dependencies nothing of a version imports (`held-unused-deps`), each
245
+ // graded by what risk tells of its package fresh (`told`, as
246
+ // addedDependencies tells it; new-dependency.js
247
+ // npmUnusedDependencySeverity), with its tier, when its package was first
248
+ // published and the version its spec installs where told: kept from the
249
+ // version compared with where there was one (`compared`) and it named it as
250
+ // the same kind of dependency by the same spec: not one marked new, nor
251
+ // `added` (its kind and name new, as one moved from another kind is), nor
252
+ // `changed` (each said with the spec it had, `was`); one risk doesn't count
253
+ // (dependencies.js npmUnusedDependencyCounted) a notice, told all the same.
254
+ // The finding as its most severe.
255
+ function unusedGraded(finding, told, compared, { added, changed }) {
256
+ const facts = new Map(told.map(row => [keyOf(row), row])), was = new Map(changed.map(row => [keyOf(row), row.from]))
257
+ const anew = new Set(added.map(keyOf))
258
+ const dependencies = finding.dependencies.map(row => {
259
+ const of = facts.get(keyOf(row))
260
+ return { ...row, ...was.has(keyOf(row)) ? { was: was.get(keyOf(row)) } : {}, severity: npmUnusedDependencyCounted(row)
261
+ ? npmUnusedDependencySeverity(of, compared && !row.added && !anew.has(keyOf(row)) && !was.has(keyOf(row))) : 'notice',
262
+ ...of?.level ? { tier: of.tier, level: of.level } : {}, ...of?.created ? { created: of.created, gap: of.gap } : {}, ...of?.installs ? { installs: of.installs } : {},
263
+ ...of?.missing ? { missing: true } : {} }
264
+ })
265
+ return { ...finding, severity: SEVERITIES[Math.min(...dependencies.map(row => SEVERITIES.indexOf(row.severity)))], dependencies }
266
+ }
267
+
268
+ // An assessment of what of it holds whatever the time (`kept`,
269
+ // npmLastingRisk's) and what is told fresh: the open version's own findings
270
+ // (`own`, held.js npmVersionFindings, and its malware), its malware and
271
+ // deprecation first and the rest last, its tier as JSON holds it (tierOf),
272
+ // and its dependencies `added`, as addedDependencies tells them, those it
273
+ // holds by another spec that nothing imports beside them (`respecified`),
274
+ // for its unused dependencies' grades. The managed
275
+ // server's (assessNpm), and a test's of versions it holds.
276
+ const FIRST = new Set(['malware', 'deprecated'])
277
+ export function npmAssessmentOf(kept, own, added = kept.dependencies.added, respecified = []) {
278
+ return {
279
+ findings: [...own.filter(({ id }) => FIRST.has(id)), ...kept.findings.map(finding => finding.id === 'held-unused-deps'
280
+ ? unusedGraded(finding, [...added, ...respecified], Boolean(kept.dependencies.held), kept.dependencies) : finding),
281
+ ...own.filter(({ id }) => !FIRST.has(id)).map(finding => finding.id === 'popularity' ? { ...finding, ...tierOf(finding.tier) } : finding)],
282
+ files: kept.files,
283
+ dependencies: { ...kept.dependencies, added },
284
+ }
285
+ }