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

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-563V5VVT.js +266 -0
  111. package/out/chunk-5AVGKFMR.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-5UJHJWFU.js +4 -0
  116. package/out/chunk-6CCSTJWB.js +1 -0
  117. package/out/chunk-6VOB5WXX.js +2 -0
  118. package/out/chunk-6WMQVUT2.js +3 -0
  119. package/out/chunk-6Y6PZ2BU.js +9 -0
  120. package/out/chunk-74JZTGTO.js +11 -0
  121. package/out/chunk-7BKKYVWR.js +7 -0
  122. package/out/chunk-7C7DWL7I.js +1 -0
  123. package/out/chunk-7HP2VYIC.js +2 -0
  124. package/out/chunk-7YFJUT2P.js +1 -0
  125. package/out/chunk-7YO4QV5P.js +1 -0
  126. package/out/chunk-A4BBBHQU.js +1 -0
  127. package/out/chunk-B64CTMVU.js +4 -0
  128. package/out/chunk-BEKYZCPX.js +14 -0
  129. package/out/chunk-C4Z6ZBXV.js +10 -0
  130. package/out/chunk-CU3FPB3P.js +1 -0
  131. package/out/chunk-DEVAORP2.js +1 -0
  132. package/out/chunk-DOYTKRIR.js +53 -0
  133. package/out/chunk-DUASGS2O.js +8 -0
  134. package/out/chunk-EEJA7JBI.js +13 -0
  135. package/out/chunk-FSU2PLJL.js +1 -0
  136. package/out/chunk-GO7FT4OD.js +1 -0
  137. package/out/chunk-GX5UXWT4.js +1 -0
  138. package/out/chunk-H4FEC57K.js +1 -0
  139. package/out/chunk-ITUKKCCK.js +10 -0
  140. package/out/chunk-IYPY47MC.js +1 -0
  141. package/out/chunk-K4XJHM2T.js +1 -0
  142. package/out/chunk-LBD3M3J6.js +1 -0
  143. package/out/chunk-LVSLM7HB.js +1 -0
  144. package/out/chunk-LVW7WWV4.js +1 -0
  145. package/out/chunk-N57OWFKY.js +15 -0
  146. package/out/chunk-NRPC74AM.js +1 -0
  147. package/out/chunk-OAWQNDAO.js +3 -0
  148. package/out/chunk-OOJM4CTU.js +1 -0
  149. package/out/chunk-PEE47ANR.js +1 -0
  150. package/out/chunk-PKLVXUEI.js +11 -0
  151. package/out/chunk-PLGH63UC.js +2 -0
  152. package/out/chunk-PNHBZGDD.js +1 -0
  153. package/out/chunk-PTFYDJUP.js +1 -0
  154. package/out/chunk-PWFUOLZK.js +17 -0
  155. package/out/chunk-PXK7EU4V.js +1 -0
  156. package/out/chunk-QD3IBSF4.js +7 -0
  157. package/out/chunk-QI66YA4F.js +1 -0
  158. package/out/chunk-QRKFPLA4.js +80 -0
  159. package/out/chunk-R3NZFXVR.js +1 -0
  160. package/out/chunk-R5O6FAW4.js +2 -0
  161. package/out/chunk-RPNO56H2.js +1 -0
  162. package/out/chunk-SSPDMA6D.js +1 -0
  163. package/out/chunk-SZWPYLPC.js +1 -0
  164. package/out/chunk-TUII34K2.js +1 -0
  165. package/out/chunk-UJP4PQJA.js +1 -0
  166. package/out/chunk-UJSJMXEG.js +1 -0
  167. package/out/chunk-UR4WYMIP.js +1 -0
  168. package/out/chunk-VHEXSGGP.js +1 -0
  169. package/out/chunk-VYYUMYVW.js +9 -0
  170. package/out/chunk-WCDYSIKG.js +1 -0
  171. package/out/chunk-WFBVS4QI.js +1 -0
  172. package/out/chunk-XM4RLC4A.js +2 -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 +354 -0
  199. package/risk/files/hosts.js +206 -0
  200. package/risk/files/readability.js +185 -0
  201. package/risk/files/runs-code.js +568 -0
  202. package/risk/files/script.js +410 -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 +269 -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 +1915 -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 +158 -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 +6327 -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,568 @@
