@cratis/pi 7.150.0 → 7.150.2
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/corpus/hooks/README.md +24 -1
- package/package/corpus/hooks/scripts/cratis-guard-secrets.sh +81 -18
- package/package/corpus/hooks/scripts/cratis-guard-shell-writes.sh +180 -33
- package/package/corpus/hooks/scripts/cratis-guard-writes.sh +3 -1
- package/package/corpus/skills/cratis-screenplay-model-authoring/references/mcp-tools.md +2 -2
- package/package/corpus/skills/cratis-screenplay-toolchain/references/screenplay-contract.json +6047 -724
- package/package/corpus/skills/cratis-screenplay-toolchain/references/screenplay-contract.version +1 -1
- package/package.json +1 -1
|
@@ -12,7 +12,7 @@ Three layers:
|
|
|
12
12
|
| Hard block | `PreToolUse` on a write | `scripts/cratis-guard-writes.sh` | zero | exits **2** — the write does not happen |
|
|
13
13
|
| Hard block | `PreToolUse` on `Bash` | `scripts/cratis-guard-shell-writes.sh` | zero tokens; one batch Write/Edit classification | exits **2** — a protected reference requires read-only syntax throughout the command; best-effort, not a sandbox |
|
|
14
14
|
| Hard block | `PreToolUse` on `Bash` | `scripts/cratis-guard-store-mutations.sh` | zero; parses only commands that mention `cratis` | exits **2** — the store-changing `cratis chronicle` command does not run |
|
|
15
|
-
| Hard block | `PreToolUse` on `Bash` | `scripts/cratis-guard-secrets.sh` | zero; parses only commands that mention a credential-bearing tool or file | exits **2** — the guard is a **best-effort, defense-in-depth text filter, NOT a sandbox**: it refuses some harmless commands rather than model the whole shell. Real protection requires **OS-level sandboxing or file permissions**. A command is *tainted* when a protected file-name stem appears in any token, including option values, in any letter case, after quotes, backslashes and line continuations are removed. Only search regex text is exempt: `-e`/`--regexp` values, or the first operand when neither `-e` nor `-f` is present. `-f`/`--file` values and all other operands are paths, not exempt patterns. The stems are `.env`, `.pem`, `.p12`, `.pfx`, `id_rsa`, `id_ed25519`, `id_ecdsa`, `id_dsa`, `.netrc`, `.npmrc`, `.pgpass`, `.pypirc`, `.git-credentials`, `.kube/`, `kubeconfig`, `credentials`, `.docker/`, `/environ` and `run/secrets`. Stars, question marks, bracket expressions and grouped pathname patterns count as the characters they can match when the pattern retains at least two literal stem characters. For `credentials` only, an embedded occurrence in a longer directory component or a recognized source filename (for example `Specs/for_ApplicationCredentials/a.cs` or `CredentialsProvider.cs`) is not a stem hit; exact `credentials`, `credentials.*` and a `credentials/` component remain protected. A command is also tainted when an assignment value or any expanded word can contain a stem. A tainted command runs only when it is a single simple command with every word literal: `cat`, `head`, `tail`, `less`, `more`, `wc`, `grep`, `egrep`, `fgrep` or `rg` of paths that are not protected (such as `cat .env.example`); `test` of literal paths; or `cp SOURCE DEST` with no options and a source that is not protected (for example, `cp .env.example example.txt`; the shell-write guard separately refuses `.env` destinations). A single simple `ls` or `stat` may also take glob operands, but not variables, substitutions, wrappers or other shell structure. Tainted searches cannot recurse, search a directory with `rg`, omit `rg` path operands, or use value-taking options other than `-e`/`--regexp`. Every other tainted command is refused, including variables, command substitution, globs for content readers, loops, conditionals, pipes, redirections, here-strings, here-documents, wrappers, `bash -c`, `eval` and transfer tools. Shell-option changes are checked even without a protected stem: modifying `shopt`, `setopt`, `unsetopt` and `emulate` are refused. `extglob` is refused as an option, not merely as text. Shell invocation `-o`/`-O` options, including clustered flags, must name a supported option. Expanded `set` arguments are refused. Only `set` flags `e`, `u`, `x`, `f`, `v`, `E`, `T` and named options `errexit`, `nounset`, `pipefail`, `xtrace`, `verbose`, `noglob`, `errtrace`, `functrace` (including their `+` forms) are supported. The message names the stem: list the directory with `ls`, then name exact non-credential paths, or use `rg -l PATTERN literal-file`. Narrowing to safe literal files is supported remediation, not a workaround; disguising the same secret read behind a variable, glob, wrapper, script or alias remains prohibited. Commands without a stem keep the full analysis. In that analysis a variable can hold any value the command assigns to it anywhere, a positional parameter or an unquoted expansion with glob or space characters is unknown, and an unknown file operand is refused. Residual classes include **shell glob grammar across bash and zsh, option toggles, option arity and quote concatenation**, as well as runtime-generated names, inherited state, filesystem indirection, user-defined commands, external programs and scripts, and searches without a named protected path. These classes are not modeled exhaustively. Use OS-level sandboxing or file permissions, harness isolation and least privilege as the primary boundary. Token files and key-extension globs (`*.key`, `*.jks`) are refused only through the listed reader, input, `dd` and transfer commands; see [`secrets.md`](../rules/secrets.md) |
|
|
15
|
+
| Hard block | `PreToolUse` on `Bash` | `scripts/cratis-guard-secrets.sh` | zero; parses only commands that mention a credential-bearing tool or file | exits **2** — the guard is a **best-effort, defense-in-depth text filter, NOT a sandbox**: it refuses some harmless commands rather than model the whole shell. Real protection requires **OS-level sandboxing or file permissions**. A command is *tainted* when a protected file-name stem appears in any token, including option values, in any letter case, after quotes, backslashes and line continuations are removed. Only search regex text is exempt: `-e`/`--regexp` values, or the first operand when neither `-e` nor `-f` is present. `-f`/`--file` values and all other operands are paths, not exempt patterns. The stems are `.env`, `.pem`, `.p12`, `.pfx`, `id_rsa`, `id_ed25519`, `id_ecdsa`, `id_dsa`, `.netrc`, `.npmrc`, `.pgpass`, `.pypirc`, `.git-credentials`, `.kube/`, `kubeconfig`, `credentials`, `.docker/`, `/environ` and `run/secrets`. The `.env` stem is a whole-stem rule: a letter or digit right after it releases the word (`Org.DS.EnvironmentalReporting`, `Environments.Specs`, `a.environment.txt`), while `.env`, `.env.local`, `.env_local`, `.env-prod`, `app.env` and `.envrc` stay protected, as do named copies such as `app.env.old`, `prod.env.local` and `app.env~` unless the name ends in `.example`, `.sample`, `.template`, `.dist` or `.defaults`, or in a source, documentation, project, build or markup extension (`.csproj`, `.sln`, `.props`, `.targets`, `.css`, `.html`, `.razor`, `.resx`, `.xaml` and similar); `.json`, `.yaml`, `.xml`, `.config`, `.ini`, `.txt` and extensionless names stay protected. Stars, question marks, bracket expressions and grouped pathname patterns count as the characters they can match when the pattern retains at least two literal stem characters. For `credentials` only, an embedded occurrence in a longer directory component or a recognized source filename (for example `Specs/for_ApplicationCredentials/a.cs` or `CredentialsProvider.cs`) is not a stem hit; exact `credentials`, `credentials.*` and a `credentials/` component remain protected. A command is also tainted when an assignment value or any expanded word can contain a stem. A tainted command runs only when it is a single simple command with every word literal: `cat`, `head`, `tail`, `less`, `more`, `wc`, `grep`, `egrep`, `fgrep` or `rg` of paths that are not protected (such as `cat .env.example`); `test` of literal paths; or `cp SOURCE DEST` with no options and a source that is not protected (for example, `cp .env.example example.txt`; the shell-write guard separately refuses `.env` destinations). A single simple `ls` or `stat` may also take glob operands, but not variables, substitutions, wrappers or other shell structure. Tainted searches cannot recurse, search a directory with `rg`, omit `rg` path operands, or use value-taking options other than `-e`/`--regexp`. Every other tainted command is refused, including variables, command substitution, globs for content readers, loops, conditionals, pipes, redirections, here-strings, here-documents, wrappers, `bash -c`, `eval` and transfer tools. Shell-option changes are checked even without a protected stem: modifying `shopt`, `setopt`, `unsetopt` and `emulate` are refused. `extglob` is refused as an option, not merely as text. Shell invocation `-o`/`-O` options, including clustered flags, must name a supported option. Expanded `set` arguments are refused. Only `set` flags `e`, `u`, `x`, `f`, `v`, `E`, `T` and named options `errexit`, `nounset`, `pipefail`, `xtrace`, `verbose`, `noglob`, `errtrace`, `functrace` (including their `+` forms) are supported. The message names the stem: list the directory with `ls`, then name exact non-credential paths, or use `rg -l PATTERN literal-file`. Narrowing to safe literal files is supported remediation, not a workaround; disguising the same secret read behind a variable, glob, wrapper, script or alias remains prohibited. Commands without a stem keep the full analysis. In that analysis a variable can hold any value the command assigns to it anywhere, a positional parameter or an unquoted expansion with glob or space characters is unknown, and an unknown file operand is refused. Residual classes include **shell glob grammar across bash and zsh, option toggles, option arity and quote concatenation**, as well as runtime-generated names, inherited state, filesystem indirection, user-defined commands, external programs and scripts, and searches without a named protected path. These classes are not modeled exhaustively. Use OS-level sandboxing or file permissions, harness isolation and least privilege as the primary boundary. A `jq`, `yq`, `gojq`, `jaq`, `awk`, `gawk`, `mawk`, `nawk` or `busybox awk` command is refused like `env` and `printenv` when any of its words, after quote and backslash removal, holds `env`, `ENV`, `ENVIRON`, `strenv`, `envsubst`, `getenv`, `PROCINFO` or `SYMTAB` as a whole token (so a field named `env` is refused while `environment` and `envelope` are readable); the flag layout is not parsed, and programs read from a file (`jq -f`, `awk -f`) or built from variables are not inspected. Token files and key-extension globs (`*.key`, `*.jks`) are refused only through the listed reader, input, `dd` and transfer commands; see [`secrets.md`](../rules/secrets.md) |
|
|
16
16
|
| Hard block | `PreToolUse` on `Bash` | `scripts/cratis-guard-pr-body.sh` | fetches shared rules only for `gh pr create/edit` | exits **2** for inline bodies, invalid notes or conflicting release labels; warns and allows when offline with no cache |
|
|
17
17
|
| Cleanup of delivered work | `PostToolUse` on `Bash` (`gh pr merge`, `git pull/merge/worktree`) and `SessionStart` (Pi: `tool_result` and `session_start`) | `scripts/cratis-cleanup-worktrees.sh --hook` | zero when the command is unrelated | never blocks; removes delivered worktrees and branches, reports what it kept |
|
|
18
18
|
| Quality gate (Claude) | `Stop` | `scripts/cratis-quality-gate.sh` | one build/test run, only when relevant files changed | exits **2** — the turn does not end |
|
|
@@ -34,6 +34,29 @@ searches with secret/certificate/key-like prefixes also remain blocked. For ripg
|
|
|
34
34
|
symlinks, not filename-only mode. Literal directory checks retain the process-cwd fallback when the
|
|
35
35
|
payload omits cwd; glob probing does not. Filesystem check/use races remain: this is not a sandbox.
|
|
36
36
|
|
|
37
|
+
The shell-write guard classifies **write-target words**, not every word. It lexes the command into
|
|
38
|
+
simple commands. When every command is a reviewed reader or staging verb (`cat`, `head`, `tail`, `wc`,
|
|
39
|
+
`ls`, `rg`, `grep`, `echo`, `git status|diff|log|show|add|commit`, `git ls-files`, `jq`, read-only
|
|
40
|
+
`awk`, and the like) with no redirect, the words of those commands are not classified, so braces,
|
|
41
|
+
brackets and filter text in a read are not refused. Redirect targets, `git commit -F`, `cd` segments and
|
|
42
|
+
every word of any command outside the reviewed set are always classified, and a command the lexer
|
|
43
|
+
cannot parse (expansions, backticks, parentheses, braces, heredocs, unterminated quotes) falls back to
|
|
44
|
+
classifying the whole text, fail-closed. Admissions beyond plain reads, each with its limit:
|
|
45
|
+
`jq` takes no value-taking option, so its filter is a plain operand; `awk` is read-only only as
|
|
46
|
+
`awk [-F sep] 'program' files` with no `>`, `|`, `@`, `system`, `getline`, `close` or `fflush` in the
|
|
47
|
+
program (conservative: `$1 > 5` is refused); a word-leading `*` or `?` glob is accepted only for
|
|
48
|
+
`cat`, `head`, `tail`, `wc`, `ls`, `grep`, `egrep`, `fgrep` and `git ls-files`, and a file named like an
|
|
49
|
+
option can still change what those print, never what they write; and exactly
|
|
50
|
+
`node .cratis/ai/hooks/scripts/cratis-check-pr.mjs` (options `--body-file`, `--label`, `--add-label`,
|
|
51
|
+
`--remove-label`, `--pr`, `--base`, `--repo` with a plain value, and `--strict`) and
|
|
52
|
+
`bash .cratis/ai/hooks/scripts/cratis-cleanup-worktrees.sh` (`--dry-run`, `--quiet`, `--no-fetch`) may
|
|
53
|
+
run: the path must match as one literal word (no `./`, `..`, absolute, variable or glob spelling), nothing
|
|
54
|
+
is admitted after a `cd`, and writes to those files stay refused. The working directory decides which
|
|
55
|
+
copy runs. A write-target word with expansion syntax (`$`, backtick, `~`, parentheses or braces) is judged by every
|
|
56
|
+
suffix that follows a slash in it (`$PWD/.cratis/x` and `~+/.cratis/x` are treated as `.cratis/x` in the working
|
|
57
|
+
directory), without parsing the expansion, and refused if a suffix is protected, which includes a read of a protected path through a variable. This is a
|
|
58
|
+
best-effort lexical check, not enforcement: a variable holding the whole path still passes.
|
|
59
|
+
|
|
37
60
|
The shell-write reference scan keeps conservative leading-dot intersections: a tool named `Bash`,
|
|
38
61
|
`SHELL`, and the hook process's options do not establish the execution shell's options. Globbed writes
|
|
39
62
|
therefore remain blocked. Read operands under protected paths are not write targets: `rg -n`/`-l`,
|
|
@@ -77,8 +77,8 @@ function addw(text, kind, k) {
|
|
|
77
77
|
for (k = 1; k <= length(text); k++) { wmask = wmask (quote_boundary ? toupper(kind) : kind); quote_boundary = 0 }
|
|
78
78
|
}
|
|
79
79
|
function flush_word() {
|
|
80
|
-
if (inword) { W[++nw] = word; WP[nw] = wplain; WM[nw] = wmask }
|
|
81
|
-
word = ""; wmask = ""; inword = 0; wquoted = 0; wplain = 0; quote_boundary = 0
|
|
80
|
+
if (inword) { W[++nw] = word; WP[nw] = wplain; WM[nw] = wmask; WU[nw] = wunquoted }
|
|
81
|
+
word = ""; wmask = ""; inword = 0; wquoted = 0; wplain = 0; quote_boundary = 0; wunquoted = 0
|
|
82
82
|
}
|
|
83
83
|
# Decodes the body of an ANSI-C string $'...' (i is just after the opening quote) into the word and returns the
|
|
84
84
|
# position after the closing quote. The same decoder is in cratis-guard-store-mutations.sh (`ansi_c`).
|
|
@@ -116,6 +116,15 @@ function stem_boundary(t, start, finish, stem, nonliteral, a, b, before, afte
|
|
|
116
116
|
if (nonliteral == "expanding") literal = t !~ /[][?*()`~^\001-\007]/
|
|
117
117
|
else literal = !nonliteral && t !~ /[][?*{}()$`~^\001-\007]/
|
|
118
118
|
if (stem == "/environ" && substr(t, finish + 1, 1) ~ /[a-z0-9_]/) return 0
|
|
119
|
+
# `.env` followed by a letter or digit is part of a longer word (Org.EnvironmentalReporting); `.envrc` stays protected.
|
|
120
|
+
# Allowlist: the boundary is released only when the stem is the literal text and the rest of its path component
|
|
121
|
+
# is plain literal characters. Any expansion, quote, escape, glob or brace there may still resolve to `.envrc`.
|
|
122
|
+
if (stem == ".env" && substr(t, finish + 1, 1) ~ /[a-z0-9]/ && substr(t, finish + 1) !~ /^rc([^a-z0-9]|$)/ &&
|
|
123
|
+
substr(t, start, finish - start + 1) == ".env") {
|
|
124
|
+
component = substr(t, finish + 1)
|
|
125
|
+
sub(/[\/ \t\n].*$/, "", component)
|
|
126
|
+
if (component ~ /^[a-z0-9._-]*$/) return 0
|
|
127
|
+
}
|
|
119
128
|
if (stem ~ /^\.(key|jks|keystore|kdbx)$/) {
|
|
120
129
|
if (substr(t, finish + 1, 1) ~ /[a-z0-9_-]/) return 0
|
|
121
130
|
# Ambiguous key words are credential extensions only at the end of a literal filename.
|
|
@@ -352,17 +361,49 @@ function literal_shape(A, M, n, k, j, b, words, count, pattern, ended, w, pat
|
|
|
352
361
|
}
|
|
353
362
|
return b != "rg" || paths > 0
|
|
354
363
|
}
|
|
355
|
-
|
|
364
|
+
# Every character that can come from command output, directly or through a parameter expansion holding it, is a wildcard.
|
|
365
|
+
function output_wild(w, m, k, j, x, depth, found, r, span) {
|
|
366
|
+
delete span
|
|
367
|
+
for (k = 1; k <= length(w); k++) {
|
|
368
|
+
if (substr(w, k, 2) != "$" "{" || tolower(substr(m, k, 1)) == "l") continue
|
|
369
|
+
depth = 0; found = 0
|
|
370
|
+
for (j = k + 1; j <= length(w); j++) {
|
|
371
|
+
if (tolower(substr(m, j, 1)) == "c") found = 1
|
|
372
|
+
else if (substr(w, j, 1) == "{") depth++
|
|
373
|
+
else if (substr(w, j, 1) == "}" && --depth == 0) break
|
|
374
|
+
}
|
|
375
|
+
if (found) for (x = k; x <= j; x++) span[x] = 1
|
|
376
|
+
}
|
|
377
|
+
for (k = 1; k <= length(w); k++) r = r ((k in span) || tolower(substr(m, k, 1)) == "c" ? "*" : substr(w, k, 1))
|
|
378
|
+
return r
|
|
379
|
+
}
|
|
380
|
+
# Unquoted output can also split into several words: scan each suffix that starts at output text.
|
|
381
|
+
function split_scan(w, m, k, r, hit) {
|
|
382
|
+
r = output_wild(w, m)
|
|
383
|
+
for (k = 1; k <= length(r); k++) {
|
|
384
|
+
if (substr(r, k, 1) != "*" || substr(r, k + 1, 1) == "*") continue
|
|
385
|
+
hit = stem_scan(substr(r, k), "raw", "expanding"); if (hit != "") return hit
|
|
386
|
+
if (substr(r, k + 1, 1) !~ /^[\/]?$/) { hit = stem_scan("." substr(r, k), "raw", "expanding"); if (hit != "") return hit }
|
|
387
|
+
}
|
|
388
|
+
return ""
|
|
389
|
+
}
|
|
390
|
+
function end_cmd( k, m, w, j, hit, exempt) {
|
|
356
391
|
flush_word()
|
|
357
392
|
if (nw > 0) {
|
|
358
393
|
commands++
|
|
359
394
|
regex_slots(W, 1, nw)
|
|
360
395
|
for (k = 1; k <= nw; k++) {
|
|
361
396
|
if (is_assign(W[k])) set_var(W[k], WM[k])
|
|
362
|
-
|
|
363
|
-
|
|
397
|
+
exempt = (k in RP) && !WU[k]
|
|
398
|
+
if (exempt && RP[k] < 0) continue
|
|
399
|
+
w = exempt ? substr(W[k], 1, RP[k]) : W[k]
|
|
364
400
|
hit = stem_scan(w, "raw", expands(w, WM[k]) ? "expanding" : 0); if (TAINT == "") TAINT = hit
|
|
365
|
-
|
|
401
|
+
# Command output may supply any text, including several names: judge it as an unresolved wildcard.
|
|
402
|
+
if (!exempt && index(WM[k], "c") + index(WM[k], "C") > 0) {
|
|
403
|
+
hit = stem_scan(output_wild(W[k], WM[k]), "raw", "expanding"); if (TAINT == "") TAINT = hit
|
|
404
|
+
if (WU[k]) { hit = split_scan(W[k], WM[k]); if (TAINT == "") TAINT = hit }
|
|
405
|
+
}
|
|
406
|
+
if (!exempt && expands(W[k], WM[k])) {
|
|
366
407
|
resolve(W[k], WM[k])
|
|
367
408
|
for (j = 1; j <= nalt; j++) { hit = stem_scan(ALT[j], "resolved"); if (TAINT == "") TAINT = hit }
|
|
368
409
|
}
|
|
@@ -379,7 +420,7 @@ function end_cmd( k, m, w, j, hit) {
|
|
|
379
420
|
V[++m] = w; VM[m] = WM[k]
|
|
380
421
|
}
|
|
381
422
|
if (m > 0) { check_inputs(V, 1, m); analyze(V, 1, m) }
|
|
382
|
-
delete W; delete WP; delete WM; delete V; delete VM; nw = 0
|
|
423
|
+
delete W; delete WP; delete WM; delete WU; delete V; delete VM; nw = 0
|
|
383
424
|
}
|
|
384
425
|
function cmdsub(s, i, n, start, depth, c, j) {
|
|
385
426
|
complex = 1
|
|
@@ -417,7 +458,7 @@ function read_heredocs(s, i, n, h, e, line, cmp, body) {
|
|
|
417
458
|
return i
|
|
418
459
|
}
|
|
419
460
|
function tokenize(s, i, n, c, c2, j, dash, delim) {
|
|
420
|
-
n = length(s); i = 1; nw = 0; word = ""; wmask = ""; inword = 0; wquoted = 0; wplain = 0; nheredoc = 0; hd_shell = 0; quote_boundary = 0
|
|
461
|
+
n = length(s); i = 1; nw = 0; word = ""; wmask = ""; inword = 0; wquoted = 0; wplain = 0; wunquoted = 0; nheredoc = 0; hd_shell = 0; quote_boundary = 0
|
|
421
462
|
delete W; delete WP; delete WM
|
|
422
463
|
while (i <= n) {
|
|
423
464
|
c = substr(s, i, 1)
|
|
@@ -451,10 +492,10 @@ function tokenize(s, i, n, c, c2, j, dash, delim) {
|
|
|
451
492
|
}
|
|
452
493
|
continue
|
|
453
494
|
}
|
|
454
|
-
if (c == "$" && substr(s, i + 1, 1) == "(") { wquoted = 1; inword = 1; i = cmdsub(s, i + 2, n); addw("x", "c"); continue }
|
|
495
|
+
if (c == "$" && substr(s, i + 1, 1) == "(") { wquoted = 1; inword = 1; wunquoted = 1; i = cmdsub(s, i + 2, n); addw("x", "c"); continue }
|
|
455
496
|
if (c == "`") {
|
|
456
497
|
complex = 1
|
|
457
|
-
wquoted = 1; inword = 1
|
|
498
|
+
wquoted = 1; inword = 1; wunquoted = 1
|
|
458
499
|
j = index(substr(s, i + 1), "`")
|
|
459
500
|
if (j == 0) { i = n + 1; continue }
|
|
460
501
|
enqueue(substr(s, i + 1, j - 1)); i += j + 1; addw("x", "c"); continue
|
|
@@ -611,6 +652,8 @@ function is_assign(w) { return w ~ /^[A-Za-z_][A-Za-z0-9_]*(\[[^]]*\])?\+?=/ }
|
|
|
611
652
|
|
|
612
653
|
# ── file classification for the reader commands ──
|
|
613
654
|
function code_file(b) { return b ~ /\.(cs|ts|tsx|js|jsx|mjs|cjs|go|py|java|kt|rs|md)$/ }
|
|
655
|
+
# Build and markup files cannot hold values, so a dotted Env segment in their names (Cratis.Env.Tool.csproj) is not an env copy.
|
|
656
|
+
function build_file(b) { return b ~ /\.(csproj|fsproj|vbproj|sln|slnx|props|targets|css|scss|less|html|htm|razor|cshtml|resx|xaml)$/ }
|
|
614
657
|
# A design-token file is not a credential: a stylesheet, or a file under a design, theme or style directory, is
|
|
615
658
|
# fine unless it sits in a secrets or credentials directory. A bare token.json (an OAuth refresh-token file) is not.
|
|
616
659
|
function token_ok(l, b) {
|
|
@@ -622,6 +665,8 @@ function sensitive_path(w, l, b) {
|
|
|
622
665
|
l = tolower(w); b = l; sub(/.*\//, "", b)
|
|
623
666
|
if (l ~ /(^|\/)\.kube\// || b == "kubeconfig" || b ~ /\.kubeconfig$/) return "a kubeconfig"
|
|
624
667
|
if (b ~ /^\.env/ || b ~ /\.env$/) { if (b ~ /\.(example|sample|template|dist|defaults?)$/) return ""; return "an .env file" }
|
|
668
|
+
# A named copy or variant (app.env.old, prod.env.local, app.env~) is protected like the file itself; templates and source files are not.
|
|
669
|
+
if (b ~ /.\.env[.~]/ && b !~ /\.(example|sample|template|dist|defaults?)$/ && !code_file(b) && !build_file(b)) return "an .env file"
|
|
625
670
|
# Unambiguous credential extensions also protect disguised source suffixes; ambiguous words
|
|
626
671
|
# such as Signing.Key.cs are source unless the credential extension is final.
|
|
627
672
|
if (b ~ /\.(pem|p12|pfx)([.]|$)/ || b ~ /\.(key|jks|keystore|kdbx)[.]*$/) return "a key or certificate file"
|
|
@@ -1236,14 +1281,31 @@ function scan_short(w, shortvals, sed_like, i, c, last) {
|
|
|
1236
1281
|
if (sed_like && c == "i") return
|
|
1237
1282
|
}
|
|
1238
1283
|
}
|
|
1284
|
+
function report_operand(A, k, b, filenames, kind) {
|
|
1285
|
+
kind = operand_kind(A, k, 0, filenames)
|
|
1286
|
+
if (kind != "" && matched_file != "") report(b " of " kind " (" matched_file ") prints credentials", "list the directory with ls, then name the exact files; for an env template read the .env.example")
|
|
1287
|
+
else if (kind != "" && via_pattern) report(b " of a glob that may match " kind " prints credentials", "list the directory with ls, then name the exact files; for an env template read the .env.example")
|
|
1288
|
+
else if (kind != "") report(b " of " kind " prints credentials", "name the file and the key you need in your report; ask the user to confirm a value instead of printing it; for an env template read the .env.example")
|
|
1289
|
+
}
|
|
1290
|
+
# A jq, yq or awk program that reads the process environment prints every variable, like env and printenv do.
|
|
1291
|
+
# Every word of the command is judged, with quotes and backslashes removed, so no flag or subcommand layout is parsed:
|
|
1292
|
+
# an environment-access identifier (env, ENV, ENVIRON, strenv, envsubst, getenv, PROCINFO, SYMTAB) as a whole token refuses the command.
|
|
1293
|
+
function env_program(b, p, t) {
|
|
1294
|
+
gsub(/[\\"']/, "", p)
|
|
1295
|
+
t = " " p " "
|
|
1296
|
+
if (t !~ /[^A-Za-z0-9_](env|ENV|ENVIRON|strenv|envsubst|getenv|PROCINFO|SYMTAB)[^A-Za-z0-9_]/) return
|
|
1297
|
+
report(b " program reads the process environment, which can hold credentials", "name one non-credential variable with printenv NAME, or test existence with [ -n \"${NAME:-}\" ] && echo set")
|
|
1298
|
+
}
|
|
1239
1299
|
function reader(A, s, n, b, k, w, mode, havepat, need, afterdd, kind, valued, valued2, shortvals, filenames) {
|
|
1240
1300
|
b = base(A[s]); mode = ""; valued = " "; valued2 = " "; shortvals = ""
|
|
1301
|
+
if (b == "busybox") { if (base(A[s + 1]) == "awk") reader(A, s + 1, n); return }
|
|
1302
|
+
if (b ~ /^(awk|gawk|mawk|nawk|jq|yq|gojq|jaq)$/) for (k = s + 1; k <= n; k++) env_program(b, A[k])
|
|
1241
1303
|
if (b ~ /^(rg|grep|egrep|fgrep)$/) {
|
|
1242
1304
|
for (k = s + 1; k <= n; k++) if (A[k] == "-l" || (b != "rg" && A[k] == "-L") || A[k] ~ /^--files-with(out)?-match(es)?$/) filenames = 1
|
|
1243
1305
|
for (k = s + 1; k <= n; k++) if (A[k] ~ /^-/ && ((b == "rg" && A[k] == "-L") || A[k] !~ /^(-[lL]|--files-with(out)?-match(es)?|--|-e|-F|--fixed-strings)$/)) filenames = 0
|
|
1244
1306
|
}
|
|
1245
1307
|
if (b ~ /^(grep|egrep|fgrep|rg|ag)$/) { mode = "pattern"; shortvals = (b ~ /^(grep|egrep|fgrep)$/) ? "mABCdD" : (b == "rg") ? "mABCdjEMrgtT" : "ABCGgmpW"; valued = " --glob --type --type-not --max-count --include --exclude --exclude-dir --context --after-context --before-context --max-depth --threads " }
|
|
1246
|
-
else if (b ~ /^(sed|awk|gawk|jq|yq)$/) { mode = "script"; shortvals = "vF"; valued = " --indent "; valued2 = " --arg --argjson --slurpfile --rawfile " }
|
|
1308
|
+
else if (b ~ /^(sed|awk|gawk|mawk|nawk|jq|yq|gojq|jaq)$/) { mode = "script"; shortvals = "vF"; valued = " --indent "; valued2 = " --arg --argjson --slurpfile --rawfile " }
|
|
1247
1309
|
# The first operand is the pattern or script unless -e/-f/--regexp/--file/--expression supplies it; only the rest are files.
|
|
1248
1310
|
havepat = 0
|
|
1249
1311
|
if (mode != "") for (k = s + 1; k <= n; k++) {
|
|
@@ -1259,14 +1321,15 @@ function reader(A, s, n, b, k, w, mode, havepat, need, afterdd, kind, valued,
|
|
|
1259
1321
|
if (!afterdd && substr(w, 1, 1) == "-" && length(w) > 1) {
|
|
1260
1322
|
if (mode != "" && w ~ /^-[a-zA-Z]/) { scan_short(w, shortvals, b == "sed"); if (sc_skip) k++ }
|
|
1261
1323
|
else if (mode != "" && (w ~ /^--(regexp|file|expression|from-file)$/ || index(valued, " " w " "))) k++
|
|
1262
|
-
else if (index(valued2, " " w " "))
|
|
1324
|
+
else if (index(valued2, " " w " ")) {
|
|
1325
|
+
# --rawfile/--slurpfile name a file that is read, so it is judged like any other operand.
|
|
1326
|
+
if (w ~ /^--(raw|slurp)file$/ && k + 2 <= n) report_operand(A, k + 2, b, filenames)
|
|
1327
|
+
k += 2
|
|
1328
|
+
}
|
|
1263
1329
|
continue
|
|
1264
1330
|
}
|
|
1265
1331
|
if (need) { need = 0; continue }
|
|
1266
|
-
|
|
1267
|
-
if (kind != "" && matched_file != "") report(b " of " kind " (" matched_file ") prints credentials", "list the directory with ls, then name the exact files; for an env template read the .env.example")
|
|
1268
|
-
else if (kind != "" && via_pattern) report(b " of a glob that may match " kind " prints credentials", "list the directory with ls, then name the exact files; for an env template read the .env.example")
|
|
1269
|
-
else if (kind != "") report(b " of " kind " prints credentials", "name the file and the key you need in your report; ask the user to confirm a value instead of printing it; for an env template read the .env.example")
|
|
1332
|
+
report_operand(A, k, b, filenames)
|
|
1270
1333
|
}
|
|
1271
1334
|
}
|
|
1272
1335
|
|
|
@@ -1400,7 +1463,7 @@ function analyze(A, s, n, w, b, k, names, p, config, get, show, first) {
|
|
|
1400
1463
|
if (p && names == 0) report(b " -p/-x with no variable name prints the whole environment, including credentials", "printenv with the one non-credential variable you need")
|
|
1401
1464
|
return
|
|
1402
1465
|
}
|
|
1403
|
-
if (b ~ /^(cat|tac|nl|less|more|head|tail|bat|batcat|strings|xxd|od|hexdump|cut|sort|uniq|base64|wc|grep|egrep|fgrep|rg|ag|sed|awk|gawk|jq|yq)$/) {
|
|
1466
|
+
if (b ~ /^(cat|tac|nl|less|more|head|tail|bat|batcat|strings|xxd|od|hexdump|cut|sort|uniq|base64|wc|grep|egrep|fgrep|rg|ag|sed|awk|gawk|mawk|nawk|busybox|jq|yq|gojq|jaq)$/) {
|
|
1404
1467
|
reader(A, s, n)
|
|
1405
1468
|
for (k = 1; k <= nrt; k++) {
|
|
1406
1469
|
if (REDIRECT_TOKENS[k] > n || operand_kind(A, REDIRECT_TOKENS[k], ROFF[k]) == "") continue
|
|
@@ -1427,7 +1490,7 @@ BEGIN {
|
|
|
1427
1490
|
nprobe = split(".env .env.local .env.production .envrc id_rsa id_ed25519 id_ecdsa id_dsa credentials token token.json tokens.json github_token .netrc .npmrc .pypirc .pgpass .git-credentials kubeconfig", PROBE, " ")
|
|
1428
1491
|
nprobe2 = split(".kube/config .aws/credentials .docker/config.json /proc/1/environ /run/secrets/token /var/run/secrets/token", PROBE2, " ")
|
|
1429
1492
|
nextension = split("pem key p12 pfx jks keystore kdbx kubeconfig env", EXT, " ")
|
|
1430
|
-
ndispatch = split("cat tac nl less more head tail bat batcat strings xxd od hexdump cut sort uniq base64 wc dd cp mv ln rsync scp tar zip shopt set setopt unsetopt emulate grep egrep fgrep rg ag sed awk gawk jq yq kubectl oc docker podman docker-compose gh vault aws az gcloud pulumi cratis printenv export declare typeset env bash sh zsh dash ksh eval", DN, " ")
|
|
1493
|
+
ndispatch = split("cat tac nl less more head tail bat batcat strings xxd od hexdump cut sort uniq base64 wc dd cp mv ln rsync scp tar zip shopt set setopt unsetopt emulate grep egrep fgrep rg ag sed awk gawk mawk nawk busybox jq yq gojq jaq kubectl oc docker podman docker-compose gh vault aws az gcloud pulumi cratis printenv export declare typeset env bash sh zsh dash ksh eval", DN, " ")
|
|
1431
1494
|
nstem = split(".env .pem .key .p12 .pfx .jks .keystore .kdbx id_rsa id_ed25519 id_ecdsa id_dsa .netrc .npmrc .pgpass .pypirc .git-credentials .kube/ kubeconfig credentials credential .docker/ /environ run/secrets", STEM, " ")
|
|
1432
1495
|
CMD = ENVIRON["GS_COMMAND"]
|
|
1433
1496
|
for (pass = 1; pass <= 2; pass++) {
|
|
@@ -42,16 +42,76 @@ function candidate(s) {
|
|
|
42
42
|
# A generic attached option prefix must not hide a path spelling (-t.pi/...).
|
|
43
43
|
if (s ~ /^-/ && match(s, /[.\/]/) && RSTART > 1) candidate(substr(s, RSTART))
|
|
44
44
|
}
|
|
45
|
-
|
|
45
|
+
# An extended-glob group (`@(a|b)`, `*(x)`) is also read as one star, so `.e@(n)v` meets `.env`.
|
|
46
|
+
# A word with a brace span and quoting or expansion syntax is also kept whole, so the classifier sees it unsplit.
|
|
47
|
+
# Whole-quoted words and `${...}` parameter expansions do not brace-expand and are not kept.
|
|
48
|
+
function extglob_star(s, guard) {
|
|
49
|
+
while (guard++ < 64 && match(s, /[@?*+!]\([^()]*\)/)) s = substr(s, 1, RSTART - 1) "*" substr(s, RSTART + RLENGTH)
|
|
50
|
+
return s
|
|
51
|
+
}
|
|
52
|
+
# Quoted text never brace-expands or opens an extended glob, so its quoting, expansion and group syntax is
|
|
53
|
+
# neutralized before the classifier, which refuses that syntax in an unquoted spelling.
|
|
54
|
+
function quoted_literal(s) {
|
|
55
|
+
if (index(s, "{")) gsub(/[$`"\047\\]/, "_", s)
|
|
56
|
+
gsub(/[@?*+!]\(/, "__", s)
|
|
57
|
+
return s
|
|
58
|
+
}
|
|
59
|
+
function raw_brace_word(w, quote, first, bare) {
|
|
60
|
+
if (!(w ~ /[$`"\047\\]/)) return
|
|
61
|
+
first = substr(w, 1, 1); quote = (first == "\047" || first == "\"") ? first : ""
|
|
62
|
+
if (quote != "" && substr(w, length(w), 1) == quote && index(substr(w, 2, length(w) - 2), quote) == 0) return
|
|
63
|
+
bare = w; gsub(/\$\{/, "", bare)
|
|
64
|
+
if (index(bare, "{")) candidate(w)
|
|
65
|
+
}
|
|
66
|
+
function raw_brace_scan(s, n, i, c, word) {
|
|
46
67
|
n = length(s)
|
|
47
68
|
for (i = 1; i <= n; i++) {
|
|
48
69
|
c = substr(s, i, 1)
|
|
49
|
-
if (c ~ /[ \t\n\r;|&<>
|
|
70
|
+
if (c ~ /[ \t\n\r;|&<>]/) { raw_brace_word(word); word = "" }
|
|
71
|
+
else word = word c
|
|
72
|
+
}
|
|
73
|
+
raw_brace_word(word)
|
|
74
|
+
}
|
|
75
|
+
# After an expansion (or a leading tilde) the prefix of a path is unknown and could be the repository root, so the
|
|
76
|
+
# literal remainder is also judged as a path relative to the working directory: the word without its variable name
|
|
77
|
+
# and every suffix that starts after a slash. A variable holding a whole protected path stays unseen.
|
|
78
|
+
function remainder(word, rest, tail, count, slash) {
|
|
79
|
+
if (word == "") return
|
|
80
|
+
rest = word
|
|
81
|
+
if (!sub(/^\{[^}]*\}/, "", rest)) sub(/^[A-Za-z0-9_~]+/, "", rest)
|
|
82
|
+
candidate(rest)
|
|
83
|
+
tail = word
|
|
84
|
+
while (count++ < 16 && (slash = index(tail, "/")) > 0) { tail = substr(tail, slash + 1); candidate(tail) }
|
|
85
|
+
}
|
|
86
|
+
# Expansion syntax is not parsed: a whitespace-delimited chunk holding any of $ ` ~ ( ) { } is judged by every
|
|
87
|
+
# suffix that follows a slash in it, so a path after a substitution, a tilde form or a nested expansion is seen
|
|
88
|
+
# even when the lexer split the substitution at an inner space.
|
|
89
|
+
function chunk_suffixes(chunk, slash, count) {
|
|
90
|
+
gsub(/["\047\\]/, "", chunk)
|
|
91
|
+
if (chunk !~ /[$`~(){}]/) return
|
|
92
|
+
while (count++ < 16 && (slash = index(chunk, "/")) > 0) { chunk = substr(chunk, slash + 1); candidate(chunk) }
|
|
93
|
+
}
|
|
94
|
+
function scan(s, n, i, c, word, whole, flat, expanded, chunk) {
|
|
95
|
+
flat = extglob_star(s)
|
|
96
|
+
if (flat != s) scan(flat)
|
|
97
|
+
n = length(s)
|
|
98
|
+
for (i = 1; i <= n; i++) {
|
|
99
|
+
c = substr(s, i, 1)
|
|
100
|
+
if (c ~ /[ \t\n\r;|&<>]/) { chunk_suffixes(chunk); chunk = "" }
|
|
101
|
+
else chunk = chunk c
|
|
102
|
+
if (c ~ /[ \t\n\r;|&<>()\047"`$=\\]/) {
|
|
103
|
+
candidate(word)
|
|
104
|
+
if (expanded || word ~ /^~/) remainder(word)
|
|
105
|
+
word = ""
|
|
106
|
+
if (c ~ /[`$]/) expanded = 1
|
|
107
|
+
else if (c ~ /[ \t\n\r;|&<>]/) expanded = 0
|
|
108
|
+
}
|
|
50
109
|
else word = word c
|
|
51
110
|
if (c ~ /[ \t\n\r;|&<>()\047"`$]/) { candidate(whole); whole = "" }
|
|
52
111
|
else whole = whole c
|
|
53
112
|
}
|
|
54
|
-
candidate(word); candidate(whole)
|
|
113
|
+
candidate(word); candidate(whole); chunk_suffixes(chunk)
|
|
114
|
+
if (expanded || word ~ /^~/) remainder(word)
|
|
55
115
|
# Braces have a second boundary view; retain the original pattern above too.
|
|
56
116
|
if (s ~ /[{}]/) { gsub(/[{}]/, " ", s); scan(s) }
|
|
57
117
|
}
|
|
@@ -84,7 +144,7 @@ function unquote(s, i, c, c2, q, out, quoted) {
|
|
|
84
144
|
if (c == "$" && c2 == "\047" && q != "\047") { i = ansi(s, i + 2) - 1; out = out ansi_word; continue }
|
|
85
145
|
if (c == "\\" && q != "\047") { if (c2 != "\n") { out = out c2; quoted = quoted c2 }; i++; continue }
|
|
86
146
|
if ((c == "\047" || c == "\"") && (q == "" || q == c)) {
|
|
87
|
-
if (q == c) { candidate(quoted); quoted = ""; q = "" } else q = c
|
|
147
|
+
if (q == c) { candidate(quoted_literal(quoted)); quoted = ""; q = "" } else q = c
|
|
88
148
|
continue
|
|
89
149
|
}
|
|
90
150
|
out = out c; if (q != "") quoted = quoted c
|
|
@@ -92,20 +152,29 @@ function unquote(s, i, c, c2, q, out, quoted) {
|
|
|
92
152
|
return out
|
|
93
153
|
}
|
|
94
154
|
function protected(s) { return s in protected_paths }
|
|
155
|
+
# A lexical or structural failure: the command text is not modeled, so write-target mode falls back to
|
|
156
|
+
# classifying the whole text. A command the policy does not accept (cmd_failed) does the same: a protected
|
|
157
|
+
# reference anywhere in a command that is not entirely read-only must keep refusing it.
|
|
158
|
+
# Only when every command is accepted are the words of pure readers dropped from classification.
|
|
159
|
+
function fail() { bad = unparsed = 1 }
|
|
160
|
+
# Reference extraction for one stretch of raw command text.
|
|
161
|
+
function emit_text(t) { scan(t); scan(unquote(t)); raw_brace_scan(t) }
|
|
95
162
|
function endword() {
|
|
96
163
|
if (!inword) return
|
|
97
164
|
if (redirect) {
|
|
98
165
|
if (!(redirect == 2 && word ~ /^([0-9]+|-)$/)) {
|
|
99
166
|
file_redirect = 1
|
|
100
|
-
if (changed_cwd ||
|
|
167
|
+
if (changed_cwd || lead_glob) unparsed = 1
|
|
168
|
+
if (changed_cwd || word ~ /[$`*?\[{~()]/ || protected(word)) bad = 1
|
|
101
169
|
}
|
|
102
170
|
redirect = 0
|
|
103
171
|
} else {
|
|
104
|
-
if (++argc > 256)
|
|
172
|
+
if (++argc > 256) fail()
|
|
105
173
|
W[argc] = word
|
|
106
174
|
globbed[argc] = glob
|
|
175
|
+
leading[argc] = lead_glob
|
|
107
176
|
}
|
|
108
|
-
word = ""; inword = glob = 0
|
|
177
|
+
word = ""; inword = glob = lead_glob = 0
|
|
109
178
|
}
|
|
110
179
|
# Each exemption permits only the reviewed read/presentation options below. No unknown
|
|
111
180
|
# options, long-option abbreviations or implicit value-taking short clusters are accepted.
|
|
@@ -150,6 +219,8 @@ function options_allowed(verb, first, i, w) {
|
|
|
150
219
|
if (w == "-m" || w == "--message") { if (++i > argc) return 0; continue }
|
|
151
220
|
if (w ~ /^-m./) continue
|
|
152
221
|
if (w == "-F") {
|
|
222
|
+
# Whether the file is protected is decided from the reference list, so its words are kept.
|
|
223
|
+
force_emit = 1
|
|
153
224
|
if (++i > argc || W[i] ~ /[$`*?\[{]/ || protected(W[i])) return 0
|
|
154
225
|
continue
|
|
155
226
|
}
|
|
@@ -162,6 +233,20 @@ function options_allowed(verb, first, i, w) {
|
|
|
162
233
|
}
|
|
163
234
|
return 1
|
|
164
235
|
}
|
|
236
|
+
# awk is read-only only for the shape awk [-F sep] 'program' operands. Every other option (-f, -v, -e, -i,
|
|
237
|
+
# --include, --, ...) is refused. awk output, execution and file reads need one of > | system getline close
|
|
238
|
+
# fflush or the gawk @ directives; a program containing any of them (in any letter case) is not accepted.
|
|
239
|
+
# This deny-list relies on awk having no other output primitive, which holds for POSIX, gawk, mawk and BSD awk.
|
|
240
|
+
function awk_allowed( i, w, program) {
|
|
241
|
+
i = 2
|
|
242
|
+
if (W[i] == "-F") { if (++i > argc || W[i] ~ /[$`]/ || globbed[i]) return 0; i++ }
|
|
243
|
+
else if (W[i] ~ /^-F/) { if (W[i] ~ /[$`]/ || globbed[i]) return 0; i++ }
|
|
244
|
+
if (i > argc || W[i] ~ /^-/ || globbed[i]) return 0
|
|
245
|
+
program = tolower(W[i])
|
|
246
|
+
if (program ~ /[>|@]/ || index(program, "system") || index(program, "getline") || index(program, "close") || index(program, "fflush")) return 0
|
|
247
|
+
for (i++; i <= argc; i++) if (W[i] ~ /^-/ && W[i] != "-") return 0
|
|
248
|
+
return 1
|
|
249
|
+
}
|
|
165
250
|
# Only literal git -C metadata (with exact arity) or staging/commit commands
|
|
166
251
|
# (with their normal option checks) are exempt; no other global option is.
|
|
167
252
|
# -C gives no protection from hostile repository config (fsmonitor, hooks, pager,
|
|
@@ -181,72 +266,114 @@ function print_only_sed( i, ended) {
|
|
|
181
266
|
}
|
|
182
267
|
return 1
|
|
183
268
|
}
|
|
184
|
-
|
|
269
|
+
# Verbs whose reviewed options are display flags only: a word-leading glob that expands to an option-shaped
|
|
270
|
+
# file name can change what they print, never what they write or run. Every other verb refuses a leading glob.
|
|
271
|
+
function glob_safe() {
|
|
272
|
+
return W[1] ~ /^(cat|head|tail|wc|ls|grep|egrep|fgrep)$/ || (W[1] == "git" && W[2] == "ls-files")
|
|
273
|
+
}
|
|
274
|
+
# The two managed corpus scripts are admitted for execution only by exact literal spelling: the path is
|
|
275
|
+
# compared as a whole word (no ./, .., variable, glob or quoting variant can equal it), every other word must
|
|
276
|
+
# be one of the script's own documented options, and nothing is admitted after a directory change, which
|
|
277
|
+
# would select a different checkout's copy. Redirects stay classified by the caller.
|
|
278
|
+
function managed_script( i) {
|
|
279
|
+
if (changed_cwd) return 0
|
|
280
|
+
if (W[1] == "node" && W[2] == ".cratis/ai/hooks/scripts/cratis-check-pr.mjs") {
|
|
281
|
+
for (i = 3; i <= argc; i++) {
|
|
282
|
+
if (W[i] == "--strict") continue
|
|
283
|
+
if (W[i] !~ /^--(body-file|label|add-label|remove-label|pr|base|repo)$/) return 0
|
|
284
|
+
if (++i > argc || W[i] == "" || W[i] ~ /^-/ || globbed[i]) return 0
|
|
285
|
+
}
|
|
286
|
+
return 1
|
|
287
|
+
}
|
|
288
|
+
if (W[1] == "bash" && W[2] == ".cratis/ai/hooks/scripts/cratis-cleanup-worktrees.sh") {
|
|
289
|
+
for (i = 3; i <= argc; i++) if (W[i] !~ /^--(dry-run|quiet|no-fetch)$/) return 0
|
|
290
|
+
return 1
|
|
291
|
+
}
|
|
292
|
+
return 0
|
|
293
|
+
}
|
|
294
|
+
function allow_command( verb, first, i) {
|
|
185
295
|
if (!argc) return
|
|
186
|
-
if (++commands > 64) {
|
|
296
|
+
if (++commands > 64) { fail(); return }
|
|
297
|
+
for (i = 1; i <= argc; i++) if (leading[i] && !glob_safe()) { cmd_failed = 1; return }
|
|
298
|
+
if (managed_script()) return
|
|
187
299
|
verb = W[1]; first = 2
|
|
188
300
|
if (verb == "cd") {
|
|
189
301
|
directory_only = 1
|
|
190
|
-
if (argc != 2 || W[2] == "" || W[2] ~ /^-/ || globbed[2] || file_redirect)
|
|
302
|
+
if (argc != 2 || W[2] == "" || W[2] ~ /^-/ || globbed[2] || file_redirect) fail()
|
|
191
303
|
changed_cwd = 1
|
|
192
304
|
return
|
|
193
305
|
}
|
|
194
|
-
if (verb == "sed") { if (!print_only_sed())
|
|
306
|
+
if (verb == "sed") { if (!print_only_sed()) cmd_failed = 1; return }
|
|
195
307
|
if (verb == "git" && W[2] == "-C") {
|
|
196
308
|
if (argc >= 4 && W[3] != "" && W[3] !~ /^-/ && !globbed[3] && W[4] ~ /^(add|commit)$/) {
|
|
197
309
|
verb = verb " " W[4]; first = 5
|
|
198
310
|
} else {
|
|
199
|
-
if (!directory_metadata())
|
|
311
|
+
if (!directory_metadata()) cmd_failed = 1
|
|
200
312
|
return
|
|
201
313
|
}
|
|
202
314
|
} else if (verb == "git") { verb = verb " " W[2]; first = 3 }
|
|
203
315
|
if (verb == "git add" || verb == "git commit") changed_index = 1
|
|
204
316
|
if (verb == "git branch") {
|
|
205
317
|
conditional_only = 1
|
|
206
|
-
if (argc != 3 || W[3] != "--show-current") {
|
|
318
|
+
if (argc != 3 || W[3] != "--show-current") { cmd_failed = 1; return }
|
|
207
319
|
}
|
|
208
|
-
if (
|
|
320
|
+
if (verb == "awk") { if (!awk_allowed()) cmd_failed = 1; return }
|
|
321
|
+
if (!(verb in verbs) || !options_allowed(verb, first)) cmd_failed = 1
|
|
209
322
|
}
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
323
|
+
# `upto` is the index just past the command's raw text; `resume` is where the next command's raw text starts.
|
|
324
|
+
# A command whose policy accepts it, with no redirect and no directory change, is a pure reader and its words
|
|
325
|
+
# are not write targets; every other command keeps all of its raw text classified.
|
|
326
|
+
function command_end(separator, upto, resume, silent) {
|
|
327
|
+
endword(); cmd_failed = 0; allow_command()
|
|
328
|
+
if (cmd_failed) { bad = 1; unparsed = 1 }
|
|
329
|
+
if (W[1] == "cd" && (separator == "|" || previous_pipe)) fail()
|
|
330
|
+
silent = argc && !cmd_failed && !cmd_redirect && !force_emit && W[1] != "cd"
|
|
331
|
+
if (!silent && upto > seg_start) emitted[++emitted_count] = substr(lexed, seg_start, upto - seg_start)
|
|
332
|
+
seg_start = resume
|
|
213
333
|
previous_pipe = (separator == "|")
|
|
214
|
-
|
|
334
|
+
cmd_redirect = force_emit = 0
|
|
335
|
+
delete W; delete globbed; delete leading; argc = 0
|
|
215
336
|
}
|
|
216
337
|
function readonly(s, i, c, c2, sq, dq) {
|
|
338
|
+
lexed = s; seg_start = 1
|
|
217
339
|
for (i = 1; i <= length(s); i++) {
|
|
218
340
|
c = substr(s, i, 1); c2 = substr(s, i + 1, 1)
|
|
219
341
|
if (sq) { if (c == "\047") sq = 0; else word = word c; continue }
|
|
220
|
-
if (c == "\\") { if (c2 != "\n") { word = word c2; inword = 1 }; i++; continue }
|
|
342
|
+
if (c == "\\") { if (c2 != "\n") { word = word c2; inword = 1 } else unparsed = 1; i++; continue }
|
|
221
343
|
if (c == "\047" && !dq) { sq = inword = 1; continue }
|
|
222
344
|
if (c == "\"") { dq = !dq; inword = 1; continue }
|
|
223
345
|
# Expansions and globs can supply option words the per-verb tables never see.
|
|
224
|
-
if (c == "`" || c == "$")
|
|
346
|
+
if (c == "`" || c == "$") fail()
|
|
225
347
|
if (dq) { word = word c; continue }
|
|
226
348
|
# Directory metadata requires a literal -C operand, not a pathname expansion.
|
|
227
349
|
if (c == "*" || c == "?" || c == "[") glob = 1
|
|
228
350
|
# A glob that starts a word, or follows a leading dash, may expand to an option word.
|
|
229
|
-
|
|
351
|
+
# Only a word-leading * or ? is deferred to the per-verb rule; one after a leading dash, or a leading
|
|
352
|
+
# bracket class (`[-]*` can name an option), is refused outright.
|
|
353
|
+
if ((c == "*" || c == "?" || c == "[") && (word == "" ? !(c == "[" && c2 ~ /[ \t]/) : substr(word, 1, 1) == "-")) {
|
|
354
|
+
if (c == "[" || word != "") fail(); else lead_glob = 1
|
|
355
|
+
}
|
|
230
356
|
if (c == "#" && !inword) { while (i <= length(s) && substr(s, i, 1) != "\n") i++; c = "\n" }
|
|
231
|
-
if (c == "(" || c == ")" || c == "{" || c == "}")
|
|
357
|
+
if (c == "(" || c == ")" || c == "{" || c == "}") fail()
|
|
232
358
|
if (c == ">" || c == "<" || (c == "&" && c2 == ">")) {
|
|
359
|
+
cmd_redirect = 1
|
|
233
360
|
if (inword && word ~ /^[0-9]+$/) { word = ""; inword = 0 } else endword()
|
|
234
|
-
if (redirect)
|
|
235
|
-
if (c == "<" && (c2 == "<" || c2 == "("))
|
|
361
|
+
if (redirect) fail()
|
|
362
|
+
if (c == "<" && (c2 == "<" || c2 == "(")) fail()
|
|
236
363
|
redirect = c == ">" || c == "&" || (c == "<" && c2 == ">")
|
|
237
364
|
if (c == ">" && c2 == "&") redirect = 2
|
|
238
365
|
if (c2 ~ /[><&|]/) { i++; if (substr(s, i + 1, 1) == ">") i++ }
|
|
239
366
|
continue
|
|
240
367
|
}
|
|
241
|
-
if (c == "&" && c2 == "&") { command_end("&&"); if (redirect)
|
|
242
|
-
if (c == "|" && c2 != "|") { has_pipeline = 1; command_end("|"); if (redirect)
|
|
243
|
-
if (c ~ /[;|&\n]/) { nonconditional_separator = 1; command_end(c); if (redirect)
|
|
368
|
+
if (c == "&" && c2 == "&") { command_end("&&", i, i + 2); if (redirect) fail(); i++; continue }
|
|
369
|
+
if (c == "|" && c2 != "|") { has_pipeline = 1; command_end("|", i, i + 1); if (redirect) fail(); continue }
|
|
370
|
+
if (c ~ /[;|&\n]/) { nonconditional_separator = 1; command_end(c, i, i + 1); if (redirect) fail(); continue }
|
|
244
371
|
if (c ~ /[ \t\r]/) { endword(); continue }
|
|
245
372
|
word = word c; inword = 1
|
|
246
373
|
}
|
|
247
|
-
command_end()
|
|
374
|
+
command_end("", length(s) + 1, length(s) + 1)
|
|
248
375
|
if (sq || dq || redirect || (conditional_only && (nonconditional_separator || has_pipeline)) ||
|
|
249
|
-
(directory_only && (nonconditional_separator || (has_pipeline && changed_index))))
|
|
376
|
+
(directory_only && (nonconditional_separator || (has_pipeline && changed_index)))) fail()
|
|
250
377
|
return !bad
|
|
251
378
|
}
|
|
252
379
|
BEGIN {
|
|
@@ -260,6 +387,10 @@ BEGIN {
|
|
|
260
387
|
policy("printf", "", ""); policy("test", "", ""); policy("[", "", "")
|
|
261
388
|
policy("true", "", ""); policy("false", "", ""); policy("pwd", "", "")
|
|
262
389
|
policy("df", "h", "")
|
|
390
|
+
# jq has no file-write or exec primitive (output goes to stdout, debug to stderr), so its filter is a plain
|
|
391
|
+
# operand. No value-taking option is accepted (--arg, -f, -L, --rawfile, --slurpfile, --from-file, --indent).
|
|
392
|
+
policy("jq", "rcesnRSjaMC", "--raw-output --compact-output --slurp --null-input --exit-status --raw-input --sort-keys --join-output --ascii-output --monochrome-output --tab")
|
|
393
|
+
policy("awk", "", "")
|
|
263
394
|
policy("git status", "sb", "--short --porcelain")
|
|
264
395
|
policy("git diff", "p", "--stat --numstat --name-only --name-status --cached --staged --check --no-color --cc")
|
|
265
396
|
policy("git log", "p", "--oneline --stat --name-only --no-color --follow")
|
|
@@ -271,12 +402,18 @@ BEGIN {
|
|
|
271
402
|
for (i = 0; i < 16; i++) HEX[substr("0123456789abcdef", i + 1, 1)] = i
|
|
272
403
|
ESC["n"] = 10; ESC["r"] = 13; ESC["t"] = 9; ESC["\\"] = 92; ESC["\047"] = 39; ESC["\""] = 34
|
|
273
404
|
text = ENVIRON["GSW_COMMAND"]
|
|
274
|
-
if (ENVIRON["GSW_MODE"] == "references") {
|
|
405
|
+
if (ENVIRON["GSW_MODE"] == "references") { emit_text(text); exit 0 }
|
|
406
|
+
if (ENVIRON["GSW_MODE"] == "targets") {
|
|
407
|
+
readonly(text)
|
|
408
|
+
if (unparsed) emit_text(text)
|
|
409
|
+
else for (i = 1; i <= emitted_count; i++) emit_text(emitted[i])
|
|
410
|
+
exit 0
|
|
411
|
+
}
|
|
275
412
|
n = split(ENVIRON["GSW_PROTECTED"], a, "\n"); for (i = 1; i <= n; i++) protected_paths[a[i]] = 1
|
|
276
413
|
exit readonly(text) ? 0 : 1
|
|
277
414
|
}
|
|
278
415
|
AWK
|
|
279
|
-
candidates_text="$(GSW_COMMAND="$command_text" GSW_MODE=
|
|
416
|
+
candidates_text="$(GSW_COMMAND="$command_text" GSW_MODE=targets LC_ALL=C awk "$program" </dev/null)" || parser_failed
|
|
280
417
|
[ -n "$candidates_text" ] || exit 0
|
|
281
418
|
candidates=()
|
|
282
419
|
while IFS= read -r candidate; do candidates[${#candidates[@]}]="$candidate"; done <<<"$candidates_text"
|
|
@@ -288,11 +425,21 @@ classifier="$script_directory/cratis-guard-writes.sh"
|
|
|
288
425
|
classifier_input="$(jq -nc --arg cwd "$cwd" --args '{cwd:$cwd,tool_input:{file_paths:$ARGS.positional}}' -- "${candidates[@]}")" || parser_failed
|
|
289
426
|
# Tool labels and this hook shell do not establish the execution shell options.
|
|
290
427
|
# Keep conservative dotfile intersections for every command, especially mutators.
|
|
291
|
-
|
|
428
|
+
classifier_error="$(mktemp)" || parser_failed
|
|
429
|
+
trap 'rm -f "$classifier_error"' EXIT
|
|
430
|
+
references="$("$BASH" "$classifier" --references <<<"$classifier_input" 2>"$classifier_error")" || {
|
|
431
|
+
# A command that is not read-only is refused whatever the classifier could model, so an unmodeled word
|
|
432
|
+
# (for example program text inside an interpreter heredoc) must not hide the refusal behind a parser
|
|
433
|
+
# failure. A read-only command keeps the fail-closed parser failure.
|
|
434
|
+
if ! GSW_COMMAND="$command_text" GSW_MODE=readonly GSW_PROTECTED="" LC_ALL=C awk "$program" </dev/null; then
|
|
435
|
+
printf '%s\n' 'BLOCKED by cratis-guard-shell-writes: this command is not read-only and its references could not all be modeled; use the edit/write tools, or report the intended change.' >&2
|
|
436
|
+
exit 2
|
|
437
|
+
fi
|
|
438
|
+
cat "$classifier_error" >&2
|
|
292
439
|
printf '%s\n' 'BLOCKED by cratis-guard-shell-writes: could not check references (write guard failed).' >&2
|
|
293
440
|
exit 2
|
|
294
441
|
}
|
|
295
442
|
[ -n "$references" ] || exit 0
|
|
296
443
|
if GSW_COMMAND="$command_text" GSW_MODE=readonly GSW_PROTECTED="$references" LC_ALL=C awk "$program" </dev/null; then exit 0; fi
|
|
297
|
-
printf "BLOCKED by cratis-guard-shell-writes: protected reference '%s' (cratis-guard-writes). Use the edit/write tools, or report the intended change.\n" "${references%%$'\n'*}" >&2
|
|
444
|
+
printf "BLOCKED by cratis-guard-shell-writes: protected reference '%s' (cratis-guard-writes). Use the edit/write tools, or report the intended change; for a read, name literal paths or use the Read/Grep tools.\n" "${references%%$'\n'*}" >&2
|
|
298
445
|
exit 2
|