@preventive/triage 1.0.0-alpha.4 → 1.0.0-alpha.41

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 (312) hide show
  1. package/LICENSE-APACHE +202 -0
  2. package/README.md +16 -0
  3. package/api/reap.ts +17 -0
  4. package/cli.js +6 -0
  5. package/client/dependency-paths.d.ts +2 -0
  6. package/client/dependency-paths.js +24 -0
  7. package/client/finding-link.js +305 -0
  8. package/client/linked-findings.d.ts +1 -0
  9. package/client/linked-findings.js +112 -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 +3 -0
  13. package/common/bundle-compare-diff.js +522 -0
  14. package/common/bundle-compare-inputs.d.ts +2 -0
  15. package/common/bundle-compare-inputs.js +65 -0
  16. package/common/bundle-metadata.d.ts +14 -0
  17. package/common/bundle-metadata.js +226 -0
  18. package/common/bundle-pkg-of.d.ts +1 -0
  19. package/common/bundle-pkg-of.js +90 -0
  20. package/common/bundle-reasons.d.ts +2 -0
  21. package/common/bundle-reasons.js +21 -0
  22. package/common/bundle-source-links.d.ts +6 -0
  23. package/common/bundle-source-links.js +216 -0
  24. package/common/bundle-sourcemap.d.ts +10 -0
  25. package/common/bundle-sourcemap.js +100 -0
  26. package/common/bundle-sources.d.ts +7 -0
  27. package/common/bundle-sources.js +310 -0
  28. package/common/bundle-stats.js +41 -0
  29. package/common/bundle-tabs.js +1 -0
  30. package/common/code-language.js +65 -0
  31. package/common/default-scan-models.ts +31 -0
  32. package/common/dependency-reviews.d.ts +23 -0
  33. package/common/dependency-reviews.js +81 -0
  34. package/common/finding-duplicates.js +58 -0
  35. package/common/finding-filters.js +274 -0
  36. package/common/finding-id.js +47 -0
  37. package/common/github-issue-labels.d.ts +1 -0
  38. package/common/github-issue-labels.js +11 -0
  39. package/common/github-pr.ts +56 -0
  40. package/common/gzip.js +59 -0
  41. package/common/line-diff.d.ts +8 -0
  42. package/common/line-diff.js +56 -0
  43. package/common/linked-workspace-groups.js +63 -0
  44. package/common/managed/comments.ts +34 -0
  45. package/common/managed/npm-packages.d.ts +5 -0
  46. package/common/managed/npm-packages.js +34 -0
  47. package/common/managed/permissions.ts +31 -0
  48. package/common/managed/report-content.ts +72 -0
  49. package/common/managed/report-filter.ts +108 -0
  50. package/common/managed/repository-alias.ts +65 -0
  51. package/common/managed/roles.ts +28 -0
  52. package/common/managed/routes.d.ts +2 -0
  53. package/common/managed/routes.js +254 -0
  54. package/common/managed/scan-models.ts +6 -0
  55. package/common/managed/team-app.d.ts +2 -0
  56. package/common/managed/team-app.js +31 -0
  57. package/common/managed/triage.ts +83 -0
  58. package/common/managed/upload-seal.ts +126 -0
  59. package/common/pretty-print.d.ts +5 -0
  60. package/common/pretty-print.js +24 -0
  61. package/common/revalidation-conflicts.js +48 -0
  62. package/common/revalidation-input-groups.js +56 -0
  63. package/common/save-error-reason.ts +20 -7
  64. package/common/scan-server.ts +13 -0
  65. package/common/server-info.ts +35 -0
  66. package/common/utf8.d.ts +3 -0
  67. package/common/utf8.js +45 -0
  68. package/common/workspace-app.js +23 -0
  69. package/common/workspace-groups.js +176 -0
  70. package/out/brotli-fallback.js +3 -3
  71. package/out/chunk-2AFQKRAM.js +51 -0
  72. package/out/chunk-2KBF5TZ7.js +2 -0
  73. package/out/chunk-2XUEZHIP.js +28 -0
  74. package/out/chunk-35VE6CDH.js +2 -0
  75. package/out/chunk-3BZ7JERB.js +1 -0
  76. package/out/chunk-4F6JSF57.js +3 -0
  77. package/out/chunk-4XCDFGDP.js +10 -0
  78. package/out/chunk-C4Z6ZBXV.js +10 -0
  79. package/out/chunk-CMQA4DM6.js +1 -0
  80. package/out/chunk-CVVJAXVY.js +2 -0
  81. package/out/chunk-DIJJNTRH.js +10 -0
  82. package/out/chunk-GJ4IUCNS.js +1 -0
  83. package/out/chunk-GNXOKIGP.js +80 -0
  84. package/out/chunk-GZ4MS3TE.js +13 -0
  85. package/out/chunk-IU3CCINR.js +3 -0
  86. package/out/chunk-K3X5EJBQ.js +2 -0
  87. package/out/chunk-KEX2ATEW.js +2 -0
  88. package/out/chunk-KVEUXIEU.js +1 -0
  89. package/out/chunk-KYCNJVB7.js +1 -0
  90. package/out/chunk-NRPC74AM.js +1 -0
  91. package/out/chunk-OOJM4CTU.js +1 -0
  92. package/out/chunk-PNHBZGDD.js +1 -0
  93. package/out/chunk-QZ3A376W.js +1 -0
  94. package/out/chunk-R5O6FAW4.js +2 -0
  95. package/out/chunk-RFZQQXSG.js +17 -0
  96. package/out/chunk-RSHMVYNS.js +1 -0
  97. package/out/chunk-TUII34K2.js +1 -0
  98. package/out/chunk-TY4RZD3Z.js +226 -0
  99. package/out/chunk-TZ5KB2Y5.js +10 -0
  100. package/out/chunk-U6OPEXIA.js +1 -0
  101. package/out/chunk-UNTRGE26.js +2 -0
  102. package/out/chunk-UXNUAIIC.js +33 -0
  103. package/out/chunk-UYAXVRVL.js +1 -0
  104. package/out/chunk-UZAZM2SX.js +2 -0
  105. package/out/chunk-V3ME6FI5.js +2 -0
  106. package/out/chunk-V5O7M2YT.js +1 -0
  107. package/out/chunk-VIHGO77Y.js +1 -0
  108. package/out/chunk-VQJCXGIY.js +7 -0
  109. package/out/chunk-W4J3MGLB.js +1 -0
  110. package/out/chunk-WGG7KQUH.js +1 -0
  111. package/out/chunk-WZDTZE64.js +1 -0
  112. package/out/chunk-XL35ZV6R.js +1 -0
  113. package/out/chunk-XYFXXB2G.js +10 -0
  114. package/out/chunk-YVZH5SHP.js +1 -0
  115. package/out/chunk-ZBBOBQ43.js +1 -0
  116. package/out/chunk-ZMQQCGF6.js +2 -0
  117. package/out/client-managed-import.js +4 -0
  118. package/out/client-managed.js +37 -0
  119. package/out/client-sync.js +10 -13
  120. package/out/github-issue-authorized.html +3 -0
  121. package/out/github-issue-authorized.js +1 -0
  122. package/out/graph.js +17 -4
  123. package/out/index.html +90 -8
  124. package/out/prism.js +6 -2
  125. package/out/stasis.svg +20 -0
  126. package/out/terminal.js +365 -39
  127. package/out/view.css +1 -1
  128. package/out/view.js +83 -117
  129. package/package.json +298 -59
  130. package/risk/files/characters.js +196 -0
  131. package/risk/files/hosts.js +205 -0
  132. package/risk/files/readability.js +171 -0
  133. package/risk/files/runs-code.js +595 -0
  134. package/risk/files/script.js +114 -0
  135. package/risk/files/stamps.js +46 -0
  136. package/risk/index.d.ts +156 -0
  137. package/risk/index.js +7 -0
  138. package/risk/npm/assess.js +223 -0
  139. package/risk/npm/combined.js +65 -0
  140. package/risk/npm/compare.js +228 -0
  141. package/risk/npm/dependencies.js +153 -0
  142. package/risk/npm/diff.js +43 -0
  143. package/risk/npm/downloads.js +84 -0
  144. package/risk/npm/files.js +34 -0
  145. package/risk/npm/held.js +60 -0
  146. package/risk/npm/install-scripts.js +34 -0
  147. package/risk/npm/malware.js +36 -0
  148. package/risk/npm/manifest.js +65 -0
  149. package/risk/npm/new-dependency.js +30 -0
  150. package/risk/npm/old-version.js +108 -0
  151. package/risk/npm/registry.js +282 -0
  152. package/risk/npm/release.js +85 -0
  153. package/risk/npm/score.js +59 -0
  154. package/risk/npm/specs.js +124 -0
  155. package/risk/npm/tarball.js +113 -0
  156. package/server-common/database-config.ts +16 -0
  157. package/server-common/initialize.ts +18 -0
  158. package/server-common/npm-advisories.ts +101 -0
  159. package/{server → server-common}/origin.ts +5 -5
  160. package/server-common/reap.ts +48 -0
  161. package/server-common/scan-config.ts +19 -0
  162. package/server-common/standalone.ts +29 -0
  163. package/server-common/storage-crypto.ts +182 -0
  164. package/server-common/storage-log.ts +38 -0
  165. package/server-common/vercel-blob.ts +103 -0
  166. package/server-e2e/app.ts +486 -0
  167. package/{server → server-e2e}/auth.ts +5 -1
  168. package/{server → server-e2e}/bus-receiver.ts +2 -1
  169. package/{server → server-e2e}/cli.js +9 -4
  170. package/{server → server-e2e}/config.ts +56 -35
  171. package/{server → server-e2e}/db-neon.ts +2 -2
  172. package/{server → server-e2e}/db-revision-sql.ts +1 -1
  173. package/{server → server-e2e}/db-stmt.ts +2 -2
  174. package/{server → server-e2e}/db.ts +7 -7
  175. package/{server → server-e2e}/http.ts +116 -18
  176. package/{server → server-e2e}/hub.ts +40 -9
  177. package/server-e2e/index.ts +17 -0
  178. package/server-e2e/lifecycle.ts +95 -0
  179. package/{server → server-e2e}/neon-driver.ts +2 -2
  180. package/{server → server-e2e}/npm-proxy.ts +12 -145
  181. package/{server → server-e2e}/objstore/blob-vercel.ts +10 -114
  182. package/{server → server-e2e}/objstore/blob.ts +1 -1
  183. package/server-e2e/objstore/fetch-mint-guard.ts +74 -0
  184. package/{server → server-e2e}/objstore/handlers.ts +9 -8
  185. package/{server → server-e2e}/objstore/init.ts +35 -18
  186. package/{server → server-e2e}/objstore/reaper.ts +1 -1
  187. package/server-e2e/objstore/rest-deny.ts +28 -0
  188. package/server-e2e/objstore/rest-mint.ts +224 -0
  189. package/{server → server-e2e}/objstore/rest.ts +27 -26
  190. package/{server → server-e2e}/objstore/sign.ts +105 -0
  191. package/{server → server-e2e}/objstore/store-neon.ts +6 -10
  192. package/{server → server-e2e}/objstore/store.ts +7 -7
  193. package/{server → server-e2e}/objstore/tokens.ts +2 -2
  194. package/{server → server-e2e}/pubsub.ts +12 -9
  195. package/{server → server-e2e}/sign.ts +4 -4
  196. package/{server → server-e2e}/sse-server.ts +102 -68
  197. package/{server → server-e2e}/sse-session.ts +28 -8
  198. package/{server → server-e2e}/static.ts +31 -21
  199. package/server-e2e/sync-handlers.ts +408 -0
  200. package/{server → server-e2e}/ws-server.ts +20 -9
  201. package/server-managed/activity.ts +245 -0
  202. package/server-managed/avatar-store.ts +12 -0
  203. package/server-managed/blob-store.ts +17 -0
  204. package/server-managed/brotli.ts +10 -0
  205. package/server-managed/bundle-advisories.ts +120 -0
  206. package/server-managed/bundle-advisory-inventory.ts +77 -0
  207. package/server-managed/bundle-build-diagnostics.d.ts +10 -0
  208. package/server-managed/bundle-build-diagnostics.js +23 -0
  209. package/server-managed/bundle-build-leases.ts +41 -0
  210. package/server-managed/bundle-build-worker.js +111 -0
  211. package/server-managed/bundle-build.ts +198 -0
  212. package/server-managed/bundle-cache.ts +199 -0
  213. package/server-managed/bundle-catalog.ts +29 -0
  214. package/server-managed/bundle-commits.ts +140 -0
  215. package/server-managed/bundle-store.ts +24 -0
  216. package/server-managed/bundle-summary-cache.ts +130 -0
  217. package/server-managed/bundle.ts +120 -0
  218. package/server-managed/cache-storage.ts +19 -0
  219. package/server-managed/cli.js +21 -0
  220. package/server-managed/combined.ts +46 -0
  221. package/server-managed/comments.ts +151 -0
  222. package/server-managed/config.ts +158 -0
  223. package/server-managed/content-access.ts +15 -0
  224. package/server-managed/crypto.ts +25 -0
  225. package/server-managed/db-methods.ts +1725 -0
  226. package/server-managed/db-neon.ts +367 -0
  227. package/server-managed/db-schema.ts +227 -0
  228. package/server-managed/db-table-names.ts +22 -0
  229. package/server-managed/db.ts +132 -0
  230. package/server-managed/dependency-reviews.ts +248 -0
  231. package/server-managed/github-app.ts +391 -0
  232. package/server-managed/github-errors.ts +68 -0
  233. package/server-managed/github-issue-links.ts +127 -0
  234. package/server-managed/github-issue-oauth.ts +47 -0
  235. package/server-managed/github-issues.ts +152 -0
  236. package/server-managed/github-metadata.ts +213 -0
  237. package/server-managed/github-oauth.ts +235 -0
  238. package/server-managed/github-pulls.ts +227 -0
  239. package/server-managed/http-response.ts +50 -0
  240. package/server-managed/http.ts +3472 -0
  241. package/server-managed/import-triage.ts +51 -0
  242. package/server-managed/index.ts +187 -0
  243. package/server-managed/kept.ts +28 -0
  244. package/server-managed/link-reports.ts +158 -0
  245. package/server-managed/locked-packages.ts +85 -0
  246. package/server-managed/lru.ts +30 -0
  247. package/server-managed/managed-issues.ts +75 -0
  248. package/server-managed/management-catalog.ts +45 -0
  249. package/server-managed/management.ts +99 -0
  250. package/server-managed/npm-insights.ts +440 -0
  251. package/server-managed/npm-kept.ts +65 -0
  252. package/server-managed/npm-loads.ts +167 -0
  253. package/server-managed/npm-manifest.ts +115 -0
  254. package/server-managed/npm-packages.ts +378 -0
  255. package/server-managed/npm-provenance.ts +123 -0
  256. package/server-managed/npm-risk.ts +177 -0
  257. package/server-managed/object-storage-disk.ts +148 -0
  258. package/server-managed/object-storage-vercel.ts +67 -0
  259. package/server-managed/object-storage.ts +74 -0
  260. package/server-managed/package-entry-points.ts +51 -0
  261. package/server-managed/pretty-print.ts +352 -0
  262. package/server-managed/public-workspace.ts +220 -0
  263. package/server-managed/rename-diff-worker.js +26 -0
  264. package/server-managed/rename-diff.ts +270 -0
  265. package/server-managed/repo-path.ts +21 -0
  266. package/server-managed/report-migration.ts +35 -0
  267. package/server-managed/report-query.ts +10 -0
  268. package/server-managed/report-response.ts +16 -0
  269. package/server-managed/report-sources.ts +178 -0
  270. package/server-managed/report-store.ts +47 -0
  271. package/server-managed/repository-aliases.ts +91 -0
  272. package/server-managed/repository-browser.ts +153 -0
  273. package/server-managed/repository-discovery.ts +82 -0
  274. package/server-managed/repository-policy.ts +27 -0
  275. package/server-managed/repository-risk.ts +420 -0
  276. package/server-managed/revisions.ts +35 -0
  277. package/server-managed/session.ts +92 -0
  278. package/server-managed/slugs.ts +38 -0
  279. package/server-managed/solidity-entry-points.ts +69 -0
  280. package/server-managed/sql-postgres.ts +31 -0
  281. package/server-managed/sql.ts +76 -0
  282. package/server-managed/static.ts +28 -0
  283. package/server-managed/storage-command.ts +16 -0
  284. package/server-managed/storage-db.ts +175 -0
  285. package/server-managed/storage-encryption.ts +279 -0
  286. package/server-managed/storage-maintenance.ts +256 -0
  287. package/server-managed/storage-payload.ts +103 -0
  288. package/server-managed/storage-stores.ts +85 -0
  289. package/server-managed/storage.ts +78 -0
  290. package/server-managed/team-app.ts +94 -0
  291. package/server-managed/team-catalog.ts +7 -0
  292. package/server-managed/team-feed.ts +173 -0
  293. package/server-managed/team-npm-scopes.ts +77 -0
  294. package/server-managed/team-reports.ts +228 -0
  295. package/server-managed/triage-response.ts +16 -0
  296. package/server-managed/type-only-worker.js +19 -0
  297. package/server-managed/type-only.ts +103 -0
  298. package/server-managed/uploads.ts +86 -0
  299. package/server-managed/upstream-cache.ts +215 -0
  300. package/server-managed/view-as.ts +58 -0
  301. package/server-managed/workspace-shares.ts +166 -0
  302. package/server.ts +50 -0
  303. package/server/index.ts +0 -477
  304. package/server/lifecycle.ts +0 -204
  305. package/server/sync-handlers.ts +0 -320
  306. /package/{LICENSE → LICENSE-MIT} +0 -0
  307. /package/{server → server-e2e}/config.example.json +0 -0
  308. /package/{server → server-e2e}/objstore/blob-fs.ts +0 -0
  309. /package/{server → server-e2e}/objstore/fs.ts +0 -0
  310. /package/{server → server-e2e}/peer.ts +0 -0
  311. /package/{server → server-e2e}/util.ts +0 -0
  312. /package/{server → server-e2e}/validation.ts +0 -0
