@cratis/pi 8.0.0 → 8.0.1

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.
@@ -514,8 +514,12 @@ Cratis AI repository pins each row with JSON-only invocations (no candidate text
514
514
  | Aliases and functions | Literal executable names | Protected references and unresolved destructive effects stay visible | What an alias, function or shadowing executable does; definitions are never loaded or run |
515
515
  | Directory resolution | Literal cwd, `cd`, Git `-C`, logical and physical (symlink) path checks | Ambiguous destructive cwd; links resolving into protected or whole-worktree scope | `CDPATH`, unmodeled directory options, symlinks created during execution |
516
516
  | Expansion and quoting | Literal and quoted operands; ordinary source globs and bounded literal-prefix probes; archive operands whose first character is a literal other than `-`/`@` (`./?x`, `dir/?x`; a quoted expansion such as `"$x"` never anchors, and `--` is no anchor) or quoting | Archive operands that can expand to options or `@` (above); unresolved destructive operands; over-budget parsing | General shell grammar (bash versus zsh), indirect expansion |
517
+ | Copy option integrity (`cp`, `rsync`) | Both guards check that active globs cannot supply options. A literal `./` or `dir/` prefix keeps the first character from being `-`. Simple unprefixed `*`/`?` source globs can still work in plain commands with known roles and a supplied absolute `cwd`, after a bounded clean filename check. | Globs that can supply an option, even after `--`. The secrets guard does not use the filename check to admit option-value or destination globs. | Changes to filenames after the check |
517
518
  | Wrappers and shell entry points | Modeled wrappers (`env`, `nice`, `sudo`, `timeout`, …) and literal nested `sh -c` bodies | Protected inline-interpreter vocabulary; executable heredocs; uncertain destructive wrapper context | Dynamic code, external scripts, generated stdin, custom wrappers, archive file-list (`-T`) contents |
518
519
 
520
+ The copy check does not authorize the transfer. Even with `./`, the secrets guard still checks for
521
+ protected sources, and the shell-write guard independently checks write scope.
522
+
519
523
  Remaining redesign (a command-shape allowlist per tool family, argument-file and dynamic-code handling, a
520
524
  recovered #1222 shape inventory) stays on #1223. Shipped hook changes reach a consuming repository through
521
525
  `cratis ai update`.
@@ -90,6 +90,43 @@ shift
90
90
  for link_word in "$@"; do linked_targets_word "$link_word"; done
91
91
  LINKS
92
92
 
93
+ # The simple-copy exception reads names only. Match a superset of ordinary shell globbing
94
+ # (include hidden names and fold ASCII case), without evaluating a word as shell code.
95
+ IFS= read -r -d '' copy_glob_program <<'COPY_GLOB' || true
96
+ const { accessSync, constants, opendirSync } = require('node:fs');
97
+
98
+ function matches(name, pattern) {
99
+ let n = 0, p = 0, star = -1, retry = 0;
100
+ while (n < name.length) {
101
+ if (pattern[p] === '*') { star = p++; retry = n; }
102
+ else if (pattern[p] === '?' || pattern[p] === name[n]) { p++; n++; }
103
+ else if (star !== -1) { p = star + 1; n = ++retry; }
104
+ else return false;
105
+ }
106
+ while (pattern[p] === '*') p++;
107
+ return p === pattern.length;
108
+ }
109
+
110
+ try {
111
+ const [cwd, ...patterns] = process.argv.slice(1);
112
+ accessSync(cwd, constants.R_OK | constants.X_OK);
113
+ const directory = opendirSync(cwd, { encoding: 'latin1' });
114
+ const folded = patterns.map(pattern => pattern.toLowerCase());
115
+ let entries = 0, count = 0;
116
+ for (let entry; (entry = directory.readSync()) !== null;) {
117
+ if (++entries > 5000 || /[^\x20-\x7e]/.test(entry.name)) process.exit(3);
118
+ for (const pattern of folded) {
119
+ if (!matches(entry.name.toLowerCase(), pattern)) continue;
120
+ if (++count > 200 || !/^[a-zA-Z0-9._@+=,:%-]+$/.test(entry.name) || entry.name.startsWith('-')) process.exit(3);
121
+ }
122
+ }
123
+ directory.closeSync();
124
+ console.log(`OK:${count}`);
125
+ } catch {
126
+ process.exitCode = 3;
127
+ }
128
+ COPY_GLOB
129
+
93
130
  # Without a supplied working directory there are no literal links to resolve. Keep the cheap textual
94
131
  # pre-filter, but always parse bulk-reader families: even a root operand can cover mounted secrets.
95
132
  command_cwd="$(jq -r '.cwd // empty' <<<"$input")" || parser_failed
@@ -131,8 +168,8 @@ function addw(text, kind, k) {
131
168
  for (k = 1; k <= length(text); k++) { wmask = wmask (quote_boundary ? toupper(kind) : kind); quote_boundary = 0 }
132
169
  }
133
170
  function flush_word() {
134
- if (inword) { W[++nw] = word; WP[nw] = wplain; WM[nw] = wmask; WU[nw] = wunquoted }
135
- word = ""; wmask = ""; inword = 0; wquoted = 0; wplain = 0; quote_boundary = 0; wunquoted = 0
171
+ if (inword) { W[++nw] = word; WP[nw] = wplain; WM[nw] = wmask; WU[nw] = wunquoted; WT[nw] = wtranslated }
172
+ word = ""; wmask = ""; inword = 0; wquoted = 0; wplain = 0; quote_boundary = 0; wunquoted = 0; wtranslated = 0
136
173
  }
137
174
  # Decodes the body of an ANSI-C string $'...' (i is just after the opening quote) into the word and returns the
138
175
  # position after the closing quote. The same decoder is in cratis-guard-store-mutations.sh (`ansi_c`).
@@ -755,11 +792,11 @@ function end_cmd( k, m, w, j, hit, exempt, path_git, paths_only, test_filter)
755
792
  if (RLENGTH == length(w)) k++
756
793
  continue
757
794
  }
758
- V[++m] = w; VM[m] = WM[k]
795
+ V[++m] = w; VM[m] = WM[k]; VT[m] = WT[k]
759
796
  }
