@skathio/somi 1.1.1 → 2.0.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/.claude/settings.json +8 -8
- package/.claude-plugin/marketplace.json +1 -1
- package/.claude-plugin/plugin.json +1 -1
- package/.copilot-extension/extension.json +11 -2
- package/.copilot-extension/marketplace.json +1 -1
- package/README.md +21 -4
- package/VERSION +1 -1
- package/agents/coder.md +8 -3
- package/agents/designer.md +22 -7
- package/agents/discovery-analyst.md +11 -0
- package/agents/planner.md +69 -9
- package/agents/refactorer.md +3 -1
- package/agents/reviewer.md +4 -0
- package/commands/adopt.md +77 -0
- package/commands/atlas.md +83 -0
- package/commands/code-loop.md +70 -44
- package/commands/code-parallel.md +3 -1
- package/commands/code.md +8 -2
- package/commands/debug.md +136 -0
- package/commands/design.md +17 -7
- package/commands/discover.md +23 -18
- package/commands/impact.md +74 -0
- package/commands/incident.md +83 -0
- package/commands/plan-loop.md +44 -19
- package/commands/plan.md +38 -26
- package/commands/pr.md +82 -0
- package/commands/release-readiness.md +70 -0
- package/commands/review-panel.md +5 -0
- package/commands/review.md +19 -0
- package/commands/ship-loop.md +18 -10
- package/commands/ship.md +4 -2
- package/commands/somi.md +85 -0
- package/commands/upgrade.md +81 -0
- package/hooks/hooks.json +8 -8
- package/hooks/lib/common.mjs +328 -0
- package/hooks/post-tool/audit-log.mjs +93 -0
- package/hooks/post-tool/lint-changed-files.mjs +212 -0
- package/hooks/pre-tool/block-dangerous-bash.mjs +382 -0
- package/hooks/pre-tool/block-secret-writes.mjs +96 -0
- package/hooks/pre-tool/gate-dep-install.mjs +229 -0
- package/hooks/pre-tool/guard-protected-paths.mjs +157 -0
- package/hooks/session-start/detect-repo-instructions.mjs +264 -0
- package/hooks/user-prompt-submit/inject-workflow-context.mjs +432 -0
- package/package.json +6 -1
- package/rules/50-collaboration.md +4 -1
- package/scripts/somi-check.mjs +243 -0
- package/scripts/somi-findings.mjs +307 -0
- package/scripts/somi-loop.mjs +354 -0
- package/scripts/validate.sh +120 -0
- package/skills/clean-code/SKILL.md +1 -1
- package/templates/ATLAS.md.tmpl +75 -0
- package/templates/BRIEF.md.tmpl +20 -4
- package/templates/DOD.md.tmpl +2 -2
- package/templates/PHASE.md.tmpl +1 -1
- package/templates/RCA.md.tmpl +53 -0
- package/templates/RD-README.md.tmpl +2 -2
- package/templates/SDD.md.tmpl +4 -4
- package/templates/SOMI-README.md.tmpl +20 -3
- package/templates/SRS.md.tmpl +1 -1
- package/templates/TDD.md.tmpl +5 -5
- package/tests/hooks/cases/audit-log.json +21 -0
- package/tests/hooks/cases/block-dangerous-bash.json +166 -0
- package/tests/hooks/cases/block-secret-writes.json +22 -0
- package/tests/hooks/cases/detect-repo-instructions.json +84 -0
- package/tests/hooks/cases/gate-dep-install.json +42 -0
- package/tests/hooks/cases/guard-protected-paths.json +30 -0
- package/tests/hooks/cases/inject-workflow-context.json +126 -0
- package/tests/hooks/cases/lint-changed-files.json +120 -0
- package/tests/hooks/run.sh +362 -0
- package/tests/scripts/goldens/somi-findings.json +336 -0
- package/tests/scripts/goldens/somi-loop.json +168 -0
- package/tests/scripts/run.sh +389 -0
- package/hooks/lib/common.sh +0 -120
- package/hooks/post-tool/audit-log.sh +0 -40
- package/hooks/post-tool/lint-changed-files.sh +0 -80
- package/hooks/pre-tool/block-dangerous-bash.sh +0 -83
- package/hooks/pre-tool/block-secret-writes.sh +0 -83
- package/hooks/pre-tool/gate-dep-install.sh +0 -72
- package/hooks/pre-tool/guard-protected-paths.sh +0 -74
- package/hooks/session-start/detect-repo-instructions.sh +0 -73
- package/hooks/user-prompt-submit/inject-workflow-context.sh +0 -143
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// hooks/pre-tool/block-secret-writes.mjs — PreToolUse hook (matcher: Write|Edit) — block
|
|
3
|
+
// writes to secret-bearing paths.
|
|
4
|
+
//
|
|
5
|
+
// Node port of hooks/pre-tool/block-secret-writes.sh (node-runtime-port, phase 2,
|
|
6
|
+
// iteration 2.2). Imports the shared read/deny/context helpers from ../lib/common.mjs
|
|
7
|
+
// (2.1, reviewer-blessed) rather than reimplementing them.
|
|
8
|
+
//
|
|
9
|
+
// We block any attempt to write/edit files that are likely to hold real secrets. If the
|
|
10
|
+
// user genuinely needs to bootstrap a `.env`, they can do it themselves or explicitly add
|
|
11
|
+
// an override in their settings.local.json.
|
|
12
|
+
//
|
|
13
|
+
// SECRET_PATTERNS mirrors block-dangerous-bash.sh's SECRET_BASENAME set
|
|
14
|
+
// (block-dangerous-bash.sh:86) — "kept in sync by comment convention, not code sharing"
|
|
15
|
+
// today (context.md §2). Preserved as a deliberate duplication in this port; do not
|
|
16
|
+
// unify the two lists — that would be a scope-creeping refactor beyond this
|
|
17
|
+
// behavior-preserving port (flagged as a future-refactor target, same as today).
|
|
18
|
+
//
|
|
19
|
+
// ERE→RegExp translation note: every pattern below is basename-only, pure-ASCII, and
|
|
20
|
+
// uses only anchors, escaped literal dots, and `?`/`*` quantifiers (plus one bare `.`
|
|
21
|
+
// wildcard in the service-account pattern) — a mechanical, one-to-one translation, with
|
|
22
|
+
// no POSIX bracket-expression classes to convert. The `.`-semantics caveat that matters
|
|
23
|
+
// for block-dangerous-bash.sh's patterns (POSIX ERE `.` matches every byte but `\n`; JS
|
|
24
|
+
// `.` additionally refuses to match `\r`/U+2028/U+2029) is scoped to matching against
|
|
25
|
+
// raw COMMAND-STRING content — irrelevant here, since every pattern below matches a
|
|
26
|
+
// normalized file-path BASENAME extracted from the JSON payload, never raw content, and
|
|
27
|
+
// no realistic basename contains a line-terminator character for that divergence to bite.
|
|
28
|
+
// One platform sensitivity 2.3+ must inherit knowingly: path.basename() is posix-vs-win32
|
|
29
|
+
// separator-aware (on Windows, `C:\Users\me\.env` → `.env` → DENY, where bash basename on
|
|
30
|
+
// Linux treats `\` as a regular character → allow). For THIS hook the Windows behavior is
|
|
31
|
+
// strictly safer; for path-heavier hooks (guard-protected-paths) the same divergence could
|
|
32
|
+
// cut the unsafe way. The Linux fixture corpus validates only the posix axis.
|
|
33
|
+
|
|
34
|
+
import path from 'node:path';
|
|
35
|
+
import { readPayload, field, denyPretool, matchesAny, runHook } from '../lib/common.mjs';
|
|
36
|
+
|
|
37
|
+
const SECRET_PATTERNS = [
|
|
38
|
+
// env files (allow .env.example, .env.sample, .env.template explicitly)
|
|
39
|
+
/^\.env$/,
|
|
40
|
+
/^\.env\.local$/,
|
|
41
|
+
/^\.env\.production$/,
|
|
42
|
+
/^\.env\.prod$/,
|
|
43
|
+
/^\.env\.staging$/,
|
|
44
|
+
/^\.env\.secret$/,
|
|
45
|
+
|
|
46
|
+
// private keys and certs
|
|
47
|
+
/\.pem$/,
|
|
48
|
+
/\.key$/,
|
|
49
|
+
/\.p12$/,
|
|
50
|
+
/\.pfx$/,
|
|
51
|
+
/\.jks$/,
|
|
52
|
+
/^id_rsa$/,
|
|
53
|
+
/^id_ed25519$/,
|
|
54
|
+
/^id_ecdsa$/,
|
|
55
|
+
/^id_dsa$/,
|
|
56
|
+
|
|
57
|
+
// cloud credentials
|
|
58
|
+
/^credentials$/, // ~/.aws/credentials
|
|
59
|
+
/^config$/, // ~/.aws/config (usually contains profile refs but no secrets)
|
|
60
|
+
|
|
61
|
+
// service-account keys
|
|
62
|
+
/-key\.json$/,
|
|
63
|
+
/-credentials\.json$/,
|
|
64
|
+
/service-account.*\.json$/,
|
|
65
|
+
|
|
66
|
+
// shell rc files (may contain export STATEMENTS with secrets)
|
|
67
|
+
/\.netrc$/,
|
|
68
|
+
/\.pgpass$/,
|
|
69
|
+
|
|
70
|
+
// vault / secret tool files
|
|
71
|
+
/\.kdbx$/,
|
|
72
|
+
/secrets?\.ya?ml$/,
|
|
73
|
+
/secrets?\.json$/,
|
|
74
|
+
];
|
|
75
|
+
|
|
76
|
+
// Allow explicit example/template files.
|
|
77
|
+
const EXAMPLE_BASENAMES = ['.env.example', '.env.sample', '.env.template', '.env.dist'];
|
|
78
|
+
|
|
79
|
+
function main() {
|
|
80
|
+
const payload = readPayload();
|
|
81
|
+
const tool = field(payload, '.tool_name');
|
|
82
|
+
const pathInput = field(payload, '.tool_input.file_path');
|
|
83
|
+
if (!pathInput) return;
|
|
84
|
+
|
|
85
|
+
// Normalise to basename for pattern matching.
|
|
86
|
+
const basename = path.basename(pathInput);
|
|
87
|
+
|
|
88
|
+
if (EXAMPLE_BASENAMES.includes(basename)) return;
|
|
89
|
+
|
|
90
|
+
if (matchesAny(basename, SECRET_PATTERNS)) {
|
|
91
|
+
denyPretool(payload, `somi refused to ${tool} \`${pathInput}\`: this path is in the secret-bearing allowlist.
|
|
92
|
+
Bootstrap secret files by hand, or commit only \`.env.example\`-style templates.`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
runHook(main);
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// hooks/pre-tool/gate-dep-install.mjs — PreToolUse hook (matcher: Bash) — gate
|
|
3
|
+
// dependency-adding commands.
|
|
4
|
+
//
|
|
5
|
+
// Node port of hooks/pre-tool/gate-dep-install.sh (node-runtime-port, phase 2,
|
|
6
|
+
// iteration 2.4). Imports the shared read/deny/audit/config helpers from
|
|
7
|
+
// ../lib/common.mjs (2.1, reviewer-blessed) rather than reimplementing them.
|
|
8
|
+
//
|
|
9
|
+
// Adding a runtime dependency crosses a trust boundary: it imports unreviewed code,
|
|
10
|
+
// expands attack surface, and creates a long-term maintenance obligation. The coder
|
|
11
|
+
// should not add deps as a drive-by — they belong in `decisions.md` or at minimum in
|
|
12
|
+
// the iteration summary so a human can sign off.
|
|
13
|
+
//
|
|
14
|
+
// This hook denies adding a *new* dep without an explicit acknowledgement
|
|
15
|
+
// (SOMI_ALLOW_DEP_INSTALL=1 in the env, set by the human for the session). Lockfile-
|
|
16
|
+
// respecting reinstalls (bare `npm install`, `yarn install`, etc.) are allowed — those
|
|
17
|
+
// don't add deps, they materialize what's already declared.
|
|
18
|
+
//
|
|
19
|
+
// COUNT CORRECTION (verified by direct read, not inherited from the phase file's
|
|
20
|
+
// prose): the bash original's DEP_ADD_PATTERNS array holds 19 patterns, not 18 — and
|
|
21
|
+
// the trailing "generic curl-to-installer" trio (brew/apt/apt-get) sits outside the
|
|
22
|
+
// six named package-manager ecosystems the file's own comments group (js: npm/yarn/
|
|
23
|
+
// pnpm/bun; python: pip/pipx/uv/poetry/conda; rust/go/ruby/php: cargo/go/gem/bundle/
|
|
24
|
+
// composer). All 19 are ported below — "port ALL of the patterns" wins over an
|
|
25
|
+
// inherited miscount; the phase file's iteration-2.4 scope line is corrected
|
|
26
|
+
// alongside this port (see the diary entry for this pass).
|
|
27
|
+
//
|
|
28
|
+
// ERE→RegExp translation notes:
|
|
29
|
+
// - `[[:space:]]` → `\s`. NOT byte-identical: POSIX `[[:space:]]` matches exactly
|
|
30
|
+
// the 6 ASCII whitespace bytes (space/tab/newline/CR/FF/VT); JS `\s` matches those
|
|
31
|
+
// same 6 PLUS additional Unicode whitespace (U+00A0, U+2028, U+2029, U+FEFF, and
|
|
32
|
+
// other Unicode Zs-category characters) — a strict SUPERSET, not the dot-narrowing
|
|
33
|
+
// divergence 2.9 has to worry about. Direction matters: a wider `\s` can only make
|
|
34
|
+
// this hook MATCH (and therefore gate) a *broader* set of verb/package-argument
|
|
35
|
+
// separators than bash would — the safe direction for a security-adjacent gate.
|
|
36
|
+
// The widening DOES produce observable fail-closed decision divergences on some
|
|
37
|
+
// Unicode-whitespace separators — e.g. `npm<NBSP>install x`: bash ALLOWS (NBSP is
|
|
38
|
+
// not [[:space:]], so no pattern matches), this port DENIES (NBSP is JS \s). Always
|
|
39
|
+
// over-gating, never under-gating (verified in review: backtracking prevents the
|
|
40
|
+
// reverse direction), and inert-to-security: every diverging input is non-functional
|
|
41
|
+
// as a real shell command anyway (bash IFS only splits ASCII whitespace, so
|
|
42
|
+
// `npm<NBSP>install` is a single nonexistent command token — nothing denied here
|
|
43
|
+
// would have worked as an unblocked command downstream).
|
|
44
|
+
// - `[^[:space:]]` → `[^\s]`: same superset relationship, applied to the negated
|
|
45
|
+
// class (excludes slightly more from "package token" chars) — same safe direction.
|
|
46
|
+
// - Bare/unescaped `.` or `.*` wildcard (the live POSIX-vs-JS `.` line-terminator
|
|
47
|
+
// divergence that matters for block-dangerous-bash.sh, 2.9): NONE of this file's
|
|
48
|
+
// 19 patterns use one. The only `.` occurrences are the escaped literal
|
|
49
|
+
// `\.txt` in the `pip install -r ...txt` pattern — an escaped dot means "literal
|
|
50
|
+
// dot", identical in ERE and JS regex syntax, so there is no line-terminator
|
|
51
|
+
// divergence to translate here. Checked every pattern by hand, not assumed.
|
|
52
|
+
// - Everything else (`+`, `*`, `?`, alternation groups, negated single-char classes
|
|
53
|
+
// `[^-]`) is byte-identical ERE/JS syntax — a mechanical, pattern-by-pattern port.
|
|
54
|
+
// - Matching is UNANCHORED substring search in both bash (`[[ "$CMD" =~ $pattern ]]`)
|
|
55
|
+
// and here (`RegExp.prototype.exec` with no `^`/`$`), and case-sensitive in both
|
|
56
|
+
// (no `shopt -s nocasematch` in the bash original for this pattern set) — no
|
|
57
|
+
// nocase wrapper needed.
|
|
58
|
+
//
|
|
59
|
+
// FIRST-MATCH-WINS / single-decision semantics: bash's per-pattern loop body ends in
|
|
60
|
+
// either `exit 0` (ALLOW, audited) or `somi::deny_pretool` (which itself calls
|
|
61
|
+
// `exit 0` — see common.sh:89) — so only the FIRST pattern to match the command is
|
|
62
|
+
// ever evaluated; the loop never reaches a second pattern. `matchesAny()` (2.1)
|
|
63
|
+
// returns the first-matching pattern's `RegExpExecArray` in array order, reproducing
|
|
64
|
+
// this exactly, and `m[0]` is the `${BASH_REMATCH[0]}` equivalent interpolated into
|
|
65
|
+
// the deny message.
|
|
66
|
+
//
|
|
67
|
+
// SOMI_ALLOW_DEP_INSTALL exact-value semantics: bash's `"${SOMI_ALLOW_DEP_INSTALL:-0}"
|
|
68
|
+
// == "1"` uses `:-`, which substitutes the default "0" for BOTH unset AND
|
|
69
|
+
// explicitly-empty ("") — so unset, "", and "0" are all NOT-opted-in, and every other
|
|
70
|
+
// string value (e.g. "true", "yes") is also NOT-opted-in; only the literal "1" opts
|
|
71
|
+
// in. `process.env.SOMI_ALLOW_DEP_INSTALL === '1'` below is an exact behavioral
|
|
72
|
+
// equivalent of that comparison (not merely a shortcut): for every value other than
|
|
73
|
+
// literal "1", both the bash substitution-then-compare and the direct JS comparison
|
|
74
|
+
// land on the same false/true outcome — verified case-by-case, not assumed.
|
|
75
|
+
//
|
|
76
|
+
// COMPOUND-COMMAND REFUSAL (config_allows_dep → configAllowsDep, the security-relevant
|
|
77
|
+
// detail this iteration was told to preserve exactly): the allowlist prefix-match only
|
|
78
|
+
// ever applies to the RAW, unmodified `$CMD`/`cmd` string — bash tests
|
|
79
|
+
// `[[ "$c" == *';'* || "$c" == *'&'* || "$c" == *'|'* ]]` against the WHOLE command
|
|
80
|
+
// text (not just the matched substring) and refuses the allow (returns 1 / false) if
|
|
81
|
+
// ANY of those three characters appears ANYWHERE in it — a single `&` (not just `&&`)
|
|
82
|
+
// or single `|` (not just `||`) is enough to refuse, same as bash's glob-style `*x*`
|
|
83
|
+
// substring test. Ported as three `.includes()` checks against the same raw string,
|
|
84
|
+
// in the same order, before any tokenization happens — not simplified to a regex or a
|
|
85
|
+
// "look compound" heuristic.
|
|
86
|
+
//
|
|
87
|
+
// TOKENIZATION judgment call (named per this iteration's "check exactly what bash
|
|
88
|
+
// checks" instruction, not a silent default): bash's `for tok in $c` performs BOTH
|
|
89
|
+
// IFS word-splitting AND pathname (glob) expansion on the unquoted `$c` — a token
|
|
90
|
+
// containing `*`/`?`/`[...]` could silently expand against files in the hook's cwd,
|
|
91
|
+
// making the allowlist check's actual token set depend on the filesystem at hook-run
|
|
92
|
+
// time. That side effect is not reproduced here: it would make the gate's outcome
|
|
93
|
+
// non-deterministic and cwd-dependent, a strictly worse property for a security
|
|
94
|
+
// control, and no realistic package-name argument or fixture case exercises it.
|
|
95
|
+
// `tokensAfterVerb()` below reproduces the WORD-SPLITTING half only (split on runs of
|
|
96
|
+
// whitespace), which is the behavior this hook's allowlist logic actually depends on.
|
|
97
|
+
//
|
|
98
|
+
// DISCOVERED DIVERGENCE, decided explicitly (differential-probe finding, not a silent
|
|
99
|
+
// default — same category of call as 2.7's prune-list decision and 2.8's regex-
|
|
100
|
+
// widening decision): the bash original's `${BASH_REMATCH[0]}` interpolation in its
|
|
101
|
+
// deny message is CORRUPTED whenever `.somi/config.json`'s `dep_install.allow` is
|
|
102
|
+
// configured (non-empty) and the command is denied via a non-compound path. Root
|
|
103
|
+
// cause, confirmed by isolated repro: `$BASH_REMATCH` is a bash GLOBAL, not scoped to
|
|
104
|
+
// the `[[ "$CMD" =~ $pattern ]]` test that finds the real match — `config_allows_dep`'s
|
|
105
|
+
// own internal `[[ "$tok" =~ ^(install|i|add|get|require)$ ]]` verb-detection test
|
|
106
|
+
// (run once `allow` is non-empty and the command isn't compound, i.e. on every
|
|
107
|
+
// allow-list-configured deny that reaches the token loop) OVERWRITES it as a side
|
|
108
|
+
// effect. By the time `somi::deny_pretool` reads `${BASH_REMATCH[0]}`, it holds the
|
|
109
|
+
// bare verb word ("install"/"i"/"add"/"get"/"require") from that internal test, not
|
|
110
|
+
// the actual matched command — e.g. `npm install lodash` denied under
|
|
111
|
+
// `{"dep_install":{"allow":["@types/"]}}` shows `` `install` `` in the bash original's
|
|
112
|
+
// message, not `` `npm install lodash` ``. Verified with a standalone bash repro
|
|
113
|
+
// (isolated from the hook, tracing `$BASH_REMATCH` before/after the verb loop) before
|
|
114
|
+
// concluding this, not inferred. The COMPOUND-command path is unaffected (its checks
|
|
115
|
+
// use `[[ == ]]` glob comparisons, never `=~`, so it returns before ever touching
|
|
116
|
+
// `$BASH_REMATCH` — confirmed by differential probe, zero divergence there).
|
|
117
|
+
// This corrupted text is NOT reproduced here. Reasons: (1) no fixture in
|
|
118
|
+
// `tests/hooks/cases/gate-dep-install.json` asserts `expect_reason` at all, so no
|
|
119
|
+
// pinned contract is broken; (2) D2's behavior-preservation contract is scoped to
|
|
120
|
+
// "identical exit codes and stdout SHAPES" (decisions.md D2) — the shape is identical,
|
|
121
|
+
// only this one interpolated substring's content differs, and only on an untested
|
|
122
|
+
// path; (3) reproducing it would mean deliberately shipping a message that names the
|
|
123
|
+
// wrong thing as "the dependency" in a security-relevant, audit-logged, user-facing
|
|
124
|
+
// deny reason — strictly worse for the human/agent reading it, not a feature. This
|
|
125
|
+
// port always interpolates the REAL `DEP_ADD_PATTERNS` match (`m[0]` from the outer
|
|
126
|
+
// `matchesAny` call, captured once and never touched by `configAllowsDep`), for every
|
|
127
|
+
// deny path, allow-list-configured or not. Flagged here and in the diary for explicit
|
|
128
|
+
// reviewer sign-off, per this iteration's "verify precisely, do not simplify or
|
|
129
|
+
// improve" instruction for `config_allows_dep` — this is the one place a literal
|
|
130
|
+
// bug-for-bug port was deliberately NOT chosen, named rather than silent.
|
|
131
|
+
import { readPayload, field, denyPretool, audit, matchesAny, runHook, config } from '../lib/common.mjs';
|
|
132
|
+
|
|
133
|
+
// Patterns for "add a new dependency". A trailing package argument is required;
|
|
134
|
+
// bare `<pm> install` (no package) is the lockfile-respecting form and is fine.
|
|
135
|
+
const DEP_ADD_PATTERNS = [
|
|
136
|
+
// npm / yarn / pnpm / bun: install with at least one positional package
|
|
137
|
+
/npm\s+(install|i|add)\s+([^-][^\s]*|--save\s+[^\s]+|--save-dev\s+[^\s]+)/,
|
|
138
|
+
/yarn\s+add\s+[^-][^\s]*/,
|
|
139
|
+
/pnpm\s+(add|install)\s+[^-][^\s]*/,
|
|
140
|
+
/bun\s+(add|install)\s+[^-][^\s]*/,
|
|
141
|
+
|
|
142
|
+
// python: pip / pipx / uv / poetry / conda
|
|
143
|
+
/pip[0-9]*\s+install\s+([^-][^\s]*|-r\s+[^\s]+\.txt)/,
|
|
144
|
+
/pip[0-9]*\s+install\s+--upgrade\s+[^-][^\s]*/,
|
|
145
|
+
/pipx\s+install\s+[^-][^\s]*/,
|
|
146
|
+
/uv\s+(add|pip\s+install)\s+[^-][^\s]*/,
|
|
147
|
+
/poetry\s+add\s+[^-][^\s]*/,
|
|
148
|
+
/conda\s+install\s+[^-][^\s]*/,
|
|
149
|
+
|
|
150
|
+
// rust / go / ruby / php
|
|
151
|
+
/cargo\s+(add|install)\s+[^-][^\s]*/,
|
|
152
|
+
/go\s+get\s+[^-][^\s]*/,
|
|
153
|
+
/go\s+install\s+[^-][^\s]*/,
|
|
154
|
+
/gem\s+install\s+[^-][^\s]*/,
|
|
155
|
+
/bundle\s+add\s+[^-][^\s]*/,
|
|
156
|
+
/composer\s+(require|install)\s+[^-][^\s]*/,
|
|
157
|
+
|
|
158
|
+
// generic curl-to-installer (`brew install` is borderline; install scripts often add tools).
|
|
159
|
+
/brew\s+install\s+[^-][^\s]*/,
|
|
160
|
+
/apt\s+(install|add)\s+[^-][^\s]*/,
|
|
161
|
+
/apt-get\s+install\s+[^-][^\s]*/,
|
|
162
|
+
];
|
|
163
|
+
|
|
164
|
+
// Verb-detection alternation, matched as a FULL-token match (`^...$`), never a
|
|
165
|
+
// substring — mirrors bash's `[[ "$tok" =~ ^(install|i|add|get|require)$ ]]`.
|
|
166
|
+
const VERB_RE = /^(install|i|add|get|require)$/;
|
|
167
|
+
|
|
168
|
+
// Splits on runs of whitespace only — see the header's TOKENIZATION note for why
|
|
169
|
+
// bash's incidental glob-expansion side effect is deliberately not reproduced.
|
|
170
|
+
function tokensAfterVerb(cmd) {
|
|
171
|
+
const words = cmd.split(/\s+/).filter(Boolean);
|
|
172
|
+
const toks = [];
|
|
173
|
+
let seenVerb = false;
|
|
174
|
+
for (const tok of words) {
|
|
175
|
+
if (seenVerb) {
|
|
176
|
+
if (tok.startsWith('-')) continue;
|
|
177
|
+
toks.push(tok);
|
|
178
|
+
} else if (VERB_RE.test(tok)) {
|
|
179
|
+
seenVerb = true;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return toks;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// Per-project allowlist from .somi/config.json (`dep_install.allow`) — committed,
|
|
186
|
+
// reviewable policy, scoped to name prefixes, unlike the all-or-nothing env opt-in.
|
|
187
|
+
// An entry is a PREFIX of a package argument: "@types/" allows `npm install
|
|
188
|
+
// @types/node`. Conservative by construction: compound commands (`;`, `&`, `|`)
|
|
189
|
+
// never qualify, and EVERY package token must match an entry. See the header's
|
|
190
|
+
// COMPOUND-COMMAND REFUSAL note.
|
|
191
|
+
function configAllowsDep(cmd) {
|
|
192
|
+
const allow = config('.dep_install.allow[]?');
|
|
193
|
+
if (allow.length === 0) return false;
|
|
194
|
+
if (cmd.includes(';') || cmd.includes('&') || cmd.includes('|')) return false;
|
|
195
|
+
|
|
196
|
+
const toks = tokensAfterVerb(cmd);
|
|
197
|
+
if (toks.length === 0) return false;
|
|
198
|
+
|
|
199
|
+
return toks.every((t) => allow.some((a) => t.startsWith(a)));
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function main() {
|
|
203
|
+
const payload = readPayload();
|
|
204
|
+
const cmd = field(payload, '.tool_input.command');
|
|
205
|
+
if (!cmd) return;
|
|
206
|
+
|
|
207
|
+
// Explicit opt-in: human acknowledged this session may add deps. See the header's
|
|
208
|
+
// SOMI_ALLOW_DEP_INSTALL note for why this direct comparison is an exact bash
|
|
209
|
+
// equivalent, not an approximation.
|
|
210
|
+
if (process.env.SOMI_ALLOW_DEP_INSTALL === '1') return;
|
|
211
|
+
|
|
212
|
+
const m = matchesAny(cmd, DEP_ADD_PATTERNS);
|
|
213
|
+
if (!m) return;
|
|
214
|
+
|
|
215
|
+
if (configAllowsDep(cmd)) {
|
|
216
|
+
audit(payload, 'ALLOW', `dep install permitted by .somi/config.json dep_install.allow: ${cmd}`);
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
denyPretool(payload, `somi refused this command: it would add a new dependency (\`${m[0]}\`).
|
|
221
|
+
Adding a dep is a decision — record it in \`.somi/plans/<slug>/decisions.md\` (or surface it in
|
|
222
|
+
the iteration summary for the human) and re-run with \`SOMI_ALLOW_DEP_INSTALL=1\` in the
|
|
223
|
+
environment for this session, or have the human run the install themselves. Projects can also
|
|
224
|
+
allowlist package-name prefixes in \`.somi/config.json\` under \`dep_install.allow\`.
|
|
225
|
+
Lockfile-respecting reinstalls (bare \`npm install\` / \`pip install -r requirements.txt\`-less,
|
|
226
|
+
\`bundle install\`, etc.) are allowed without acknowledgement.`);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
runHook(main);
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// hooks/pre-tool/guard-protected-paths.mjs — PreToolUse hook (matcher: Write|Edit) — block
|
|
3
|
+
// writes to paths the agent shouldn't be editing.
|
|
4
|
+
//
|
|
5
|
+
// Node port of hooks/pre-tool/guard-protected-paths.sh (node-runtime-port, phase 2,
|
|
6
|
+
// iteration 2.3). Imports the shared read/deny/config helpers from ../lib/common.mjs (2.1,
|
|
7
|
+
// reviewer-blessed) rather than reimplementing them.
|
|
8
|
+
//
|
|
9
|
+
// Targets the most common "agent overstep" cases: .git internals, vendored deps, build
|
|
10
|
+
// outputs, and the SOMI plugin install (so an agent doesn't rewrite its own ruleset under
|
|
11
|
+
// itself). Lockfile writes are blocked by default (use `npm install <pkg>` etc instead), but a
|
|
12
|
+
// project can opt out via SOMI_ALLOW_LOCKFILES=1 or .somi/config.json's `lockfiles.allow_edit`.
|
|
13
|
+
//
|
|
14
|
+
// --- glob translation (bash `shopt -s extglob` -> JS) --------------------------------------
|
|
15
|
+
// PROTECTED_GLOBS is grepped exhaustively from guard-protected-paths.sh: every entry uses ONLY
|
|
16
|
+
// the plain `*` wildcard — no `?`, no `[...]` bracket expressions, no extglob operator
|
|
17
|
+
// (`@()`/`+()`/`!()`/etc). `shopt -s extglob` is enabled in the bash original but nothing in
|
|
18
|
+
// this pattern set actually exercises extglob syntax, so globToRegExp() below is deliberately
|
|
19
|
+
// scoped to `*`-only translation, not a general fnmatch engine.
|
|
20
|
+
// - `*` -> `[\s\S]*`, NOT `.*`. `[[ str == pattern ]]` is bash's plain pattern matching (no
|
|
21
|
+
// FNM_PATHNAME), so `*` matches `/` AND newlines — verified empirically:
|
|
22
|
+
// `[[ $'a\nb' == a*b ]]` is true, and `[[ "/x/node_modules/y" == */node_modules/* ]]` is
|
|
23
|
+
// true (crosses the `/`). JS `.` excludes `\n` (and, without `/s`, `\r`/U+2028/U+2029) — the
|
|
24
|
+
// same class of divergence block-secret-writes.mjs (2.2) flagged for ERE `.` vs JS `.`, here
|
|
25
|
+
// applied to glob `*`. `[\s\S]*` matches every character unconditionally, closing the gap.
|
|
26
|
+
// - Literal segments (`.git`, `node_modules`, ...) are regex-escaped. `.` has no special
|
|
27
|
+
// meaning in glob/fnmatch syntax (unlike ERE, where it's "any char") — escaping it prevents
|
|
28
|
+
// a literal `.git` segment from silently becoming a regex wildcard.
|
|
29
|
+
// - Whole pattern is anchored `^...$`: `[[ str == pattern ]]` matches the ENTIRE string, not a
|
|
30
|
+
// substring — distinct from block-dangerous-bash.sh's `=~` (substring search). The leading/
|
|
31
|
+
// trailing `*` in each pattern is what makes "anywhere in the path" work; the match itself
|
|
32
|
+
// is still full-string.
|
|
33
|
+
//
|
|
34
|
+
// --- lockfile-gate precedence reconciliation ------------------------------------------------
|
|
35
|
+
// Precedence: SOMI_ALLOW_LOCKFILES (env, session override, even "0") > .somi/config.json's
|
|
36
|
+
// `lockfiles.allow_edit` (committed project policy) > default deny. scripts/somi-check.mjs's
|
|
37
|
+
// lockfilesAllowed() (1.3, reviewer-blessed) already solved this exact precedence problem,
|
|
38
|
+
// including the jq-text string-vs-boolean subtlety: bash's `somi::config` shells out to
|
|
39
|
+
// `jq -r`, so a config author writing `"allow_edit": "true"` (a JSON STRING) produces the
|
|
40
|
+
// identical jq -r output ("true") as `"allow_edit": true` (the boolean) — both are honored as
|
|
41
|
+
// allowed. common.mjs's config() (2.1) already reproduces that jq -r text shape for scalar
|
|
42
|
+
// reads (returns '' for null/undefined/false, else the string itself or JSON.stringify(cur) for
|
|
43
|
+
// a non-string scalar), so `config('.lockfiles.allow_edit') === 'true'` below reproduces the
|
|
44
|
+
// SAME text comparison bash performs, without re-implementing a config reader the way
|
|
45
|
+
// somi-check.mjs (which predates common.mjs) had to. Env-side fall-through matches too: bash's
|
|
46
|
+
// `${SOMI_ALLOW_LOCKFILES:-}` treats unset AND explicitly-empty (`SOMI_ALLOW_LOCKFILES=""`)
|
|
47
|
+
// identically (falls through to the config check); the `env !== undefined && env !== ''` guard
|
|
48
|
+
// below reproduces that, matching lockfilesAllowed()'s own env branch. Verified by hand-tracing
|
|
49
|
+
// every fixture case plus the differential probes (see diary).
|
|
50
|
+
//
|
|
51
|
+
// --- platform reasoning (posix vs win32) ----------------------------------------------------
|
|
52
|
+
// This is the "path-heavier hook" block-secret-writes.mjs's header warned 2.3+ to reason
|
|
53
|
+
// through explicitly — its own divergence there was Windows-SAFER; here one of them is not:
|
|
54
|
+
// 1. PROTECTED_GLOBS are forward-slash literals (`*/node_modules/*`). If `tool_input.file_path`
|
|
55
|
+
// ever arrives with backslash separators (a native-Windows path not already normalized to
|
|
56
|
+
// forward slashes upstream), the translated regex requires a literal `/` and will NOT match
|
|
57
|
+
// a `\`-separated path — a protected write could slip through undenied. This reproduces the
|
|
58
|
+
// bash original's identical gap (its globs were just as forward-slash-only) rather than
|
|
59
|
+
// introducing a new one; documented per this iteration's brief, not fixed — fixing would
|
|
60
|
+
// mean choosing a separator-normalization policy this behavior-preserving port doesn't own.
|
|
61
|
+
// 2. The relative-path check (`pathInput.startsWith('/')`) hardcodes a literal POSIX leading-
|
|
62
|
+
// `/` test, deliberately NOT `path.isAbsolute()` (platform-aware; would treat `C:\...` as
|
|
63
|
+
// absolute on win32) — this mirrors bash's own `[[ "$PATH_INPUT" != /* ]]` glob-prefix test
|
|
64
|
+
// byte-for-byte. A Windows-style absolute path would be misclassified as relative and get
|
|
65
|
+
// the project root prepended — the same failure bash's own check would have on such input
|
|
66
|
+
// (bash never ran natively on Windows either, so this is not a new gap either).
|
|
67
|
+
// 3. `path.basename()` (platform-default: posix on Linux, win32 on Windows) is used for the
|
|
68
|
+
// lockfile-name comparison. Unlike (1), this divergence is SAFE: win32's basename
|
|
69
|
+
// recognizes BOTH `/` and `\` as separators (documented Node behavior), so on Windows it is
|
|
70
|
+
// a superset of bash's posix-only basename — never narrower. Same conclusion class as
|
|
71
|
+
// block-secret-writes.mjs's own basename note, on the safe side here too.
|
|
72
|
+
|
|
73
|
+
import path from 'node:path';
|
|
74
|
+
import { readPayload, field, denyPretool, runHook, projectRoot, config } from '../lib/common.mjs';
|
|
75
|
+
|
|
76
|
+
const PROTECTED_GLOBS = [
|
|
77
|
+
// Source-control internals.
|
|
78
|
+
'*/.git/*',
|
|
79
|
+
|
|
80
|
+
// SOMI plugin install — never let an agent rewrite the ruleset under itself.
|
|
81
|
+
'*/.claude/plugins/somi/*',
|
|
82
|
+
|
|
83
|
+
// Vendored deps & build outputs.
|
|
84
|
+
'*/node_modules/*',
|
|
85
|
+
'*/vendor/*',
|
|
86
|
+
'*/dist/*',
|
|
87
|
+
'*/build/*',
|
|
88
|
+
'*/target/*',
|
|
89
|
+
'*/.next/*',
|
|
90
|
+
'*/.nuxt/*',
|
|
91
|
+
'*/__pycache__/*',
|
|
92
|
+
];
|
|
93
|
+
|
|
94
|
+
const LOCKFILES = [
|
|
95
|
+
'package-lock.json',
|
|
96
|
+
'yarn.lock',
|
|
97
|
+
'pnpm-lock.yaml',
|
|
98
|
+
'Cargo.lock',
|
|
99
|
+
'Gemfile.lock',
|
|
100
|
+
'poetry.lock',
|
|
101
|
+
'uv.lock',
|
|
102
|
+
'composer.lock',
|
|
103
|
+
'go.sum',
|
|
104
|
+
];
|
|
105
|
+
|
|
106
|
+
// See the glob-translation notes in the header comment.
|
|
107
|
+
function globToRegExp(glob) {
|
|
108
|
+
const body = glob
|
|
109
|
+
.split('*')
|
|
110
|
+
.map((segment) => segment.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'))
|
|
111
|
+
.join('[\\s\\S]*');
|
|
112
|
+
return new RegExp(`^${body}$`);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const PROTECTED_PATTERNS = PROTECTED_GLOBS.map((glob) => ({ glob, re: globToRegExp(glob) }));
|
|
116
|
+
|
|
117
|
+
// See the lockfile-gate reconciliation notes in the header comment.
|
|
118
|
+
function lockfilesAllowed() {
|
|
119
|
+
const env = process.env.SOMI_ALLOW_LOCKFILES;
|
|
120
|
+
if (env !== undefined && env !== '') return env === '1';
|
|
121
|
+
return config('.lockfiles.allow_edit') === 'true';
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function main() {
|
|
125
|
+
const payload = readPayload();
|
|
126
|
+
const tool = field(payload, '.tool_name');
|
|
127
|
+
let pathInput = field(payload, '.tool_input.file_path');
|
|
128
|
+
if (!pathInput) return;
|
|
129
|
+
|
|
130
|
+
// Normalise relative paths so the leading-`*/` globs above still match (a relative
|
|
131
|
+
// `node_modules/x.js` would otherwise slip past `*/node_modules/*`). Reuses common.mjs's
|
|
132
|
+
// projectRoot() — the centralized ${...}-unexpanded-variable guard (2.1) — rather than
|
|
133
|
+
// re-duplicating it here.
|
|
134
|
+
if (!pathInput.startsWith('/')) {
|
|
135
|
+
pathInput = `${projectRoot()}/${pathInput}`;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
for (const { glob, re } of PROTECTED_PATTERNS) {
|
|
139
|
+
if (re.test(pathInput)) {
|
|
140
|
+
denyPretool(payload, `somi refused to ${tool} \`${pathInput}\`: this path matches a protected glob (\`${glob}\`).
|
|
141
|
+
These paths are owned by tooling (git / package managers / SOMI itself); changes belong in their respective workflows.`);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// Lockfile gate. Precedence: env (session override, even "0") > .somi/config.json
|
|
146
|
+
// (`lockfiles.allow_edit`, committed project policy) > default (deny).
|
|
147
|
+
if (!lockfilesAllowed()) {
|
|
148
|
+
const basename = path.basename(pathInput);
|
|
149
|
+
if (LOCKFILES.includes(basename)) {
|
|
150
|
+
denyPretool(payload, `somi refused to ${tool} \`${pathInput}\`: lockfiles should be regenerated by the package manager, not hand-edited.
|
|
151
|
+
If you genuinely need to hand-edit this lockfile, set SOMI_ALLOW_LOCKFILES=1 in your environment
|
|
152
|
+
for this session (or set \`lockfiles.allow_edit: true\` in \`.somi/config.json\` as project policy).`);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
runHook(main);
|