1
+ // What a file's code can do beyond itself, for a reviewer: run a string as
2
+ // code (eval, Function with new or without, node:vm, Module's _compile) or
3
+ // other programs (child_process), reach for credentials (a credential file's
4
+ // path, a token's name, `gh auth token`, the cloud's instance metadata, the
5
+ // GitHub Actions runner's process or another process's memory, trufflehog)
6
+ // or the whole environment, publish to npm (`npm publish`, `/-/whoami`, a
7
+ // registry PUT), run a shell command fetching or handing off code (`curl …
8
+ // https://…`, `| sh`, `powershell -…`, rundll32, osascript, `/dev/tcp/`),
9
+ // and name a place code runs from again later (a shell's rc file,
10
+ // LaunchAgents, systemd's user units, `.claude/settings.json`,
11
+ // `.vscode/tasks.json`), as a file's stamps say (stamps.js) and a version's
12
+ // findings name what is new to a file. A shell script (`.sh`, `.bat`, `.ps1`
13
+ // …, or a shell's hashbang) is read whole for the last two and the
14
+ // credentials a command reaches for, its text its commands.
15
+ // A quick read, not a parse: a script's code with its strings, regular
16
+ // expressions and comments set aside as script.js tells them, its
17
+ // strings read for the paths, names and commands they hold, a module by the
18
+ // string naming it where it is loaded (`require('child_process')`,
19
+ // `import … from 'node:child_process'`); a declaration file (.d.ts) read as
20
+ // TypeScript, as node runs it (stripping its types, `declare function
21
+ // eval(…)` among them, a function named, not called). A call written not to
22
+ // read as one (`window['ev' + 'al']`) goes unseen.
23
+ //
24
+ // Labels to assist a reviewer, making the plain cases less likely to be
25
+ // missed, not a check to rely on: code can come to eval, Function,
26
+ // child_process, credentials or the registry in more ways than any reading of
27
+ // its text tells (a name built as it runs, `(…).constructor('…')`, a module
28
+ // required by a name computed, a path or URL put together, the
29
+ // registry's host read from config), so a file without a label is not one
30
+ // without them.
31
+ import { NPM_MAX_FOUND, jsMatchAll, jsOffset, lineOf, npmIsScript } from './script.js'
32
+ import { stretched } from './characters.js'
33
+
34
+ // What may come between a call's parts: blanks and comments
35
+ // (`Function/* note */(…)`), each comment ending at its first `*/`.
36
+ const COMMENT = String.raw`/\*(?:[^*]|\*(?!/)){0,100}\*/|//[^\n]{0,100}\n`
37
+ const GAP = String.raw`(?:\s|${COMMENT})*`
38
+ // Function called: as it is, optionally (`Function?.(…)`), or by its call or
39
+ // apply (`Function.call(null, …)`), each making a function of its last
40
+ // argument.
41
+ const FUNCTION_CALLED = String.raw`(?:${GAP}\??\.${GAP}(?:call|apply))?${GAP}(?:\?\.${GAP})?\(`
42
+ // But not where the function made returns `this` alone
43
+ // (`Function('return this')()`, `new Function("return this;")`): the global
44
+ // object, as older code finds it where `globalThis` may not be, lodash's and
45
+ // core-js's among them, running nothing of its own.
46
+ const GLOBAL_FOUND = String.raw`(?!${GAP}(?<quote>["'\u0060])\s*return\s+this\s*;?\s*\k<quote>${GAP}\))`
47
+ // The environment, whole: not one variable of it (`process.env.HOME`,
48
+ // `process.env[name]`).
49
+ const ENV = String.raw`process${GAP}\.${GAP}env(?![\p{ID_Continue}$])(?!${GAP}(?:\??\.|\[))`
50
+ // Its values read out whole (`JSON.stringify(process.env)`,
51
+ // `Object.entries(process.env)`, `new URLSearchParams(process.env)`), or a
52
+ // copy of them (`JSON.stringify({ ...process.env })`), not as a program is
53
+ // handed it (`{ env: process.env }`, `{ ...process.env }`), nor its
54
+ // names listed (`Object.keys(process.env)`, as debug picks its DEBUG_ ones),
55
+ // nor one variable asked for (`'HOME' in process.env`), nor its entries or
56
+ // values picked among as they are read (`Object.entries(process.env)
57
+ // .reduce(…)`, as npm's agent finds its proxy's, `.filter(…)`, `.find(…)`).
58
+ // Matched to its `(`, the rest looked ahead on, so a copy's `{` is one
59
+ // jsMatchAll reads.
60
+ const PICKED = String.raw`${GAP}\)${GAP}\??\.${GAP}(?:filter|find|findLast|some|every|reduce)(?![\p{ID_Continue}$])`
61
+ const COPY = String.raw`(?:\{${GAP}\.\.\.${GAP})?`
62
+ const ENV_WHOLE = String.raw`(?=JSON|Object|URLSearchParams)(?<![\p{ID_Continue}$.])(?:JSON${GAP}\.${GAP}stringify|URLSearchParams)${GAP}\((?=${GAP}${COPY}${ENV})`
63
+ + String.raw`|(?=Object)(?<![\p{ID_Continue}$.])Object${GAP}\.${GAP}(?:values|entries)${GAP}\((?=${GAP}${COPY}${ENV}(?!${PICKED}))`
64
+ // What a shell command, and a place code runs from again later, may say
65
+ // (SHELL, PERSISTENCE), and what of the credentials is told in any case:
66
+ // trufflehog (TRUFFLEHOG), GCP's metadata host, and a string naming gh
67
+ // (`'gh'`, `'/usr/bin/gh'`, `'GH.EXE'`) as a program run with its arguments
68
+ // apart is, whatever is between them (`'gh', ['auth', /* … */ 'token']`).
69
+ // A shell, as a hashbang or a pipe names it (`#!/bin/ksh`, `| fish`,
70
+ // BusyBox's `ash`, `| powershell`); after a pipe, by its path or env's too (`| /bin/sh`,
71
+ // `| /usr/bin/env bash`), env's options, their values, and settings before
72
+ // it on its line (`env -S`, `env -u NAME`, `env MODE=x`).
73
+ export const SHELLS = String.raw`(?:(?:ba|z|da|k|mk|c|tc|fi|a)?sh|pwsh|powershell)`
74
+ const SHELL_PATH = String.raw`(?:(?:\/[\w.-]{1,40}){0,8}\/)?(?:env(?:[^\S\n]+(?:-[\w-]{1,20}(?:=\S{0,100}|[^\S\n]+(?!-)[^\s=]{1,100})?|\w{1,40}=\S{0,100})){0,6}[^\S\n]+)?`
75
+ const SHELL_OR_PLACE = String.raw`curl|wget|\|\s*(?:sudo|${SHELL_PATH}${SHELLS})|powershell|pwsh|rundll32|osascript|mshta|certutil|bitsadmin|\/dev\/(?:tcp|udp)|iex|invoke-expression`
76
+ + String.raw`|\$profile|profile\.ps1|config\.fish|conf\.d|bashrc|zshrc|zshenv|zlogin|bash_profile|bash_login|zprofile|\.profile|Launch(?:Agents|Daemons)|systemd|systemctl|\.claude|\.vscode|trufflehog|metadata\.google\.internal|["'\x60\/\\]gh(?:\.exe)?["'\x60]`
77
+ // What says one of the credentials a command, an endpoint or a process holds
78
+ // (HOLDS' gh-token, cloud-metadata, runner-worker and process-memory), as
79
+ // code and its strings may say it, in the case it is written: `gh auth
80
+ // token`'s command, lowercase as gh reads it, the metadata endpoint's
81
+ // address, `Runner.Worker` and `/proc/`; what is told in any case is
82
+ // SHELL_OR_PLACE's.
83
+ const SAYS_CREDENTIALS = String.raw`auth(?:[^\S\n]|[\\^\x60]\r?\n)+token|169\.254\.169\.254|Runner\.Worker|\/proc\/`
84
+ // Any of RUNS_CODE, as a text may say it: a shell command or a place in any
85
+ // case, as Windows' are written (`PowerShell`, `RUNDLL32`).
86
+ const SAYS_SHELL_OR_PLACE = new RegExp(SHELL_OR_PLACE, 'iu')
87
+ const SAYS_CODE = new RegExp(String.raw`eval|Function${FUNCTION_CALLED}|vm["'\x60]|_compile|child_process|\.(?:npmrc|ssh|aws)|_TOKEN|${SAYS_CREDENTIALS}|(?:stringify|values|entries|URLSearchParams)${GAP}\(${GAP}(?:\{${GAP}\.\.\.${GAP})?process${GAP}\.${GAP}env|publish|whoami|registry\.(?:npmjs\.org|yarnpkg\.com)`, 'u')
88
+ const SAYS_ANY = { test: text => SAYS_CODE.test(text) || SAYS_SHELL_OR_PLACE.test(text) }
89
+
90
+ // A name read as one, not as a property's (`vm.eval`, `vm . eval`, a spread's
91
+ // `...eval` aside) or part of another's (`evaluate`), or as the global
92
+ // object's (`window.eval`, `globalThis?.eval`); looked back on only where the
93
+ // name or the global object's starts, so a long run of blanks reads in one go.
94
+ const GLOBAL = 'globalThis|window|global|self'
95
+ const named = name => String.raw`(?=${GLOBAL}|${name})(?<![\p{ID_Continue}$#])(?<!(?<!\.)\.${GAP})(?<!\bfunction${GAP})(?:(?:${GLOBAL})${GAP}\??\.${GAP})?${name}`
96
+ // A token's name, as its value is read (`process.env.GITHUB_TOKEN || null`,
97
+ // `env.NPM_TOKEN`, `||=`), not where its absence is told
98
+ // (`!process.env.GITHUB_TOKEN`, `!(process.env.GITHUB_TOKEN)`) or it is
99
+ // dropped (`delete process.env.NPM_TOKEN`), the token the whole of what is
100
+ // negated or deleted (ABSENT_ENDS: not `!process.env.GITHUB_TOKEN.trim()`,
101
+ // `!(process.env.GITHUB_TOKEN === x)`), nor where it is only set
102
+ // (`process.env.NPM_TOKEN = token`), but for a default as it is destructured
103
+ // (`{ NPM_TOKEN = '' } = process.env`, a line of its own too), nor declared
104
+ // (`const NPM_TOKEN = …`, TypeScript's `let GITHUB_TOKEN: string`), nor typed
105
+ // (a member, `GITHUB_TOKEN?: string`, `NPM_TOKEN: string | undefined`, or a
106
+ // parameter): looked back on, a few names and dots at most, only where the
107
+ // name starts.
108
+ const TOKEN_VAR = '(?:NPM|GITHUB)_TOKEN'
109
+ const SET = String.raw`\s{0,4}=(?![=>])`
110
+ // A name typed, not read: optional (`?:`), or a primitive's type after it,
111
+ // as no destructuring names what it reads (`{ NPM_TOKEN: token }`); not
112
+ // `undefined`, a ternary's choice (`x ? env.NPM_TOKEN : undefined`).
113
+ const TYPED = String.raw`\s{0,4}(?:\?:|:\s{0,4}(?:string|number|boolean|unknown|any)(?![\p{ID_Continue}$]))`
114
+ // What may follow a token negated or deleted whole: the parentheses around
115
+ // it closed, nothing read of what they hold (`!(token).trim()`, `!(token)()`), or else what
116
+ // ends an operand (`&&`, `||`, `??`, a ternary's `?`, `;`, `,`, `:`, `}`, `]`,
117
+ // the line's end).
118
+ const ABSENT_ENDS = String.raw`\s{0,4}(?:(?:\)\s{0,4}){1,4}(?![\s)]{0,8}(?:[.[(]|\?\.))|&&|\|\||\?\?|\?(?!\.)|[;,:}\]]|$)`
119
+ const TOKEN_NAME = String.raw`(?=NPM|GITHUB)(?<![\p{ID_Continue}$])(?<!\b(?:const|let|var)\s{1,4})`
120
+ + String.raw`(?:(?<!(?:!|\bdelete\s)[\s(]{0,4}(?:[\p{ID_Continue}$]{1,64}\s{0,4}\??\.\s{0,4}){0,4})|(?!${TOKEN_VAR}${ABSENT_ENDS}))`
121
+ + String.raw`${TOKEN_VAR}(?![\p{ID_Continue}$])(?!${TYPED})(?:(?!${SET})|(?<=[{,]\s{0,64}${TOKEN_VAR}))`
122
+ // eval as a key, not read: a property's name or a member typed, coming
123
+ // first or after another (`{ eval: x }`, WebAuthn's `extensions: { prf: {
124
+ // eval: { first } } }`, `const { eval: run } = vm`, `{ a: 1, eval: 2 }`, `a;
125
+ // eval: T`); not where anything else comes before it, as a ternary's choice
126
+ // does, whole or last of its branch (`c ? eval : f`, `c ? x && eval : f`),
127
+ // nor a worker told to run its string as code (`new Worker(code, { eval:
128
+ // true })`, minified `{eval:!0}`), nor eval destructured from the global
129
+ // object (`const { eval: run } = globalThis`, `({ a, eval: run } = window)`).
130
+ const KEYED = String.raw`(?<=[{,;]${GAP}eval)\s*:(?!${GAP}(?:true|!0)(?![\p{ID_Continue}$]))`
131
+ + String.raw`(?![^{};]{0,200}\}${GAP}=${GAP}(?:${GLOBAL})(?![\p{ID_Continue}$]))`
132
+ // What a script's code does, read beside its strings, regular expressions and
133
+ // comments as script.js's jsMatchAll tells them, each taking its own text,
134
+ // and in none of them: eval,
135
+ // not a property TypeScript types as optional (`eval?: T`, JavaScript's `?:`
136
+ // none of its own) nor a key (KEYED), and Function called, but to find the
137
+ // global object;
138
+ // Module's _compile, called
139
+ // (`module._compile(source, filename)`) or hooked (`Module.prototype._compile
140
+ // = …`), not named in a type (`typeof Module.prototype._compile`); a token's
141
+ // name, its value read; the environment read out whole; and a PUT request's
142
+ // call (`axios.put(…)`).
143
+ const READS = String.raw`(?<eval>${named('eval')}(?![\p{ID_Continue}$])(?!\s*\?:)(?!${KEYED}))|(?<function>${named('Function')}${FUNCTION_CALLED}${GLOBAL_FOUND})`
144
+ + String.raw`|(?<compile>\.${GAP}_compile(?:${FUNCTION_CALLED}|${GAP}=(?![=>])))|(?<tokens>${TOKEN_NAME})|(?<env>${ENV_WHOLE})|(?<put>\.${GAP}put${GAP}\()`
145
+ // What a string holds that a kind is: a credential file's path, npm's, SSH's
146
+ // or AWS's (`~/.npmrc`, `.ssh/id_rsa`, `.aws/credentials`, not `x.npmrc` or
147
+ // `.sshd`); a token's name where it is read, the string the name alone (a
148
+ // key, `process.env['GITHUB_TOKEN']`) or the name expanded (a shell's or
149
+ // .npmrc's `$GITHUB_TOKEN`, `${NPM_TOKEN}`, cmd's `%NPM_TOKEN%`), not where
150
+ // text names it (`requires a token in GITHUB_TOKEN`); a credential a
151
+ // command, an endpoint or a process holds (`gh auth token`, the cloud's
152
+ // metadata endpoint, `Runner.Worker`, `/proc/<pid>/mem`, trufflehog, as
153
+ // HOLDS tells them); a command publishing (`npm publish`, `pnpm
154
+ // publish …`, options before it as npm takes them, `npm --tag next publish`)
155
+ // or the registry's who-am-I, a publisher's first step with a token. And the
156
+ // registry, where a PUT request is one publishing (REGISTRY_URL).
157
+ // Told first of a string by what any of them says.
158
+ const HOLDS_CODE = new RegExp(String.raw`\.(?:npmrc|ssh|aws)|_TOKEN|${SAYS_CREDENTIALS}|publish|whoami|registry\.`, 'u')
159
+ const HOLDS_ANY = { test: text => HOLDS_CODE.test(text) || SAYS_SHELL_OR_PLACE.test(text) }
160
+ // Before a token's name as a key, its absence told or the token dropped, as
161
+ // TOKEN_NAME tells of one as a name (`!process.env['GITHUB_TOKEN']`,
162
+ // `delete process.env['NPM_TOKEN']`): a few names and dots at most, and
163
+ // after it, its key's bracket closed, what may follow it whole (ABSENT_ENDS,
164
+ // not `!process.env['NPM_TOKEN'].length`). After it, the token set
165
+ // (`process.env['NPM_TOKEN'] = token`).
166
+ const KEY_SET = new RegExp(String.raw`^\s{0,4}\]${SET}`, 'u')
167
+ const KEY_ENDS = new RegExp(String.raw`^\s{0,4}\]${ABSENT_ENDS}`, 'mu')
168
+ const KEY_ABSENT = /(?:!|\bdelete\s)[\s(]{0,4}(?:[\p{ID_Continue}$]{1,64}\s{0,4}\??\.\s{0,4}){0,4}[\p{ID_Continue}$]{1,64}\s{0,4}(?:\?\.\s{0,4})?\[\s{0,4}$/u
169
+ // A string's text as JavaScript reads it, its escapes decoded (`\x2f`,
170
+ // `\u002f`, `\u{2f}`, sloppy mode's octal `\057`, `\/`, `\\`, `\n`, `\t`), a
171
+ // line continued (a backslash before a line's end) joined: what a URL in it
172
+ // spells, however its separators are escaped or its lines broken.
173
+ const ESCAPE = /\\(?:x([\da-f]{2})|u([\da-f]{4})|u\{([\da-f]{1,6})\}|([0-3][0-7]{0,2}|[4-7][0-7]?)|(\r\n|[^]))/giu
174
+ const CONTROLS = new Map([['n', '\n'], ['r', '\r'], ['t', '\t'], ['b', '\b'], ['f', '\f'], ['v', '\v']])
175
+ const LINE_ENDS = new Set(['\n', '\r', '\r\n', '\u2028', '\u2029'])
176
+ const unescaped = text => text.replaceAll(ESCAPE, (escape, hex, unit, point, octal, char) => {
177
+ if (char !== undefined) return LINE_ENDS.has(char) ? '' : CONTROLS.get(char) ?? char
178
+ const code = octal === undefined ? Number.parseInt(hex ?? unit ?? point, 16) : Number.parseInt(octal, 8)
179
+ return code <= 0x10_FFFF ? String.fromCodePoint(code) : escape
180
+ })
181
+ // The registry's host in a string, as JavaScript reads it (unescaped) and a
182
+ // URL's parsing does, its tabs and line breaks gone, as a URL's authority: after two slashes, backslashes URLs take for them too, or
183
+ // after an http(s) scheme's colon (its slashes optional there, as URLs take
184
+ // them), its userinfo between, to its last `@` before the URL's path, query or
185
+ // fragment, quotes and all, 256
186
+ // characters at most, as credentials are, so a URL's every start is read
187
+ // in bounded time (`https://registry.npmjs.org/`, `.npmrc`'s
188
+ // `//registry.npmjs.org/`, `https://${token}@registry.npmjs.org/`,
189
+ // `https:\\\\registry.npmjs.org`, `https:\x2f\x2fregistry.npmjs.org`,
190
+ // `https:registry.npmjs.org`); or starting the string, its whole or before its
191
+ // path, port, query or fragment (npm-profile's `'registry.npmjs.org'`, a URL joined,
192
+ // `'https://' + 'registry.npmjs.org/'`). Not in prose, as anywhere else
193
+ // (`registry.npmjs.org</span>`, `asks registry.npmjs.org`, `[registry.npmjs.org]`,
194
+ // `note:registry.npmjs.org`, `user@registry.npmjs.org`, `docs/registry.npmjs.org`).
195
+ const REGISTRY_HOST = String.raw`registry\.(?:npmjs\.org|yarnpkg\.com)`
196
+ const REGISTRY_URL = new RegExp(String.raw`(?:[/\\]{2}|\bhttps?:[/\\]?)(?:[^/\\?#]{0,256}@)?${REGISTRY_HOST}|^(["'\u0060])${REGISTRY_HOST}(?=\1$|[/:?#])`, 'iu')
197
+ const URL_STRIPPED = /[\t\n\r]/gu
198
+ // A shell command fetching or handing off code: curl or wget given a URL
199
+ // (`curl -fsSL https://…`, `ftp://`, `https:\/\/` as a string may escape
200
+ // it), long options before it too (`-H "Authorization: …"`), a quoted
201
+ // argument's `;`, `|` and `&` no command's end (`-d "a=1&b=2"`), its line
202
+ // continued too (`curl -fsSL \⏎ https://…`, `curl\⏎`, cmd's `^`,
203
+ // PowerShell's backtick), not the next line's, the next command's (`curl
204
+ // --version; echo https://…`) nor the next curl's, a download piped to a
205
+ // shell (`… | sh`, `payload|sudo -E -- bash`, `| ksh`, `| /bin/sh`, `cat
206
+ // hash | sh`, not a list of shells, `bash|zsh|sh`, a regular expression's
207
+ // alternatives, `(x|ss|sh)$`, nor `|| sh`, nor an escaped pipe, grep's
208
+ // `'bash\|zsh'`), PowerShell given options (`powershell -nop -enc …`),
209
+ // rundll32, osascript and mshta, certutil fetching (`certutil -urlcache -f
210
+ // http://…`) and bitsadmin's `/transfer`, and bash's network redirection
211
+ // (`/dev/tcp/host/port`), and PowerShell running what it fetches (`iex (iwr
212
+ // https://…).Content`, `irm https://… | iex`, long options before the pipe
213
+ // too, `iwr … -Headers @{…} | iex`, `IEX (New-Object
214
+ // Net.WebClient).DownloadString(…)`), its line continued by a backtick too,
215
+ // not the next pipeline's, `iex $x && iwr …`); in any case, as Windows' are
216
+ // written, and by its path (`/usr/bin/curl`).
217
+ const CONTINUED = String.raw`[\\^\u0060]\r?\n`
218
+ const ON_ITS_LINE = String.raw`(?:[^\n;|&]|${CONTINUED})`
219
+ // PowerShell's own, its backtick alone (not `\⏎`, a new statement there).
220
+ const POWERSHELL_CONTINUED = String.raw`\u0060\r?\n`
221
+ // A command's text up to its end, its arguments' quotes kept, a quote left
222
+ // open (`it's`) as any other character.
223
+ const ARGUMENTS = String.raw`(?:[^\n;|&"']|${CONTINUED}|"[^"\n]*"|'[^'\n]*'|"(?![^"\n]*")|'(?![^'\n]*'))`
224
+ const APART = String.raw`(?:[^\S\n]|${CONTINUED})`
225
+ // A command's options, a few, and their values (`-E`, `-u root`, `--now`),
226
+ // on its line or one continued.
227
+ const OPTIONS = String.raw`(?:${APART}+--?\w[\w-]*(?:=\S*|${APART}+(?!-)\S+)?){0,6}?`
228
+ const SHELL = String.raw`(?<![\w.-])(?:curl|wget)(?:\.exe)?${APART}(?:(?!(?:curl|wget)(?![\w-]))${ARGUMENTS}){0,2000}?["']?(?:https?|s?ftps?|scp):\\?\/\\?\/|(?<![|\\]|(?<![\w.\/-])${SHELLS}\s*)\|\s*(?:sudo${OPTIONS}(?:${APART}+--)?${APART}+)?${SHELL_PATH}${SHELLS}(?![\w)|$-])`
229
+ + String.raw`|(?<![\w.-])(?:powershell|pwsh)(?:\.exe)?[^\S\n]+-\w|(?<![\w.-])(?:rundll32|osascript|mshta)(?:\.exe)?(?![\w-])`
230
+ + String.raw`|(?<![\w.-])certutil(?:\.exe)?${APART}${ON_ITS_LINE}{0,100}?-urlcache|(?<![\w.-])bitsadmin(?:\.exe)?${APART}${ON_ITS_LINE}{0,100}?\/transfer|\/dev\/(?:tcp|udp)\/`
231
+ + String.raw`|(?<![\w$.-])(?:iex|invoke-expression)(?![\w-])(?:[^\n;|&]|${POWERSHELL_CONTINUED}){0,200}?(?:(?<![\w$.-])(?:iwr|irm|invoke-webrequest|invoke-restmethod)(?![\w-])|\.downloadstring\s*\()`
232
+ + String.raw`|(?<![\w$.-])(?:iwr|irm|invoke-webrequest|invoke-restmethod)(?![\w-])(?:(?!(?:iwr|irm|invoke-webrequest|invoke-restmethod)(?![\w-]))(?:[^\n;|&]|${POWERSHELL_CONTINUED})){0,2000}\|\s*(?:iex|invoke-expression)(?![\w-])`
233
+ // A place code runs from again later: a shell's rc file (`~/.bashrc`,
234
+ // `.zshrc`, `.zshenv`, `.zlogin`, `.bash_profile`, `.bash_login`,
235
+ // `.zprofile`, and `.profile` in a path, `~/.profile`, not a selector's
236
+ // class, nor one after another, `.nav`), PowerShell's profile (`$PROFILE`,
237
+ // `Microsoft.PowerShell_profile.ps1`, all hosts' `PowerShell\Profile.ps1`,
238
+ // `$PSHOME\Profile.ps1`, not another folder's), macOS's LaunchAgents and
239
+ // LaunchDaemons, fish's (`~/.config/fish/config.fish`, `conf.d`), systemd's
240
+ // user units (`~/.config/systemd/user`, `systemctl --user enable`, other
241
+ // options before it, `--now`, or `--user` after it in its command,
242
+ // `systemctl enable --user`; not `start`, which runs a unit once), and the
243
+ // settings an AI agent or an editor runs commands from on its own
244
+ // (`.claude/settings.json`, `.vscode/tasks.json`); in any case, as Windows'
245
+ // and macOS's paths are, a path's separator a slash or a backslash, escaped
246
+ // in a string as code writes it (`"\\.claude\\settings.json"`, `\/`).
247
+ const SEPARATOR = String.raw`(?:\\?\/|\\{1,2})`
248
+ const PERSISTENCE = String.raw`(?<![\w.-])\.(?:bashrc|zshrc|zshenv|zlogin|bash_profile|bash_login|zprofile)(?![\w-])|(?<=[\/\\])(?<!\.[\w-]*[\/\\])\.profile(?![\w-])|(?<![\w-])Launch(?:Agents|Daemons)(?![\w-])|systemd${SEPARATOR}user(?![\w-])|fish${SEPARATOR}(?:config\.fish|conf\.d)(?![\w-])`
249
+ + String.raw`|(?<![\w$])\$profile(?![\w-])|(?<![\w.-])Microsoft\.\w+_profile\.ps1(?![\w-])|(?:PowerShell|\$pshome)${SEPARATOR}profile\.ps1(?![\w-])|systemctl${OPTIONS}${APART}+--user${OPTIONS}${APART}+enable(?![\w-])|systemctl${OPTIONS}${APART}+enable(?:${APART}+[^\s;|&]+){0,6}?${APART}+--user(?![\w-])|(?<![\w.-])\.claude${SEPARATOR}settings(?:\.local)?\.json|(?<![\w.-])\.vscode${SEPARATOR}tasks\.json`
250
+ // Credentials a command, an endpoint or a process holds, as recent npm
251
+ // malware reached for them: the GitHub CLI printing its token (`gh auth
252
+ // token`, by its path too, `gh.exe`, its name in any case as Windows runs
253
+ // it, `GH.EXE`, its line continued as a shell command's, `gh auth \⏎
254
+ // token`), not its other commands (`gh auth status`); the cloud's
255
+ // instance metadata endpoint, AWS's, Azure's and GCP's
256
+ // (`http://169.254.169.254/latest/meta-data/…`, `metadata.google.internal`,
257
+ // in any case as DNS reads it), as a host whole (HOST_START, HOST_END): not
258
+ // an address or a host either starts or ends (`169.254.169.2540`,
259
+ // `foo-169.254.169.254`, `169.254.169.254.example.com`,
260
+ // `metadata.google.internal.example.com`; a trailing dot,
261
+ // `metadata.google.internal.`, its host still), nor in a URL's path or
262
+ // userinfo (`https://example.com/169.254.169.254`,
263
+ // `https://169.254.169.254@example.com`; WHATWG's `\@` a path's, its host
264
+ // the address still), nor Azure's instance metadata
265
+ // (`169.254.169.254/metadata/instance/compute`, after a port too, `:80`, its
266
+ // slashes escaped too, `\/`), a machine's region and size, not its
267
+ // identity's token, as msal asks it; the GitHub Actions runner's process
268
+ // (`Runner.Worker`, `Runner.Worker.exe`, `GitHub.Runner.Worker`), whose
269
+ // memory holds a job's secrets, not in another name (`MyRunner.Worker`,
270
+ // `Runner.Workers`) nor case (`runner.worker`); another process's memory, by
271
+ // its pid's digits or a glob (`/proc/1234/mem`, `/proc/*/mem`,
272
+ // `/proc/[0-9]*/mem`) or a substitution (`/proc/${pid}/mem`, a shell's
273
+ // `$pid`, `$(pgrep …)` and backticks, Python's `{pid}`, a format's `%d`),
274
+ // not a process's own (`/proc/self/mem`) nor what else /proc tells of one
275
+ // (`/proc/1234/status`); and trufflehog, a scanner for secrets, in any case
276
+ // (`TruffleHog`), not in another name (`trufflehogs`, `TRUFFLEHOG_VERSION`).
277
+ const GH_TOKEN = String.raw`(?<![\w.-])[gG][hH](?:\.[eE][xX][eE])?${APART}+auth${APART}+token(?![\w-])`
278
+ const PID = String.raw`[\d*?[\]-]{1,20}|\$\{[^}\n]{0,100}\}|\$\([^)\n]{0,200}\)|\u0060[^\u0060\n]{0,200}\u0060|\$\w{1,64}|\{\w{0,64}\}|%d`
279
+ // A host whole, a URL's or alone: not after a word, a dot or a hyphen, nor a
280
+ // path's slash (`https://example.com/169.254.169.254`), a URL's two
281
+ // (`//`, `\/\/`) its start after a scheme's colon or alone
282
+ // (`//169.254.169.254`), not after a path's character
283
+ // (`https://example.com//169.254.169.254`); nor before more of a name or
284
+ // userinfo's `@` (`https://169.254.169.254@example.com`, `…:password@`).
285
+ const HOST_START = String.raw`(?<![\w.-]|(?<![\/\\])\/|[\w.~%-](?:\\?\/){2})`
286
+ const HOST_END = String.raw`(?![\w-]|\.[\w-]|(?::[^\s\/@'"\x60]{0,64})?@)`
287
+ const TRUFFLEHOG = String.raw`(?<![\p{ID_Continue}$-])trufflehog(?![\p{ID_Continue}$-])`
288
+ const HOLDS = [
289
+ ['credential-files', /(?<![\w$.-])\.(?:npmrc|ssh|aws)(?![\w-])/gu],
290
+ ['tokens', new RegExp(String.raw`^["'\u0060]${TOKEN_VAR}["'\u0060]$|(?:\$\{?|%)${TOKEN_VAR}(?![\p{ID_Continue}$])`, 'gu')],
291
+ ['gh-token', new RegExp(GH_TOKEN, 'gu')],
292
+ ['cloud-metadata', new RegExp(String.raw`${HOST_START}169\.254\.169\.254${HOST_END}(?!(?::\d{1,5})?\\?\/metadata\\?\/instance(?![\w-]))|${HOST_START}metadata\.google\.internal${HOST_END}`, 'giu')],
293
+ ['runner-worker', /(?<![\w$-])Runner\.Worker(?![\w$-])/gu],
294
+ ['process-memory', new RegExp(String.raw`\/proc\/(?:${PID})\/mem(?![\w-])`, 'gu')],
295
+ ['trufflehog', new RegExp(TRUFFLEHOG, 'giu')],
296
+ ['publish', /(?<![\w-])(?:npm|pnpm|yarn)(?:\s+-{1,2}\w[\w-]*(?:=\S*|\s+(?!-)\S+)?){0,8}?\s+publish(?![\w-])|\/-\/whoami(?![\w-])/gu],
297
+ ['shell', new RegExp(SHELL, 'giu')],
298
+ ['persistence', new RegExp(PERSISTENCE, 'giu')],
299
+ ]
300
+ // What a shell script holds, read whole: its commands, the places they write
301
+ // to, and the credentials a command, a process or its memory holds (`gh auth
302
+ // token`, `Runner.Worker`, `/proc/<pid>/mem`, trufflehog); not the cloud's
303
+ // metadata endpoint, whose fetching is a shell command already (`curl
304
+ // http://169.254.169.254/…`) and which a script provisioning a machine asks
305
+ // its region and id, nor a credential file or a token, which a script
306
+ // publishing a package names.
307
+ const SHELL_KINDS = new Set(['gh-token', 'runner-worker', 'process-memory', 'trufflehog', 'shell', 'persistence'])
308
+ const SHELL_HOLDS = HOLDS.filter(([kind]) => SHELL_KINDS.has(kind))
309
+ // A shell script, by its extension, its name a shell's startup file's
310
+ // (`.bashrc`, `.profile`), or its hashbang: a shell's, by its path or env's,
311
+ // env's options and settings before it (`#!/bin/ksh`, `#!/usr/bin/env -S
312
+ // bash -e`, `#!/usr/bin/env -S MODE=x bash`, `#!/usr/bin/env pwsh`), past a
313
+ // byte order mark.
314
+ const SHELL_SCRIPT = /(?:\.(?:sh|bash|zsh|ksh|mksh|dash|ash|csh|tcsh|fish|command|bat|cmd|ps1|psm1)|(?:^|[\\/])\.(?:bashrc|zshrc|zshenv|zlogin|bash_profile|bash_login|zprofile|profile))$/iu
315
+ // A shell script's comment, a line's whole (`# curl … | sh`) or its end
316
+ // after a command (`echo ok # don't curl …`, `echo ok;# …`) where the quotes
317
+ // before it are closed, escapes read (`"a \" b"`, PowerShell's by a
318
+ // backtick), not in one (`"a # b"`), and no other `#` (`$#`, `<#`) is;
319
+ // PowerShell's, by its extension or hashbang, its blocks too (`<# … #>`,
320
+ // opened where a command or value may start, `$x=<# … #>1`, not in a
321
+ // string, `"<#"`, `"a <#"`, to the end where none closes); a batch file's, cmd's `::`
322
+ // and `REM` (`@REM`, and `REM` after `&` or `||`, `echo ok & REM …`); as
323
+ // none of its commands.
324
+ const SHELL_COMMENT = new RegExp(String.raw`^(?:[ \t]*|(${stretched(() => String.raw`[^'"\n#\\]|\\.|'${stretched(String.raw`[^'\n]`)}'|"${stretched(String.raw`[^"\n\\]|\\.`)}"`)}[ \t;&|]))#${stretched('.')}$`, 'gmu')
325
+ const POWERSHELL = /\.psm?1$/iu
326
+ // A hashbang to the shell it names, by its path or env's (SHELL_SCRIPT).
327
+ const HASHBANG = String.raw`^\uFEFF?#!\s*${stretched(String.raw`\S`)}\/(?:env\s+(?:(?:-\S${stretched(String.raw`\S`)}|\w+=${stretched(String.raw`\S`)})\s+)*)?`
328
+ const POWERSHELL_HASHBANG = new RegExp(String.raw`${HASHBANG}pwsh(?![\w-])`, 'u')
329
+ const POWERSHELL_COMMENT = new RegExp(String.raw`^(${stretched(() => String.raw`[^'"\n#\u0060]|\u0060.|'${stretched(String.raw`[^'\n]`)}'|"${stretched(String.raw`[^"\n\u0060]|\u0060.`)}"`)})`
330
+ + String.raw`(?:(?<=^|[\s;({=,])<#${stretched(String.raw`[^#]|#(?!>)`)}(?:#>|(?![\s\S]))|(?<=^|[ \t;&|])#${stretched('.')}$)`, 'gmu')
331
+ const BATCH = /\.(?:bat|cmd)$/iu
332
+ // Its first group the command before a comment, as the others': none.
333
+ const BATCH_COMMENT = new RegExp(String.raw`()(?:^[ \t]*@?(?:::|rem(?![\w-]))|(?<=&|\|\|)[ \t]*@?rem(?![\w-]))${stretched('.')}$`, 'gimu')
334
+ const SHELL_HASHBANG = new RegExp(String.raw`${HASHBANG}${SHELLS}(?![\w-])`, 'u')
335
+ // A directory and a file in it named apart, as path.join takes them
336
+ // (`join(home, '.claude', 'settings.json')`): a place code runs from again
337
+ // later where a file names both.
338
+ const PARTS = [
339
+ ['claude', /^(["'\u0060])\.claude\1$/iu, /^(["'\u0060])settings(?:\.local)?\.json\1$/iu],
340
+ ['vscode', /^(["'\u0060])\.vscode\1$/iu, /^(["'\u0060])tasks\.json\1$/iu],
341
+ ]
342
+ // `.profile` named apart where the home directory is the part before it
343
+ // (`join(os.homedir(), '.profile')`, `resolve(process.env.HOME, …)`, `'~'`),
344
+ // not a selector's class alone.
345
+ const PROFILE = /^(["'\u0060])\.profile\1$/iu
346
+ const HOME_BEFORE = /(?:homedir\s*\(\s*\)|\b(?:HOME|USERPROFILE)|(["'\u0060])~\1)\s*,\s*$/u
347
+ // A module a kind loads: vm, child_process, and libnpmpublish, npm's own
348
+ // publishing.
349
+ const MODULES = [
350
+ ['vm', /^(["'\u0060])(?:node:)?vm\1$/u],
351
+ ['child-process', /^(["'\u0060])(?:node:)?child_process\1$/u],
352
+ ['publish', /^(["'\u0060])libnpmpublish\1$/u],
353
+ ]
354
+ // A string a program is run with publishing (`spawn('npm', ['publish'])`),
355
+ // after the one naming it, its options and their values between
356
+ // (`['--tag', 'next', 'publish']`), and a request's method set to PUT
357
+ // (`{ method: 'PUT' }`, `options.method = 'put'`).
358
+ const PROGRAM = /^(["'\u0060])(?:npm|pnpm|yarn)\1$/u
359
+ const PUBLISH = /^(["'\u0060])publish\1$/u
360
+ const OPTION = /^(["'\u0060])-/u
361
+ const ARGUMENTS_BETWEEN = new RegExp(String.raw`^(?:[\s,[]|${COMMENT})*$`, 'u')
362
+ const PUT = /^(["'\u0060])put\1$/iu
363
+ const METHOD = new RegExp(String.raw`\bmethod["']?${GAP}[:=]${GAP}$`, 'u')
364
+ // A program run with its arguments apart, by its path too, a Windows one's
365
+ // from its drive with spaces in it (`spawn('curl', ['-fsSL', 'https://…'])`,
366
+ // `execFile('powershell.exe', ['-NoP', '-enc', …])`, `spawn('systemctl',
367
+ // ['--user', 'enable', …])`, `spawnSync('gh', ['auth', 'token'])`,
368
+ // `execFileSync('C:\\Program Files\\GitHub CLI\\gh.exe', …)`): its command
369
+ // line, its arguments' strings after its name, read as a shell command is, a
370
+ // variable's or a property's value between (`['-o', output, 'https://…']`,
371
+ // `outputs[i]`, `options?.out`), its first 20 arguments, however long.
372
+ const COMMAND = new RegExp(String.raw`^(["'\u0060])(?:${stretched(String.raw`\S`)}[\\/]|[a-z]:[\\/]${stretched(String.raw`[^\n]`)}[\\/])?(?:curl|wget|certutil|bitsadmin|powershell|pwsh|systemctl|gh)(?:\.exe)?\1$`, 'iu')
373
+ // An argument a shell would need quoted, its separators no command's end
374
+ // (`'Cookie: a=b; c=d'`), its own double quotes single then.
375
+ const SEPARATES = /[;|&]/u
376
+ const VALUES_BETWEEN = new RegExp(String.raw`^(?:[\s,[\]\w$.?]|${COMMENT})*$`, 'u')
377
+ // A place named by its parts one after the other, as path.join takes them
378
+ // (`join(home, '.config', 'systemd', 'user')`), read as a path is.
379
+ const PLACE = new RegExp(PERSISTENCE, 'iu')
380
+ // What comes before a module's name where code loads it: `require(` (or
381
+ // `require?.(`), a bundler's `__require(` or `__non_webpack_require__(`,
382
+ // `import(`, `import` and `from`, comments allowed between
383
+ // (`import(/* webpackIgnore: true */ …)`); a name not loaded, as in a list of
384
+ // node's modules, is none, nor one a type-only import or export names
385
+ // (`import type { … } from`, `import type cp = require(`, comments allowed as
386
+ // in a load), which TypeScript erases (`import type from` imports a default
387
+ // named type, `import type = require(` one named so).
388
+ const LOADS = new RegExp(String.raw`(?:(?:require[\w$]*${GAP}(?:\?\.${GAP})?|\bimport${GAP})\(|\b(?:from|import))${GAP}$`, 'u')
389
+ const TYPE_ONLY = new RegExp(String.raw`\b(?:import|export)${GAP}type\b(?!${GAP}(?:from\b|=))[^;'"\x60]*(?:\bfrom|=${GAP}require${GAP}\()${GAP}$`, 'u')
390
+
391
+ // Each kind, in the order its tags show: its tag, what it does, its name in
392
+ // the tag's tooltip, a row of its own where kinds share the tag (a string run
393
+ // as code by any way, `eval`'s; a credential file, a token, a command, an
394
+ // endpoint or a process, `credentials`'), and for one marked as worse than
395
+ // the others' warning, `critical`: the whole environment read out,
396
+ // publishing, and a place code runs from again later.
397
+ const AS_CODE = { tag: 'eval', note: 'Runs a string as code' }
398
+ const CREDENTIALS = { tag: 'credentials', note: 'Reaches for credentials' }
399
+ export const RUNS_CODE = new Map([
400
+ ['eval', { ...AS_CODE, name: 'eval' }],
401
+ ['function', { ...AS_CODE, name: 'Function' }],
402
+ ['vm', { ...AS_CODE, name: 'vm' }],
403
+ ['compile', { ...AS_CODE, name: 'Module._compile' }],
404
+ ['child-process', { tag: 'child_process', note: 'Runs other programs', name: 'child_process' }],
405
+ ['credential-files', { ...CREDENTIALS, name: '.npmrc, .ssh or .aws' }],
406
+ ['tokens', { ...CREDENTIALS, name: 'NPM_TOKEN or GITHUB_TOKEN' }],
407
+ ['gh-token', { ...CREDENTIALS, name: 'gh auth token' }],
408
+ ['cloud-metadata', { ...CREDENTIALS, name: '169.254.169.254 or metadata.google.internal' }],
409
+ ['runner-worker', { ...CREDENTIALS, name: 'Runner.Worker' }],
410
+ ['process-memory', { ...CREDENTIALS, name: '/proc/<pid>/mem' }],
411
+ ['trufflehog', { ...CREDENTIALS, name: 'trufflehog' }],
412
+ ['env', { tag: 'whole env', note: 'Reads out the whole environment', name: 'process.env, by JSON.stringify, Object.entries or values, URLSearchParams', mark: 'critical' }],
413
+ ['publish', { tag: 'publish', note: 'Publishes to npm', name: 'npm publish, /-/whoami or a registry PUT', mark: 'critical' }],
414
+ ['shell', { tag: 'shell', note: 'Runs shell commands fetching or handing off code', name: 'curl or wget a URL, | sh, powershell -…, iex, rundll32, osascript, /dev/tcp' }],
415
+ ['persistence', { tag: 'persistence', note: 'Names a place code runs from again later', name: '.bashrc, .zshrc, $PROFILE, LaunchAgents, systemd user units, .claude/settings.json, .vscode/tasks.json', mark: 'critical' }],
416
+ ])
417
+
418
+ // The kinds a file's code holds, each with the lines it is on, of the first
419
+ // NPM_MAX_FOUND places it does each, in RUNS_CODE's order; null for none, or
420
+ // for a file that is no script, by its name or as node runs it (`nodeRuns`,
421
+ // npmIsScript's `script`). A shell script's, read whole, are its shell
422
+ // commands and the places it names; one with a shell's hashbang is one
423
+ // whatever its extension, and read as JavaScript too where it is that, as
424
+ // a launcher (`#!/bin/sh` then `exec node "$0"`) is both.
425
+ export function npmRunsCode(path, text, nodeRuns = false) {
426
+ const script = npmIsScript(path, text, nodeRuns)
427
+ const shell = typeof text === 'string' && (SHELL_SCRIPT.test(path) || SHELL_HASHBANG.test(text))
428
+ if (!script && !shell) return null
429
+ const found = new Map()
430
+ const at = (kind, offset) => {
431
+ const offsets = found.get(kind)
432
+ if (!offsets) found.set(kind, [offset])
433
+ else if (offsets.length < NPM_MAX_FOUND) offsets.push(offset)
434
+ }
435
+ if (script) read(text, at)
436
+ if (shell && SAYS_ANY.test(text)) {
437
+ // Its comments blanked, a space for each code unit, so each offset is where it was.
438
+ const comments = BATCH.test(path) ? BATCH_COMMENT : POWERSHELL.test(path) || POWERSHELL_HASHBANG.test(text) ? POWERSHELL_COMMENT : SHELL_COMMENT
439
+ const commands = text.replace(comments, (comment, command = '') => command + comment.slice(command.length).replaceAll(/[^\n]/gu, char => ' '.repeat(char.length)))
440
+ for (const [kind, holds] of SHELL_HOLDS) for (const { index } of commands.matchAll(holds)) at(kind, index)
441
+ }
442
+ // A PUT request publishing where the file names the registry; a place
443
+ // named by its directory and its file apart.
444
+ if (found.has('registry')) for (const offset of found.get('put') ?? []) at('publish', offset)
445
+ for (const [part] of PARTS) if (found.has(`${part}-file`)) for (const offset of found.get(`${part}-dir`) ?? []) at('persistence', offset)
446
+ if (![...RUNS_CODE.keys()].some(kind => found.has(kind))) return null
447
+ const line = lineOf(text)
448
+ return new Map([...RUNS_CODE.keys()].filter(kind => found.has(kind)).map(kind => [kind, [...new Set(found.get(kind).map(line))].toSorted((a, b) => a - b)]))
449
+ }
450
+
451
+ // Where a file's code does a kind, as at(kind, offset), in order: its code's,
452
+ // a template literal's substitutions' (`${eval(x)}`) among it, what its
453
+ // strings and its templates' text hold, a string naming a module where it is
454
+ // loaded; a PUT request (`put`) and the registry's host (`registry`) as their
455
+ // own, for the file to pair. Most code says none of it at all, told before
456
+ // anything is read.
457
+ function read(code, at) {
458
+ if (!SAYS_ANY.test(code)) return
459
+ // The string before, and where, a place's part maybe; the command line
460
+ // since a program's name, and where it is named; where the string before
461
+ // ends; whether the strings since a program's name are its arguments, an
462
+ // option or its value each, and whether the last is an option.
463
+ let before = null, command = null, end = 0, option = false, program = false
464
+ // Those of the code around a template, kept while what its substitutions
465
+ // hold, which follows it whole (jsMatchAll), is read as code of its own.
466
+ const around = []
467
+ for (const match of jsMatchAll(code, READS)) {
468
+ const { 0: token, 1: text, groups, index } = match
469
+ while (around.length > 0 && index >= around.at(-1).end) ({ before, command, end, option, program } = around.pop())
470
+ if (index < end) {
471
+ around.push({ before, command, end, option, program })
472
+ before = command = null
473
+ option = program = false
474
+ end = index
475
+ }
476
+ if (text === undefined) {
477
+ for (const kind of CODE_KINDS) if (groups[kind]) at(kind, index)
478
+ continue
479
+ }
480
+ if (text[0] === '/') continue
481
+ // A token's name as the string's whole (`within` 0, its quote) is a key,
482
+ // not one where its absence is told, it is dropped or only set.
483
+ const keyed = () => {
484
+ const after = code.slice(index + token.length, index + token.length + 20)
485
+ return !KEY_SET.test(after) && (!KEY_ENDS.test(after) || !KEY_ABSENT.test(code.slice(Math.max(0, index - 300), index)))
486
+ }
487
+ // Each kind searched for before its places are listed, as matchAll copies
488
+ // its regular expression each time.
489
+ const held = HOLDS_ANY.test(text) ? HOLDS.flatMap(([kind, holds]) => text.search(holds) === -1 ? [] : [...text.matchAll(holds)].map(({ index: within }) => [kind, within])) : []
490
+ for (const [kind, within] of held) if (kind !== 'tokens' || within > 0 || keyed()) at(kind, jsOffset(match, within))
491
+ if (text.includes('registry.') && REGISTRY_URL.test(unescaped(text).replaceAll(URL_STRIPPED, ''))) at('registry', index)
492
+ for (const [kind, name] of MODULES) if (name.test(text) && loaded(code, index)) at(kind, index)
493
+ if (PROFILE.test(text) && HOME_BEFORE.test(code.slice(Math.max(0, index - 100), index))) at('persistence', index)
494
+ for (const [part, dir, file] of PARTS) {
495
+ if (dir.test(text)) at(`${part}-dir`, index)
496
+ if (file.test(text)) at(`${part}-file`, index)
497
+ }
498
+ if (program && ARGUMENTS_BETWEEN.test(code.slice(end, index))) {
499
+ if (PUBLISH.test(text)) at('publish', index)
500
+ program = OPTION.test(text) || option
501
+ option = OPTION.test(text)
502
+ } else program = false
503
+ if (PROGRAM.test(text)) [program, option] = [true, false]
504
+ if (command && command.arguments < 20 && VALUES_BETWEEN.test(code.slice(end, index))) {
505
+ const argument = text.slice(1, -1)
506
+ command.line += ` ${SEPARATES.test(argument) ? `"${argument.replaceAll('"', "'")}"` : argument}`
507
+ command.arguments++
508
+ const { line } = command
509
+ const hit = SHELL_HOLDS.find(([, holds]) => line.search(holds) !== -1)
510
+ if (hit) {
511
+ at(hit[0], command.at)
512
+ command = null
513
+ }
514
+ } else command = null
515
+ if (COMMAND.test(text)) command = { arguments: 0, at: index, line: text.slice(1, -1) }
516
+ if (before && before.text.length + text.length < 200 && ARGUMENTS_BETWEEN.test(code.slice(end, index))) {
517
+ const [first, second] = [before.text.slice(1, -1), text.slice(1, -1)]
518
+ if (!PLACE.test(first) && !PLACE.test(second) && PLACE.test(`${first}/${second}`)) at('persistence', before.at)
519
+ }
520
+ before = { text, at: index }
521
+ if (PUT.test(text) && METHOD.test(code.slice(Math.max(0, index - 300), index))) at('put', index)
522
+ end = Math.max(end, index + token.length)
523
+ }
524
+ }
525
+
526
+ // The kinds code itself does, by READS' groups.
527
+ const CODE_KINDS = ['eval', 'function', 'compile', 'tokens', 'env', 'put']
528
+
529
+ // Whether the string at `index` in code names a module where it is loaded.
530
+ function loaded(code, index) {
531
+ const before = code.slice(Math.max(0, index - 300), index)
532
+ return LOADS.test(before) && !TYPE_ONLY.test(before)
533
+ }
534
+
535
+ // What `found` holds (npmRunsCode's kinds, hosts.js's services) that
536
+ // `was` didn't, each with its lines: what is new to a file, as its tags say,
537
+ // `was` null for one the version compared with didn't hold, or held none in.
538
+ // Null for none.
539
+ export function npmFoundAdded(found, was) {
540
+ if (!found) return null
541
+ const added = new Map([...found].filter(([kind]) => !was?.has(kind)))
542
+ return added.size > 0 ? added : null
543
+ }
544
+
545
+ // The kinds any of `founds` holds, as their tags name them, in their order,
546
+ // a tag kinds share once: `eval, child_process`.
547
+ export const npmRunsCodeKinds = founds => [...new Set([...RUNS_CODE].filter(([kind]) => founds.some(found => found.has(kind))).map(([, { tag }]) => tag))].join(', ')
548
+
549
+ // What `found` holds (npmRunsCode's) but the kinds whose tags `tags` names
550
+ // (npmRunsCodeWithout), or of those kinds alone (npmRunsCodeTagged), each
551
+ // with its lines; null for none.
552
+ function npmRunsCodeOf(found, kept) {
553
+ const of = new Map([...found ?? []].filter(([kind]) => kept(RUNS_CODE.get(kind).tag)))
554
+ return of.size > 0 ? of : null
555
+ }
556
+ export const npmRunsCodeWithout = (found, tags) => npmRunsCodeOf(found, tag => !tags.has(tag))
557
+ export const npmRunsCodeTagged = (found, tags) => npmRunsCodeOf(found, tag => tags.has(tag))
558
+
559
+ // The kinds that reach for credentials, by their tag: a version's finding of
560
+ // their own (risk/npm/held.js `held-credentials`), apart from the code its
561
+ // files run beyond themselves.
562
+ export const CREDENTIAL_TAGS = new Set([CREDENTIALS.tag])
563
+
564
+ // Its tags as a file's flags (file-flags.js), in their order, each once: a
565
+ // warning's (`high`), or for one marked worse, `critical`; none for none.
566
+ export const npmRunsCodeFlags = found => [...new Map([...found?.keys() ?? []].map(kind => RUNS_CODE.get(kind))
567
+ .map(({ tag, mark }) => [tag, { name: tag, severity: mark ?? 'high' }])).values()]
568
+