@bookedsolid/rea 0.31.0 → 0.32.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,166 @@
1
+ /**
2
+ * Shared stdin payload primitive for the Node-binary hook tier.
3
+ *
4
+ * 0.32.0 — extracts the `INPUT=$(cat) ; jq -r '.tool_input.command'`
5
+ * pattern that every bash hook in `hooks/` repeats. The Node-binary
6
+ * scan-bash already does this work in `runHookScanBash` (lines 225-258
7
+ * of `src/cli/hook.ts`); the Phase 1 pilots landing in 0.32.0 need
8
+ * the same primitive without copy-pasting the parsing + type-guard +
9
+ * fail-closed-on-malformed-JSON dance into each new hook.
10
+ *
11
+ * The shape mirrors the bash hooks' contract verbatim:
12
+ *
13
+ * - `tool_input.command` is the only field we read; bash hooks only
14
+ * ever ran `jq -r '.tool_input.command // ""'` against this payload.
15
+ * - `tool_name` is also surfaced because two bash hooks
16
+ * (`pr-issue-link-gate.sh` and `security-disclosure-gate.sh`)
17
+ * short-circuit when the tool isn't `Bash`.
18
+ *
19
+ * Failure modes:
20
+ *
21
+ * - Empty stdin → `{ command: '', toolName: '' }`. The bash hooks
22
+ * allow on empty command (`[[ -z "$CMD" ]] && exit 0`); the Node
23
+ * port preserves this by returning empty strings rather than
24
+ * throwing.
25
+ * - Malformed JSON → throws `MalformedPayloadError`. The caller
26
+ * decides whether to fail-closed (block) or fail-open (allow);
27
+ * `runHookScanBash` chose fail-closed (block) and the Phase 1
28
+ * pilots match that posture for consistency.
29
+ * - `tool_input.command` is non-string → throws `TypePayloadError`.
30
+ * A crafted payload like `{"tool_input":{"command":["rm","-rf"]}}`
31
+ * would silently coerce to `''` if we used `String(c)`; that
32
+ * would translate into a free allow. Refuse instead.
33
+ */
34
+ import { Buffer } from 'node:buffer';
35
+ /**
36
+ * Thrown when stdin contains content that is not valid JSON.
37
+ *
38
+ * Distinct error class so callers can `instanceof` discriminate without
39
+ * leaning on string matching of the message.
40
+ */
41
+ export class MalformedPayloadError extends Error {
42
+ constructor(message) {
43
+ super(message);
44
+ this.name = 'MalformedPayloadError';
45
+ }
46
+ }
47
+ /**
48
+ * Thrown when the JSON parses but `tool_input.command` is present and
49
+ * has the wrong type (anything other than `string` / `undefined`).
50
+ */
51
+ export class TypePayloadError extends Error {
52
+ constructor(message) {
53
+ super(message);
54
+ this.name = 'TypePayloadError';
55
+ }
56
+ }
57
+ /**
58
+ * Parse a Claude Code PreToolUse stdin payload. Pure function — no I/O.
59
+ *
60
+ * @param raw Bytes / string read from the hook's stdin (the `INPUT=$(cat)`
61
+ * equivalent).
62
+ * @returns A normalized `HookPayload` with both fields always defined.
63
+ * @throws MalformedPayloadError if the input is not parseable JSON.
64
+ * @throws TypePayloadError if `tool_input.command` is present with a
65
+ * non-string type.
66
+ */
67
+ export function parseHookPayload(raw) {
68
+ const text = typeof raw === 'string' ? raw : raw.toString('utf8');
69
+ if (text.trim().length === 0) {
70
+ return { toolName: '', command: '' };
71
+ }
72
+ let parsed;
73
+ try {
74
+ parsed = JSON.parse(text);
75
+ }
76
+ catch (err) {
77
+ const detail = err instanceof Error ? err.message : String(err);
78
+ throw new MalformedPayloadError(`hook payload is not valid JSON: ${detail}`);
79
+ }
80
+ if (parsed === null) {
81
+ // Top-level `null` mirrors `jq -r '.tool_name // ""'` returning ``
82
+ // — the bash hooks treated this as "no tool, allow on empty cmd".
83
+ return { toolName: '', command: '' };
84
+ }
85
+ if (typeof parsed !== 'object' || Array.isArray(parsed)) {
86
+ // Top-level primitives (number, string, boolean) and arrays are
87
+ // unambiguously malformed — Claude Code never emits these shapes.
88
+ // Fail-closed so a crafted payload can't sneak past as a no-op.
89
+ throw new MalformedPayloadError(`hook payload top-level is ${Array.isArray(parsed) ? 'array' : typeof parsed}, expected object`);
90
+ }
91
+ const toolName = typeof parsed.tool_name === 'string' ? parsed.tool_name : '';
92
+ const ti = parsed.tool_input;
93
+ let command = '';
94
+ if (ti !== undefined && ti !== null) {
95
+ if (typeof ti !== 'object') {
96
+ throw new TypePayloadError(`hook payload tool_input is ${typeof ti}, expected object`);
97
+ }
98
+ const c = ti.command;
99
+ if (c !== undefined && typeof c !== 'string') {
100
+ throw new TypePayloadError(`hook payload tool_input.command is ${typeof c}, expected string`);
101
+ }
102
+ if (typeof c === 'string')
103
+ command = c;
104
+ }
105
+ return { toolName, command };
106
+ }
107
+ /**
108
+ * Read all of stdin into a string with a soft byte cap and a hard
109
+ * timeout. Mirrors the `readStdinWithTimeout` helper in
110
+ * `src/cli/hook.ts` (which scans a fixed timeout but no byte cap).
111
+ *
112
+ * The cap (default 1 MiB) defends against a misbehaving caller piping
113
+ * an unbounded payload — we'd otherwise sit in the read loop forever
114
+ * even if the caller eventually closed stdin.
115
+ *
116
+ * @param timeoutMs How long to wait for stdin to close before resolving
117
+ * with whatever we have. Default 5_000 ms.
118
+ * @param maxBytes Soft cap on total bytes accepted. Default 1 MiB.
119
+ * Once reached, additional chunks are dropped silently
120
+ * (the caller still gets a parseable string back).
121
+ */
122
+ export function readStdinWithTimeout(timeoutMs = 5_000, maxBytes = 1024 * 1024) {
123
+ return new Promise((resolve) => {
124
+ if (process.stdin.isTTY) {
125
+ resolve('');
126
+ return;
127
+ }
128
+ let buf = '';
129
+ let bytesRead = 0;
130
+ let resolved = false;
131
+ const finish = () => {
132
+ if (resolved)
133
+ return;
134
+ resolved = true;
135
+ clearTimeout(timer);
136
+ try {
137
+ process.stdin.removeAllListeners('data');
138
+ process.stdin.removeAllListeners('end');
139
+ process.stdin.removeAllListeners('error');
140
+ }
141
+ catch {
142
+ /* best effort */
143
+ }
144
+ resolve(buf);
145
+ };
146
+ const timer = setTimeout(finish, timeoutMs);
147
+ process.stdin.setEncoding('utf8');
148
+ process.stdin.on('data', (chunk) => {
149
+ const chunkBytes = Buffer.byteLength(chunk, 'utf8');
150
+ if (bytesRead + chunkBytes > maxBytes) {
151
+ // Truncate to the cap; further chunks are dropped silently.
152
+ const remaining = Math.max(0, maxBytes - bytesRead);
153
+ if (remaining > 0) {
154
+ buf += chunk.slice(0, remaining);
155
+ bytesRead = maxBytes;
156
+ }
157
+ finish();
158
+ return;
159
+ }
160
+ buf += chunk;
161
+ bytesRead += chunkBytes;
162
+ });
163
+ process.stdin.on('end', finish);
164
+ process.stdin.on('error', finish);
165
+ });
166
+ }
@@ -0,0 +1,100 @@
1
+ /**
2
+ * Quote-aware shell-segment splitter for the Node-binary hook tier.
3
+ *
4
+ * 0.32.0 — port of the relevant primitives in
5
+ * `hooks/_lib/cmd-segments.sh`. The bash helper is 1002 LOC of
6
+ * defense-in-depth (heredoc unwrapping, nested-shell recursion,
7
+ * env-var-assignment stripping, etc.) — most of those branches exist
8
+ * to defend against bypass attempts in WRITE-tier gates (`dangerous-
9
+ * bash-interceptor`, `dependency-audit-gate`). The Phase 1 pilots
10
+ * landing in 0.32.0 (`security-disclosure-gate`,
11
+ * `attribution-advisory`) only need the SUBSET of segment behavior
12
+ * those two hooks actually exercise:
13
+ *
14
+ * 1. Split the input on shell command separators (`;`, `&&`, `||`,
15
+ * `|`, `&`, newline) while masking separators that appear inside
16
+ * matched `"..."` and `'...'` quote spans.
17
+ * 2. For each segment, strip leading `sudo`, `exec`, `time`, `then`,
18
+ * `do`, `else`, `fi`, and `VAR=value` env-prefixes so the
19
+ * caller's regex can anchor at the segment's actual command head.
20
+ * 3. Expose two query primitives:
21
+ * - `anySegmentStartsWith(cmd, regexHead)`
22
+ * true if any segment's prefix-stripped head matches the
23
+ * head-anchored regex.
24
+ * - `anySegmentMatches(cmd, regex)`
25
+ * true if any segment's raw (non-stripped) text contains a
26
+ * match for the regex (used for content scans like
27
+ * `Co-Authored-By:` markers inside `git commit -m "..."`).
28
+ *
29
+ * Out-of-scope vs. the bash helper:
30
+ *
31
+ * - No heredoc body extraction. The pilots match on the command
32
+ * line, not on heredoc contents. (Body-file resolution in
33
+ * `security-disclosure-gate` is done separately by reading the
34
+ * file path off the command.)
35
+ * - No nested-shell unwrapping (`bash -c 'PAYLOAD'`). The
36
+ * bash-scanner walker already handles that for the WRITE gates;
37
+ * the Phase 1 pilots inherit the SECURITY guarantee that any
38
+ * hostile nested shell would have been refused by the bash-scanner
39
+ * tier BEFORE this advisory tier ran.
40
+ * - No backtick/command-substitution recursion.
41
+ *
42
+ * If a future pilot needs those branches, port them here in a
43
+ * subsequent release. The CURRENT pilots' bash counterparts call only
44
+ * `any_segment_starts_with` and `any_segment_matches` against
45
+ * direct-stdin commands.
46
+ *
47
+ * Quote-handling parity with cmd-segments.sh:
48
+ *
49
+ * - Double-quoted spans (`"..."`): `\"` and `\\` are literal escapes;
50
+ * all other characters are literal.
51
+ * - Single-quoted spans (`'...'`): no escape semantics; every
52
+ * character is literal until the next `'`.
53
+ * - Unterminated quote spans extend to end-of-input (caller's bug —
54
+ * we still emit a single segment for it rather than throwing).
55
+ * - Backslash outside quotes escapes the following character (so
56
+ * `git commit \&\& foo` parses as a single segment, matching
57
+ * bash's behavior).
58
+ */
59
+ /**
60
+ * A single emitted segment. `raw` preserves the original (post-
61
+ * unmasking) text; `head` is the prefix-stripped form used for
62
+ * head-anchored matchers.
63
+ */
64
+ export interface CommandSegment {
65
+ raw: string;
66
+ head: string;
67
+ }
68
+ /**
69
+ * Split `cmd` into segments using the quote-aware masking → split →
70
+ * unmask pipeline. Returns an array of `{ raw, head }` tuples in the
71
+ * order they appeared in the original command.
72
+ */
73
+ export declare function splitSegments(cmd: string): CommandSegment[];
74
+ /**
75
+ * Returns true if any segment's prefix-stripped head matches the
76
+ * head-anchored regex. The regex must NOT include a `^` anchor —
77
+ * we anchor by testing against the head of the segment via
78
+ * `regex.test(head.slice(0, match.length))` simulation. In practice
79
+ * we just run the regex against the head with the regex already
80
+ * head-anchored by virtue of `head` containing only the prefix-
81
+ * stripped form.
82
+ *
83
+ * The bash counterpart uses `grep -qiE PATTERN <<<"$head"` so we
84
+ * match the same posture: case-INSENSITIVE, extended regex.
85
+ *
86
+ * @param regexSource ERE source. We compile with case-insensitive
87
+ * flag. Caller passes the same string they would
88
+ * have passed to `any_segment_starts_with` in bash.
89
+ * The regex is internally anchored with `^`.
90
+ */
91
+ export declare function anySegmentStartsWith(cmd: string, regexSource: string): boolean;
92
+ /**
93
+ * Returns true if any segment's RAW text contains a match for the
94
+ * regex (no head anchoring). Mirrors `any_segment_matches` — used for
95
+ * content-scan patterns like `Co-Authored-By:` markers inside
96
+ * quoted `git commit -m "..."` arguments.
97
+ *
98
+ * Case-INSENSITIVE, extended regex. Same posture as the bash helper.
99
+ */
100
+ export declare function anySegmentMatches(cmd: string, regexSource: string): boolean;