@@ -0,0 +1,595 @@
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, as does one in a template
23
+ // literal in another's substitution (`${`${eval(x)}`}`), whose backtick ends
24
+ // the outer one as the regular expression telling strings reads it.
25
+ //
26
+ // Labels to assist a reviewer, making the plain cases less likely to be
27
+ // missed, not a check to rely on: code can come to eval, Function,
28
+ // child_process, credentials or the registry in more ways than any reading of
29
+ // its text tells (a name built as it runs, `(…).constructor('…')`, a module
30
+ // required by a name computed, a path or URL put together, the
31
+ // registry's host read from config), so a file without a label is not one
32
+ // without them.
33
+ import { JS_STRING_OR_COMMENT, NPM_MAX_FOUND, lineOf, npmIsScript } from './script.js'
34
+
35
+ // What may come between a call's parts: blanks and comments
36
+ // (`Function/* note */(…)`), each comment ending at its first `*/`.
37
+ const COMMENT = String.raw`/\*(?:[^*]|\*(?!/)){0,100}\*/|//[^\n]{0,100}\n`
38
+ const GAP = String.raw`(?:\s|${COMMENT})*`
39
+ // Function called: as it is, optionally (`Function?.(…)`), or by its call or
40
+ // apply (`Function.call(null, …)`), each making a function of its last
41
+ // argument.
42
+ const FUNCTION_CALLED = String.raw`(?:${GAP}\??\.${GAP}(?:call|apply))?${GAP}(?:\?\.${GAP})?\(`
43
+ // But not where the function made returns `this` alone
44
+ // (`Function('return this')()`, `new Function("return this;")`): the global
45
+ // object, as older code finds it where `globalThis` may not be, lodash's and
46
+ // core-js's among them, running nothing of its own.
47
+ const GLOBAL_FOUND = String.raw`(?!${GAP}(?<quote>["'\u0060])\s*return\s+this\s*;?\s*\k<quote>${GAP}\))`
48
+ // The environment, whole: not one variable of it (`process.env.HOME`,
49
+ // `process.env[name]`).
50
+ const ENV = String.raw`process${GAP}\.${GAP}env(?![\p{ID_Continue}$])(?!${GAP}(?:\??\.|\[))`
51
+ // Its values read out whole (`JSON.stringify(process.env)`,
52
+ // `Object.entries(process.env)`, `new URLSearchParams(process.env)`), or a
53
+ // copy of them (`JSON.stringify({ ...process.env })`), not as a program is
54
+ // handed it (`{ env: process.env }`, `{ ...process.env }`), nor its
55
+ // names listed (`Object.keys(process.env)`, as debug picks its DEBUG_ ones),
56
+ // nor one variable asked for (`'HOME' in process.env`), nor its entries or
57
+ // values picked among as they are read (`Object.entries(process.env)
58
+ // .reduce(…)`, as npm's agent finds its proxy's, `.filter(…)`, `.find(…)`).
59
+ const PICKED = String.raw`${GAP}\)${GAP}\??\.${GAP}(?:filter|find|findLast|some|every|reduce)(?![\p{ID_Continue}$])`
60
+ const COPY = String.raw`(?:\{${GAP}\.\.\.${GAP})?`
61
+ const ENV_WHOLE = String.raw`(?=JSON|Object|URLSearchParams)(?<![\p{ID_Continue}$.])(?:JSON${GAP}\.${GAP}stringify|URLSearchParams)${GAP}\(${GAP}${COPY}${ENV}`
62
+ + String.raw`|(?=Object)(?<![\p{ID_Continue}$.])Object${GAP}\.${GAP}(?:values|entries)${GAP}\(${GAP}${COPY}${ENV}(?!${PICKED})`
63
+ // What a shell command, and a place code runs from again later, may say
64
+ // (SHELL, PERSISTENCE), and what of the credentials is told in any case:
65
+ // trufflehog (TRUFFLEHOG), GCP's metadata host, and a string naming gh
66
+ // (`'gh'`, `'/usr/bin/gh'`, `'GH.EXE'`) as a program run with its arguments
67
+ // apart is, whatever is between them (`'gh', ['auth', /* … */ 'token']`).
68
+ // A shell, as a hashbang or a pipe names it (`#!/bin/ksh`, `| fish`,
69
+ // BusyBox's `ash`, `| powershell`); after a pipe, by its path or env's too (`| /bin/sh`,
70
+ // `| /usr/bin/env bash`), env's options, their values, and settings before
71
+ // it on its line (`env -S`, `env -u NAME`, `env MODE=x`).
72
+ const SHELLS = String.raw`(?:(?:ba|z|da|k|mk|c|tc|fi|a)?sh|pwsh|powershell)`
73
+ 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]+)?`
74
+ 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`
75
+ + 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]`
76
+ // What says one of the credentials a command, an endpoint or a process holds
77
+ // (HOLDS' gh-token, cloud-metadata, runner-worker and process-memory), as
78
+ // code and its strings may say it, in the case it is written: `gh auth
79
+ // token`'s command, lowercase as gh reads it, the metadata endpoint's
80
+ // address, `Runner.Worker` and `/proc/`; what is told in any case is
81
+ // SHELL_OR_PLACE's.
82
+ const SAYS_CREDENTIALS = String.raw`auth(?:[^\S\n]|[\\^\x60]\r?\n)+token|169\.254\.169\.254|Runner\.Worker|\/proc\/`
83
+ // Any of RUNS_CODE, as a text may say it: a shell command or a place in any
84
+ // case, as Windows' are written (`PowerShell`, `RUNDLL32`).
85
+ const SAYS_SHELL_OR_PLACE = new RegExp(SHELL_OR_PLACE, 'iu')
86
+ 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')
87
+ const SAYS_ANY = { test: text => SAYS_CODE.test(text) || SAYS_SHELL_OR_PLACE.test(text) }
88
+
89
+ // A name read as one, not as a property's (`vm.eval`, `vm . eval`, a spread's
90
+ // `...eval` aside) or part of another's (`evaluate`), or as the global
91
+ // object's (`window.eval`, `globalThis?.eval`); looked back on only where the
92
+ // name or the global object's starts, so a long run of blanks reads in one go.
93
+ const GLOBAL = 'globalThis|window|global|self'
94
+ const named = name => String.raw`(?=${GLOBAL}|${name})(?<![\p{ID_Continue}$#])(?<!(?<!\.)\.${GAP})(?<!\bfunction${GAP})(?:(?:${GLOBAL})${GAP}\??\.${GAP})?${name}`
95
+ // A token's name, as its value is read (`process.env.GITHUB_TOKEN || null`,
96
+ // `env.NPM_TOKEN`, `||=`), not where its absence is told
97
+ // (`!process.env.GITHUB_TOKEN`, `!(process.env.GITHUB_TOKEN)`) or it is
98
+ // dropped (`delete process.env.NPM_TOKEN`), the token the whole of what is
99
+ // negated or deleted (ABSENT_ENDS: not `!process.env.GITHUB_TOKEN.trim()`,
100
+ // `!(process.env.GITHUB_TOKEN === x)`), nor where it is only set
101
+ // (`process.env.NPM_TOKEN = token`), but for a default as it is destructured
102
+ // (`{ NPM_TOKEN = '' } = process.env`, a line of its own too), nor declared
103
+ // (`const NPM_TOKEN = …`, TypeScript's `let GITHUB_TOKEN: string`), nor typed
104
+ // (a member, `GITHUB_TOKEN?: string`, `NPM_TOKEN: string | undefined`, or a
105
+ // parameter): looked back on, a few names and dots at most, only where the
106
+ // name starts.
107
+ const TOKEN_VAR = '(?:NPM|GITHUB)_TOKEN'
108
+ const SET = String.raw`\s{0,4}=(?![=>])`
109
+ // A name typed, not read: optional (`?:`), or a primitive's type after it,
110
+ // as no destructuring names what it reads (`{ NPM_TOKEN: token }`); not
111
+ // `undefined`, a ternary's choice (`x ? env.NPM_TOKEN : undefined`).
112
+ const TYPED = String.raw`\s{0,4}(?:\?:|:\s{0,4}(?:string|number|boolean|unknown|any)(?![\p{ID_Continue}$]))`
113
+ // What may follow a token negated or deleted whole: the parentheses around
114
+ // it closed, nothing read of what they hold (`!(token).trim()`, `!(token)()`), or else what
115
+ // ends an operand (`&&`, `||`, `??`, a ternary's `?`, `;`, `,`, `:`, `}`, `]`,
116
+ // the line's end).
117
+ const ABSENT_ENDS = String.raw`\s{0,4}(?:(?:\)\s{0,4}){1,4}(?![\s)]{0,8}(?:[.[(]|\?\.))|&&|\|\||\?\?|\?(?!\.)|[;,:}\]]|$)`
118
+ const TOKEN_NAME = String.raw`(?=NPM|GITHUB)(?<![\p{ID_Continue}$])(?<!\b(?:const|let|var)\s{1,4})`
119
+ + String.raw`(?:(?<!(?:!|\bdelete\s)[\s(]{0,4}(?:[\p{ID_Continue}$]{1,64}\s{0,4}\??\.\s{0,4}){0,4})|(?!${TOKEN_VAR}${ABSENT_ENDS}))`
120
+ + String.raw`${TOKEN_VAR}(?![\p{ID_Continue}$])(?!${TYPED})(?:(?!${SET})|(?<=[{,]\s{0,64}${TOKEN_VAR}))`
121
+ // A script's strings, regular expressions and comments, as script.js
122
+ // tells them, each taking its own text, and its code in none of them: eval,
123
+ // not a property TypeScript types as optional (`eval?: T`, JavaScript's `?:`
124
+ // none of its own), and Function called, but to find the global object;
125
+ // Module's _compile, called
126
+ // (`module._compile(source, filename)`) or hooked (`Module.prototype._compile
127
+ // = …`), not named in a type (`typeof Module.prototype._compile`); a token's
128
+ // name, its value read; the environment read out whole; and a PUT request's
129
+ // call (`axios.put(…)`).
130
+ const READS = new RegExp(String.raw`${JS_STRING_OR_COMMENT.source}|(?<eval>${named('eval')}(?![\p{ID_Continue}$])(?!\s*\?:))|(?<function>${named('Function')}${FUNCTION_CALLED}${GLOBAL_FOUND})`
131
+ + String.raw`|(?<compile>\.${GAP}_compile(?:${FUNCTION_CALLED}|${GAP}=(?![=>])))|(?<tokens>${TOKEN_NAME})|(?<env>${ENV_WHOLE})|(?<put>\.${GAP}put${GAP}\()`, 'gmu')
132
+ // What a string holds that a kind is: a credential file's path, npm's, SSH's
133
+ // or AWS's (`~/.npmrc`, `.ssh/id_rsa`, `.aws/credentials`, not `x.npmrc` or
134
+ // `.sshd`); a token's name where it is read, the string the name alone (a
135
+ // key, `process.env['GITHUB_TOKEN']`) or the name expanded (a shell's or
136
+ // .npmrc's `$GITHUB_TOKEN`, `${NPM_TOKEN}`, cmd's `%NPM_TOKEN%`), not where
137
+ // text names it (`requires a token in GITHUB_TOKEN`); a credential a
138
+ // command, an endpoint or a process holds (`gh auth token`, the cloud's
139
+ // metadata endpoint, `Runner.Worker`, `/proc/<pid>/mem`, trufflehog, as
140
+ // HOLDS tells them); a command publishing (`npm publish`, `pnpm
141
+ // publish …`, options before it as npm takes them, `npm --tag next publish`)
142
+ // or the registry's who-am-I, a publisher's first step with a token. And the
143
+ // registry, where a PUT request is one publishing (REGISTRY_URL).
144
+ // Told first of a string by what any of them says.
145
+ const HOLDS_CODE = new RegExp(String.raw`\.(?:npmrc|ssh|aws)|_TOKEN|${SAYS_CREDENTIALS}|publish|whoami|registry\.`, 'u')
146
+ const HOLDS_ANY = { test: text => HOLDS_CODE.test(text) || SAYS_SHELL_OR_PLACE.test(text) }
147
+ // Before a token's name as a key, its absence told or the token dropped, as
148
+ // TOKEN_NAME tells of one as a name (`!process.env['GITHUB_TOKEN']`,
149
+ // `delete process.env['NPM_TOKEN']`): a few names and dots at most, and
150
+ // after it, its key's bracket closed, what may follow it whole (ABSENT_ENDS,
151
+ // not `!process.env['NPM_TOKEN'].length`). After it, the token set
152
+ // (`process.env['NPM_TOKEN'] = token`).
153
+ const KEY_SET = new RegExp(String.raw`^\s{0,4}\]${SET}`, 'u')
154
+ const KEY_ENDS = new RegExp(String.raw`^\s{0,4}\]${ABSENT_ENDS}`, 'mu')
155
+ 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
156
+ // A string's text as JavaScript reads it, its escapes decoded (`\x2f`,
157
+ // `\u002f`, `\u{2f}`, sloppy mode's octal `\057`, `\/`, `\\`, `\n`, `\t`), a
158
+ // line continued (a backslash before a line's end) joined: what a URL in it
159
+ // spells, however its separators are escaped or its lines broken.
160
+ 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
161
+ const CONTROLS = new Map([['n', '\n'], ['r', '\r'], ['t', '\t'], ['b', '\b'], ['f', '\f'], ['v', '\v']])
162
+ const LINE_ENDS = new Set(['\n', '\r', '\r\n', '\u2028', '\u2029'])
163
+ const unescaped = text => text.replace(ESCAPE, (escape, hex, unit, point, octal, char) => {
164
+ if (char !== undefined) return LINE_ENDS.has(char) ? '' : CONTROLS.get(char) ?? char
165
+ const code = octal === undefined ? Number.parseInt(hex ?? unit ?? point, 16) : Number.parseInt(octal, 8)
166
+ return code <= 0x10_FFFF ? String.fromCodePoint(code) : escape
167
+ })
168
+ // The registry's host in a string, as JavaScript reads it (unescaped) and a
169
+ // 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
170
+ // after an http(s) scheme's colon (its slashes optional there, as URLs take
171
+ // them), its userinfo between, to its last `@` before the URL's path, query or
172
+ // fragment, quotes and all, 256
173
+ // characters at most, as credentials are, so a URL's every start is read
174
+ // in bounded time (`https://registry.npmjs.org/`, `.npmrc`'s
175
+ // `//registry.npmjs.org/`, `https://${token}@registry.npmjs.org/`,
176
+ // `https:\\\\registry.npmjs.org`, `https:\x2f\x2fregistry.npmjs.org`,
177
+ // `https:registry.npmjs.org`); or starting the string, its whole or before its
178
+ // path, port, query or fragment (npm-profile's `'registry.npmjs.org'`, a URL joined,
179
+ // `'https://' + 'registry.npmjs.org/'`). Not in prose, as anywhere else
180
+ // (`registry.npmjs.org</span>`, `asks registry.npmjs.org`, `[registry.npmjs.org]`,
181
+ // `note:registry.npmjs.org`, `user@registry.npmjs.org`, `docs/registry.npmjs.org`).
182
+ const REGISTRY_HOST = String.raw`registry\.(?:npmjs\.org|yarnpkg\.com)`
183
+ const REGISTRY_URL = new RegExp(String.raw`(?:[/\\]{2}|\bhttps?:[/\\]?)(?:[^/\\?#]{0,256}@)?${REGISTRY_HOST}|^(["'\u0060])${REGISTRY_HOST}(?=\1$|[/:?#])`, 'iu')
184
+ const URL_STRIPPED = /[\t\n\r]/gu
185
+ // A shell command fetching or handing off code: curl or wget given a URL
186
+ // (`curl -fsSL https://…`, `ftp://`, `https:\/\/` as a string may escape
187
+ // it), long options before it too (`-H "Authorization: …"`), a quoted
188
+ // argument's `;`, `|` and `&` no command's end (`-d "a=1&b=2"`), its line
189
+ // continued too (`curl -fsSL \⏎ https://…`, `curl\⏎`, cmd's `^`,
190
+ // PowerShell's backtick), not the next line's, the next command's (`curl
191
+ // --version; echo https://…`) nor the next curl's, a download piped to a
192
+ // shell (`… | sh`, `payload|sudo -E -- bash`, `| ksh`, `| /bin/sh`, `cat
193
+ // hash | sh`, not a list of shells, `bash|zsh|sh`, a regular expression's
194
+ // alternatives, `(x|ss|sh)$`, nor `|| sh`, nor an escaped pipe, grep's
195
+ // `'bash\|zsh'`), PowerShell given options (`powershell -nop -enc …`),
196
+ // rundll32, osascript and mshta, certutil fetching (`certutil -urlcache -f
197
+ // http://…`) and bitsadmin's `/transfer`, and bash's network redirection
198
+ // (`/dev/tcp/host/port`), and PowerShell running what it fetches (`iex (iwr
199
+ // https://…).Content`, `irm https://… | iex`, long options before the pipe
200
+ // too, `iwr … -Headers @{…} | iex`, `IEX (New-Object
201
+ // Net.WebClient).DownloadString(…)`), its line continued by a backtick too,
202
+ // not the next pipeline's, `iex $x && iwr …`); in any case, as Windows' are
203
+ // written, and by its path (`/usr/bin/curl`).
204
+ const CONTINUED = String.raw`[\\^\u0060]\r?\n`
205
+ const ON_ITS_LINE = String.raw`(?:[^\n;|&]|${CONTINUED})`
206
+ // PowerShell's own, its backtick alone (not `\⏎`, a new statement there).
207
+ const POWERSHELL_CONTINUED = String.raw`\u0060\r?\n`
208
+ // A command's text up to its end, its arguments' quotes kept, a quote left
209
+ // open (`it's`) as any other character.
210
+ const ARGUMENTS = String.raw`(?:[^\n;|&"']|${CONTINUED}|"[^"\n]*"|'[^'\n]*'|"(?![^"\n]*")|'(?![^'\n]*'))`
211
+ const APART = String.raw`(?:[^\S\n]|${CONTINUED})`
212
+ // A command's options, a few, and their values (`-E`, `-u root`, `--now`),
213
+ // on its line or one continued.
214
+ const OPTIONS = String.raw`(?:${APART}+--?\w[\w-]*(?:=\S*|${APART}+(?!-)\S+)?){0,6}?`
215
+ 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)|$-])`
216
+ + String.raw`|(?<![\w.-])(?:powershell|pwsh)(?:\.exe)?[^\S\n]+-\w|(?<![\w.-])(?:rundll32|osascript|mshta)(?:\.exe)?(?![\w-])`
217
+ + 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)\/`
218
+ + String.raw`|(?<![\w$.-])(?:iex|invoke-expression)(?![\w-])(?:[^\n;|&]|${POWERSHELL_CONTINUED}){0,200}?(?:(?<![\w$.-])(?:iwr|irm|invoke-webrequest|invoke-restmethod)(?![\w-])|\.downloadstring\s*\()`
219
+ + 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-])`
220
+ // A place code runs from again later: a shell's rc file (`~/.bashrc`,
221
+ // `.zshrc`, `.zshenv`, `.zlogin`, `.bash_profile`, `.bash_login`,
222
+ // `.zprofile`, and `.profile` in a path, `~/.profile`, not a selector's
223
+ // class, nor one after another, `.nav`), PowerShell's profile (`$PROFILE`,
224
+ // `Microsoft.PowerShell_profile.ps1`, all hosts' `PowerShell\Profile.ps1`,
225
+ // `$PSHOME\Profile.ps1`, not another folder's), macOS's LaunchAgents and
226
+ // LaunchDaemons, fish's (`~/.config/fish/config.fish`, `conf.d`), systemd's
227
+ // user units (`~/.config/systemd/user`, `systemctl --user enable`, other
228
+ // options before it, `--now`, or `--user` after it in its command,
229
+ // `systemctl enable --user`; not `start`, which runs a unit once), and the
230
+ // settings an AI agent or an editor runs commands from on its own
231
+ // (`.claude/settings.json`, `.vscode/tasks.json`); in any case, as Windows'
232
+ // and macOS's paths are, a path's separator a slash or a backslash, escaped
233
+ // in a string as code writes it (`"\\.claude\\settings.json"`, `\/`).
234
+ const SEPARATOR = String.raw`(?:\\?\/|\\{1,2})`
235
+ 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-])`
236
+ + 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`
237
+ // Credentials a command, an endpoint or a process holds, as recent npm
238
+ // malware reached for them: the GitHub CLI printing its token (`gh auth
239
+ // token`, by its path too, `gh.exe`, its name in any case as Windows runs
240
+ // it, `GH.EXE`, its line continued as a shell command's, `gh auth \⏎
241
+ // token`), not its other commands (`gh auth status`); the cloud's
242
+ // instance metadata endpoint, AWS's, Azure's and GCP's
243
+ // (`http://169.254.169.254/latest/meta-data/…`, `metadata.google.internal`,
244
+ // in any case as DNS reads it), as a host whole (HOST_START, HOST_END): not
245
+ // an address or a host either starts or ends (`169.254.169.2540`,
246
+ // `foo-169.254.169.254`, `169.254.169.254.example.com`,
247
+ // `metadata.google.internal.example.com`; a trailing dot,
248
+ // `metadata.google.internal.`, its host still), nor in a URL's path or
249
+ // userinfo (`https://example.com/169.254.169.254`,
250
+ // `https://169.254.169.254@example.com`; WHATWG's `\@` a path's, its host
251
+ // the address still), nor Azure's instance metadata
252
+ // (`169.254.169.254/metadata/instance/compute`, after a port too, `:80`, its
253
+ // slashes escaped too, `\/`), a machine's region and size, not its
254
+ // identity's token, as msal asks it; the GitHub Actions runner's process
255
+ // (`Runner.Worker`, `Runner.Worker.exe`, `GitHub.Runner.Worker`), whose
256
+ // memory holds a job's secrets, not in another name (`MyRunner.Worker`,
257
+ // `Runner.Workers`) nor case (`runner.worker`); another process's memory, by
258
+ // its pid's digits or a glob (`/proc/1234/mem`, `/proc/*/mem`,
259
+ // `/proc/[0-9]*/mem`) or a substitution (`/proc/${pid}/mem`, a shell's
260
+ // `$pid`, `$(pgrep …)` and backticks, Python's `{pid}`, a format's `%d`),
261
+ // not a process's own (`/proc/self/mem`) nor what else /proc tells of one
262
+ // (`/proc/1234/status`); and trufflehog, a scanner for secrets, in any case
263
+ // (`TruffleHog`), not in another name (`trufflehogs`, `TRUFFLEHOG_VERSION`).
264
+ const GH_TOKEN = String.raw`(?<![\w.-])[gG][hH](?:\.[eE][xX][eE])?${APART}+auth${APART}+token(?![\w-])`
265
+ 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`
266
+ // A host whole, a URL's or alone: not after a word, a dot or a hyphen, nor a
267
+ // path's slash (`https://example.com/169.254.169.254`), a URL's two
268
+ // (`//`, `\/\/`) its start after a scheme's colon or alone
269
+ // (`//169.254.169.254`), not after a path's character
270
+ // (`https://example.com//169.254.169.254`); nor before more of a name or
271
+ // userinfo's `@` (`https://169.254.169.254@example.com`, `…:password@`).
272
+ const HOST_START = String.raw`(?<![\w.-]|(?<![\/\\])\/|[\w.~%-](?:\\?\/){2})`
273
+ const HOST_END = String.raw`(?![\w-]|\.[\w-]|(?::[^\s\/@'"\x60]{0,64})?@)`
274
+ const TRUFFLEHOG = String.raw`(?<![\p{ID_Continue}$-])trufflehog(?![\p{ID_Continue}$-])`
275
+ const HOLDS = [
276
+ ['credential-files', /(?<![\w$.-])\.(?:npmrc|ssh|aws)(?![\w-])/gu],
277
+ ['tokens', new RegExp(String.raw`^["'\u0060]${TOKEN_VAR}["'\u0060]$|(?:\$\{?|%)${TOKEN_VAR}(?![\p{ID_Continue}$])`, 'gu')],
278
+ ['gh-token', new RegExp(GH_TOKEN, 'gu')],
279
+ ['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')],
280
+ ['runner-worker', /(?<![\w$-])Runner\.Worker(?![\w$-])/gu],
281
+ ['process-memory', new RegExp(String.raw`\/proc\/(?:${PID})\/mem(?![\w-])`, 'gu')],
282
+ ['trufflehog', new RegExp(TRUFFLEHOG, 'giu')],
283
+ ['publish', /(?<![\w-])(?:npm|pnpm|yarn)(?:\s+-{1,2}\w[\w-]*(?:=\S*|\s+(?!-)\S+)?){0,8}?\s+publish(?![\w-])|\/-\/whoami(?![\w-])/gu],
284
+ ['shell', new RegExp(SHELL, 'giu')],
285
+ ['persistence', new RegExp(PERSISTENCE, 'giu')],
286
+ ]
287
+ // What a shell script holds, read whole: its commands, the places they write
288
+ // to, and the credentials a command, a process or its memory holds (`gh auth
289
+ // token`, `Runner.Worker`, `/proc/<pid>/mem`, trufflehog); not the cloud's
290
+ // metadata endpoint, whose fetching is a shell command already (`curl
291
+ // http://169.254.169.254/…`) and which a script provisioning a machine asks
292
+ // its region and id, nor a credential file or a token, which a script
293
+ // publishing a package names.
294
+ const SHELL_KINDS = new Set(['gh-token', 'runner-worker', 'process-memory', 'trufflehog', 'shell', 'persistence'])
295
+ const SHELL_HOLDS = HOLDS.filter(([kind]) => SHELL_KINDS.has(kind))
296
+ // A shell script, by its extension, its name a shell's startup file's
297
+ // (`.bashrc`, `.profile`), or its hashbang: a shell's, by its path or env's,
298
+ // env's options and settings before it (`#!/bin/ksh`, `#!/usr/bin/env -S
299
+ // bash -e`, `#!/usr/bin/env -S MODE=x bash`, `#!/usr/bin/env pwsh`), past a
300
+ // byte order mark.
301
+ 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
302
+ // A shell script's comment, a line's whole (`# curl … | sh`) or its end
303
+ // after a command (`echo ok # don't curl …`, `echo ok;# …`) where the quotes
304
+ // before it are closed, escapes read (`"a \" b"`, PowerShell's by a
305
+ // backtick), not in one (`"a # b"`), and no other `#` (`$#`, `<#`) is;
306
+ // PowerShell's, by its extension or hashbang, its blocks too (`<# … #>`,
307
+ // opened where a command or value may start, `$x=<# … #>1`, not in a
308
+ // string, `"<#"`, `"a <#"`, to the end where none closes); a batch file's, cmd's `::`
309
+ // and `REM` (`@REM`, and `REM` after `&` or `||`, `echo ok & REM …`); as
310
+ // none of its commands.
311
+ const SHELL_COMMENT = /^[ \t]*#.*$|^((?:[^'"\n#\\]|\\.|'[^'\n]*'|"(?:[^"\n\\]|\\.)*")*?[ \t;&|])#.*$/gmu
312
+ const POWERSHELL = /\.psm?1$/iu
313
+ const POWERSHELL_HASHBANG = /^\uFEFF?#!\s*\S*\/(?:env\s+(?:(?:-\S+|\w+=\S*)\s+)*)?pwsh(?![\w-])/u
314
+ const POWERSHELL_COMMENT = /^((?:[^'"\n#\u0060]|\u0060.|'[^'\n]*'|"(?:[^"\n\u0060]|\u0060.)*")*?)(?:(?<=^|[\s;({=,])<#[\s\S]*?(?:#>|(?![\s\S]))|(?<=^|[ \t;&|])#.*$)/gmu
315
+ const BATCH = /\.(?:bat|cmd)$/iu
316
+ const BATCH_COMMENT = /^[ \t]*@?(?:::|rem(?![\w-])).*$|(?<=&|\|\|)[ \t]*@?rem(?![\w-]).*$/gimu
317
+ const SHELL_HASHBANG = new RegExp(String.raw`^\uFEFF?#!\s*\S*\/(?:env\s+(?:(?:-\S+|\w+=\S*)\s+)*)?${SHELLS}(?![\w-])`, 'u')
318
+ // A directory and a file in it named apart, as path.join takes them
319
+ // (`join(home, '.claude', 'settings.json')`): a place code runs from again
320
+ // later where a file names both.
321
+ const PARTS = [
322
+ ['claude', /^(["'\u0060])\.claude\1$/iu, /^(["'\u0060])settings(?:\.local)?\.json\1$/iu],
323
+ ['vscode', /^(["'\u0060])\.vscode\1$/iu, /^(["'\u0060])tasks\.json\1$/iu],
324
+ ]
325
+ // `.profile` named apart where the home directory is the part before it
326
+ // (`join(os.homedir(), '.profile')`, `resolve(process.env.HOME, …)`, `'~'`),
327
+ // not a selector's class alone.
328
+ const PROFILE = /^(["'\u0060])\.profile\1$/iu
329
+ const HOME_BEFORE = /(?:homedir\s*\(\s*\)|\b(?:HOME|USERPROFILE)|(["'\u0060])~\1)\s*,\s*$/u
330
+ // A module a kind loads: vm, child_process, and libnpmpublish, npm's own
331
+ // publishing.
332
+ const MODULES = [
333
+ ['vm', /^(["'\u0060])(?:node:)?vm\1$/u],
334
+ ['child-process', /^(["'\u0060])(?:node:)?child_process\1$/u],
335
+ ['publish', /^(["'\u0060])libnpmpublish\1$/u],
336
+ ]
337
+ // A string a program is run with publishing (`spawn('npm', ['publish'])`),
338
+ // after the one naming it, its options and their values between
339
+ // (`['--tag', 'next', 'publish']`), and a request's method set to PUT
340
+ // (`{ method: 'PUT' }`, `options.method = 'put'`).
341
+ const PROGRAM = /^(["'\u0060])(?:npm|pnpm|yarn)\1$/u
342
+ const PUBLISH = /^(["'\u0060])publish\1$/u
343
+ const OPTION = /^(["'\u0060])-/u
344
+ const ARGUMENTS_BETWEEN = new RegExp(String.raw`^(?:[\s,[]|${COMMENT})*$`, 'u')
345
+ const PUT = /^(["'\u0060])put\1$/iu
346
+ const METHOD = new RegExp(String.raw`\bmethod["']?${GAP}[:=]${GAP}$`, 'u')
347
+ // A program run with its arguments apart, by its path too, a Windows one's
348
+ // from its drive with spaces in it (`spawn('curl', ['-fsSL', 'https://…'])`,
349
+ // `execFile('powershell.exe', ['-NoP', '-enc', …])`, `spawn('systemctl',
350
+ // ['--user', 'enable', …])`, `spawnSync('gh', ['auth', 'token'])`,
351
+ // `execFileSync('C:\\Program Files\\GitHub CLI\\gh.exe', …)`): its command
352
+ // line, its arguments' strings after its name, read as a shell command is, a
353
+ // variable's or a property's value between (`['-o', output, 'https://…']`,
354
+ // `outputs[i]`, `options?.out`), its first 20 arguments, however long.
355
+ const COMMAND = /^(["'\u0060])(?:\S*[\\/]|[a-z]:[\\/][^\n]*[\\/])?(?:curl|wget|certutil|bitsadmin|powershell|pwsh|systemctl|gh)(?:\.exe)?\1$/iu
356
+ // An argument a shell would need quoted, its separators no command's end
357
+ // (`'Cookie: a=b; c=d'`), its own double quotes single then.
358
+ const SEPARATES = /[;|&]/u
359
+ const VALUES_BETWEEN = new RegExp(String.raw`^(?:[\s,[\]\w$.?]|${COMMENT})*$`, 'u')
360
+ // A place named by its parts one after the other, as path.join takes them
361
+ // (`join(home, '.config', 'systemd', 'user')`), read as a path is.
362
+ const PLACE = new RegExp(PERSISTENCE, 'iu')
363
+ // What comes before a module's name where code loads it: `require(` (or
364
+ // `require?.(`), a bundler's `__require(` or `__non_webpack_require__(`,
365
+ // `import(`, `import` and `from`, comments allowed between
366
+ // (`import(/* webpackIgnore: true */ …)`); a name not loaded, as in a list of
367
+ // node's modules, is none, nor one a type-only import or export names
368
+ // (`import type { … } from`, `import type cp = require(`, comments allowed as
369
+ // in a load), which TypeScript erases (`import type from` imports a default
370
+ // named type, `import type = require(` one named so).
371
+ const LOADS = new RegExp(String.raw`(?:(?:require[\w$]*${GAP}(?:\?\.${GAP})?|\bimport${GAP})\(|\b(?:from|import))${GAP}$`, 'u')
372
+ const TYPE_ONLY = new RegExp(String.raw`\b(?:import|export)${GAP}type\b(?!${GAP}(?:from\b|=))[^;'"\x60]*(?:\bfrom|=${GAP}require${GAP}\()${GAP}$`, 'u')
373
+
374
+ // Each kind, in the order its tags show: its tag, what it does, its name in
375
+ // the tag's tooltip, a row of its own where kinds share the tag (a string run
376
+ // as code by any way, `eval`'s; a credential file, a token, a command, an
377
+ // endpoint or a process, `credentials`'), and for one marked as worse than
378
+ // the others' warning, `critical`: the whole environment read out,
379
+ // publishing, and a place code runs from again later.
380
+ const AS_CODE = { tag: 'eval', note: 'Runs a string as code' }
381
+ const CREDENTIALS = { tag: 'credentials', note: 'Reaches for credentials' }
382
+ export const RUNS_CODE = new Map([
383
+ ['eval', { ...AS_CODE, name: 'eval' }],
384
+ ['function', { ...AS_CODE, name: 'Function' }],
385
+ ['vm', { ...AS_CODE, name: 'vm' }],
386
+ ['compile', { ...AS_CODE, name: 'Module._compile' }],
387
+ ['child-process', { tag: 'child_process', note: 'Runs other programs', name: 'child_process' }],
388
+ ['credential-files', { ...CREDENTIALS, name: '.npmrc, .ssh or .aws' }],
389
+ ['tokens', { ...CREDENTIALS, name: 'NPM_TOKEN or GITHUB_TOKEN' }],
390
+ ['gh-token', { ...CREDENTIALS, name: 'gh auth token' }],
391
+ ['cloud-metadata', { ...CREDENTIALS, name: '169.254.169.254 or metadata.google.internal' }],
392
+ ['runner-worker', { ...CREDENTIALS, name: 'Runner.Worker' }],
393
+ ['process-memory', { ...CREDENTIALS, name: '/proc/<pid>/mem' }],
394
+ ['trufflehog', { ...CREDENTIALS, name: 'trufflehog' }],
395
+ ['env', { tag: 'whole env', note: 'Reads out the whole environment', name: 'process.env, by JSON.stringify, Object.entries or values, URLSearchParams', mark: 'critical' }],
396
+ ['publish', { tag: 'publish', note: 'Publishes to npm', name: 'npm publish, /-/whoami or a registry PUT', mark: 'critical' }],
397
+ ['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' }],
398
+ ['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' }],
399
+ ])
400
+
401
+ // The kinds a file's code holds, each with the lines it is on, of the first
402
+ // NPM_MAX_FOUND places it does each, in RUNS_CODE's order; null for none, or
403
+ // for a file that is no script. A shell script's, read whole, are its shell
404
+ // commands and the places it names; one with a shell's hashbang is one
405
+ // whatever its extension, and read as JavaScript too where it is that, as
406
+ // a launcher (`#!/bin/sh` then `exec node "$0"`) is both.
407
+ export function npmRunsCode(path, text) {
408
+ const script = npmIsScript(path, text)
409
+ const shell = typeof text === 'string' && (SHELL_SCRIPT.test(path) || SHELL_HASHBANG.test(text))
410
+ if (!script && !shell) return null
411
+ const found = new Map()
412
+ const at = (kind, offset) => {
413
+ const offsets = found.get(kind)
414
+ if (!offsets) found.set(kind, [offset])
415
+ else if (offsets.length < NPM_MAX_FOUND) offsets.push(offset)
416
+ }
417
+ if (script) read(text, 0, at)
418
+ if (shell && SAYS_ANY.test(text)) {
419
+ // Its comments blanked, its lines where they were.
420
+ const comments = BATCH.test(path) ? BATCH_COMMENT : POWERSHELL.test(path) || POWERSHELL_HASHBANG.test(text) ? POWERSHELL_COMMENT : SHELL_COMMENT
421
+ const commands = text.replace(comments, (comment, command = '') => command + comment.slice(command.length).replaceAll(/[^\n]/gu, ' '))
422
+ for (const [kind, holds] of SHELL_HOLDS) for (const { index } of commands.matchAll(holds)) at(kind, index)
423
+ }
424
+ // A PUT request publishing where the file names the registry; a place
425
+ // named by its directory and its file apart.
426
+ if (found.has('registry')) for (const offset of found.get('put') ?? []) at('publish', offset)
427
+ for (const [part] of PARTS) if (found.has(`${part}-file`)) for (const offset of found.get(`${part}-dir`) ?? []) at('persistence', offset)
428
+ if (![...RUNS_CODE.keys()].some(kind => found.has(kind))) return null
429
+ const line = lineOf(text)
430
+ 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)]))
431
+ }
432
+
433
+ // Where code (a file's, or a template literal's substitution's, at `base` in
434
+ // the file) does a kind, as at(kind, offset), in order: its code's, what its
435
+ // strings hold, a string naming a module where it is loaded, and what a
436
+ // template literal's substitutions (`${eval(x)}`) do, code again, read as its
437
+ // own; a PUT request (`put`) and the registry's host (`registry`) as their
438
+ // own, for the file to pair. Most code says none of it at all, told before
439
+ // anything is read.
440
+ function read(code, base, at) {
441
+ if (!SAYS_ANY.test(code)) return
442
+ // The string before, and where, a place's part maybe; the command line
443
+ // since a program's name, and where it is named; where the string before
444
+ // ends; whether the strings since a program's name are its arguments, an
445
+ // option or its value each, and whether the last is an option.
446
+ let before = null, command = null, end = 0, option = false, program = false
447
+ for (const { 0: token, 1: string, groups, index } of code.matchAll(READS)) {
448
+ if (string === undefined) {
449
+ for (const kind of CODE_KINDS) if (groups[kind]) at(kind, base + index)
450
+ continue
451
+ }
452
+ if (string[0] === '/') continue
453
+ const inner = string[0] === '`' && SAYS_ANY.test(string) ? substitutions(string) : []
454
+ for (const [from, to] of inner) read(string.slice(from, to), base + index + from, at)
455
+ // What a string holds, a template's text alone, its substitutions' code
456
+ // read as code, its comments and regular expressions aside.
457
+ const text = inner.length === 0 ? string : textOf(string, inner)
458
+ // A token's name as the string's whole (`within` 0, its quote) is a key,
459
+ // not one where its absence is told, it is dropped or only set.
460
+ const keyed = () => {
461
+ const after = code.slice(index + token.length, index + token.length + 20)
462
+ return !KEY_SET.test(after) && (!KEY_ENDS.test(after) || !KEY_ABSENT.test(code.slice(Math.max(0, index - 300), index)))
463
+ }
464
+ // Each kind searched for before its places are listed, as matchAll copies
465
+ // its regular expression each time.
466
+ const held = HOLDS_ANY.test(text) ? HOLDS.flatMap(([kind, holds]) => text.search(holds) === -1 ? [] : [...text.matchAll(holds)].map(({ index: within }) => [kind, within])) : []
467
+ for (const [kind, within] of held) if (kind !== 'tokens' || within > 0 || keyed()) at(kind, base + index + within)
468
+ if (text.includes('registry.') && REGISTRY_URL.test(unescaped(text).replace(URL_STRIPPED, ''))) at('registry', base + index)
469
+ for (const [kind, name] of MODULES) if (name.test(string) && loaded(code, index)) at(kind, base + index)
470
+ if (PROFILE.test(string) && HOME_BEFORE.test(code.slice(Math.max(0, index - 100), index))) at('persistence', base + index)
471
+ for (const [part, dir, file] of PARTS) {
472
+ if (dir.test(string)) at(`${part}-dir`, base + index)
473
+ if (file.test(string)) at(`${part}-file`, base + index)
474
+ }
475
+ if (program && ARGUMENTS_BETWEEN.test(code.slice(end, index))) {
476
+ if (PUBLISH.test(string)) at('publish', base + index)
477
+ program = OPTION.test(string) || option
478
+ option = OPTION.test(string)
479
+ } else program = false
480
+ if (PROGRAM.test(string)) [program, option] = [true, false]
481
+ if (command && command.arguments < 20 && VALUES_BETWEEN.test(code.slice(end, index))) {
482
+ const argument = text.slice(1, -1)
483
+ command.line += ` ${SEPARATES.test(argument) ? `"${argument.replaceAll('"', "'")}"` : argument}`
484
+ command.arguments++
485
+ const { line } = command
486
+ const hit = SHELL_HOLDS.find(([, holds]) => line.search(holds) !== -1)
487
+ if (hit) {
488
+ at(hit[0], command.at)
489
+ command = null
490
+ }
491
+ } else command = null
492
+ if (COMMAND.test(string)) command = { arguments: 0, at: base + index, line: string.slice(1, -1) }
493
+ if (before && before.text.length + text.length < 200 && ARGUMENTS_BETWEEN.test(code.slice(end, index))) {
494
+ const [first, second] = [before.text.slice(1, -1), text.slice(1, -1)]
495
+ if (!PLACE.test(first) && !PLACE.test(second) && PLACE.test(`${first}/${second}`)) at('persistence', before.at)
496
+ }
497
+ before = { text, at: base + index }
498
+ if (PUT.test(string) && METHOD.test(code.slice(Math.max(0, index - 300), index))) at('put', base + index)
499
+ end = index + token.length
500
+ }
501
+ }
502
+
503
+ // The kinds code itself does, by READS' groups.
504
+ const CODE_KINDS = ['eval', 'function', 'compile', 'tokens', 'env', 'put']
505
+
506
+ // Whether the string at `index` in code names a module where it is loaded.
507
+ function loaded(code, index) {
508
+ const before = code.slice(Math.max(0, index - 300), index)
509
+ return LOADS.test(before) && !TYPE_ONLY.test(before)
510
+ }
511
+
512
+ // A template literal with its substitutions' code blanked, its text in place.
513
+ function textOf(template, inner) {
514
+ const parts = []
515
+ let from = 0
516
+ for (const [start, end] of inner) {
517
+ parts.push(template.slice(from, start), ' '.repeat(end - start))
518
+ from = end
519
+ }
520
+ parts.push(template.slice(from))
521
+ return parts.join('')
522
+ }
523
+
524
+ // Where each of a template literal's substitutions' code starts and ends, in
525
+ // it: from its `${` to the `}` that closes it.
526
+ function substitutions(template) {
527
+ const found = []
528
+ for (let i = template.indexOf('${'); i !== -1; i = template.indexOf('${', i)) {
529
+ // Escaped by an odd run of backslashes before it (`\${`, not `\\${`).
530
+ let run = 0
531
+ while (template[i - 1 - run] === '\\') run++
532
+ if (run % 2 === 1) {
533
+ i += 2
534
+ continue
535
+ }
536
+ const end = closing(template, i + 2)
537
+ found.push([i + 2, end])
538
+ i = end
539
+ }
540
+ return found
541
+ }
542
+
543
+ // A string, regular expression or comment where it starts, told as
544
+ // JS_STRING_OR_COMMENT tells them in a whole text.
545
+ const TOKEN = new RegExp(JS_STRING_OR_COMMENT.source, 'muy')
546
+
547
+ // Where the `}` closing code from `from` in a text is, or the text's end for
548
+ // none: the first its braces leave unbalanced, those in its strings, regular
549
+ // expressions and comments aside (`${/* } */ x}`, `${/}/.test(x)}`).
550
+ function closing(text, from) {
551
+ let depth = 0
552
+ for (let at = from; at < text.length; at++) {
553
+ const char = text[at]
554
+ // Tried where a token can start: a quote, a slash, or a run of blanks, a
555
+ // comment's leading blanks taken from where they start.
556
+ if ('"\'`/ \t'.includes(char)) {
557
+ TOKEN.lastIndex = at
558
+ const token = TOKEN.exec(text)
559
+ if (token) {
560
+ at += token[0].length - 1
561
+ continue
562
+ }
563
+ }
564
+ if (char === '{') depth++
565
+ else if (char === '}' && depth-- === 0) return at
566
+ }
567
+ return text.length
568
+ }
569
+
570
+ // What `found` holds (npmRunsCode's kinds, hosts.js's services) that
571
+ // `was` didn't, each with its lines: what is new to a file, as its tags say,
572
+ // `was` null for one the version compared with didn't hold, or held none in.
573
+ // Null for none.
574
+ export function npmFoundAdded(found, was) {
575
+ if (!found) return null
576
+ const added = new Map([...found].filter(([kind]) => !was?.has(kind)))
577
+ return added.size > 0 ? added : null
578
+ }
579
+
580
+ // The kinds any of `founds` holds, as their tags name them, in their order,
581
+ // a tag kinds share once: `eval, child_process`.
582
+ export const npmRunsCodeKinds = founds => [...new Set([...RUNS_CODE].filter(([kind]) => founds.some(found => found.has(kind))).map(([, { tag }]) => tag))].join(', ')
583
+
584
+ // What `found` holds (npmRunsCode's) but the kinds whose tags `tags` names,
585
+ // each with its lines; null for none.
586
+ export function npmRunsCodeWithout(found, tags) {
587
+ const kept = new Map([...found ?? []].filter(([kind]) => !tags.has(RUNS_CODE.get(kind).tag)))
588
+ return kept.size > 0 ? kept : null
589
+ }
590
+
591
+ // Its tags as a file's flags (file-flags.js), in their order, each once: a
592
+ // warning's (`high`), or for one marked worse, `critical`; none for none.
593
+ export const npmRunsCodeFlags = found => [...new Map([...found?.keys() ?? []].map(kind => RUNS_CODE.get(kind))
594
+ .map(({ tag, mark }) => [tag, { name: tag, severity: mark ?? 'high' }])).values()]
595
+