@bookedsolid/rea 0.32.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.
- package/dist/cli/hook.js +49 -0
- package/dist/hooks/_lib/payload.d.ts +38 -0
- package/dist/hooks/_lib/payload.js +79 -0
- package/dist/hooks/_lib/segments.d.ts +127 -0
- package/dist/hooks/_lib/segments.js +628 -16
- package/dist/hooks/architecture-review-gate/index.d.ts +58 -0
- package/dist/hooks/architecture-review-gate/index.js +250 -0
- package/dist/hooks/changeset-security-gate/index.d.ts +71 -0
- package/dist/hooks/changeset-security-gate/index.js +330 -0
- package/dist/hooks/dangerous-bash-interceptor/index.d.ts +103 -0
- package/dist/hooks/dangerous-bash-interceptor/index.js +669 -0
- package/dist/hooks/dependency-audit-gate/index.d.ts +91 -0
- package/dist/hooks/dependency-audit-gate/index.js +294 -0
- package/dist/hooks/env-file-protection/index.d.ts +55 -0
- package/dist/hooks/env-file-protection/index.js +159 -0
- package/dist/hooks/local-review-gate/index.d.ts +145 -0
- package/dist/hooks/local-review-gate/index.js +374 -0
- package/dist/hooks/secret-scanner/index.d.ts +143 -0
- package/dist/hooks/secret-scanner/index.js +404 -0
- package/hooks/architecture-review-gate.sh +92 -77
- package/hooks/changeset-security-gate.sh +114 -149
- package/hooks/dangerous-bash-interceptor.sh +168 -386
- package/hooks/dependency-audit-gate.sh +115 -156
- package/hooks/env-file-protection.sh +130 -97
- package/hooks/local-review-gate.sh +523 -410
- package/hooks/secret-scanner.sh +210 -200
- package/package.json +1 -1
- package/templates/architecture-review-gate.dogfood-staged.sh +116 -0
- package/templates/changeset-security-gate.dogfood-staged.sh +137 -0
- package/templates/dangerous-bash-interceptor.dogfood-staged.sh +196 -0
- package/templates/dependency-audit-gate.dogfood-staged.sh +138 -0
- package/templates/env-file-protection.dogfood-staged.sh +157 -0
- package/templates/local-review-gate.dogfood-staged.sh +573 -0
- package/templates/secret-scanner.dogfood-staged.sh +240 -0
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node-binary port of `hooks/changeset-security-gate.sh`.
|
|
3
|
+
*
|
|
4
|
+
* 0.33.0 Phase 1 port #3.
|
|
5
|
+
*
|
|
6
|
+
* Guards `.changeset/*.md` files against two failure modes:
|
|
7
|
+
*
|
|
8
|
+
* 1. SECURITY DISCLOSURE LEAK — a GHSA or CVE identifier in a
|
|
9
|
+
* changeset file becomes public via CHANGELOG.md when the
|
|
10
|
+
* release ships. Block the write.
|
|
11
|
+
* 2. MISSING OR MALFORMED FRONTMATTER — a changeset without a
|
|
12
|
+
* proper frontmatter block is silently ignored by the
|
|
13
|
+
* changesets tool, wasting the release entry. Block the write.
|
|
14
|
+
*
|
|
15
|
+
* Behavioral contract preserves the bash hook byte-for-byte:
|
|
16
|
+
*
|
|
17
|
+
* 1. HALT check → exit 2 with shared banner.
|
|
18
|
+
* 2. Tool filter: only `Write`, `Edit`, `MultiEdit`, `NotebookEdit`.
|
|
19
|
+
* Any other tool exits 0.
|
|
20
|
+
* 3. File-path filter: only `.changeset/*.md` files. The
|
|
21
|
+
* `.changeset/README.md` companion is excluded (it's metadata
|
|
22
|
+
* for the changesets tool itself).
|
|
23
|
+
* 4. Security disclosure scan on the resolved content. The
|
|
24
|
+
* ordered pattern list is reproduced verbatim. First match wins;
|
|
25
|
+
* emit the `MATCHED_PATTERN` placeholder.
|
|
26
|
+
* 5. MultiEdit short-circuit for frontmatter: MultiEdit's
|
|
27
|
+
* `edits[].new_string` is a list of replacement FRAGMENTS, not
|
|
28
|
+
* a full file. Running frontmatter validation against the
|
|
29
|
+
* concatenated fragments would reject every legitimate edit.
|
|
30
|
+
* The bash hook added this exemption in 0.15.0; we mirror it.
|
|
31
|
+
* The disclosure scan still runs on the fragments because
|
|
32
|
+
* GHSA/CVE patterns match per-fragment without structural
|
|
33
|
+
* assumption.
|
|
34
|
+
* 6. Frontmatter validation:
|
|
35
|
+
* a. Must start with `---`.
|
|
36
|
+
* b. Must contain at least one `<pkg>: (patch|minor|major)`
|
|
37
|
+
* entry inside the first `---`/`---` block. Accepts
|
|
38
|
+
* single-quoted, double-quoted, and unquoted package
|
|
39
|
+
* names — same explicit alternation form as the bash hook
|
|
40
|
+
* (0.15.0 codex round-1 P2-1 fix).
|
|
41
|
+
* c. Must have a non-empty description after the closing
|
|
42
|
+
* `---`.
|
|
43
|
+
*
|
|
44
|
+
* Block emissions use the Claude Code PreToolUse JSON-on-stdout
|
|
45
|
+
* protocol via `emitJsonBlock`, mirroring `_lib/common.sh::json_output`
|
|
46
|
+
* — JSON on stdout AND the human reason on stderr, exit 2.
|
|
47
|
+
*/
|
|
48
|
+
import { checkHalt, formatHaltBanner } from '../_lib/halt-check.js';
|
|
49
|
+
import { parseWriteHookPayload, MalformedPayloadError, TypePayloadError, readStdinWithTimeout, } from '../_lib/payload.js';
|
|
50
|
+
/**
|
|
51
|
+
* Tool names accepted by this gate. Mirrors the bash hook's
|
|
52
|
+
* `[[ "$TOOL_NAME" != "Write" && ... ]]` chain.
|
|
53
|
+
*/
|
|
54
|
+
const ACCEPTED_TOOLS = new Set([
|
|
55
|
+
'Write',
|
|
56
|
+
'Edit',
|
|
57
|
+
'MultiEdit',
|
|
58
|
+
'NotebookEdit',
|
|
59
|
+
]);
|
|
60
|
+
/**
|
|
61
|
+
* Pattern list for the disclosure scan. Order matters — first match
|
|
62
|
+
* wins, and the matched pattern string lands in the operator banner.
|
|
63
|
+
*/
|
|
64
|
+
const DISCLOSURE_PATTERNS = [
|
|
65
|
+
/GHSA-[0-9A-Za-z]{4}-[0-9A-Za-z]{4}-[0-9A-Za-z]{4}/,
|
|
66
|
+
/CVE-[0-9]{4}-[0-9]+/,
|
|
67
|
+
];
|
|
68
|
+
/**
|
|
69
|
+
* Source strings for the disclosure patterns — these are what the
|
|
70
|
+
* bash hook emitted in its `MATCHED_PATTERN` placeholder so the
|
|
71
|
+
* operator banner matches byte-for-byte.
|
|
72
|
+
*/
|
|
73
|
+
const DISCLOSURE_PATTERN_SOURCES = [
|
|
74
|
+
'GHSA-[0-9A-Za-z]{4}-[0-9A-Za-z]{4}-[0-9A-Za-z]{4}',
|
|
75
|
+
'CVE-[0-9]{4}-[0-9]+',
|
|
76
|
+
];
|
|
77
|
+
/**
|
|
78
|
+
* Frontmatter package-bump line. Accepts:
|
|
79
|
+
* - "@scope/name": patch
|
|
80
|
+
* - '@scope/name': minor
|
|
81
|
+
* - @scope/name : major (unquoted)
|
|
82
|
+
* Mirrors the bash hook's explicit-alternation form (codex P2-1).
|
|
83
|
+
*/
|
|
84
|
+
const FRONTMATTER_BUMP_PATTERN = /^("[^"]+"|'[^']+'|[^"'\s]+): (patch|minor|major)/;
|
|
85
|
+
function emitJsonBlock(reason) {
|
|
86
|
+
const obj = {
|
|
87
|
+
hookSpecificOutput: {
|
|
88
|
+
hookEventName: 'PreToolUse',
|
|
89
|
+
permissionDecision: 'deny',
|
|
90
|
+
permissionDecisionReason: reason,
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
return { json: JSON.stringify(obj) + '\n', stderr: reason + '\n' };
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Test the file path against the `.changeset/*.md` predicate. Mirrors
|
|
97
|
+
* the bash hook's two grep calls:
|
|
98
|
+
* - must match `\.changeset/[^/]+\.md$`
|
|
99
|
+
* - must NOT match `\.changeset/README\.md$`
|
|
100
|
+
*/
|
|
101
|
+
function isChangesetFile(filePath) {
|
|
102
|
+
if (!/\.changeset\/[^/]+\.md$/.test(filePath))
|
|
103
|
+
return false;
|
|
104
|
+
if (/\.changeset\/README\.md$/.test(filePath))
|
|
105
|
+
return false;
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Find the first matching disclosure pattern. Returns the source
|
|
110
|
+
* string (for the operator banner) or `null` when none match.
|
|
111
|
+
*/
|
|
112
|
+
function firstDisclosureMatch(content) {
|
|
113
|
+
for (let i = 0; i < DISCLOSURE_PATTERNS.length; i += 1) {
|
|
114
|
+
const re = DISCLOSURE_PATTERNS[i];
|
|
115
|
+
if (re !== undefined && re.test(content)) {
|
|
116
|
+
return DISCLOSURE_PATTERN_SOURCES[i] ?? null;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Extract the frontmatter block (between the first `---` and the
|
|
123
|
+
* second `---`). Returns the lines BETWEEN those delimiters, NOT
|
|
124
|
+
* including the delimiters themselves. Mirrors the bash hook's
|
|
125
|
+
* `awk '/^---/{count++; if(count==2){exit} next} count==1{print}'`.
|
|
126
|
+
*
|
|
127
|
+
* When the second `---` is missing the function returns whatever was
|
|
128
|
+
* captured after the first `---`; the frontmatter validation regex
|
|
129
|
+
* then fails for lack of a bump entry, exactly as bash awk would.
|
|
130
|
+
*/
|
|
131
|
+
function extractFrontmatter(content) {
|
|
132
|
+
const lines = content.split('\n');
|
|
133
|
+
let dashCount = 0;
|
|
134
|
+
const out = [];
|
|
135
|
+
for (const line of lines) {
|
|
136
|
+
if (/^---/.test(line)) {
|
|
137
|
+
dashCount += 1;
|
|
138
|
+
if (dashCount === 2)
|
|
139
|
+
break;
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
if (dashCount === 1)
|
|
143
|
+
out.push(line);
|
|
144
|
+
}
|
|
145
|
+
return out.join('\n');
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Extract the first non-empty line AFTER the closing `---`. Mirrors
|
|
149
|
+
* the bash hook's `awk 'BEGIN{count=0} /^---/{count++; next} count>=2{print}'
|
|
150
|
+
* | grep -v '^[[:space:]]*$' | head -1`.
|
|
151
|
+
*/
|
|
152
|
+
function extractDescription(content) {
|
|
153
|
+
const lines = content.split('\n');
|
|
154
|
+
let dashCount = 0;
|
|
155
|
+
for (const line of lines) {
|
|
156
|
+
if (/^---/.test(line)) {
|
|
157
|
+
dashCount += 1;
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
if (dashCount < 2)
|
|
161
|
+
continue;
|
|
162
|
+
if (line.trim().length === 0)
|
|
163
|
+
continue;
|
|
164
|
+
return line;
|
|
165
|
+
}
|
|
166
|
+
return '';
|
|
167
|
+
}
|
|
168
|
+
function buildDisclosureBanner(matched) {
|
|
169
|
+
return `CHANGESET SECURITY GATE: This changeset contains a security advisory identifier (matched: '${matched}').
|
|
170
|
+
|
|
171
|
+
Do NOT reference GHSA IDs or CVE numbers in changeset files before the advisory is published.
|
|
172
|
+
Changeset files are committed to git — this creates pre-disclosure in public history and CHANGELOG.
|
|
173
|
+
|
|
174
|
+
CORRECT approach for security fix changesets:
|
|
175
|
+
Use vague language only — no identifiers, no vulnerability details.
|
|
176
|
+
|
|
177
|
+
WRONG: 'fix(hooks): patch GHSA-3w3m-7gg4-f82g — symlink-guard now covers Edit tool'
|
|
178
|
+
RIGHT: 'security: extend symlink protection to cover all write-capable tools'
|
|
179
|
+
|
|
180
|
+
WRONG: 'security: fix CVE-2026-1234 prompt injection via tool descriptions'
|
|
181
|
+
RIGHT: 'security: harden middleware chain against indirect instruction attacks'
|
|
182
|
+
|
|
183
|
+
After the release ships:
|
|
184
|
+
1. Publish the GitHub Security Advisory (Security tab → Advisories → Publish)
|
|
185
|
+
2. The GHSA becomes the detailed public disclosure document
|
|
186
|
+
3. Optionally update CHANGELOG.md post-publish to add the GHSA reference`;
|
|
187
|
+
}
|
|
188
|
+
const MISSING_FRONTMATTER_BANNER = `CHANGESET FORMAT GATE: Missing frontmatter block.
|
|
189
|
+
|
|
190
|
+
Every changeset must start with a frontmatter block specifying which package to bump:
|
|
191
|
+
|
|
192
|
+
---
|
|
193
|
+
'@bookedsolid/rea': patch
|
|
194
|
+
---
|
|
195
|
+
|
|
196
|
+
Brief description of what changed and why (close #N if applicable).
|
|
197
|
+
|
|
198
|
+
Bump types: patch (bug fix/security), minor (new feature), major (breaking change)`;
|
|
199
|
+
const INVALID_FRONTMATTER_BANNER = `CHANGESET FORMAT GATE: Frontmatter does not contain a valid package bump entry.
|
|
200
|
+
|
|
201
|
+
The frontmatter must include at least one package/bump pair:
|
|
202
|
+
|
|
203
|
+
---
|
|
204
|
+
'@bookedsolid/rea': patch
|
|
205
|
+
---
|
|
206
|
+
|
|
207
|
+
Valid bump types: patch | minor | major`;
|
|
208
|
+
const MISSING_DESCRIPTION_BANNER = `CHANGESET FORMAT GATE: Missing description after frontmatter.
|
|
209
|
+
|
|
210
|
+
Add a meaningful description explaining what changed and why:
|
|
211
|
+
|
|
212
|
+
---
|
|
213
|
+
'@bookedsolid/rea': patch
|
|
214
|
+
---
|
|
215
|
+
|
|
216
|
+
fix(gateway): policy-loader now uses async I/O with 500ms TTL cache
|
|
217
|
+
|
|
218
|
+
Previously, loadPolicy used fs.readFileSync on every tool invocation, blocking
|
|
219
|
+
the event loop under concurrency. Closes #34.`;
|
|
220
|
+
export async function runChangesetSecurityGate(options = {}) {
|
|
221
|
+
const reaRoot = options.reaRoot ?? process.env['CLAUDE_PROJECT_DIR'] ?? process.cwd();
|
|
222
|
+
let stderr = '';
|
|
223
|
+
let stdout = '';
|
|
224
|
+
const writeStderr = (s) => {
|
|
225
|
+
stderr += s;
|
|
226
|
+
if (options.stderrWrite)
|
|
227
|
+
options.stderrWrite(s);
|
|
228
|
+
};
|
|
229
|
+
const writeStdout = (s) => {
|
|
230
|
+
stdout += s;
|
|
231
|
+
if (options.stdoutWrite)
|
|
232
|
+
options.stdoutWrite(s);
|
|
233
|
+
};
|
|
234
|
+
// 1. HALT.
|
|
235
|
+
const halt = checkHalt(reaRoot);
|
|
236
|
+
if (halt.halted) {
|
|
237
|
+
writeStderr(formatHaltBanner(halt.reason));
|
|
238
|
+
return { exitCode: 2, stderr, stdout };
|
|
239
|
+
}
|
|
240
|
+
// 2. Stdin.
|
|
241
|
+
const stdinRaw = options.stdinOverride !== undefined
|
|
242
|
+
? options.stdinOverride
|
|
243
|
+
: await readStdinWithTimeout(5_000);
|
|
244
|
+
let toolName = '';
|
|
245
|
+
let filePath = '';
|
|
246
|
+
let content = '';
|
|
247
|
+
try {
|
|
248
|
+
const payload = parseWriteHookPayload(stdinRaw);
|
|
249
|
+
toolName = payload.toolName;
|
|
250
|
+
filePath = payload.filePath;
|
|
251
|
+
content = payload.content;
|
|
252
|
+
}
|
|
253
|
+
catch (err) {
|
|
254
|
+
if (err instanceof MalformedPayloadError || err instanceof TypePayloadError) {
|
|
255
|
+
writeStderr(`changeset-security-gate: ${err.message} — refusing on uncertainty.\n`);
|
|
256
|
+
return { exitCode: 2, stderr, stdout };
|
|
257
|
+
}
|
|
258
|
+
throw err;
|
|
259
|
+
}
|
|
260
|
+
// 3. Tool filter.
|
|
261
|
+
if (toolName !== '' && !ACCEPTED_TOOLS.has(toolName)) {
|
|
262
|
+
return { exitCode: 0, stderr, stdout };
|
|
263
|
+
}
|
|
264
|
+
// 4. Path filter.
|
|
265
|
+
if (filePath.length === 0 || !isChangesetFile(filePath)) {
|
|
266
|
+
return { exitCode: 0, stderr, stdout };
|
|
267
|
+
}
|
|
268
|
+
// 5. Disclosure scan (runs for ALL accepted tools incl. MultiEdit).
|
|
269
|
+
const matched = firstDisclosureMatch(content);
|
|
270
|
+
if (matched !== null) {
|
|
271
|
+
const out = emitJsonBlock(buildDisclosureBanner(matched));
|
|
272
|
+
writeStdout(out.json);
|
|
273
|
+
writeStderr(out.stderr);
|
|
274
|
+
return { exitCode: 2, stderr, stdout };
|
|
275
|
+
}
|
|
276
|
+
// 6. MultiEdit short-circuit for frontmatter validation. The bash
|
|
277
|
+
// hook exits 0 here — the disclosure scan above is the only
|
|
278
|
+
// enforcement for fragment-style writes.
|
|
279
|
+
if (toolName === 'MultiEdit') {
|
|
280
|
+
return { exitCode: 0, stderr, stdout };
|
|
281
|
+
}
|
|
282
|
+
// 7. Frontmatter validation.
|
|
283
|
+
const firstLine = content.split('\n', 1)[0] ?? '';
|
|
284
|
+
if (!/^---/.test(firstLine)) {
|
|
285
|
+
const out = emitJsonBlock(MISSING_FRONTMATTER_BANNER);
|
|
286
|
+
writeStdout(out.json);
|
|
287
|
+
writeStderr(out.stderr);
|
|
288
|
+
return { exitCode: 2, stderr, stdout };
|
|
289
|
+
}
|
|
290
|
+
const frontmatter = extractFrontmatter(content);
|
|
291
|
+
let hasBump = false;
|
|
292
|
+
for (const line of frontmatter.split('\n')) {
|
|
293
|
+
if (FRONTMATTER_BUMP_PATTERN.test(line)) {
|
|
294
|
+
hasBump = true;
|
|
295
|
+
break;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
if (!hasBump) {
|
|
299
|
+
const out = emitJsonBlock(INVALID_FRONTMATTER_BANNER);
|
|
300
|
+
writeStdout(out.json);
|
|
301
|
+
writeStderr(out.stderr);
|
|
302
|
+
return { exitCode: 2, stderr, stdout };
|
|
303
|
+
}
|
|
304
|
+
const description = extractDescription(content);
|
|
305
|
+
if (description.length === 0) {
|
|
306
|
+
const out = emitJsonBlock(MISSING_DESCRIPTION_BANNER);
|
|
307
|
+
writeStdout(out.json);
|
|
308
|
+
writeStderr(out.stderr);
|
|
309
|
+
return { exitCode: 2, stderr, stdout };
|
|
310
|
+
}
|
|
311
|
+
return { exitCode: 0, stderr, stdout };
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* CLI entry — `rea hook changeset-security-gate`.
|
|
315
|
+
*/
|
|
316
|
+
export async function runHookChangesetSecurityGate(options = {}) {
|
|
317
|
+
const result = await runChangesetSecurityGate({
|
|
318
|
+
...options,
|
|
319
|
+
stderrWrite: (s) => process.stderr.write(s),
|
|
320
|
+
stdoutWrite: (s) => process.stdout.write(s),
|
|
321
|
+
});
|
|
322
|
+
process.exit(result.exitCode);
|
|
323
|
+
}
|
|
324
|
+
export const __INTERNAL_DISCLOSURE_PATTERNS_FOR_TESTS = DISCLOSURE_PATTERN_SOURCES;
|
|
325
|
+
export const __INTERNAL_FRONTMATTER_PATTERN_FOR_TESTS = FRONTMATTER_BUMP_PATTERN;
|
|
326
|
+
export const __INTERNAL_BANNERS_FOR_TESTS = {
|
|
327
|
+
MISSING_FRONTMATTER_BANNER,
|
|
328
|
+
INVALID_FRONTMATTER_BANNER,
|
|
329
|
+
MISSING_DESCRIPTION_BANNER,
|
|
330
|
+
};
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node-binary port of `hooks/dangerous-bash-interceptor.sh`.
|
|
3
|
+
*
|
|
4
|
+
* 0.34.0 Phase 2 port #1 (tier-2 medium-complexity hooks with enforcer
|
|
5
|
+
* logic). This is the agent-runaway gate — it refuses destructive Bash
|
|
6
|
+
* commands before Claude Code dispatches them. Every refusal class in
|
|
7
|
+
* the 414-LOC bash body must be preserved byte-for-byte; the bypass
|
|
8
|
+
* corpus pinned across 0.13–0.27 demands it.
|
|
9
|
+
*
|
|
10
|
+
* Behavioral contract — preserves the bash hook byte-for-byte:
|
|
11
|
+
*
|
|
12
|
+
* 1. HALT check → exit 2 with the shared banner.
|
|
13
|
+
* 2. Read stdin, extract `tool_input.command`. Non-Bash payloads or
|
|
14
|
+
* empty command → exit 0.
|
|
15
|
+
* 3. Compute smart exclusion flags:
|
|
16
|
+
* - `CMD_IS_REBASE_SAFE` → segments that begin with
|
|
17
|
+
* `git rebase --abort|--continue` skip the H2 rebase advisory.
|
|
18
|
+
* - `CMD_IS_CLEAN_DRY` → segments that begin with
|
|
19
|
+
* `git clean -n|--dry-run` skip the H5 destructive-clean check.
|
|
20
|
+
* 4. Run every HIGH check (H1–H17, M1) against the command. Each
|
|
21
|
+
* check returns 0..N matches; matches are accumulated into the
|
|
22
|
+
* violations table. The accumulator preserves the original bash
|
|
23
|
+
* hook's first-match-wins-per-check semantics — H1 fires once
|
|
24
|
+
* per command even if multiple push segments are unsafe.
|
|
25
|
+
* 5. If any HIGH match → emit "BASH INTERCEPTED" banner + exit 2.
|
|
26
|
+
* Else if MEDIUM-only → emit "BASH ADVISORY" banner + exit 0.
|
|
27
|
+
* Else exit 0 silently.
|
|
28
|
+
*
|
|
29
|
+
* The pattern catalog is in `RULES` below. Each rule is a self-
|
|
30
|
+
* contained closure with a stable identifier (`H1`, `H2`, …) so a
|
|
31
|
+
* future rule addition lands as a one-line array push, not a rewrite.
|
|
32
|
+
* Identifiers match the bash hook's `add_high "H<N>: …"` shape so
|
|
33
|
+
* audit/log consumers grepping for `H12` continue to work.
|
|
34
|
+
*
|
|
35
|
+
* Key parity choices:
|
|
36
|
+
*
|
|
37
|
+
* - Segment-anchored detection via `anySegmentStartsWith` (and
|
|
38
|
+
* `forEachSegment` for per-segment work). The bash 0.15.0 fix
|
|
39
|
+
* (segment-aware instead of full-command grep) is reproduced here.
|
|
40
|
+
* - Env-var-prefix shapes (H10 `HUSKY=0 git`, H15 `REA_BYPASS=…`,
|
|
41
|
+
* H16 alias/function defs) use `anySegmentRawMatches` since the
|
|
42
|
+
* prefix IS the signal — `stripSegmentPrefix` would eat it.
|
|
43
|
+
* - H12 (`curl|sh` pipe-RCE) scans the whole command via
|
|
44
|
+
* `quoteMaskedCmd` because pipe-RCE is a multi-segment property
|
|
45
|
+
* (`|` is the separator that joins curl to sh). The bash hook's
|
|
46
|
+
* `_rea_unwrap_nested_shells` is mirrored via `unwrapNestedShells`
|
|
47
|
+
* so inner payloads of `bash -c "curl … | sh"` are also scanned.
|
|
48
|
+
* - H17 (context-protection) reads
|
|
49
|
+
* `policy.context_protection.delegate_to_subagent` via the canonical
|
|
50
|
+
* YAML loader (matches the bash hook's 0.16.0 fix J.2).
|
|
51
|
+
*/
|
|
52
|
+
import type { Buffer } from 'node:buffer';
|
|
53
|
+
export interface DangerousBashOptions {
|
|
54
|
+
reaRoot?: string;
|
|
55
|
+
stdinOverride?: string | Buffer;
|
|
56
|
+
stderrWrite?: (s: string) => void;
|
|
57
|
+
}
|
|
58
|
+
export interface DangerousBashResult {
|
|
59
|
+
exitCode: number;
|
|
60
|
+
stderr: string;
|
|
61
|
+
/** Test seam — violations the run accumulated, in catalog order. */
|
|
62
|
+
violations: Violation[];
|
|
63
|
+
}
|
|
64
|
+
export interface Violation {
|
|
65
|
+
severity: 'HIGH' | 'MEDIUM';
|
|
66
|
+
/** Stable identifier (`H1`, `H10`, `M1`, …) — matches bash labels. */
|
|
67
|
+
id: string;
|
|
68
|
+
/** Banner headline. */
|
|
69
|
+
label: string;
|
|
70
|
+
/** Banner explanation paragraph. */
|
|
71
|
+
detail: string;
|
|
72
|
+
/** Suggested alternatives. */
|
|
73
|
+
alternatives: string[];
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Rule descriptor + execution closure. The closure receives the raw
|
|
77
|
+
* command + the active exclusion flags and returns 0..N violations
|
|
78
|
+
* for that rule.
|
|
79
|
+
*/
|
|
80
|
+
interface RuleContext {
|
|
81
|
+
cmd: string;
|
|
82
|
+
cmdIsRebaseSafe: boolean;
|
|
83
|
+
cmdIsCleanDry: boolean;
|
|
84
|
+
delegatePatterns: string[];
|
|
85
|
+
}
|
|
86
|
+
interface Rule {
|
|
87
|
+
id: string;
|
|
88
|
+
severity: 'HIGH' | 'MEDIUM';
|
|
89
|
+
run: (ctx: RuleContext) => Violation[];
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Pure executor. Returns `{ exitCode, stderr, violations }`; the CLI
|
|
93
|
+
* wrapper translates them into `process.stderr.write` + `process.exit`.
|
|
94
|
+
*/
|
|
95
|
+
export declare function runDangerousBashInterceptor(options?: DangerousBashOptions): Promise<DangerousBashResult>;
|
|
96
|
+
/**
|
|
97
|
+
* CLI entry point — `rea hook dangerous-bash-interceptor`.
|
|
98
|
+
*/
|
|
99
|
+
export declare function runHookDangerousBashInterceptor(options?: DangerousBashOptions): Promise<void>;
|
|
100
|
+
export declare const __INTERNAL_FOR_TESTS: {
|
|
101
|
+
RULES: readonly Rule[];
|
|
102
|
+
};
|
|
103
|
+
export {};
|