0sec-cli 0.15.0 → 0.17.0

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 (209) hide show
  1. package/0sec.js +322 -0
  2. package/README.md +63 -52
  3. package/attacks/data-exfiltration/pii-leakage.yaml +27 -0
  4. package/attacks/encoding-bypass/base64-encoding.yaml +24 -0
  5. package/attacks/jailbreak/dan-roleplay.yaml +27 -0
  6. package/attacks/jailbreak/hypothetical-scenario.yaml +25 -0
  7. package/attacks/jailbreak/multilingual-bypass.yaml +22 -0
  8. package/attacks/output-manipulation/harmful-content.yaml +25 -0
  9. package/attacks/prompt-injection/context-manipulation.yaml +32 -0
  10. package/attacks/prompt-injection/direct-injection.yaml +28 -0
  11. package/attacks/prompt-injection/indirect-injection.yaml +33 -0
  12. package/attacks/system-prompt-extraction/direct-ask.yaml +30 -0
  13. package/attacks/system-prompt-extraction/markdown-exfil.yaml +26 -0
  14. package/attacks/tool-misuse/ssrf-via-tools.yaml +27 -0
  15. package/chunks/adapt-loop-ECQG7454.js +18 -0
  16. package/chunks/adgraph-JLGA6RYI.js +54 -0
  17. package/chunks/agent/skills/frameworks/entra-id.yaml +63 -0
  18. package/chunks/agent/skills/frameworks/graphql-introspection.yaml +116 -0
  19. package/chunks/agent/skills/frameworks/nextjs.yaml +82 -0
  20. package/chunks/agent/skills/frameworks/python-web.yaml +85 -0
  21. package/chunks/agent/skills/frameworks/supabase.yaml +83 -0
  22. package/chunks/agent/skills/frameworks/wordpress-deep.yaml +122 -0
  23. package/chunks/agent/skills/techniques/ad-attack-paths.yaml +57 -0
  24. package/chunks/agent/skills/techniques/advisory-disclosure.yaml +59 -0
  25. package/chunks/agent/skills/techniques/assumption-mining.yaml +53 -0
  26. package/chunks/agent/skills/techniques/blind-exploitation.yaml +143 -0
  27. package/chunks/agent/skills/techniques/crypto-misuse.yaml +88 -0
  28. package/chunks/agent/skills/techniques/cve-poc-adaptation.yaml +82 -0
  29. package/chunks/agent/skills/techniques/entra-attack-paths.yaml +55 -0
  30. package/chunks/agent/skills/techniques/http-conformance-diff.yaml +48 -0
  31. package/chunks/agent/skills/techniques/jwt-attacks.yaml +137 -0
  32. package/chunks/agent/skills/techniques/kernel-weaponization.yaml +53 -0
  33. package/chunks/agent/skills/techniques/llm-excessive-agency.yaml +55 -0
  34. package/chunks/agent/skills/techniques/llm-insecure-output-handling.yaml +56 -0
  35. package/chunks/agent/skills/techniques/llm-prompt-injection.yaml +54 -0
  36. package/chunks/agent/skills/techniques/llm-prompt-layer-write.yaml +68 -0
  37. package/chunks/agent/skills/techniques/llm-rag-poisoning.yaml +57 -0
  38. package/chunks/agent/skills/techniques/llm-safety-eval.yaml +51 -0
  39. package/chunks/agent/skills/techniques/npm-ecosystem.yaml +51 -0
  40. package/chunks/agent/skills/techniques/poc-verification.yaml +55 -0
  41. package/chunks/agent/skills/techniques/race-condition.yaml +122 -0
  42. package/chunks/agent/skills/techniques/scoped-fix.yaml +49 -0
  43. package/chunks/agent/skills/techniques/seedless-depth-review.yaml +59 -0
  44. package/chunks/agent/skills/techniques/spec-differential.yaml +48 -0
  45. package/chunks/agent/skills/techniques/variant-hunting.yaml +55 -0
  46. package/chunks/agent/skills/vulnerabilities/cardano-eutxo-validators.yaml +72 -0
  47. package/chunks/agent/skills/vulnerabilities/command-injection.yaml +99 -0
  48. package/chunks/agent/skills/vulnerabilities/deserialization-chains.yaml +106 -0
  49. package/chunks/agent/skills/vulnerabilities/native-memory-safety.yaml +93 -0
  50. package/chunks/agent/skills/vulnerabilities/path-traversal.yaml +78 -0
  51. package/chunks/agent/skills/vulnerabilities/prototype-pollution.yaml +132 -0
  52. package/chunks/agent/skills/vulnerabilities/request-smuggling.yaml +129 -0
  53. package/chunks/agent/skills/vulnerabilities/sqli-advanced.yaml +102 -0
  54. package/chunks/agent/skills/vulnerabilities/ssrf-bypass.yaml +84 -0
  55. package/chunks/agent/skills/vulnerabilities/ssti-exploitation.yaml +112 -0
  56. package/chunks/agent/skills/vulnerabilities/structural-sqli.yaml +91 -0
  57. package/chunks/appsec-catalog-CBWHGTEL.js +24 -0
  58. package/chunks/artifact-scraper-GJEZMOHF.js +34 -0
  59. package/chunks/assumption-mining-BVMMUZHB.js +79 -0
  60. package/chunks/chunk-2CJ776PV.js +2430 -0
  61. package/chunks/chunk-2RMLOJVB.js +47 -0
  62. package/chunks/chunk-2SANI5RH.js +4728 -0
  63. package/chunks/chunk-3MOLBTLS.js +953 -0
  64. package/chunks/chunk-3QFDYBZQ.js +5108 -0
  65. package/chunks/chunk-47TQJWDH.js +3729 -0
  66. package/chunks/chunk-4Y4KQJXE.js +600 -0
  67. package/chunks/chunk-53G27VPS.js +136 -0
  68. package/chunks/chunk-57ZENEX2.js +169 -0
  69. package/chunks/chunk-5G3ZXFBW.js +182 -0
  70. package/chunks/chunk-6LKRLK2R.js +542 -0
  71. package/chunks/chunk-7DQEV5QI.js +287 -0
  72. package/chunks/chunk-A6CLR72I.js +235 -0
  73. package/chunks/chunk-BKZFDZ23.js +361 -0
  74. package/chunks/chunk-CLHCDHP4.js +471 -0
  75. package/chunks/chunk-D6S3IIPG.js +518 -0
  76. package/chunks/chunk-DN25OQQA.js +1070 -0
  77. package/chunks/chunk-DW5UWPFY.js +275 -0
  78. package/chunks/chunk-F3WBKITT.js +687 -0
  79. package/chunks/chunk-H2FFLZNK.js +3 -0
  80. package/chunks/chunk-H44E2CRN.js +193 -0
  81. package/chunks/chunk-HDWV7PGI.js +596 -0
  82. package/chunks/chunk-HE7LCA7G.js +381 -0
  83. package/chunks/chunk-HT6P7RY3.js +2597 -0
  84. package/chunks/chunk-IOL7D5YV.js +515 -0
  85. package/chunks/chunk-IORC6MQX.js +72828 -0
  86. package/chunks/chunk-IR537GON.js +49 -0
  87. package/chunks/chunk-K26SZ37E.js +5735 -0
  88. package/chunks/chunk-KKGE5RQE.js +1094 -0
  89. package/chunks/chunk-KLTNTE2Z.js +121 -0
  90. package/chunks/chunk-KTQDLNSR.js +1732 -0
  91. package/chunks/chunk-LOHTE223.js +2572 -0
  92. package/chunks/chunk-LP3HHYQU.js +245 -0
  93. package/chunks/chunk-MMLDQR4H.js +852 -0
  94. package/chunks/chunk-MYVT64FN.js +16 -0
  95. package/chunks/chunk-O462Y7P2.js +33019 -0
  96. package/chunks/chunk-OEFNRYI2.js +150 -0
  97. package/chunks/chunk-P6WKNFWX.js +1968 -0
  98. package/chunks/chunk-QI233I24.js +333 -0
  99. package/chunks/chunk-QJQKHO7G.js +917 -0
  100. package/chunks/chunk-QKICO43A.js +808 -0
  101. package/chunks/chunk-QOKTUOU2.js +102 -0
  102. package/chunks/chunk-RDZYQQLW.js +596 -0
  103. package/chunks/chunk-RJWOYEOG.js +271 -0
  104. package/chunks/chunk-RRMJC3ZE.js +105 -0
  105. package/chunks/chunk-RWONANDA.js +973 -0
  106. package/chunks/chunk-SAFFWQW4.js +5921 -0
  107. package/chunks/chunk-SF4KZ4O3.js +502 -0
  108. package/chunks/chunk-SNKTC4BP.js +1422 -0
  109. package/chunks/chunk-SOG2U7B3.js +662 -0
  110. package/chunks/chunk-SZJCPG2I.js +110 -0
  111. package/chunks/chunk-UJ4IK5PO.js +2010 -0
  112. package/chunks/chunk-UM3ZNQIM.js +9329 -0
  113. package/chunks/chunk-VGRDNSHA.js +3249 -0
  114. package/chunks/chunk-VQG4FT5D.js +685 -0
  115. package/chunks/chunk-WKNNZVJS.js +2798 -0
  116. package/chunks/chunk-WU6AFRAZ.js +1167 -0
  117. package/chunks/chunk-WVTBZEQO.js +1531 -0
  118. package/chunks/chunk-YLMN3N25.js +4054 -0
  119. package/chunks/commands-U3AMV5ZU.js +16795 -0
  120. package/chunks/corpus-v1.json +403 -0
  121. package/chunks/cost-ledger-ZCKMBJEN.js +13 -0
  122. package/chunks/data/appsec-archetypes.json +102 -0
  123. package/chunks/data/chromium-archetypes.json +204 -0
  124. package/chunks/data/freebsd-archetypes.json +171 -0
  125. package/chunks/data/kernel-archetypes.json +611 -0
  126. package/chunks/db-26NGQFKO.js +16 -0
  127. package/chunks/disclose-A6AEIEDA.js +132 -0
  128. package/chunks/dist-FDKALV4R.js +2772 -0
  129. package/chunks/dist-GL66KMUX.js +74 -0
  130. package/chunks/dist-JR67XKYO.js +159 -0
  131. package/chunks/eval-runner-DDLE5RQ3.js +27 -0
  132. package/chunks/example-manifest.json +95 -0
  133. package/chunks/exploit-agent-XP23ISN7.js +98 -0
  134. package/chunks/exploit-autoclimb-E6T7RRSC.js +81 -0
  135. package/chunks/exploit-climb-2FAUP6XU.js +358 -0
  136. package/chunks/fix-IG7LYXH3.js +12 -0
  137. package/chunks/github-issues-MJ6OYOOU.js +157 -0
  138. package/chunks/harness-HWOYFKFY.js +22 -0
  139. package/chunks/http-conformance-DU66MZIU.js +11 -0
  140. package/chunks/http-sender-GWH2IYEA.js +10 -0
  141. package/chunks/hunt-scan-XPELSPQQ.js +38 -0
  142. package/chunks/identity-6ZAIIWOR.js +191 -0
  143. package/chunks/kernel-primitive-TENE3R7T.js +44 -0
  144. package/chunks/kernel-vm-runner-4F6QSNFY.js +62 -0
  145. package/chunks/memsafety-scan-J7SECV5W.js +16 -0
  146. package/chunks/native-loop-NTONUDP7.js +54 -0
  147. package/chunks/npm-detectors-KB5Y5ZFX.js +69 -0
  148. package/chunks/npm-dynamic-discovery-AHROOZDB.js +13 -0
  149. package/chunks/orchestrate-ZUWAUWBC.js +62 -0
  150. package/chunks/pipeline-FCARFZU3.js +14 -0
  151. package/chunks/pre-recon-cve-66EB6G4M.js +351 -0
  152. package/chunks/prepare-MYY743TK.js +13 -0
  153. package/chunks/process-3Q7QJIOZ.js +13 -0
  154. package/chunks/replay-runner-RRR4E2A7.js +39 -0
  155. package/chunks/run-ESNN4V5W.js +32632 -0
  156. package/chunks/runtime-62ZQAH7H.js +43 -0
  157. package/chunks/runtime-J7PLXZNM.js +12 -0
  158. package/chunks/scan-stream-FHI2FYZE.js +96 -0
  159. package/chunks/scope-BI7BF4ZY.js +18 -0
  160. package/chunks/session-store-BCFQYDCE.js +28 -0
  161. package/chunks/source-files-PWRQR6LY.js +12 -0
  162. package/chunks/specdrift-WCLH6TTQ.js +19 -0
  163. package/chunks/variant-candidates-6KM6F6MD.js +13 -0
  164. package/chunks/web-recon-prepass-DPVCBRZA.js +1148 -0
  165. package/dashboard/assets/0sec-icon-66SreztZ.gif +0 -0
  166. package/dashboard/assets/bot-yILPnRgD.js +1 -0
  167. package/dashboard/assets/chevron-down-BeDJ-Vka.js +1 -0
  168. package/dashboard/assets/circle-alert-BEnMBMT-.js +1 -0
  169. package/dashboard/assets/client-DRYsuOGl.js +9 -0
  170. package/dashboard/assets/copy-Bi5vzoF9.js +1 -0
  171. package/dashboard/assets/desktop-CXTQonNm.js +32 -0
  172. package/dashboard/assets/desktop-NOekk4QS.css +1 -0
  173. package/dashboard/assets/dist-CmX-h7a4.js +1 -0
  174. package/dashboard/assets/dist-DAwYQmf_.js +45 -0
  175. package/dashboard/assets/findings-page-CACW9nw2.js +5 -0
  176. package/dashboard/assets/format-PoISqsES.js +1 -0
  177. package/dashboard/assets/geist-cyrillic-ext-wght-normal-DjL33-gN.woff2 +0 -0
  178. package/dashboard/assets/geist-cyrillic-wght-normal-BEAKL7Jp.woff2 +0 -0
  179. package/dashboard/assets/geist-latin-ext-wght-normal-DC-KSUi6.woff2 +0 -0
  180. package/dashboard/assets/geist-latin-wght-normal-BgDaEnEv.woff2 +0 -0
  181. package/dashboard/assets/geist-vietnamese-wght-normal-6IgcOCM7.woff2 +0 -0
  182. package/dashboard/assets/ibm-plex-mono-cyrillic-400-normal-BSMlKf0J.woff2 +0 -0
  183. package/dashboard/assets/ibm-plex-mono-cyrillic-400-normal-CEL4l2ZJ.woff +0 -0
  184. package/dashboard/assets/ibm-plex-mono-cyrillic-ext-400-normal-DMdlQ8Kv.woff +0 -0
  185. package/dashboard/assets/ibm-plex-mono-cyrillic-ext-400-normal-xuaO2J-f.woff2 +0 -0
  186. package/dashboard/assets/ibm-plex-mono-latin-400-normal-CvHOgSBP.woff +0 -0
  187. package/dashboard/assets/ibm-plex-mono-latin-400-normal-DMJ8VG8y.woff2 +0 -0
  188. package/dashboard/assets/ibm-plex-mono-latin-ext-400-normal-BmRBH3aV.woff2 +0 -0
  189. package/dashboard/assets/ibm-plex-mono-latin-ext-400-normal-D3D2R8hC.woff +0 -0
  190. package/dashboard/assets/ibm-plex-mono-vietnamese-400-normal-BulugwFq.woff2 +0 -0
  191. package/dashboard/assets/ibm-plex-mono-vietnamese-400-normal-DDuiU_S-.woff +0 -0
  192. package/dashboard/assets/jsx-runtime-C7oxC63R.js +1 -0
  193. package/dashboard/assets/live-page-DrgQqQK5.js +1 -0
  194. package/dashboard/assets/meta-tile-CcskIV_o.js +1 -0
  195. package/dashboard/assets/operations-BKE8T-vY.css +2 -0
  196. package/dashboard/assets/operations-D8nUP5_m.js +4 -0
  197. package/dashboard/assets/operations-app-CQQhG54n.js +2 -0
  198. package/dashboard/assets/overview-page-DTGS8dPo.js +1 -0
  199. package/dashboard/assets/page-header-MFVvEtZW.js +1 -0
  200. package/dashboard/assets/play-Dwo-9xt6.js +1 -0
  201. package/dashboard/assets/scans-page-ChVtq8Us.js +1 -0
  202. package/dashboard/assets/search-CiooTLck.js +1 -0
  203. package/dashboard/assets/siren-CCBwTSuk.js +1 -0
  204. package/dashboard/assets/table-BYS-nIVA.js +1 -0
  205. package/dashboard/assets/tabs-DivxbhQy.js +1 -0
  206. package/dashboard/desktop.html +21 -0
  207. package/dashboard/index.html +15 -0
  208. package/package.json +30 -18
  209. package/bin/0sec.cjs +0 -361