760
797
  if (m > 0) { check_inputs(V, 1, m); analyze(V, 1, m) }
761
798
  nwrap = 0
762
- delete W; delete WP; delete WM; delete WU; delete V; delete VM; nw = 0
799
+ delete W; delete WP; delete WM; delete WU; delete WT; delete V; delete VM; delete VT; nw = 0
763
800
  }
764
801
  function cmdsub(s, i, n, start, depth, c, j) {
765
802
  complex = 1
@@ -867,7 +904,7 @@ function tokenize(s, i, n, c, c2, j, dash, delim) {
867
904
  for (i = 1; i <= ncd; i++) CWD[i] = QUEUE_DIR[qi, i]
868
905
  effective_dir = CWD[1]; cwd_depth = 0; cwd_conditional = 0; nwrap = 0
869
906
  clear_pipeline(); pipe_input = 0; filter_compound = 0
870
- n = length(s); i = 1; nw = 0; word = ""; wmask = ""; inword = 0; wquoted = 0; wplain = 0; wunquoted = 0; nheredoc = 0; hd_shell = 0; hd_interp = 0; quote_boundary = 0
907
+ n = length(s); i = 1; nw = 0; word = ""; wmask = ""; inword = 0; wquoted = 0; wplain = 0; wunquoted = 0; wtranslated = 0; nheredoc = 0; hd_shell = 0; hd_interp = 0; quote_boundary = 0
871
908
  delete W; delete WP; delete WM
872
909
  while (i <= n) {
873
910
  c = substr(s, i, 1)
@@ -883,12 +920,16 @@ function tokenize(s, i, n, c, c2, j, dash, delim) {
883
920
  addw(substr(s, i + 1, j - 1), "l"); quote_boundary = 1; i += j + 1; continue
884
921
  }
885
922
  if (c == "$" && substr(s, i + 1, 1) == "\047") { wquoted = 1; inword = 1; quote_boundary = 1; i = ansi_c(s, i + 2, n); quote_boundary = 1; continue }
886
- if (c == "$" && substr(s, i + 1, 1) == "\"") { i++; c = "\"" }
923
+ if (c == "$" && substr(s, i + 1, 1) == "\"") { wtranslated = 1; i++; c = "\"" }
887
924
  if (c == "\"") {
888
925
  wquoted = 1; inword = 1; quote_boundary = 1; i++; dq_closed = 0
889
926
  while (i <= n) {
890
927
  c = substr(s, i, 1)
891
- if (c == "\\") { addw(substr(s, i + 1, 1), "l"); i += 2; continue }
928
+ if (c == "\\") {
929
+ c2 = substr(s, i + 1, 1)
930
+ if (c2 != "\n") addw(c2, "l")
931
+ i += 2; continue
932
+ }
892
933
  if (c == "\"") { quote_boundary = 1; dq_closed = 1; i++; break }
893
934
  if (c == "$" && substr(s, i + 1, 1) == "(") { i = cmdsub(s, i + 2, n); addw("x", "c"); continue }
894
935
  if (c == "`") {
@@ -2094,6 +2135,84 @@ function drop_redirects(A, s, n, k, m, w) {
2094
2135
  }
2095
2136
  return m
2096
2137
  }
2138
+ # A static path pattern keeps its prefix through brace expansion and globbing. No part can
2139
+ # split into an independent word. For `#` repetition, require a literal directory and a
2140
+ # literal suffix atom before the first `#`, so the repetition cannot consume the prefix.
2141
+ function anchored_copy_pattern(w, m, translated) {
2142
+ if (translated || m !~ /^[uUdDlL]+$/ || w !~ /^[a-zA-Z0-9_.\/][][a-zA-Z0-9_.*?,\/{}#-]*$/) return 0
2143
+ return !index(w, "#") || w ~ /^\/?([a-zA-Z0-9_.][a-zA-Z0-9_.-]*\/)+[a-zA-Z0-9_.-]+#/
2144
+ }
2145
+ # Inspect decoded words with their quote/escape masks. A glob must retain a literal first
2146
+ # character other than `-`; an empty quote or an option terminator contributes no character.
2147
+ function copy_glob_risky(w, m, translated, k, c, mk, glob, unstable, first) {
2148
+ first = substr(w, 1, 1)
2149
+ unstable = translated || first == "-" || tolower(substr(m, 1, 1)) == "u" && first == "~"
2150
+ for (k = 1; k <= length(w); k++) {
2151
+ c = substr(w, k, 1); mk = tolower(substr(m, k, 1))
2152
+ if (mk == "c" || c == "$" && mk != "l") unstable = 1
2153
+ if (mk != "u") continue
2154
+ if (c ~ /[{#]/) unstable = 1
2155
+ # Do not dismiss an active `[` from the decoded spelling: an escaped member can
2156
+ # look like a class opener even when the shell sees an ordinary bracket expression.
2157
+ if (c ~ /[*?^(]/ || c == "[" || glob_negation(w, k)) {
2158
+ glob = 1
2159
+ if (k == 1 || c == "(" && k == 2 && tolower(substr(m, 1, 1)) == "u" && first ~ /[+!@]/) unstable = 1
2160
+ }
2161
+ }
2162
+ return glob && unstable && !anchored_copy_pattern(w, m, translated)
2163
+ }
2164
+ function simple_copy_pattern(w, m) { return length(w) <= 128 && w ~ /^[*?][a-zA-Z0-9._*?-]*$/ && m ~ /^u+$/ }
2165
+ function copy_word_expands(w, m, k) {
2166
+ if (expands(w, m)) return 1
2167
+ for (k = 1; k <= length(w); k++) if (tolower(substr(m, k, 1)) == "u" && substr(w, k, 1) ~ /[#^]/) return 1
2168
+ return 0
2169
+ }
2170
+ # Keep value-free flags aligned with the copy schemas in the shell-write guard. This
2171
+ # only establishes operand roles for the metadata exception, not permission to copy.
2172
+ function copy_value_free_flag(w, b) {
2173
+ if (b == "cp") return w ~ /^-[rRaTpfvniuLPHdl]+$/ || w ~ /^--(archive|recursive|no-target-directory|force|verbose|no-clobber|update)$/
2174
+ return w ~ /^-[rapvniuLPHdzctgoDlq]+$/ || w ~ /^--(archive|recursive|force|verbose|update|compress|times|perms|ignore-existing|existing|dry-run|delete|delete-before|delete-during|delete-delay|delete-after|delete-excluded)$/
2175
+ }
2176
+ # Establish source roles only for a plain invocation with known option arity and a fixed destination.
2177
+ function copy_source_start(A, s, n, b, k, w) {
2178
+ if (s != 1 || n != nw || W[1] != b || WM[1] !~ /^u+$/ || qi != 1 || commands != 1 ||
2179
+ filter_compound || complex || nwrap || ENVIRON["GS_GLOB_SAFE"] != "1" || ENVIRON["GS_CWD"] !~ /^\//) return 0
2180
+ for (k = s + 1; k < n; k++) {
2181
+ w = A[k]
2182
+ if (expands(w, mask_of(A, k)) || VT[k]) break
2183
+ if (w == "--") { k++; break }
2184
+ if (!copy_value_free_flag(w, b)) break
2185
+ }
2186
+ return k < n ? k : 0
2187
+ }
2188
+ function copy_glob_probe(patterns, cmd, line, result, complete, status) {
2189
+ cmd = "NODE_OPTIONS= NODE_PATH= node -e " shell_quote(ENVIRON["GS_COPY_GLOB_PROGRAM"]) " -- " \
2190
+ shell_quote(ENVIRON["GS_CWD"]) patterns " 2>/dev/null"
2191
+ result = cmd | getline line
2192
+ if (result == 1 && line ~ /^OK:[0-9][0-9]?[0-9]?$/ && substr(line, 4) + 0 <= 200) complete = (cmd | getline line) == 0
2193
+ status = close(cmd)
2194
+ return complete && status == 0
2195
+ }
2196
+ function copy_globs_safe(A, s, n, b, k, w, m, start, patterns, count) {
2197
+ for (k = s + 1; k <= n; k++) {
2198
+ w = A[k]; m = mask_of(A, k)
2199
+ # The tokenizer separates input operators and their targets; quoted lookalikes are arguments.
2200
+ if (w ~ /^[0-9]*[<>]+&?-?$/ && m ~ /^u+$/) { k++; continue }
2201
+ if (!copy_glob_risky(w, m, VT[k])) continue
2202
+ if (!simple_copy_pattern(w, m) || VT[k]) return 0
2203
+ if (!start) start = copy_source_start(A, s, n, b)
2204
+ if (!start || k < start || k == n || ++count > 16) return 0
2205
+ patterns = patterns " " shell_quote(w)
2206
+ }
2207
+ if (!count) return 1
2208
+ for (k = start; k <= n; k++) {
2209
+ w = A[k]; m = mask_of(A, k)
2210
+ if (w ~ /^[-~]/ || w == "" || VT[k]) return 0
2211
+ if (!copy_word_expands(w, m)) continue
2212
+ if (k == n || !(simple_copy_pattern(w, m) || anchored_copy_pattern(w, m, VT[k]))) return 0
2213
+ }
2214
+ return copy_glob_probe(patterns)
2215
+ }
2097
2216
  function transfer_files(A, s, n, b, k, last, options, kind, w, eq, off, ended) {
2098
2217
  for (k = s + 1; k <= n; k++) if (A[k] ~ /^-./) options = 1
2099
2218
  last = n
@@ -2181,6 +2300,8 @@ function analyze(A, s, n, w, b, k, names, p, config, get, show, first) {
2181
2300
  for (k = s + 1; k <= n; k++) if (A[k] ~ /^if=/ && (config = operand_kind(A, k, 3)) != "") report("dd reads " config, "name only a non-credential input file")
2182
2301
  return
2183
2302
  }
2303
+ if (b ~ /^(cp|rsync)$/ && !copy_globs_safe(A, s, n, b))
2304
+ report(b " glob can supply an option", "use literal paths or prefix the glob with ./ to retain a non-option first character")
2184
2305
  if (b ~ /^(cp|mv|ln|rsync|scp|tar|zip)$/) { transfer_files(A, s, n, b); return }
2185
2306
  if (b ~ /^(export|declare|typeset|local|readonly)$/) for (k = s + 1; k <= n; k++) if (is_assign(A[k])) set_var(A[k], mask_of(A, k))
2186
2307
  if (b == "bash" || b == "sh" || b == "zsh" || b == "dash" || b == "ksh") { if (pipe_input || (cwd_depth > 0 && PIPE_SCOPE[cwd_depth])) read_pending_find(); shell_invocation(A, s, n); return }
@@ -2290,7 +2411,7 @@ AWK
2290
2411
  # Never infer a missing hook cwd for filesystem-dependent glob decisions.
2291
2412
  glob_shell_safe=1
2292
2413
  if [[ -n "${BASH_ENV+x}" || -n "${ENV+x}" || -n "${GLOBIGNORE+x}" ]] || shopt -q dotglob || shopt -q nocaseglob; then glob_shell_safe=0; fi
2293
- findings="$(GS_LINK_PROGRAM="$link_program" GS_COMMAND="$command_text" GS_CWD="$command_cwd" GS_CWD_FALLBACK="${command_cwd:-$PWD}" GS_GLOB_SAFE="$glob_shell_safe" LC_ALL=C awk "$awk_program" </dev/null)" || parser_failed
2414
+ findings="$(GS_LINK_PROGRAM="$link_program" GS_COPY_GLOB_PROGRAM="$copy_glob_program" GS_COMMAND="$command_text" GS_CWD="$command_cwd" GS_CWD_FALLBACK="${command_cwd:-$PWD}" GS_GLOB_SAFE="$glob_shell_safe" LC_ALL=C awk "$awk_program" </dev/null)" || parser_failed
2294
2415
  [ -n "$findings" ] || exit 0
2295
2416
 
2296
2417
  {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cratis/pi",
3
- "version": "8.0.0",
3
+ "version": "8.0.1",
4
4
  "description": "Configuration-aware Cratis AI integration for Pi",
5
5
  "type": "module",
6
6
  "files": [