@coworker-jp/aidr 0.1.292 → 0.1.293

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coworker-jp/aidr",
3
- "version": "0.1.292",
3
+ "version": "0.1.293",
4
4
  "description": "AIDR setup CLI - installs ai-scanner hooks for 19+ AI coding agents",
5
5
  "type": "module",
6
6
  "bin": {
@@ -13,7 +13,11 @@ const SCRIPT_FILES = [
13
13
  "scan_webfetch.sh", "scan_websearch.sh",
14
14
  "log_web_urls.sh",
15
15
  ];
16
- const BIN_FILES = ["ai-scanner", "opengrep"];
16
+ // `.ai-scanner-unscanned` は hook が「無検査でツールコールが通った」ことを
17
+ // 記録するファイル (#1774)。**消す対象に入れておくこと** —— 残ると bin
18
+ // ディレクトリが空にならず `rmdirIfEmpty` が効かないので、アンインストール後に
19
+ // ディレクトリと記録が居座る。7 エージェントすべて同じ表を持つ。
20
+ const BIN_FILES = ["ai-scanner", "opengrep", ".ai-scanner-unscanned"];
17
21
 
18
22
  export const meta = {
19
23
  name: "claude",
@@ -27,7 +27,11 @@ const SCRIPT_FILES = [
27
27
  "pre_shell.sh",
28
28
  "post_shell.sh",
29
29
  ];
30
- const BIN_FILES = ["ai-scanner", "opengrep"];
30
+ // `.ai-scanner-unscanned` は hook が「無検査でツールコールが通った」ことを
31
+ // 記録するファイル (#1774)。**消す対象に入れておくこと** —— 残ると bin
32
+ // ディレクトリが空にならず `rmdirIfEmpty` が効かないので、アンインストール後に
33
+ // ディレクトリと記録が居座る。7 エージェントすべて同じ表を持つ。
34
+ const BIN_FILES = ["ai-scanner", "opengrep", ".ai-scanner-unscanned"];
31
35
  const ROOT_FILES = ["AGENTS.md"];
32
36
 
33
37
  export const meta = {
@@ -28,7 +28,11 @@ const SCRIPT_FILES = [
28
28
  "post_read.sh",
29
29
  "post_write.sh",
30
30
  ];
31
- const BIN_FILES = ["ai-scanner", "opengrep"];
31
+ // `.ai-scanner-unscanned` は hook が「無検査でツールコールが通った」ことを
32
+ // 記録するファイル (#1774)。**消す対象に入れておくこと** —— 残ると bin
33
+ // ディレクトリが空にならず `rmdirIfEmpty` が効かないので、アンインストール後に
34
+ // ディレクトリと記録が居座る。7 エージェントすべて同じ表を持つ。
35
+ const BIN_FILES = ["ai-scanner", "opengrep", ".ai-scanner-unscanned"];
32
36
 
33
37
  export const meta = {
34
38
  name: "cursor",
@@ -5,7 +5,11 @@ import * as T from "../templates.mjs";
5
5
  import { buildGeminiSettings, mergeGeminiSettings, unmergeGeminiSettings } from "../merge.mjs";
6
6
 
7
7
  const HOOK_FILES = ["aidr-pre.sh"];
8
- const BIN_FILES = ["ai-scanner", "opengrep"];
8
+ // `.ai-scanner-unscanned` は hook が「無検査でツールコールが通った」ことを
9
+ // 記録するファイル (#1774)。**消す対象に入れておくこと** —— 残ると bin
10
+ // ディレクトリが空にならず `rmdirIfEmpty` が効かないので、アンインストール後に
11
+ // ディレクトリと記録が居座る。7 エージェントすべて同じ表を持つ。
12
+ const BIN_FILES = ["ai-scanner", "opengrep", ".ai-scanner-unscanned"];
9
13
 
10
14
  export const meta = {
11
15
  name: "gemini",
@@ -18,7 +18,11 @@ const SCRIPT_FILES = [
18
18
  "post_read.sh",
19
19
  "post_write.sh",
20
20
  ];
21
- const BIN_FILES = ["ai-scanner", "opengrep"];
21
+ // `.ai-scanner-unscanned` は hook が「無検査でツールコールが通った」ことを
22
+ // 記録するファイル (#1774)。**消す対象に入れておくこと** —— 残ると bin
23
+ // ディレクトリが空にならず `rmdirIfEmpty` が効かないので、アンインストール後に
24
+ // ディレクトリと記録が居座る。7 エージェントすべて同じ表を持つ。
25
+ const BIN_FILES = ["ai-scanner", "opengrep", ".ai-scanner-unscanned"];
22
26
 
23
27
  export const meta = {
24
28
  name: "kiro",
@@ -2,7 +2,11 @@ import path from "node:path";
2
2
  import fsp from "node:fs/promises";
3
3
  import { pathExists, rmdirIfEmpty } from "../fs-utils.mjs";
4
4
 
5
- const BIN_FILES = ["ai-scanner", "opengrep"];
5
+ // `.ai-scanner-unscanned` は hook が「無検査でツールコールが通った」ことを
6
+ // 記録するファイル (#1774)。**消す対象に入れておくこと** —— 残ると bin
7
+ // ディレクトリが空にならず `rmdirIfEmpty` が効かないので、アンインストール後に
8
+ // ディレクトリと記録が居座る。7 エージェントすべて同じ表を持つ。
9
+ const BIN_FILES = ["ai-scanner", "opengrep", ".ai-scanner-unscanned"];
6
10
 
7
11
  export const meta = {
8
12
  name: "standalone",
@@ -25,7 +25,11 @@ const SCRIPT_FILES = [
25
25
  "post_read.sh",
26
26
  "post_write.sh",
27
27
  ];
28
- const BIN_FILES = ["ai-scanner", "opengrep"];
28
+ // `.ai-scanner-unscanned` は hook が「無検査でツールコールが通った」ことを
29
+ // 記録するファイル (#1774)。**消す対象に入れておくこと** —— 残ると bin
30
+ // ディレクトリが空にならず `rmdirIfEmpty` が効かないので、アンインストール後に
31
+ // ディレクトリと記録が居座る。7 エージェントすべて同じ表を持つ。
32
+ const BIN_FILES = ["ai-scanner", "opengrep", ".ai-scanner-unscanned"];
29
33
 
30
34
  export const meta = {
31
35
  name: "windsurf",
@@ -65,11 +65,117 @@ async function fetchBuffer(url, accessKey) {
65
65
  return Buffer.from(ab);
66
66
  }
67
67
 
68
- async function fetchText(url, accessKey) {
68
+ /**
69
+ * Refused to install because the download could not be checked against a
70
+ * publisher checksum. `status` is set ONLY when the endpoint gave a definite
71
+ * "not there" for the sidecar, so `fetchBinary` can treat an incompletely
72
+ * published pinned release exactly like a missing pinned payload (fall back to
73
+ * the published key, which brings its own checksum) instead of turning a half
74
+ * release into an unverified install.
75
+ */
76
+ export class IntegrityUnverifiableError extends Error {
77
+ constructor(message, { status } = {}) {
78
+ super(message);
79
+ this.name = "IntegrityUnverifiableError";
80
+ if (status !== undefined) this.status = status;
81
+ }
82
+ }
83
+
84
+ /** A publisher checksum is 64 lowercase-or-uppercase hex characters. */
85
+ const SHA256_HEX_RE = /^[0-9a-fA-F]{64}$/;
86
+
87
+ /** Statuses that mean "this object is not there", as opposed to "ask again". */
88
+ const DEFINITELY_ABSENT = new Set([404, 410]);
89
+
90
+ /**
91
+ * Statuses that will not change on a retry but are NOT evidence of absence: the
92
+ * caller was refused. Kept apart from `DEFINITELY_ABSENT` so a rejected access
93
+ * key never makes `fetchBinary` fall back to another key (same reason as the
94
+ * payload's own 403 rule below) and never reads as "the release is incomplete".
95
+ */
96
+ const REFUSED = new Set([401, 403]);
97
+
98
+ /** How many times a transient sidecar failure is re-asked before giving up. */
99
+ export const CHECKSUM_ATTEMPTS = 3;
100
+ const CHECKSUM_RETRY_MS = 400;
101
+
102
+ /**
103
+ * Fetch the sibling `.sha256` sidecar and classify the outcome. The three
104
+ * outcomes are kept apart on purpose — the previous implementation collapsed
105
+ * every one of them into `null` (`if (!res.ok) return null` plus a
106
+ * `.catch(() => null)`), which is how "the object is gone" became
107
+ * indistinguishable from "the network blinked", and both became "install it
108
+ * anyway":
109
+ *
110
+ * { kind: "hash", hex } 200 carrying a usable digest
111
+ * { kind: "absent", status } the endpoint answered that it is not there
112
+ * { kind: "unusable", detail } reachable but no digest came back, or the
113
+ * service/network could not answer at all
114
+ *
115
+ * A 200 whose body is empty / whitespace / not 64 hex characters is `unusable`,
116
+ * NOT `absent`: an empty sidecar object and an error page both used to pass the
117
+ * old `if (expected)` test as falsy-or-garbage and skip verification.
118
+ */
119
+ async function fetchChecksum(url, accessKey, { attempts = CHECKSUM_ATTEMPTS, sleep = defaultSleep } = {}) {
69
120
  const headers = accessKey ? { "x-access-key": accessKey } : {};
70
- const res = await fetch(url, { redirect: "follow", headers });
71
- if (!res.ok) return null;
72
- return (await res.text()).trim();
121
+ let last = null;
122
+ for (let attempt = 1; attempt <= attempts; attempt++) {
123
+ let res;
124
+ try {
125
+ res = await fetch(url, { redirect: "follow", headers });
126
+ } catch (e) {
127
+ last = { kind: "unusable", detail: `${url} could not be reached (${e.message})` };
128
+ if (attempt < attempts) { await sleep(CHECKSUM_RETRY_MS * attempt); continue; }
129
+ return last;
130
+ }
131
+ if (DEFINITELY_ABSENT.has(res.status)) {
132
+ // Not a transient condition: asking again cannot make it appear.
133
+ return { kind: "absent", status: res.status, detail: `${url} -> HTTP ${res.status}` };
134
+ }
135
+ if (REFUSED.has(res.status)) {
136
+ return {
137
+ kind: "unusable",
138
+ detail: `${url} -> HTTP ${res.status} (the access key was refused for the checksum)`,
139
+ };
140
+ }
141
+ if (!res.ok) {
142
+ last = { kind: "unusable", detail: `${url} -> HTTP ${res.status}` };
143
+ if (attempt < attempts) { await sleep(CHECKSUM_RETRY_MS * attempt); continue; }
144
+ return last;
145
+ }
146
+ const body = (await res.text().catch(() => "")).trim();
147
+ const token = body.split(/\s+/)[0] || "";
148
+ if (SHA256_HEX_RE.test(token)) return { kind: "hash", hex: token.toLowerCase() };
149
+ // Reachable, answered 200, and still no digest. Re-asking a served object
150
+ // will not change its bytes, so this is decided on the first look.
151
+ return {
152
+ kind: "unusable",
153
+ detail: body
154
+ ? `${url} did not return a SHA256 digest (got ${JSON.stringify(token.slice(0, 32))})`
155
+ : `${url} returned an empty checksum`,
156
+ };
157
+ }
158
+ return last;
159
+ }
160
+
161
+ function defaultSleep(ms) {
162
+ return new Promise((resolve) => setTimeout(resolve, ms));
163
+ }
164
+
165
+ /**
166
+ * Throw unless this asset was checked against a publisher checksum. Callers use
167
+ * this so `verified` is a CONDITION, not a caption: it used to be read only to
168
+ * decide whether to append ", sha256 verified" to a log line, which meant a code
169
+ * path returning `verified: false` would install the binary and merely print
170
+ * one word less. Every consumer of `fetchAsset` / `fetchBinary` must call this
171
+ * before treating the file as installed.
172
+ */
173
+ export function assertVerified(result, label = "the downloaded file") {
174
+ if (result?.verified === true) return result;
175
+ throw new IntegrityUnverifiableError(
176
+ `${label} was not checked against a publisher SHA256 checksum, so it was not installed. ` +
177
+ `Re-run the installer; if it keeps happening, contact your IT administrator.`,
178
+ );
73
179
  }
74
180
 
75
181
  async function clearQuarantine(destPath) {
@@ -82,28 +188,53 @@ async function clearQuarantine(destPath) {
82
188
  }
83
189
 
84
190
  /**
85
- * Download an asset from `binUrl` into `destPath`, validating SHA256 when a
86
- * sibling `.sha256` file is available. Sets executable mode and strips macOS
87
- * quarantine. Returns { path, sha256, verified }.
191
+ * Download an asset from `binUrl` into `destPath`, validating it against the
192
+ * sibling `.sha256` published beside it. Sets executable mode and strips macOS
193
+ * quarantine. Returns { path, sha256, verified } with `verified` always true —
194
+ * the alternative is a throw, never a returned `verified: false` (#1773).
195
+ * `checksumOpts` is a test seam ({ attempts, sleep }) for the retry loop.
88
196
  */
89
- export async function fetchAsset(binUrl, destPath, { accessKey } = {}) {
197
+ export async function fetchAsset(binUrl, destPath, { accessKey, checksumOpts } = {}) {
90
198
  const shaUrl = `${binUrl}.sha256`;
91
199
  await fsp.mkdir(path.dirname(destPath), { recursive: true });
92
200
 
93
201
  const buf = await fetchBuffer(binUrl, accessKey);
94
202
  const sha256 = crypto.createHash("sha256").update(buf).digest("hex");
95
203
 
96
- let verified = false;
97
- const expected = await fetchText(shaUrl, accessKey).catch(() => null);
98
- if (expected) {
99
- const want = expected.split(/\s+/)[0].toLowerCase();
100
- if (want !== sha256) {
101
- throw new Error(`SHA256 mismatch for ${binUrl}: got ${sha256}, expected ${want}`);
102
- }
103
- verified = true;
104
- } else {
105
- console.warn(`warning: ${shaUrl} unavailable; skipping SHA256 verification`);
204
+ // FAIL-CLOSED (#1773). No checksum ⇒ nothing is written to `destPath`.
205
+ //
206
+ // This is the same rule the scanner's own self-updater has followed since
207
+ // #319 ("a missing sha256 means DO NOT install", docker/scanner/src/main.rs
208
+ // `check_download_hash`) and the same rule `fetchOpengrep` below follows
209
+ // against its pinned hash. Only the FIRST install — this function — used to
210
+ // be exempt, which is exactly backwards: at first install there is no
211
+ // previously-verified binary in place to fall back on.
212
+ //
213
+ // Suppressing one small object must not be able to downgrade the check on a
214
+ // security product's executable, and it is far easier to delete a hash than
215
+ // to forge one. So the outcomes are: verified install, or no install plus a
216
+ // loud, actionable status — never a quiet unverified install.
217
+ const checksum = await fetchChecksum(shaUrl, accessKey, checksumOpts);
218
+ if (checksum.kind === "absent") {
219
+ throw new IntegrityUnverifiableError(
220
+ `no published SHA256 checksum for ${binUrl} (${checksum.detail}), so the download was ` +
221
+ `discarded without being installed. Re-run the installer in a few minutes; if it keeps ` +
222
+ `happening, contact your IT administrator — the release is incomplete.`,
223
+ { status: checksum.status },
224
+ );
225
+ }
226
+ if (checksum.kind !== "hash") {
227
+ throw new IntegrityUnverifiableError(
228
+ `could not read the SHA256 checksum for ${binUrl} after ${CHECKSUM_ATTEMPTS} attempts ` +
229
+ `(${checksum.detail}), so the download was discarded without being installed. ` +
230
+ `Check the network connection and re-run the installer; if it keeps happening, ` +
231
+ `contact your IT administrator.`,
232
+ );
233
+ }
234
+ if (checksum.hex !== sha256) {
235
+ throw new Error(`SHA256 mismatch for ${binUrl}: got ${sha256}, expected ${checksum.hex}`);
106
236
  }
237
+ const verified = true;
107
238
 
108
239
  const tmp = `${destPath}.tmp-${process.pid}`;
109
240
  await fsp.writeFile(tmp, buf);
@@ -159,6 +290,11 @@ export async function fetchBinary(destPath, { env = "prod", accessKey, version,
159
290
  const result = await fetchAsset(`${base}/v${version}/${platform}`, destPath, { accessKey });
160
291
  return { ...result, platform, requestedVersion: version, servedUnpinned: false };
161
292
  } catch (e) {
293
+ // A pinned release whose CHECKSUM is a definite 404 is incomplete in exactly
294
+ // the same way as one whose binary is (#1773): half a release is not a
295
+ // release. It falls back here, and the published key it falls back to
296
+ // carries its own checksum — the fallback can never widen into an
297
+ // unverified install, because `fetchAsset` refuses that on both keys.
162
298
  if (e?.status !== 404) throw e;
163
299
  const result = await fetchAsset(unpinnedUrl, destPath, { accessKey });
164
300
  return { ...result, platform, requestedVersion: version, servedUnpinned: true };
package/src/cli.mjs CHANGED
@@ -10,7 +10,7 @@ import { Command } from "commander";
10
10
  const execFileP = promisify(execFile);
11
11
  import { AGENT_ORDER, AGENT_REGISTRY, loadAgent } from "./agents/index.mjs";
12
12
  import { verifyKey, maskKey } from "./verify.mjs";
13
- import { fetchBinary, fetchOpengrep } from "./binary-fetcher.mjs";
13
+ import { fetchBinary, fetchOpengrep, assertVerified } from "./binary-fetcher.mjs";
14
14
  import { detectInstalled } from "./detect.mjs";
15
15
  import { evaluateInstallGate, formatLocalPcGuidance } from "./env-gate.mjs";
16
16
  import { resolveCallerHome, isRunningUnderSudo, chownToCaller } from "./sudo-user.mjs";
@@ -200,20 +200,30 @@ async function cmdInstall(opts) {
200
200
  // dir so ai-scanner's sibling-lookup finds opengrep without relying on
201
201
  // PATH. Users can point AI_SCANNER_OPENGREP elsewhere to substitute
202
202
  // their own LGPL-2.1 build.
203
- const scannerRes = await fetchBinary(path.join(primaryDir, "ai-scanner"), fetchOpts);
203
+ const scannerRes = assertVerified(
204
+ await fetchBinary(path.join(primaryDir, "ai-scanner"), fetchOpts),
205
+ "the ai-scanner binary",
206
+ );
204
207
  binaryOutcome.scannerInstalled = true;
205
- console.log(`binary: ${scannerRes.path} (${scannerRes.platform}${scannerRes.verified ? ", sha256 verified" : ""})`);
208
+ // Stated unconditionally, because it is now a condition of getting here
209
+ // rather than a caption on a file that may or may not have been checked
210
+ // (#1773): `fetchAsset` throws unless the bytes matched the published
211
+ // SHA256, and `assertVerified` is the second lock on that.
212
+ console.log(`binary: ${scannerRes.path} (${scannerRes.platform}, sha256 verified)`);
206
213
  if (scannerRes.servedUnpinned) {
207
214
  // Say what was asked for, what arrived, and how to check — the pinned
208
215
  // build genuinely has no bytes behind it, but protection IS in place,
209
216
  // so this is a note, not an alarm.
210
- console.error(`note: release v${scannerRes.requestedVersion} of the scanner is not published for ${scannerRes.platform} yet, so it could not be the one installed.`);
217
+ console.error(`note: release v${scannerRes.requestedVersion} of the scanner is not fully published for ${scannerRes.platform} yet (its binary or its checksum is not there), so it could not be the one installed.`);
211
218
  console.error(`note: the currently published build was installed instead, so scanning is active. Check which build you have with: ${scannerRes.path} version`);
212
219
  }
213
220
  // Opengrep is fetched unmodified from its public upstream GitHub
214
221
  // release (LGPL-2.1); no access-key gating is needed here.
215
- const opengrepRes = await fetchOpengrep(path.join(primaryDir, "opengrep"));
216
- console.log(`binary: ${opengrepRes.path} (opengrep ${opengrepRes.platform}${opengrepRes.verified ? ", sha256 verified" : ""})`);
222
+ const opengrepRes = assertVerified(
223
+ await fetchOpengrep(path.join(primaryDir, "opengrep")),
224
+ "the opengrep engine",
225
+ );
226
+ console.log(`binary: ${opengrepRes.path} (opengrep ${opengrepRes.platform}, sha256 verified)`);
217
227
 
218
228
  const copyOps = restDirs.flatMap(restDir =>
219
229
  ["ai-scanner", "opengrep"].map(async (file) => {
@@ -570,13 +580,55 @@ async function cmdDoctor() {
570
580
  await fs.access(bin, fs.constants.F_OK);
571
581
  present = true;
572
582
  } catch {}
573
- return `binary: ${bin} ${present ? "[present]" : "[missing]"}`;
583
+ const out = [`binary: ${bin} ${present ? "[present]" : "[missing]"}`];
584
+ // What the hooks recorded while there was no binary (#1774). Without this,
585
+ // "[missing]" was the only trace, and it said nothing about how long the
586
+ // machine had been unscanned or how much had gone through unscanned —
587
+ // start_scanner.sh removes the record as soon as a binary is in place, so
588
+ // its presence describes the CURRENT state, not history.
589
+ const unscanned = await readUnscannedRecord(path.dirname(bin));
590
+ if (unscanned) {
591
+ out.push(
592
+ ` ${unscanned.count} tool call(s) ran with no scanner in place, ` +
593
+ `from ${unscanned.first.toISOString()} to ${unscanned.last.toISOString()}.`,
594
+ );
595
+ out.push(present
596
+ ? " A binary is in place now, and this record has not been cleared yet: " +
597
+ "it is cleared at the next session start. Whether anything was missed in that " +
598
+ "window is not knowable from here — nothing scanned those calls."
599
+ // INSTALL_COMMAND_HINT, not a literal: the DEV package must never tell
600
+ // anyone to install PROD.
601
+ : ` Those calls were not scanned. Re-run the installer (${INSTALL_COMMAND_HINT}), ` +
602
+ "or contact your IT administrator.");
603
+ }
604
+ return out.join("\n");
574
605
  }));
575
606
  for (const line of lines) {
576
607
  if (line) console.log(line);
577
608
  }
578
609
  }
579
610
 
611
+ /**
612
+ * Read the `.ai-scanner-unscanned` record the scan hooks write next to the
613
+ * binary when they exit without one (templates.mjs `note_unscanned`). Three
614
+ * integers on one line: first seen, last seen, count. A malformed or absent
615
+ * file yields null — this is a diagnostic read, so it must never throw and must
616
+ * never invent a number (a count of 0 would read as "nothing was missed").
617
+ */
618
+ export async function readUnscannedRecord(binDir) {
619
+ let raw;
620
+ try {
621
+ raw = await fs.readFile(path.join(binDir, ".ai-scanner-unscanned"), "utf8");
622
+ } catch {
623
+ return null;
624
+ }
625
+ const parts = raw.split(/\r?\n/)[0].trim().split(/\s+/);
626
+ if (parts.length < 3) return null;
627
+ const [first, last, count] = parts.map((n) => (/^\d+$/.test(n) ? Number(n) : null));
628
+ if (first === null || last === null || count === null || count === 0) return null;
629
+ return { first: new Date(first * 1000), last: new Date(last * 1000), count };
630
+ }
631
+
580
632
  async function cmdListAgents() {
581
633
  for (const name of AGENT_ORDER) {
582
634
  const mod = await loadAgent(name);
package/src/red-team.mjs CHANGED
@@ -16,7 +16,7 @@ import path from "node:path";
16
16
  import { execFile } from "node:child_process";
17
17
  import { promisify } from "node:util";
18
18
 
19
- import { fetchAsset, detectPlatform } from "./binary-fetcher.mjs";
19
+ import { fetchAsset, detectPlatform, assertVerified } from "./binary-fetcher.mjs";
20
20
  import { downloadBase } from "./verify.mjs";
21
21
 
22
22
  const execFileP = promisify(execFile);
@@ -97,7 +97,13 @@ export async function fetchRedTeamBinary(
97
97
  const platform = detectPlatform();
98
98
  const rel = product === DEFAULT_RED_TEAM_PRODUCT ? platform : `${product}/${platform}`;
99
99
  const binUrl = `${downloadBase(env)}/red-team/${rel}`;
100
- const result = await fetchAsset(binUrl, destPath, { accessKey });
100
+ // 検証できなかったものは `fetchAsset` throw するので、ここに来た時点で
101
+ // 公開 SHA256 と一致している。`assertVerified` はその 2 つ目の錠で、
102
+ // 「verified を表示にしか使っていない」状態 (#1773) に戻ることを防ぐ。
103
+ const result = assertVerified(
104
+ await fetchAsset(binUrl, destPath, { accessKey }),
105
+ `the ${product} binary`,
106
+ );
101
107
  return { ...result, platform, product };
102
108
  }
103
109
 
@@ -281,7 +287,7 @@ export async function installRedTeam({
281
287
  await fs.mkdir(binDir, { recursive: true });
282
288
  const res = await fetchRedTeamBinary(binPath, { env, accessKey, product });
283
289
  stdout(
284
- `binary: ${res.path} (${res.platform}${res.verified ? ", sha256 verified" : ""})`,
290
+ `binary: ${res.path} (${res.platform}, sha256 verified)`,
285
291
  );
286
292
 
287
293
  const args = buildMcpAddArgs(binPath, accessKey, scope, meta.mcpServerName);
package/src/templates.mjs CHANGED
@@ -4,6 +4,7 @@
4
4
  // only the access key and the verify endpoint differ.
5
5
 
6
6
  import { buildClaudeSettings, buildGeminiSettings } from "./merge.mjs";
7
+ import { isDevDownloadBase } from "./verify.mjs";
7
8
 
8
9
  export function getSettingsJson(accessKey, scope = "project") {
9
10
  return JSON.stringify(buildClaudeSettings(accessKey, scope), null, 2) + "\n";
@@ -15,6 +16,14 @@ export function getSettingsJson(accessKey, scope = "project") {
15
16
  // is kept for signature stability with callers and the wider TS template.
16
17
  // eslint-disable-next-line no-unused-vars
17
18
  export function getStartScannerSh(downloadBase = "https://pytd2jd5p7.execute-api.ap-northeast-1.amazonaws.com/download", agentType = "claude-code") {
19
+ // Which installer to quote back at the user, derived from the endpoint this
20
+ // script was generated against. The DEV package must never tell anyone to
21
+ // install PROD (same rule as cli.mjs's INSTALL_COMMAND_HINT) — and here the
22
+ // package name is not in scope, so the endpoint is the only evidence
23
+ // available. Anything that is not the DEV endpoint is treated as PROD.
24
+ const installCommand = isDevDownloadBase(downloadBase)
25
+ ? "npx @coworker-jp/aidr-dev install"
26
+ : "npx @coworker-jp/aidr install";
18
27
  return `#!/bin/bash
19
28
  # SessionStart hook: Setup ai-scanner for the current platform
20
29
  # - Linux (x86_64 / aarch64): download native binary from S3
@@ -46,9 +55,41 @@ should_check_update() {
46
55
  [ $(($(date +%s) - last)) -ge "$PULL_INTERVAL" ]
47
56
  }
48
57
 
58
+ # True for exactly 64 hex characters — the shape of a SHA-256 digest.
59
+ is_sha256() {
60
+ [ "\${#1}" -eq 64 ] || return 1
61
+ case "$1" in *[!0-9a-fA-F]*) return 1 ;; esac
62
+ return 0
63
+ }
64
+
65
+ # Lower-case SHA-256 of $1, using whichever tool this machine has. Returns
66
+ # non-zero when it has none — which is refused below, not waved through.
67
+ sha256_of() {
68
+ local out
69
+ if command -v sha256sum >/dev/null 2>&1; then
70
+ out=$(sha256sum "$1" 2>/dev/null) || return 1
71
+ elif command -v shasum >/dev/null 2>&1; then
72
+ out=$(shasum -a 256 "$1" 2>/dev/null) || return 1
73
+ elif command -v openssl >/dev/null 2>&1; then
74
+ out=$(openssl dgst -sha256 "$1" 2>/dev/null | awk '{print $NF}') || return 1
75
+ else
76
+ return 1
77
+ fi
78
+ printf '%s' "\${out%% *}" | tr 'A-F' 'a-f'
79
+ }
80
+
49
81
  # Download binary from $1 to $BIN, strip quarantine (macOS), chmod +x.
50
82
  # Uses curl's native --max-time so no external \`timeout\` dependency.
51
83
  # Access key is required by the /download endpoint.
84
+ #
85
+ # FAIL-CLOSED on the checksum (#1786). This path used to install whatever came
86
+ # back, unchecked. Every other place the product obtains this binary verifies it
87
+ # — the scanner's own self-update (#319), the sentinel daemon's self-provision,
88
+ # and \`aidr install\` (#1773) — and this one is the one that runs on a machine
89
+ # whose binary already went missing once. Refusing leaves the machine in the
90
+ # same state a failed download leaves it in (no binary, and the caller says so
91
+ # out loud), which is strictly better than running bytes nobody checked.
92
+ # \`$UNVERIFIED_REASON\` carries WHY, so the message can name it.
52
93
  download_binary() {
53
94
  local url="$1"
54
95
  local tmp=$(mktemp "\${BIN_DIR}/ai-scanner.XXXXXX")
@@ -56,16 +97,67 @@ download_binary() {
56
97
  if [ -n "\${AI_SCANNER_ACCESS_KEY:-}" ]; then
57
98
  auth_args=(-H "x-access-key: \${AI_SCANNER_ACCESS_KEY}")
58
99
  fi
59
- if curl -sfL --max-time 60 "\${auth_args[@]}" -o "$tmp" "$url" 2>/dev/null; then
60
- chmod +x "$tmp"
61
- # macOS: strip Gatekeeper quarantine so unsigned binary can execute
62
- # (no-op on Linux where xattr isn't typically present)
63
- xattr -d com.apple.quarantine "$tmp" 2>/dev/null || true
64
- mv -f "$tmp" "$BIN"
65
- return 0
100
+ if ! curl -sfL --max-time 60 "\${auth_args[@]}" -o "$tmp" "$url" 2>/dev/null; then
101
+ rm -f "$tmp"
102
+ UNVERIFIED_REASON="the download from $url did not complete"
103
+ return 1
66
104
  fi
67
- rm -f "$tmp"
68
- return 1
105
+
106
+ local want got
107
+ want=$(curl -sfL --retry 2 --max-time 30 "\${auth_args[@]}" "\${url}.sha256" 2>/dev/null \
108
+ | tr -d '\\r' | awk 'NR==1{print $1}')
109
+ if ! is_sha256 "$want"; then
110
+ rm -f "$tmp"
111
+ UNVERIFIED_REASON="no published checksum was readable at \${url}.sha256, so the download could not be checked"
112
+ return 1
113
+ fi
114
+ if ! got=$(sha256_of "$tmp"); then
115
+ rm -f "$tmp"
116
+ UNVERIFIED_REASON="this machine has none of sha256sum, shasum or openssl, so the download could not be checked"
117
+ return 1
118
+ fi
119
+ if [ "$got" != "$(printf '%s' "$want" | tr 'A-F' 'a-f')" ]; then
120
+ rm -f "$tmp"
121
+ UNVERIFIED_REASON="the download did not match its published checksum (expected $want, got $got)"
122
+ return 1
123
+ fi
124
+
125
+ chmod +x "$tmp"
126
+ # macOS: strip Gatekeeper quarantine so unsigned binary can execute
127
+ # (no-op on Linux where xattr isn't typically present)
128
+ xattr -d com.apple.quarantine "$tmp" 2>/dev/null || true
129
+ mv -f "$tmp" "$BIN"
130
+ return 0
131
+ }
132
+
133
+ # What has already run unscanned on this machine, if anything (#1774). The scan
134
+ # hooks append to this file every time they exit without a binary; this is the
135
+ # first moment anyone can say it out loud, and it is deleted the moment a binary
136
+ # is in place so a healed machine stops claiming to be blind.
137
+ UNSCANNED_STAMP="\${BIN_DIR}/.ai-scanner-unscanned"
138
+
139
+ report_unscanned_history() {
140
+ local line first count
141
+ [ -r "$UNSCANNED_STAMP" ] || return 0
142
+ read -r line < "$UNSCANNED_STAMP" 2>/dev/null || return 0
143
+ set -- $line
144
+ first="\${1:-}"; count="\${3:-}"
145
+ case "$count" in ''|*[!0-9]*) return 0 ;; esac
146
+ # \`date -d @<epoch>\` is GNU, \`date -r <epoch>\` is BSD/macOS, and the epoch
147
+ # itself is the fallback — printing nothing here would drop the fact.
148
+ local when
149
+ when=$(date -d "@$first" 2>/dev/null || date -r "$first" 2>/dev/null || echo "epoch $first")
150
+ echo "ai-scanner: $count tool call(s) have already run unscanned on this machine (first at $when)." >&2
151
+ }
152
+
153
+ # There is no build for this platform, so this machine will not be scanned.
154
+ # Said with the next action attached, because a lone "Warning: Unsupported …"
155
+ # reads as cosmetic (docs/product/UI_COPY.md 【非交渉】) — and unlike a failed
156
+ # download, re-running the installer cannot fix it, so the action is different.
157
+ unsupported_platform() {
158
+ echo "ai-scanner: this session is NOT being scanned — there is no scanner build for this platform ($1)." >&2
159
+ report_unscanned_history
160
+ echo "ai-scanner: tool calls keep working, and they run unscanned on this machine. Contact your IT administrator with this message." >&2
69
161
  }
70
162
 
71
163
  OS=$(uname -s)
@@ -77,7 +169,7 @@ case "$OS" in
77
169
  x86_64) PLATFORM_SLUG="linux-x86_64" ;;
78
170
  aarch64|arm64) PLATFORM_SLUG="linux-aarch64" ;;
79
171
  *)
80
- echo "Warning: Unsupported Linux architecture: $ARCH (expected x86_64 or aarch64)" >&2
172
+ unsupported_platform "Linux/$ARCH"
81
173
  exit 0
82
174
  ;;
83
175
  esac
@@ -91,13 +183,13 @@ case "$OS" in
91
183
  # — only its contents became universal, never its name.
92
184
  arm64|x86_64) PLATFORM_SLUG="darwin-aarch64" ;;
93
185
  *)
94
- echo "Warning: Unsupported macOS architecture: $ARCH" >&2
186
+ unsupported_platform "macOS/$ARCH"
95
187
  exit 0
96
188
  ;;
97
189
  esac
98
190
  ;;
99
191
  *)
100
- echo "Warning: Unsupported OS for ai-scanner: $OS" >&2
192
+ unsupported_platform "$OS"
101
193
  exit 0
102
194
  ;;
103
195
  esac
@@ -105,13 +197,24 @@ esac
105
197
  # Binary missing → fetch it
106
198
  if [ ! -x "$BIN" ]; then
107
199
  if download_binary "\${DOWNLOAD_BASE}/\${PLATFORM_SLUG}"; then
108
- echo "ai-scanner binary installed (\${PLATFORM_SLUG})" >&2
200
+ echo "ai-scanner binary installed (\${PLATFORM_SLUG}, checksum verified)" >&2
201
+ rm -f "$UNSCANNED_STAMP"
109
202
  else
110
- echo "Warning: Failed to download ai-scanner binary from \${DOWNLOAD_BASE}/\${PLATFORM_SLUG}" >&2
203
+ # docs/product/UI_COPY.md 【非交渉】: the fact, stated so it cannot be
204
+ # read as harmless, and then the next action. The old single line
205
+ # ("Warning: Failed to download …") said neither what it meant for the
206
+ # session nor what to do, and it was the ONLY trace anywhere (#1774).
207
+ echo "ai-scanner: this session is NOT being scanned — the scanner is not installed on this machine and could not be installed now: \${UNVERIFIED_REASON:-the download failed}." >&2
208
+ report_unscanned_history
209
+ echo "ai-scanner: tool calls keep working, and they run unscanned until it is in place. Re-run the installer (${installCommand}), or contact your IT administrator with this message." >&2
111
210
  fi
112
211
  exit 0
113
212
  fi
114
213
 
214
+ # A binary IS in place. Clear any record of having run unscanned, so the record
215
+ # only ever describes the machine's CURRENT state.
216
+ rm -f "$UNSCANNED_STAMP"
217
+
115
218
  # Binary exists — throttled self-update check
116
219
  if should_check_update; then
117
220
  "$BIN" update 2>/dev/null || true
@@ -148,6 +251,42 @@ exit 0
148
251
  // generous enough to absorb cold-start variance while still bounding a
149
252
  // truly hung scanner. AI_SCANNER_HOOK_TIMEOUT (seconds) lets operators
150
253
  // tune further per-environment.
254
+ // A tool call ran with NO scanner in place (issue #1774).
255
+ //
256
+ // The hook still exits 0 — fail-open is non-negotiable (CLAUDE.md "Fail-open
257
+ // Design"): the scanner's own absence must never block the user's work. What
258
+ // was missing is everything AFTER that: nothing re-fetched, nothing recorded,
259
+ // nothing visible. From the outside a machine whose binary vanished looked
260
+ // exactly like a healthy one, because "healthy" also produces no output.
261
+ //
262
+ // So the fact is written down next to where the binary belongs: first time
263
+ // seen, last time seen, and how many tool calls have run unscanned. That file
264
+ // is what `start_scanner.sh` reads to tell the user at the next session start,
265
+ // and what `aidr doctor` reads on demand. `start_scanner.sh` DELETES it once a
266
+ // binary is in place, so a machine that healed stops claiming to be blind.
267
+ //
268
+ // Never fails, never blocks: every write is best-effort and the function always
269
+ // returns 0. A bookkeeping error must not become a scanning outage.
270
+ //
271
+ // Two hooks firing at once can lose a count (last write wins). That is
272
+ // accepted: this is a signal that scanning stopped, not an accounting ledger.
273
+ const UNSCANNED_NOTE = `note_unscanned() {
274
+ local dir stamp now first count line
275
+ dir="$(dirname "$BIN")"
276
+ [ -d "$dir" ] || return 0
277
+ stamp="$dir/.ai-scanner-unscanned"
278
+ now="$(date +%s 2>/dev/null)" || return 0
279
+ first="$now"; count=0
280
+ if [ -r "$stamp" ]; then
281
+ read -r line < "$stamp" 2>/dev/null || line=""
282
+ set -- $line
283
+ case "\${1:-}" in ''|*[!0-9]*) ;; *) first="$1" ;; esac
284
+ case "\${3:-}" in ''|*[!0-9]*) ;; *) count="$3" ;; esac
285
+ fi
286
+ printf '%s %s %s\\n' "$first" "$now" "$((count + 1))" > "$stamp" 2>/dev/null || true
287
+ return 0
288
+ }`;
289
+
151
290
  const SCAN_HELPERS = `run_scanner() {
152
291
  if command -v timeout >/dev/null 2>&1; then
153
292
  timeout --kill-after=2s "\${AI_SCANNER_HOOK_TIMEOUT:-30}" "$BIN" "$@"
@@ -165,7 +304,8 @@ export const SCAN_BASH_SH = `#!/bin/bash
165
304
  # PostToolUse hook for Bash: Scan command output.
166
305
  # Thin Claude Code adapter — Rust handles cap, detection log, actor resolution.
167
306
  BIN="\${AI_SCANNER_BIN:-$HOME/.claude/bin/ai-scanner}"
168
- [ ! -x "$BIN" ] && exit 0
307
+ ${UNSCANNED_NOTE}
308
+ [ ! -x "$BIN" ] && { note_unscanned; exit 0; }
169
309
 
170
310
  export AI_SCANNER_DETECTION_LOG="\${CLAUDE_PROJECT_DIR:-.}/.claude/logs/detections.log"
171
311
 
@@ -181,7 +321,8 @@ export const SCAN_BASH_INPUT_SH = `#!/bin/bash
181
321
  # PreToolUse hook for Bash: Scan command before execution.
182
322
  # Thin Claude Code adapter — Rust handles cap, detection log, actor resolution.
183
323
  BIN="\${AI_SCANNER_BIN:-$HOME/.claude/bin/ai-scanner}"
184
- [ ! -x "$BIN" ] && exit 0
324
+ ${UNSCANNED_NOTE}
325
+ [ ! -x "$BIN" ] && { note_unscanned; exit 0; }
185
326
 
186
327
  export AI_SCANNER_DETECTION_LOG="\${CLAUDE_PROJECT_DIR:-.}/.claude/logs/detections.log"
187
328
 
@@ -198,7 +339,8 @@ export const SCAN_EDIT_SH = `#!/bin/bash
198
339
  # Thin Claude Code adapter — Rust handles cap, exclude dirs, detection log.
199
340
  # Only the Edit-vs-MultiEdit content extraction is Claude Code specific.
200
341
  BIN="\${AI_SCANNER_BIN:-$HOME/.claude/bin/ai-scanner}"
201
- [ ! -x "$BIN" ] && exit 0
342
+ ${UNSCANNED_NOTE}
343
+ [ ! -x "$BIN" ] && { note_unscanned; exit 0; }
202
344
 
203
345
  export AI_SCANNER_DETECTION_LOG="\${CLAUDE_PROJECT_DIR:-.}/.claude/logs/detections.log"
204
346
 
@@ -223,7 +365,8 @@ export const SCAN_READ_SH = `#!/bin/bash
223
365
  # PostToolUse hook for Read: Scan file content Claude just read.
224
366
  # Thin Claude Code adapter — Rust handles cap, exclude dirs, detection log.
225
367
  BIN="\${AI_SCANNER_BIN:-$HOME/.claude/bin/ai-scanner}"
226
- [ ! -x "$BIN" ] && exit 0
368
+ ${UNSCANNED_NOTE}
369
+ [ ! -x "$BIN" ] && { note_unscanned; exit 0; }
227
370
 
228
371
  export AI_SCANNER_DETECTION_LOG="\${CLAUDE_PROJECT_DIR:-.}/.claude/logs/detections.log"
229
372
 
@@ -242,7 +385,8 @@ export const SCAN_EDIT_OUTPUT_SH = `#!/bin/bash
242
385
  # PostToolUse hook for Edit/MultiEdit/Write: Scan final file content.
243
386
  # Thin Claude Code adapter — Rust handles cap, exclude dirs, detection log.
244
387
  BIN="\${AI_SCANNER_BIN:-$HOME/.claude/bin/ai-scanner}"
245
- [ ! -x "$BIN" ] && exit 0
388
+ ${UNSCANNED_NOTE}
389
+ [ ! -x "$BIN" ] && { note_unscanned; exit 0; }
246
390
 
247
391
  export AI_SCANNER_DETECTION_LOG="\${CLAUDE_PROJECT_DIR:-.}/.claude/logs/detections.log"
248
392
 
@@ -262,7 +406,8 @@ export const SCAN_READ_INPUT_SH = `#!/bin/bash
262
406
  # binary, including actor resolution, exclude-dir filtering, content cap, and
263
407
  # detection-log writing.
264
408
  BIN="\${AI_SCANNER_BIN:-$HOME/.claude/bin/ai-scanner}"
265
- [ ! -x "$BIN" ] && exit 0
409
+ ${UNSCANNED_NOTE}
410
+ [ ! -x "$BIN" ] && { note_unscanned; exit 0; }
266
411
 
267
412
  export AI_SCANNER_DETECTION_LOG="\${CLAUDE_PROJECT_DIR:-.}/.claude/logs/detections.log"
268
413
 
@@ -279,7 +424,8 @@ export const SCAN_WEBFETCH_SH = `#!/bin/bash
279
424
  # PreToolUse hook for WebFetch: Scan URL before Claude fetches it.
280
425
  # Thin Claude Code adapter — Rust handles phishing scoring + YARA + detection log.
281
426
  BIN="\${AI_SCANNER_BIN:-$HOME/.claude/bin/ai-scanner}"
282
- [ ! -x "$BIN" ] && exit 0
427
+ ${UNSCANNED_NOTE}
428
+ [ ! -x "$BIN" ] && { note_unscanned; exit 0; }
283
429
 
284
430
  export AI_SCANNER_DETECTION_LOG="\${CLAUDE_PROJECT_DIR:-.}/.claude/logs/detections.log"
285
431
 
@@ -295,7 +441,8 @@ export const SCAN_WEBSEARCH_SH = `#!/bin/bash
295
441
  # PostToolUse hook for WebSearch: Scan search results for phishing URLs.
296
442
  # Thin Claude Code adapter — Rust handles cap, phishing scoring, detection log.
297
443
  BIN="\${AI_SCANNER_BIN:-$HOME/.claude/bin/ai-scanner}"
298
- [ ! -x "$BIN" ] && exit 0
444
+ ${UNSCANNED_NOTE}
445
+ [ ! -x "$BIN" ] && { note_unscanned; exit 0; }
299
446
 
300
447
  export AI_SCANNER_DETECTION_LOG="\${CLAUDE_PROJECT_DIR:-.}/.claude/logs/detections.log"
301
448
 
@@ -354,6 +501,8 @@ export CLAUDE_PROJECT_DIR="\${CLAUDE_PROJECT_DIR:-.}"
354
501
  # \`timeout\` (timed out) is mapped to 0 so a hung scanner never blocks the
355
502
  # tool call. Bare macOS (no coreutils) falls through without a timeout —
356
503
  # install coreutils (\`brew install coreutils\`) to get the safety net.
504
+ ${UNSCANNED_NOTE}
505
+
357
506
  run_scanner() {
358
507
  if command -v timeout >/dev/null 2>&1; then
359
508
  timeout --kill-after=2s "\${AI_SCANNER_HOOK_TIMEOUT}" "\${AI_SCANNER_BIN}" "$@"
@@ -388,7 +537,7 @@ EOF
388
537
  export const CURSOR_PRE_SHELL_SH = `#!/bin/bash
389
538
  SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
390
539
  source "$SCRIPT_DIR/env.sh"
391
- BIN="$AI_SCANNER_BIN"; [ ! -x "$BIN" ] && exit 0
540
+ BIN="$AI_SCANNER_BIN"; [ ! -x "$BIN" ] && { note_unscanned; exit 0; }
392
541
  export AI_SCANNER_DETECTION_LOG
393
542
  jq -r '.tool_input.command // ""' | run_scanner scan bash-input
394
543
  `;
@@ -396,7 +545,7 @@ jq -r '.tool_input.command // ""' | run_scanner scan bash-input
396
545
  export const CURSOR_PRE_READ_SH = `#!/bin/bash
397
546
  SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
398
547
  source "$SCRIPT_DIR/env.sh"
399
- BIN="$AI_SCANNER_BIN"; [ ! -x "$BIN" ] && exit 0
548
+ BIN="$AI_SCANNER_BIN"; [ ! -x "$BIN" ] && { note_unscanned; exit 0; }
400
549
  export AI_SCANNER_DETECTION_LOG
401
550
  FILE_PATH=$(jq -r '.tool_input.file_path // ""')
402
551
  [ -z "$FILE_PATH" ] || [ "$FILE_PATH" = "null" ] && exit 0
@@ -406,7 +555,7 @@ run_scanner scan read-input --file "$FILE_PATH" </dev/null
406
555
  export const CURSOR_PRE_EDIT_SH = `#!/bin/bash
407
556
  SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
408
557
  source "$SCRIPT_DIR/env.sh"
409
- BIN="$AI_SCANNER_BIN"; [ ! -x "$BIN" ] && exit 0
558
+ BIN="$AI_SCANNER_BIN"; [ ! -x "$BIN" ] && { note_unscanned; exit 0; }
410
559
  export AI_SCANNER_DETECTION_LOG
411
560
  INPUT=$(cat)
412
561
  FILE_PATH=$(jq -r '.tool_input.file_path // ""' <<<"$INPUT")
@@ -423,7 +572,7 @@ printf '%s' "$CONTENT" | run_scanner scan edit-input --file "$FILE_PATH"
423
572
  export const CURSOR_POST_SHELL_SH = `#!/bin/bash
424
573
  SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
425
574
  source "$SCRIPT_DIR/env.sh"
426
- BIN="$AI_SCANNER_BIN"; [ ! -x "$BIN" ] && exit 0
575
+ BIN="$AI_SCANNER_BIN"; [ ! -x "$BIN" ] && { note_unscanned; exit 0; }
427
576
  export AI_SCANNER_DETECTION_LOG
428
577
  INPUT=$(cat)
429
578
  COMMAND=$(jq -r '.tool_output // ""' <<<"$INPUT" | head -1 && jq -r '.tool_input.command // ""' <<<"$INPUT")
@@ -433,7 +582,7 @@ jq -r '.tool_output // ""' <<<"$INPUT" | run_scanner scan bash-output --command
433
582
  export const CURSOR_POST_READ_SH = `#!/bin/bash
434
583
  SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
435
584
  source "$SCRIPT_DIR/env.sh"
436
- BIN="$AI_SCANNER_BIN"; [ ! -x "$BIN" ] && exit 0
585
+ BIN="$AI_SCANNER_BIN"; [ ! -x "$BIN" ] && { note_unscanned; exit 0; }
437
586
  export AI_SCANNER_DETECTION_LOG
438
587
  INPUT=$(cat)
439
588
  FILE_PATH=$(jq -r '.tool_input.file_path // ""' <<<"$INPUT")
@@ -444,7 +593,7 @@ jq -r '.tool_output // ""' <<<"$INPUT" | run_scanner scan read-output --file "$F
444
593
  export const CURSOR_POST_WRITE_SH = `#!/bin/bash
445
594
  SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
446
595
  source "$SCRIPT_DIR/env.sh"
447
- BIN="$AI_SCANNER_BIN"; [ ! -x "$BIN" ] && exit 0
596
+ BIN="$AI_SCANNER_BIN"; [ ! -x "$BIN" ] && { note_unscanned; exit 0; }
448
597
  export AI_SCANNER_DETECTION_LOG
449
598
  INPUT=$(cat)
450
599
  FILE_PATH=$(jq -r '.tool_input.file_path // ""' <<<"$INPUT")
@@ -475,7 +624,7 @@ return 0 2>/dev/null || true
475
624
  export const WINDSURF_PRE_COMMAND_SH = `#!/bin/bash
476
625
  SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
477
626
  source "$SCRIPT_DIR/ensure_scanner.sh"
478
- BIN="$AI_SCANNER_BIN"; [ ! -x "$BIN" ] && exit 0
627
+ BIN="$AI_SCANNER_BIN"; [ ! -x "$BIN" ] && { note_unscanned; exit 0; }
479
628
  export AI_SCANNER_DETECTION_LOG
480
629
  CONTENT=$(jq -r '.tool_info.command_line // ""')
481
630
  [ -z "$CONTENT" ] || [ "$CONTENT" = "null" ] && exit 0
@@ -485,7 +634,7 @@ printf '%s' "$CONTENT" | run_scanner scan bash-input
485
634
  export const WINDSURF_PRE_READ_SH = `#!/bin/bash
486
635
  SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
487
636
  source "$SCRIPT_DIR/ensure_scanner.sh"
488
- BIN="$AI_SCANNER_BIN"; [ ! -x "$BIN" ] && exit 0
637
+ BIN="$AI_SCANNER_BIN"; [ ! -x "$BIN" ] && { note_unscanned; exit 0; }
489
638
  export AI_SCANNER_DETECTION_LOG
490
639
  FILE_PATH=$(jq -r '.tool_info.file_path // ""')
491
640
  [ -z "$FILE_PATH" ] || [ "$FILE_PATH" = "null" ] && exit 0
@@ -495,7 +644,7 @@ run_scanner scan read-input --file "$FILE_PATH" </dev/null
495
644
  export const WINDSURF_PRE_WRITE_SH = `#!/bin/bash
496
645
  SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
497
646
  source "$SCRIPT_DIR/ensure_scanner.sh"
498
- BIN="$AI_SCANNER_BIN"; [ ! -x "$BIN" ] && exit 0
647
+ BIN="$AI_SCANNER_BIN"; [ ! -x "$BIN" ] && { note_unscanned; exit 0; }
499
648
  export AI_SCANNER_DETECTION_LOG
500
649
  INPUT=$(cat)
501
650
  FILE_PATH=$(jq -r '.tool_info.file_path // ""' <<<"$INPUT")
@@ -507,7 +656,7 @@ printf '%s' "$CONTENT" | run_scanner scan edit-input --file "$FILE_PATH"
507
656
  export const WINDSURF_POST_READ_SH = `#!/bin/bash
508
657
  SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
509
658
  source "$SCRIPT_DIR/ensure_scanner.sh"
510
- BIN="$AI_SCANNER_BIN"; [ ! -x "$BIN" ] && exit 0
659
+ BIN="$AI_SCANNER_BIN"; [ ! -x "$BIN" ] && { note_unscanned; exit 0; }
511
660
  export AI_SCANNER_DETECTION_LOG
512
661
  FILE_PATH=$(jq -r '.tool_info.file_path // ""')
513
662
  [ -z "$FILE_PATH" ] || [ ! -f "$FILE_PATH" ] && exit 0
@@ -517,7 +666,7 @@ run_scanner scan read-output --file "$FILE_PATH" < "$FILE_PATH"
517
666
  export const WINDSURF_POST_WRITE_SH = `#!/bin/bash
518
667
  SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
519
668
  source "$SCRIPT_DIR/ensure_scanner.sh"
520
- BIN="$AI_SCANNER_BIN"; [ ! -x "$BIN" ] && exit 0
669
+ BIN="$AI_SCANNER_BIN"; [ ! -x "$BIN" ] && { note_unscanned; exit 0; }
521
670
  export AI_SCANNER_DETECTION_LOG
522
671
  FILE_PATH=$(jq -r '.tool_info.file_path // ""')
523
672
  [ -z "$FILE_PATH" ] || [ ! -f "$FILE_PATH" ] && exit 0
@@ -560,7 +709,7 @@ exec "$SCRIPT_DIR/start_scanner.sh"
560
709
  export const KIRO_PRE_SHELL_SH = `#!/bin/bash
561
710
  SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
562
711
  source "$SCRIPT_DIR/env.sh"
563
- BIN="$AI_SCANNER_BIN"; [ ! -x "$BIN" ] && exit 0
712
+ BIN="$AI_SCANNER_BIN"; [ ! -x "$BIN" ] && { note_unscanned; exit 0; }
564
713
  export AI_SCANNER_DETECTION_LOG
565
714
  jq -r '.tool_input.command // ""' | run_scanner scan bash-input
566
715
  `;
@@ -568,7 +717,7 @@ jq -r '.tool_input.command // ""' | run_scanner scan bash-input
568
717
  export const KIRO_PRE_READ_SH = `#!/bin/bash
569
718
  SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
570
719
  source "$SCRIPT_DIR/env.sh"
571
- BIN="$AI_SCANNER_BIN"; [ ! -x "$BIN" ] && exit 0
720
+ BIN="$AI_SCANNER_BIN"; [ ! -x "$BIN" ] && { note_unscanned; exit 0; }
572
721
  export AI_SCANNER_DETECTION_LOG
573
722
  FILE_PATH=$(jq -r '.tool_input.file_path // .tool_input.operations[0].path // ""')
574
723
  [ -z "$FILE_PATH" ] || [ "$FILE_PATH" = "null" ] && exit 0
@@ -578,7 +727,7 @@ run_scanner scan read-input --file "$FILE_PATH" </dev/null
578
727
  export const KIRO_PRE_WRITE_SH = `#!/bin/bash
579
728
  SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
580
729
  source "$SCRIPT_DIR/env.sh"
581
- BIN="$AI_SCANNER_BIN"; [ ! -x "$BIN" ] && exit 0
730
+ BIN="$AI_SCANNER_BIN"; [ ! -x "$BIN" ] && { note_unscanned; exit 0; }
582
731
  export AI_SCANNER_DETECTION_LOG
583
732
  INPUT=$(cat)
584
733
  FILE_PATH=$(jq -r '.tool_input.file_path // .tool_input.path // ""' <<<"$INPUT")
@@ -590,7 +739,7 @@ printf '%s' "$CONTENT" | run_scanner scan edit-input --file "$FILE_PATH"
590
739
  export const KIRO_POST_SHELL_SH = `#!/bin/bash
591
740
  SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
592
741
  source "$SCRIPT_DIR/env.sh"
593
- BIN="$AI_SCANNER_BIN"; [ ! -x "$BIN" ] && exit 0
742
+ BIN="$AI_SCANNER_BIN"; [ ! -x "$BIN" ] && { note_unscanned; exit 0; }
594
743
  export AI_SCANNER_DETECTION_LOG
595
744
  INPUT=$(cat)
596
745
  COMMAND=$(jq -r '.tool_input.command // ""' <<<"$INPUT")
@@ -601,7 +750,7 @@ printf '%s' "$RESPONSE" | run_scanner scan bash-output --command "$COMMAND"
601
750
  export const KIRO_POST_READ_SH = `#!/bin/bash
602
751
  SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
603
752
  source "$SCRIPT_DIR/env.sh"
604
- BIN="$AI_SCANNER_BIN"; [ ! -x "$BIN" ] && exit 0
753
+ BIN="$AI_SCANNER_BIN"; [ ! -x "$BIN" ] && { note_unscanned; exit 0; }
605
754
  export AI_SCANNER_DETECTION_LOG
606
755
  INPUT=$(cat)
607
756
  FILE_PATH=$(jq -r '.tool_input.file_path // .tool_input.operations[0].path // ""' <<<"$INPUT")
@@ -613,7 +762,7 @@ printf '%s' "$RESPONSE" | run_scanner scan read-output --file "$FILE_PATH"
613
762
  export const KIRO_POST_WRITE_SH = `#!/bin/bash
614
763
  SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
615
764
  source "$SCRIPT_DIR/env.sh"
616
- BIN="$AI_SCANNER_BIN"; [ ! -x "$BIN" ] && exit 0
765
+ BIN="$AI_SCANNER_BIN"; [ ! -x "$BIN" ] && { note_unscanned; exit 0; }
617
766
  export AI_SCANNER_DETECTION_LOG
618
767
  INPUT=$(cat)
619
768
  FILE_PATH=$(jq -r '.tool_input.file_path // .tool_input.path // ""' <<<"$INPUT")
@@ -635,7 +784,8 @@ export function getGeminiPreHookSh(binary) {
635
784
  # the run_scanner timeout helper is inlined here. See agentEnvSh for the
636
785
  # canonical version and the rationale for fail-open on exit 124.
637
786
  BIN="${binary}"
638
- [ ! -x "$BIN" ] && exit 0
787
+ ${UNSCANNED_NOTE}
788
+ [ ! -x "$BIN" ] && { note_unscanned; exit 0; }
639
789
 
640
790
  run_scanner() {
641
791
  if command -v timeout >/dev/null 2>&1; then
@@ -705,7 +855,7 @@ export const CODEX_PRE_SHELL_SH = `#!/bin/bash
705
855
  # Codex PreToolUse Bash hook: scan command and return block JSON to Codex.
706
856
  SCRIPT_DIR="$(cd "$(dirname "\${BASH_SOURCE[0]:-$0}")" && pwd)"
707
857
  source "$SCRIPT_DIR/env.sh"
708
- BIN="$AI_SCANNER_BIN"; [ ! -x "$BIN" ] && exit 0
858
+ BIN="$AI_SCANNER_BIN"; [ ! -x "$BIN" ] && { note_unscanned; exit 0; }
709
859
  export AI_SCANNER_DETECTION_LOG
710
860
  INPUT=$(cat)
711
861
  CMD=$(jq -r '.tool_input.command // ""' <<<"$INPUT" 2>/dev/null)
@@ -735,7 +885,7 @@ export const CODEX_POST_SHELL_SH = `#!/bin/bash
735
885
  # Codex PostToolUse Bash hook: scan command output and return feedback JSON.
736
886
  SCRIPT_DIR="$(cd "$(dirname "\${BASH_SOURCE[0]:-$0}")" && pwd)"
737
887
  source "$SCRIPT_DIR/env.sh"
738
- BIN="$AI_SCANNER_BIN"; [ ! -x "$BIN" ] && exit 0
888
+ BIN="$AI_SCANNER_BIN"; [ ! -x "$BIN" ] && { note_unscanned; exit 0; }
739
889
  export AI_SCANNER_DETECTION_LOG
740
890
  INPUT=$(cat)
741
891
  CMD=$(jq -r '.tool_input.command // ""' <<<"$INPUT" 2>/dev/null)
package/src/verify.mjs CHANGED
@@ -40,6 +40,17 @@ export function downloadBaseOverride(env = process.env) {
40
40
  return null;
41
41
  }
42
42
 
43
+ /**
44
+ * Is this download base the DEV endpoint? Used by the hook templates to quote
45
+ * the right installer back at the user (the DEV package must never tell anyone
46
+ * to install PROD). Derived from `ENDPOINTS` rather than matched against a
47
+ * literal, so changing the DEV endpoint cannot leave a second, stale copy of
48
+ * that decision behind.
49
+ */
50
+ export function isDevDownloadBase(base = "") {
51
+ return typeof base === "string" && base.startsWith(ENDPOINTS.dev);
52
+ }
53
+
43
54
  export function downloadBase(env = "prod") {
44
55
  const override = downloadBaseOverride();
45
56
  if (override) return `${override}/download`;