package/bin/0sec.cjs DELETED
@@ -1,361 +0,0 @@
1
- #!/usr/bin/env node
2
- /**
3
- * 0sec-cli npm launcher.
4
- *
5
- * The npm package is a launcher that downloads the right standalone binary
6
- * for the host platform on first run, caches it, and re-execs every
7
- * subsequent run with the user's args. The standalone binary has the Bun
8
- * runtime baked in, so the full OpenTUI experience is available even
9
- * though npm itself runs under Node.
10
- *
11
- * The build script (`scripts/build-npm-shim.mjs`) substitutes
12
- * `0.15.0` with the published version at build time, so this
13
- * launcher always pulls the binary that matches its own npm version.
14
- *
15
- * Cache layout:
16
- * ~/.0sec/cache/v<version>/0sec (executable)
17
- * ~/.0sec/cache/v<version>/0sec.exe (Windows)
18
- *
19
- * Env knobs:
20
- * 0SEC_BINARY — explicit path to a binary; bypasses
21
- * the cache + download entirely.
22
- * Used by tests and operators who
23
- * build from source.
24
- * 0SEC_NO_DOWNLOAD=1 — never download; if the binary isn't
25
- * cached, print the install.sh fallback
26
- * URL and exit 1.
27
- * 0SEC_DOWNLOAD_TIMEOUT_MS — per-attempt timeout for the GH
28
- * release download (default: 120000).
29
- *
30
- * Supply-chain integrity (added 2026-05-16):
31
- * Every downloaded binary is verified against the SHA-256 manifest
32
- * published as `checksums.txt` alongside the release binaries before
33
- * it is made executable. A mismatch, a missing manifest, or a manifest
34
- * that doesn't list the expected asset filename aborts the install
35
- * instead of caching the binary. See `verifyChecksum` below and the
36
- * "Binary integrity" section of SECURITY.md.
37
- */
38
-
39
- "use strict";
40
-
41
- const {
42
- existsSync,
43
- mkdirSync,
44
- chmodSync,
45
- renameSync,
46
- statSync,
47
- createWriteStream,
48
- unlinkSync,
49
- createReadStream,
50
- } = require("node:fs");
51
- const { homedir, tmpdir } = require("node:os");
52
- const { join, dirname } = require("node:path");
53
- const { spawn } = require("node:child_process");
54
- const { request } = require("node:https");
55
- const { createHash } = require("node:crypto");
56
-
57
- const VERSION = "0.15.0";
58
- const REPO = "0sec-labs/0sec";
59
- const CHECKSUMS_FILENAME = "checksums.txt";
60
-
61
- const RESET = "\x1b[0m";
62
- const BOLD = "\x1b[1m";
63
- const DIM = "\x1b[2m";
64
- const ORANGE = "\x1b[38;2;250;178;131m";
65
- const RED = "\x1b[31m";
66
-
67
- function err(msg) {
68
- process.stderr.write(`${RED}[0sec]${RESET} ${msg}\n`);
69
- }
70
-
71
- function detectAsset() {
72
- const platform = process.platform;
73
- const arch = process.arch;
74
-
75
- if (platform === "darwin" && arch === "arm64") return "0sec-darwin-arm64";
76
- if (platform === "linux" && arch === "x64") return "0sec-linux-x64";
77
- if (platform === "linux" && arch === "arm64") return "0sec-linux-arm64";
78
- if (platform === "win32" && arch === "x64") return "0sec-windows-x64.exe";
79
- return null;
80
- }
81
-
82
- function cachePath(asset) {
83
- return join(homedir(), ".0sec", "cache", `v${VERSION}`, asset);
84
- }
85
-
86
- function downloadUrl(asset) {
87
- return `https://github.com/${REPO}/releases/download/v${VERSION}/${asset}`;
88
- }
89
-
90
- function checksumsUrl() {
91
- return `https://github.com/${REPO}/releases/download/v${VERSION}/${CHECKSUMS_FILENAME}`;
92
- }
93
-
94
- // Follow redirects (GH releases bounce through codeload). Returns a
95
- // readable stream of body bytes once we land on a 2xx, or rejects.
96
- function fetchFollowRedirects(url, timeoutMs) {
97
- return new Promise((resolve, reject) => {
98
- let redirects = 0;
99
- const MAX_REDIRECTS = 5;
100
-
101
- const go = (currentUrl) => {
102
- const req = request(currentUrl, { method: "GET" }, (res) => {
103
- if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
104
- redirects++;
105
- if (redirects > MAX_REDIRECTS) {
106
- reject(new Error(`too many redirects (${MAX_REDIRECTS}+) for ${url}`));
107
- return;
108
- }
109
- // Drain the redirect response body so the socket can be reused.
110
- res.resume();
111
- // GH gives absolute URLs in Location, but be defensive.
112
- const next = new URL(res.headers.location, currentUrl).toString();
113
- go(next);
114
- return;
115
- }
116
- if (res.statusCode !== 200) {
117
- reject(new Error(`HTTP ${res.statusCode} fetching ${currentUrl}`));
118
- res.resume();
119
- return;
120
- }
121
- resolve(res);
122
- });
123
- req.setTimeout(timeoutMs, () => {
124
- req.destroy(new Error(`download timeout after ${timeoutMs}ms`));
125
- });
126
- req.on("error", reject);
127
- req.end();
128
- };
129
-
130
- go(url);
131
- });
132
- }
133
-
134
- // Buffer a full HTTP body (used for the small `checksums.txt` manifest).
135
- async function fetchTextBody(url, timeoutMs) {
136
- const res = await fetchFollowRedirects(url, timeoutMs);
137
- return new Promise((resolve, reject) => {
138
- const chunks = [];
139
- res.on("data", (c) => chunks.push(c));
140
- res.on("error", reject);
141
- res.on("end", () => resolve(Buffer.concat(chunks).toString("utf8")));
142
- });
143
- }
144
-
145
- // ─── Checksum verification ─────────────────────────────────────────────
146
- //
147
- // Pure helpers, exported via `module.exports` so they can be unit-tested
148
- // without standing up the full launcher. Anything that touches the
149
- // network or filesystem state lives above; the parsing + comparison
150
- // logic stays here and stays deterministic.
151
-
152
- /**
153
- * Parse a `sha256sum`-format manifest:
154
- *
155
- * <64-hex-digit> <filename>
156
- * <64-hex-digit> *<filename> (binary-mode marker)
157
- *
158
- * Lines that don't match are ignored (so a trailing blank line or
159
- * informational comment doesn't kill the parse). Filenames are taken
160
- * verbatim — no path normalisation, no quoting tricks.
161
- *
162
- * Returns a Map<filename, lowercase-hex-digest>.
163
- */
164
- function parseChecksumsManifest(text) {
165
- const out = new Map();
166
- if (typeof text !== "string") {
167
- throw new TypeError("checksums manifest must be a string");
168
- }
169
- for (const rawLine of text.split(/\r?\n/)) {
170
- const line = rawLine.trim();
171
- if (!line || line.startsWith("#")) continue;
172
- // GNU `sha256sum`: "<hex> <name>" (two spaces, text mode) or
173
- // "<hex> *<name>" (single space + asterisk, binary mode). BSD
174
- // `shasum -a 256` produces the same line shape.
175
- const m = line.match(/^([0-9a-fA-F]{64})\s+\*?(.+?)\s*$/);
176
- if (!m) continue;
177
- out.set(m[2], m[1].toLowerCase());
178
- }
179
- return out;
180
- }
181
-
182
- /** Compute a hex-encoded SHA-256 of a file by streaming it. */
183
- function sha256File(path) {
184
- return new Promise((resolve, reject) => {
185
- const h = createHash("sha256");
186
- const s = createReadStream(path);
187
- s.on("error", reject);
188
- s.on("data", (chunk) => h.update(chunk));
189
- s.on("end", () => resolve(h.digest("hex")));
190
- });
191
- }
192
-
193
- /**
194
- * Verify a downloaded asset against a checksums-manifest text.
195
- *
196
- * Throws (with an actionable message) if:
197
- * - the manifest parses to zero entries
198
- * - the manifest has no entry for `expectedAsset`
199
- * - the computed digest doesn't match the manifest entry
200
- *
201
- * Resolves to the matched hex digest on success.
202
- */
203
- async function verifyChecksum({ filePath, expectedAsset, manifestText }) {
204
- const entries = parseChecksumsManifest(manifestText);
205
- if (entries.size === 0) {
206
- throw new Error(
207
- `checksums manifest is empty or unparseable — refusing to trust ${expectedAsset}`,
208
- );
209
- }
210
- const expected = entries.get(expectedAsset);
211
- if (!expected) {
212
- throw new Error(
213
- `checksums manifest has no entry for "${expectedAsset}" — refusing to install (manifest lists: ${[...entries.keys()].join(", ")})`,
214
- );
215
- }
216
- const actual = await sha256File(filePath);
217
- if (actual.toLowerCase() !== expected.toLowerCase()) {
218
- throw new Error(
219
- `SHA-256 mismatch for ${expectedAsset}\n expected: ${expected}\n actual: ${actual}\nrefusing to install a binary that doesn't match the release manifest.`,
220
- );
221
- }
222
- return actual;
223
- }
224
-
225
- function printInstallFallback(asset) {
226
- const url = downloadUrl(asset);
227
- err("could not provision the standalone binary automatically.");
228
- err("");
229
- err("install via:");
230
- err(` ${ORANGE}curl -fsSL https://raw.githubusercontent.com/${REPO}/main/install.sh | bash${RESET}`);
231
- err("");
232
- err("or download the binary directly:");
233
- err(` ${url}`);
234
- err("");
235
- }
236
-
237
- async function ensureBinary() {
238
- // Operator override — used by tests + source builds.
239
- const explicit = process.env["0SEC_BINARY"];
240
- if (explicit && existsSync(explicit)) return explicit;
241
-
242
- const asset = detectAsset();
243
- if (!asset) {
244
- err(`unsupported platform: ${process.platform}/${process.arch}`);
245
- err("supported: darwin-arm64, linux-x64, linux-arm64, windows-x64");
246
- err("");
247
- err(`Intel Mac users: install Bun and build from source — see ${ORANGE}https://github.com/${REPO}#standalone-binary${RESET}`);
248
- process.exit(2);
249
- }
250
-
251
- const cached = cachePath(asset);
252
- if (existsSync(cached)) return cached;
253
-
254
- if (process.env["0SEC_NO_DOWNLOAD"] === "1") {
255
- printInstallFallback(asset);
256
- process.exit(1);
257
- }
258
-
259
- // Lazy first-run UX: tell the user we're fetching ~75-130 MB so they
260
- // don't think the CLI is hung.
261
- const url = downloadUrl(asset);
262
- process.stderr.write(`${DIM}[0sec] first-run setup — downloading ${asset} (${url})${RESET}\n`);
263
-
264
- const cacheDir = dirname(cached);
265
- mkdirSync(cacheDir, { recursive: true });
266
- const tmpFile = join(tmpdir(), `0sec-${process.pid}-${Date.now()}-${asset}`);
267
-
268
- try {
269
- const timeoutMs = Number(process.env["0SEC_DOWNLOAD_TIMEOUT_MS"] ?? 120000);
270
-
271
- // 1) Fetch the SHA-256 manifest BEFORE the binary. If the release
272
- // is missing its manifest (e.g. an old pre-0.11.x tag) we abort
273
- // here rather than ever writing an unverified binary to disk.
274
- const checksumsTxt = await fetchTextBody(checksumsUrl(), timeoutMs).catch((e) => {
275
- throw new Error(
276
- `could not fetch ${CHECKSUMS_FILENAME} from the release: ${e.message}. ` +
277
- `Refusing to install an unverified binary.`,
278
- );
279
- });
280
-
281
- // 2) Stream the binary into a tmp file.
282
- const body = await fetchFollowRedirects(url, timeoutMs);
283
- await new Promise((resolve, reject) => {
284
- // Note: we set mode 0o600 here. The binary stays non-executable
285
- // on disk until verifyChecksum succeeds — defence in depth in
286
- // case some other process scans tmpdir.
287
- const out = createWriteStream(tmpFile, { mode: 0o600 });
288
- body.pipe(out);
289
- body.on("error", reject);
290
- out.on("error", reject);
291
- out.on("finish", resolve);
292
- });
293
-
294
- // Sanity-check size; an empty file means we got something pathological.
295
- const size = statSync(tmpFile).size;
296
- if (size < 1024 * 1024) {
297
- try { unlinkSync(tmpFile); } catch { /* ignore */ }
298
- throw new Error(`downloaded file is only ${size} bytes — refusing to cache`);
299
- }
300
-
301
- // 3) Verify SHA-256 against the manifest BEFORE chmod +x. A
302
- // mismatch never gets the exec bit and never reaches the cache.
303
- await verifyChecksum({
304
- filePath: tmpFile,
305
- expectedAsset: asset,
306
- manifestText: checksumsTxt,
307
- });
308
-
309
- // 4) Only now mark executable + publish into the cache atomically.
310
- chmodSync(tmpFile, 0o755);
311
- renameSync(tmpFile, cached);
312
- process.stderr.write(`${DIM}[0sec] cached at ${cached} (sha-256 verified)${RESET}\n`);
313
- return cached;
314
- } catch (e) {
315
- try { unlinkSync(tmpFile); } catch { /* ignore */ }
316
- err(`download failed: ${e.message}`);
317
- err("");
318
- printInstallFallback(asset);
319
- process.exit(1);
320
- }
321
- }
322
-
323
- // Only run the launcher when invoked directly. When required from a
324
- // test file we expose the pure helpers via module.exports so the
325
- // verification logic can be unit-tested without spawning a binary.
326
- if (require.main === module) {
327
- (async () => {
328
- const binary = await ensureBinary();
329
- // Re-exec the user's args against the standalone binary. stdio:'inherit'
330
- // wires the parent's terminal through so OpenTUI works as if invoked
331
- // directly. The launcher exits with the binary's own exit code.
332
- const child = spawn(binary, process.argv.slice(2), {
333
- stdio: "inherit",
334
- env: process.env,
335
- });
336
- child.on("error", (e) => {
337
- err(`failed to spawn ${binary}: ${e.message}`);
338
- process.exit(1);
339
- });
340
- child.on("exit", (code, signal) => {
341
- if (signal) {
342
- // Mirror the signal so shells see the right exit reason.
343
- process.kill(process.pid, signal);
344
- return;
345
- }
346
- process.exit(code ?? 0);
347
- });
348
- })().catch((e) => {
349
- err(`launcher error: ${e instanceof Error ? e.message : String(e)}`);
350
- process.exit(1);
351
- });
352
- }
353
-
354
- module.exports = {
355
- parseChecksumsManifest,
356
- sha256File,
357
- verifyChecksum,
358
- detectAsset,
359
- downloadUrl,
360
- checksumsUrl,
361
- };