@bookedsolid/rea 0.33.0 → 0.34.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.
@@ -0,0 +1,240 @@
1
+ #!/bin/bash
2
+ # PreToolUse hook: secret-scanner.sh
3
+ # 0.34.0+ — Node-binary shim for `rea hook secret-scanner`.
4
+ #
5
+ # Pre-0.34.0 the gate's full body lived here as bash (230 LOC, the
6
+ # awk line filter + 17-pattern catalog + placeholder-rejection + the
7
+ # MultiEdit fragment join). The migration to the Node binary moves
8
+ # the pattern catalog + filter + placeholder evaluation into
9
+ # `src/hooks/secret-scanner/index.ts`. This shim is the Claude Code
10
+ # dispatcher's view of the hook — it forwards stdin to the CLI and
11
+ # exits with whatever the CLI returns.
12
+ #
13
+ # Behavioral contract is preserved byte-for-byte: exit 0 on no-match
14
+ # or MEDIUM-only advisory, exit 2 on HALT / HIGH match / malformed
15
+ # payload.
16
+ #
17
+ # # Shim short-circuits (codex round-1 P2 fix)
18
+ #
19
+ # The 0.34.0 round-0 shim deferred ALL decisions to the CLI, including
20
+ # empty-content and `.env.example` suffix exclusion. That regressed
21
+ # benign workflows on fresh/unbuilt installs: clearing a file or
22
+ # editing an example env file would fail closed when `dist/cli/index.js`
23
+ # wasn't built yet.
24
+ #
25
+ # Round-1 P2 fix: replicate the pre-0.34.0 bash body's three
26
+ # short-circuits in the shim BEFORE CLI resolution:
27
+ # - Empty content (no `content`, `new_string`, `edits[]`, or
28
+ # `new_source` in the payload) → exit 0 silently.
29
+ # - file_path / notebook_path with `.env.example` or `.env.sample`
30
+ # suffix → exit 0 silently.
31
+ # The full pattern catalog + filter + placeholder rejection still
32
+ # lives in the CLI.
33
+ #
34
+ # # CLI-resolution trust boundary
35
+ #
36
+ # Mirrors the 0.32.0 final shim shape.
37
+ #
38
+ # # Fail-closed posture
39
+ #
40
+ # secret-scanner is Write/Edit/MultiEdit/NotebookEdit tier — the
41
+ # pre-0.34.0 bash body refused credential-bearing writes without any
42
+ # compiled CLI. Early-exit branches fail closed AFTER the shim
43
+ # short-circuits.
44
+
45
+ set -uo pipefail
46
+
47
+ # 1. HALT check.
48
+ # shellcheck source=_lib/halt-check.sh
49
+ source "$(dirname "$0")/_lib/halt-check.sh"
50
+ check_halt
51
+ REA_ROOT=$(rea_root)
52
+
53
+ proj="${CLAUDE_PROJECT_DIR:-$REA_ROOT}"
54
+
55
+ # 2. Capture stdin once.
56
+ INPUT=$(cat)
57
+
58
+ # 3. Short-circuit: empty-content / file-suffix exclusion. Mirrors
59
+ # the pre-0.34.0 bash body's `[[ -z "$CONTENT" ]] && exit 0` and
60
+ # the `*.env.example | *.env.sample` suffix check. We do these in
61
+ # the shim so unbuilt installs don't fail closed on benign writes.
62
+ if command -v jq >/dev/null 2>&1; then
63
+ # Compose content the same way `parseWriteHookPayload` does:
64
+ # priority content > new_string > join(edits[].new_string) > new_source.
65
+ # 0.34.0 round-2 fix: every value goes through `tostring` so a
66
+ # non-string `new_string` (object/number/null) doesn't trip jq with
67
+ # a "Cannot iterate" error → empty CONTENT → exit 0 bypass. Mirrors
68
+ # the 0.14.0 secret-scanner fix that originally closed this class.
69
+ #
70
+ # 0.34.0 round-4 P2 fix: capture jq's exit code SEPARATELY rather
71
+ # than swallowing it with `|| true`. Pre-fix, invalid JSON or a
72
+ # schema mismatch yielded empty CONTENT → exit 0 silent allow.
73
+ # Post-fix we distinguish:
74
+ # - jq exit 0 + empty CONTENT → valid payload, no content (the
75
+ # bash hook also exit 0'd here)
76
+ # - jq exit 0 + non-empty → enter suffix-check + CLI forward
77
+ # - jq exit != 0 (parse fail) → fall through to CLI forward;
78
+ # the CLI re-parses with Zod and
79
+ # refuses on malformed payload
80
+ # The third branch does NOT exit 0 — we want CLI enforcement to
81
+ # decide. The CLI's parser fails closed.
82
+ CONTENT=$(printf '%s' "$INPUT" | jq -r '
83
+ (.tool_input.content // .tool_input.new_string //
84
+ (
85
+ if (.tool_input.edits | type) == "array"
86
+ then (.tool_input.edits | map((.new_string // "") | tostring) | join("\n"))
87
+ else ""
88
+ end
89
+ ) //
90
+ .tool_input.new_source // ""
91
+ ) | tostring
92
+ ' 2>/dev/null)
93
+ jq_content_status=$?
94
+ FILE_PATH=$(printf '%s' "$INPUT" | jq -r '
95
+ .tool_input.file_path // .tool_input.notebook_path // ""
96
+ ' 2>/dev/null)
97
+ jq_path_status=$?
98
+ # Only honor the shim short-circuits when BOTH jq probes parsed
99
+ # cleanly. Otherwise forward to the CLI which fails closed via Zod.
100
+ if [ "$jq_content_status" -eq 0 ] && [ "$jq_path_status" -eq 0 ]; then
101
+ if [ -z "$CONTENT" ]; then
102
+ exit 0
103
+ fi
104
+ # Suffix-based exclusion. Mirrors the bash hook's:
105
+ # if [[ "$FILE_PATH" == *.env.example || "$FILE_PATH" == *.env.sample ]]; then exit 0; fi
106
+ case "$FILE_PATH" in
107
+ *.env.example|*.env.sample) exit 0 ;;
108
+ esac
109
+ fi
110
+ # jq parse failure → do NOT short-circuit. Fall through to the CLI
111
+ # forward at section 7. The CLI will refuse on malformed payload.
112
+ fi
113
+ # When jq is unavailable, fall through — the CLI does the same parse
114
+ # in TypeScript-space and will short-circuit on empty content there.
115
+
116
+ # 4. Resolve the rea CLI through the fixed 2-tier sandboxed order.
117
+ REA_ARGV=()
118
+ RESOLVED_CLI_PATH=""
119
+ if [ -f "$proj/node_modules/@bookedsolid/rea/dist/cli/index.js" ]; then
120
+ REA_ARGV=(node "$proj/node_modules/@bookedsolid/rea/dist/cli/index.js")
121
+ RESOLVED_CLI_PATH="$proj/node_modules/@bookedsolid/rea/dist/cli/index.js"
122
+ elif [ -f "$proj/dist/cli/index.js" ]; then
123
+ REA_ARGV=(node "$proj/dist/cli/index.js")
124
+ RESOLVED_CLI_PATH="$proj/dist/cli/index.js"
125
+ fi
126
+
127
+ if [ "${#REA_ARGV[@]}" -eq 0 ]; then
128
+ # 4b. Relevance pre-gate (round-7 P1). The round-0 shim refused ALL
129
+ # writes when the CLI was missing, but the pre-0.34.0 bash body
130
+ # only refused writes containing credential patterns. On a fresh
131
+ # install (`npx rea init` flow, pre-`pnpm build` checkout) the
132
+ # CLI isn't built yet but consumers need to write files — config,
133
+ # source, docs, etc. Fix: substring scan the content for the
134
+ # credential markers in the catalog. When CLI is missing AND no
135
+ # marker matches, exit 0 (the pre-0.34.0 body would have done
136
+ # the same — no pattern hit). When CLI is missing AND a marker
137
+ # DOES match, preserve fail-closed (refuse rather than silently
138
+ # allow a credential-shaped write).
139
+ #
140
+ # Substrings cover every entry in SECRET_PATTERNS (catalog in
141
+ # `src/hooks/secret-scanner/index.ts`). Coarse — over-trigger is
142
+ # fine, under-trigger is the bypass we MUST avoid. Same posture
143
+ # as the round-7 dangerous-bash relevance pre-gate.
144
+ CONTENT_FOR_SCAN=""
145
+ if [ -n "${CONTENT:-}" ]; then
146
+ CONTENT_FOR_SCAN="$CONTENT"
147
+ else
148
+ # CONTENT may not have been populated (jq missing, parse failure).
149
+ # Fall back to the raw payload so the substring scan still catches
150
+ # credential markers embedded in JSON-string form.
151
+ CONTENT_FOR_SCAN="$INPUT"
152
+ fi
153
+ CRED_RELEVANT=0
154
+ case "$CONTENT_FOR_SCAN" in
155
+ *"AKIA"*) CRED_RELEVANT=1 ;;
156
+ *"AWS_SECRET_ACCESS_KEY"*|*"aws_secret_access_key"*) CRED_RELEVANT=1 ;;
157
+ *"-----BEGIN"*) CRED_RELEVANT=1 ;;
158
+ *"sk-ant-"*) CRED_RELEVANT=1 ;;
159
+ *"ghp_"*|*"ghs_"*|*"gho_"*|*"ghu_"*|*"ghr_"*) CRED_RELEVANT=1 ;;
160
+ *"github_pat_"*) CRED_RELEVANT=1 ;;
161
+ *"sk_live_"*|*"rk_live_"*|*"pk_live_"*) CRED_RELEVANT=1 ;;
162
+ *"sk_test_"*|*"rk_test_"*|*"pk_test_"*) CRED_RELEVANT=1 ;;
163
+ *"whsec_"*) CRED_RELEVANT=1 ;;
164
+ *"SECRET"*|*"PASSWORD"*|*"PRIVATE_KEY"*|*"API_SECRET"*) CRED_RELEVANT=1 ;;
165
+ *"SUPABASE_SERVICE_ROLE_KEY"*|*"SUPABASE_ANON_KEY"*) CRED_RELEVANT=1 ;;
166
+ *"ANTHROPIC_API_KEY"*|*"STRIPE_SECRET"*|*"DATABASE_URL"*) CRED_RELEVANT=1 ;;
167
+ *"postgresql://"*) CRED_RELEVANT=1 ;;
168
+ *"eyJ"*) CRED_RELEVANT=1 ;; # JWT prefix — catches Supabase keys
169
+ esac
170
+ if [ "$CRED_RELEVANT" -eq 0 ]; then
171
+ # No credential marker. The pre-0.34.0 bash body would have allowed
172
+ # this write — exit 0 to unblock `npx rea init` and pre-build
173
+ # checkouts.
174
+ exit 0
175
+ fi
176
+ # Credential marker matched. Preserve fail-closed posture.
177
+ printf 'rea: secret-scanner cannot run — the rea CLI is not built.\n' >&2
178
+ printf 'Run `pnpm install && pnpm build` (or `npm install` for a consumer install) to restore protection.\n' >&2
179
+ printf 'This shim fails closed because the pre-0.34.0 bash body enforced secret refusal without a CLI.\n' >&2
180
+ exit 2
181
+ fi
182
+
183
+ # 5. Realpath sandbox check.
184
+ if ! command -v node >/dev/null 2>&1; then
185
+ printf 'rea: secret-scanner cannot run — `node` is not on PATH.\n' >&2
186
+ printf 'Install Node 22+ (engines.node) to restore credential refusal.\n' >&2
187
+ exit 2
188
+ fi
189
+
190
+ sandbox_check=$(node -e '
191
+ const fs = require("fs");
192
+ const path = require("path");
193
+ const cli = process.argv[1];
194
+ const projDir = process.argv[2];
195
+ let real, realProj;
196
+ try { real = fs.realpathSync(cli); } catch (e) {
197
+ process.stdout.write("bad:realpath"); process.exit(1);
198
+ }
199
+ try { realProj = fs.realpathSync(projDir); } catch (e) {
200
+ process.stdout.write("bad:realpath-proj"); process.exit(1);
201
+ }
202
+ const sep = path.sep;
203
+ const projWithSep = realProj.endsWith(sep) ? realProj : realProj + sep;
204
+ if (!(real === realProj || real.startsWith(projWithSep))) {
205
+ process.stdout.write("bad:cli-escapes-project"); process.exit(1);
206
+ }
207
+ let cur = path.dirname(path.dirname(path.dirname(real)));
208
+ let found = false;
209
+ for (let i = 0; i < 20 && cur && cur !== path.dirname(cur); i += 1) {
210
+ const pj = path.join(cur, "package.json");
211
+ if (fs.existsSync(pj)) {
212
+ try {
213
+ const data = JSON.parse(fs.readFileSync(pj, "utf8"));
214
+ if (data && data.name === "@bookedsolid/rea") { found = true; break; }
215
+ } catch (e) { /* keep walking */ }
216
+ }
217
+ cur = path.dirname(cur);
218
+ }
219
+ if (!found) { process.stdout.write("bad:no-rea-pkg-json"); process.exit(1); }
220
+ process.stdout.write("ok");
221
+ ' -- "$RESOLVED_CLI_PATH" "$proj" 2>/dev/null)
222
+
223
+ if [ "$sandbox_check" != "ok" ]; then
224
+ printf 'rea: secret-scanner FAILED sandbox check (%s) — refusing.\n' "$sandbox_check" >&2
225
+ exit 2
226
+ fi
227
+
228
+ # 6. Version-probe.
229
+ probe_out=$("${REA_ARGV[@]}" hook secret-scanner --help 2>&1)
230
+ probe_status=$?
231
+ if [ "$probe_status" -ne 0 ] || ! printf '%s' "$probe_out" | grep -q -e 'secret-scanner'; then
232
+ printf 'rea: this shim requires the `rea hook secret-scanner` subcommand (introduced in 0.34.0).\n' >&2
233
+ printf 'The resolved CLI at %s does not implement it.\n' "$RESOLVED_CLI_PATH" >&2
234
+ printf 'Run `pnpm install` (or `npm install`) to sync the CLI; refusing in the meantime to preserve enforcement.\n' >&2
235
+ exit 2
236
+ fi
237
+
238
+ # 7. Forward stdin (already captured up-front).
239
+ printf '%s' "$INPUT" | "${REA_ARGV[@]}" hook secret-scanner
240
+ exit $?