@cratis/pi 8.9.0 → 8.9.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.
@@ -171,6 +171,47 @@ try {
171
171
  }
172
172
  COPY_GLOB
173
173
 
174
+ # Directory exemptions are based on names only: do not read files or descend into symlinks.
175
+ # Exit 1 for a non-directory; any incomplete or unsafe directory scan denies the exemption.
176
+ IFS= read -r -d '' token_directory_program <<'TOKEN_DIRECTORY' || true
177
+ const { lstatSync, opendirSync, realpathSync } = require('node:fs');
178
+ const { join } = require('node:path');
179
+ const [cwd, operand, tokenPattern, codePattern] = process.argv.slice(1);
180
+ const tokenName = new RegExp(tokenPattern);
181
+ const codeName = new RegExp(codePattern);
182
+ let remaining = 5000;
183
+
184
+ function clean(path, depth) {
185
+ if (depth > 32 || !lstatSync(path).isDirectory()) return false;
186
+ const directory = opendirSync(path);
187
+ try {
188
+ for (let entry; (entry = directory.readSync()) !== null;) {
189
+ if (--remaining < 0 || entry.name.includes('\uFFFD')) return false;
190
+ const name = entry.name.toLowerCase();
191
+ if (tokenName.test(name) && (!codeName.test(name) || entry.isDirectory())) return false;
192
+ if (entry.isDirectory() && !clean(join(path, entry.name), depth + 1)) return false;
193
+ }
194
+ return true;
195
+ } finally {
196
+ directory.closeSync();
197
+ }
198
+ }
199
+
200
+ try {
201
+ let path = operand.startsWith('/') ? '/' : realpathSync(cwd);
202
+ for (const part of operand.split('/')) {
203
+ if (!part) continue;
204
+ path = join(path, part);
205
+ const entry = lstatSync(path, { throwIfNoEntry: false });
206
+ if (entry?.isSymbolicLink()) process.exit(3);
207
+ if (!entry?.isDirectory()) process.exit(1);
208
+ }
209
+ if (!clean(path, 0)) process.exitCode = 3;
210
+ } catch {
211
+ process.exitCode = 3;
212
+ }
213
+ TOKEN_DIRECTORY
214
+
174
215
  # Without a supplied working directory there are no literal links to resolve. Keep the cheap textual
175
216
  # pre-filter, but always parse bulk-reader families: even a root operand can cover mounted secrets.
176
217
  command_cwd="$(jq -r '.cwd // empty' <<<"$input")" || parser_failed
@@ -189,7 +230,15 @@ if [ -z "$command_cwd" ]; then
189
230
  esac
190
231
  fi
191
232
 
192
- IFS= read -r -d '' awk_program <<'AWK' || true
233
+ # Never infer a missing hook cwd for filesystem-dependent glob decisions.
234
+ glob_shell_safe=1
235
+ if [[ -n "${BASH_ENV+x}" || -n "${ENV+x}" || -n "${GLOBIGNORE+x}" ]] || shopt -q dotglob || shopt -q nocaseglob; then glob_shell_safe=0; fi
236
+ # Keep the heredoc outside the command substitution for Bash 3.2.
237
+ run_parser() {
238
+ # The parser exceeds Linux's per-argument limit. Feed its source on a separate descriptor.
239
+ GS_LINK_PROGRAM="$link_program" GS_COPY_GLOB_PROGRAM="$copy_glob_program" GS_TOKEN_DIRECTORY_PROGRAM="$token_directory_program" \
240
+ GS_COMMAND="$command_text" GS_CWD="$command_cwd" GS_CWD_FALLBACK="${command_cwd:-$PWD}" GS_GLOB_SAFE="$glob_shell_safe" \
241
+ LC_ALL=C awk -f /dev/fd/3 </dev/null 3<<'AWK'
193
242
  function report(what, alt) {
194
243
  if (what in seen) return
195
244
  seen[what] = 1
@@ -478,6 +527,25 @@ function directory_operand(w, path) {
478
527
  if (path !~ /^\//) path = ENVIRON["GS_CWD_FALLBACK"] "/" path
479
528
  return system("test -d " shell_quote(path)) == 0
480
529
  }
530
+ function bare_token_name(b) { return b ~ /^\.?tokens?$/ }
531
+ # Only probe a literal directory operand of a simple command, not shell-generated paths.
532
+ function token_directory(w, path, leaf, cmd, status) {
533
+ if (filter_compound || nwrap || qi != 1 || w !~ /^[A-Za-z0-9_.\/-]+$/) return 0
534
+ path = w; sub(/\/+$/, "", path); leaf = base(path)
535
+ if (!token_name(leaf) || token_ok(tolower(path), leaf)) return 0
536
+ if (w !~ /^\// && (ENVIRON["GS_CWD"] !~ /^\// || ncd != 1 || cwd_at(1) != ENVIRON["GS_CWD"])) return 0
537
+ if (!(path in TOKEN_DIRECTORY_PROBES)) {
538
+ if (++token_directory_probes > 16) return 2
539
+ cmd = "NODE_OPTIONS= NODE_PATH= node -e " shell_quote(ENVIRON["GS_TOKEN_DIRECTORY_PROGRAM"]) " -- " \
540
+ shell_quote(ENVIRON["GS_CWD"]) " " shell_quote(path) " " shell_quote(token_name_pattern()) " " \
541
+ shell_quote(code_name_pattern()) " >/dev/null 2>&1"
542
+ TOKEN_DIRECTORY_PROBES[path] = system(cmd)
543
+ }
544
+ status = TOKEN_DIRECTORY_PROBES[path]
545
+ if (status == 1) return 0
546
+ # Bare token directories can themselves be credential stores.
547
+ return status != 0 || bare_token_name(leaf) ? 2 : 1
548
+ }
481
549
  function metadata_shape(A, M, n, k, j, c, mk) {
482
550
  if (A[1] != "ls" && A[1] != "stat") return 0
483
551
  for (k = 1; k <= n; k++) for (j = 1; j <= length(A[k]); j++) {
@@ -510,7 +578,7 @@ function git_metadata(words, count, subcommand, k, w, ended, summary) {
510
578
  # Path-only Git operations do not turn credential vocabulary into a secret filename.
511
579
  # Exempt only plain relative paths or one-colon object specs with a simple ref.
512
580
  # Never interpret complex Git grammar; keep the original operand for conservative checks.
513
- function git_path_kind(w, pathspec, repo_only, parts, count, k, kind, path, allowed, option, directory) {
581
+ function git_path_kind(w, pathspec, repo_only, local_add, parts, count, k, kind, path, allowed, option, directory, token_dir, source_leaf, plain_path) {
514
582
  option = w ~ /^-/
515
583
  if (option) {
516
584
  kind = sensitive_path(w)
@@ -529,15 +597,21 @@ function git_path_kind(w, pathspec, repo_only, parts, count, k, kind, path, a
529
597
  if (repo_only && !allowed) return "an operand outside the Git path allowlist"
530
598
  if (allowed) w = path
531
599
  directory = allowed && tolower(base(w)) == "credentials" && tolower(w) !~ /(^|\/)\.aws\/credentials$/ && directory_operand(w)
532
- kind = sensitive_path(w)
600
+ plain_path = !option && w ~ /^[A-Za-z0-9_.\/-]+$/
601
+ if (local_add && plain_path) token_dir = token_directory(w)
602
+ kind = sensitive_path(w, token_dir)
533
603
  if (kind != "" && !(directory && kind == "a credentials file")) return kind
604
+ source_leaf = plain_path && (code_file(base(w)) || build_file(base(w)))
534
605
  # Inspect every component without guessing where unsupported revision/pathspec syntax ends.
535
606
  count = split(w, parts, /[\/:]/)
536
607
  for (k = 1; k <= count; k++) {
537
608
  if ((k == count && index(w, ":") == 0) || (k < count && tolower(parts[k]) == "credentials")) continue
538
- kind = sensitive_path(parts[k])
609
+ kind = sensitive_path(parts[k], k < count && (source_leaf || (token_dir == 1 && !bare_token_name(base(parts[k])))))
610
+ # Preserve the conservative Git check for non-source content in token directories.
611
+ if (kind == "a token file" && k < count && plain_path) return "a token directory"
539
612
  if (kind != "") return kind
540
613
  }
614
+ if (token_dir == 2) return "a token directory"
541
615
  if (!allowed && stem_scan(w, "raw") != "") return "a protected Git operand"
542
616
  return ""
543
617
  }
@@ -550,7 +624,7 @@ function git_paths(A, M, n, k, ended, pathspec) {
550
624
  if (!ended && A[2] == "diff" && A[k] ~ /^--no-index($|=)/) return 0
551
625
  pathspec = ended || A[2] ~ /^(add|checkout|restore|rm|mv)$/
552
626
  # Options are checked for sensitive values but are never positional path exemptions.
553
- if (git_path_kind(A[k], pathspec, A[k] !~ /^-/) != "") return 0
627
+ if (git_path_kind(A[k], pathspec, A[k] !~ /^-/, A[2] == "add") != "") return 0
554
628
  }
555
629
  return 1
556
630
  }
@@ -1285,7 +1359,8 @@ function skipopts(A, s, n, vals) {
1285
1359
  function is_assign(w) { return w ~ /^[A-Za-z_][A-Za-z0-9_]*(\[[^]]*\])?\+?=/ }
1286
1360
 
1287
1361
  # ── file classification for the reader commands ──
1288
- function code_file(b) { return b ~ /\.(cs|fs|vb|ts|tsx|js|jsx|mjs|cjs|go|py|java|kt|kts|ex|exs|rb|rs|swift|md|mdx)$/ }
1362
+ function code_name_pattern() { return "[.](cs|fs|vb|ts|tsx|js|jsx|mjs|cjs|go|py|java|kt|kts|ex|exs|rb|rs|swift|md|mdx)$" }
1363
+ function code_file(b) { return b ~ code_name_pattern() }
1289
1364
  # Build and markup files cannot hold values, so a dotted Env segment in their names (Cratis.Env.Tool.csproj) is not an env copy.
1290
1365
  function build_file(b) { return b ~ /\.(csproj|fsproj|vbproj|sln|slnx|props|targets|css|scss|less|html|htm|razor|cshtml|resx|xaml)$/ }
1291
1366
  # A design-token file is not a credential: a stylesheet, or a file under a design, theme or style directory, is
@@ -1336,7 +1411,9 @@ function mount_ancestor(w, m, dir, path, mount, parent, k, paths, count, home
1336
1411
  }
1337
1412
  return 0
1338
1413
  }
1339
- function sensitive_path(w, l, b, mount, normalized, k) {
1414
+ function token_name_pattern() { return "(^|[._-])tokens?($|[._-])" }
1415
+ function token_name(b) { return b ~ token_name_pattern() }
1416
+ function sensitive_path(w, non_token_file, l, b, mount, normalized, k) {
1340
1417
  l = tolower(w); b = l; sub(/.*\//, "", b)
1341
1418
  if (l ~ /(^|\/)\.kube\// || b == "kubeconfig" || b ~ /\.kubeconfig$/) return "a kubeconfig"
1342
1419
  # Every name starting with .env stays protected (.env2, .envprod, .envrc.local, .env-prod); only the
@@ -1348,7 +1425,7 @@ function sensitive_path(w, l, b, mount, normalized, k) {
1348
1425
  # such as Signing.Key.cs are source unless the credential extension is final.
1349
1426
  if (b ~ /\.(pem|p12|pfx)([.]|$)/ || b ~ /\.(key|jks|keystore|kdbx)[.]*$/) return "a key or certificate file"
1350
1427
  if (b == "id_rsa" || b == "id_ed25519" || b == "id_ecdsa" || b == "id_dsa") return "a private key"
1351
- if (b ~ /(^|[._-])tokens?($|[._-])/ && !code_file(b) && !token_ok(l, b)) return "a token file"
1428
+ if (!non_token_file && token_name(b) && !code_file(b) && !token_ok(l, b)) return "a token file"
1352
1429
  if (b == ".netrc" || b == ".npmrc" || b == ".pgpass" || b == ".pypirc" || b == ".git-credentials") return "a credentials file"
1353
1430
  if (b == "credentials" || b ~ /credential.*\.json([.]|$)/ ||
1354
1431
  b ~ /credentials.*\.(yaml|yml|xml|ini|toml|txt|conf|properties|config)([.]|$)/ ||
@@ -2377,7 +2454,7 @@ function copy_globs_safe(A, s, n, b, k, w, m, start, patterns, count) {
2377
2454
  }
2378
2455
  return copy_glob_probe(patterns)
2379
2456
  }
2380
- function transfer_files(A, s, n, b, k, last, options, kind, w, eq, off, ended) {
2457
+ function transfer_files(A, s, n, b, k, last, options, kind, w, eq, off, ended, m, token_dir) {
2381
2458
  for (k = s + 1; k <= n; k++) if (A[k] ~ /^-./) options = 1
2382
2459
  last = n
2383
2460
  if (b ~ /^(cp|mv|ln)$/ && !options && n == s + 2) last--
@@ -2389,18 +2466,23 @@ function transfer_files(A, s, n, b, k, last, options, kind, w, eq, off, ended
2389
2466
  if (w !~ /^--/ || !eq) continue
2390
2467
  off = eq
2391
2468
  }
2392
- kind = operand_kind(A, k, off)
2469
+ m = mask_of(A, k)
2470
+ token_dir = b == "mv" && s == 1 && !off && literal_word(w, m) && !expands(w, m) ? token_directory(w) : 0
2471
+ kind = token_dir ? sensitive_path(w, 1) : operand_kind(A, k, off)
2472
+ if (kind == "" && token_dir == 2) kind = "a token directory"
2393
2473
  if (kind != "") report(b " reads " kind, "name only non-credential source files")
2394
2474
  }
2395
2475
  }
2396
2476
  # Git grep reuses pattern/file separation; ls-files never reaches this content path.
2397
- function git_content(A, s, n, subcommand, k, w, kind, ended, pathspec) {
2477
+ function git_content(A, s, n, subcommand, k, w, kind, ended, pathspec, m, local_add) {
2398
2478
  subcommand = A[s + 1]
2399
2479
  if (subcommand ~ /^(add|checkout|restore|rm|mv|diff|log|show)$/) {
2400
2480
  for (k = s + 2; k <= n; k++) {
2401
2481
  if (!ended && A[k] == "--") { ended = 1; continue }
2402
2482
  pathspec = ended || subcommand ~ /^(add|checkout|restore|rm|mv)$/
2403
- kind = git_path_kind(A[k], pathspec)
2483
+ w = A[k]; m = mask_of(A, k)
2484
+ local_add = s == 1 && subcommand == "add" && literal_word(w, m) && !expands(w, m)
2485
+ kind = git_path_kind(w, pathspec, 0, local_add)
2404
2486
  if (kind != "") report("git " subcommand " names " kind, "name only non-credential source files; list filenames with git ls-files")
2405
2487
  }
2406
2488
  }
@@ -2573,11 +2655,9 @@ BEGIN {
2573
2655
  }
2574
2656
  }
2575
2657
  AWK
2658
+ }
2576
2659
 
2577
- # Never infer a missing hook cwd for filesystem-dependent glob decisions.
2578
- glob_shell_safe=1
2579
- if [[ -n "${BASH_ENV+x}" || -n "${ENV+x}" || -n "${GLOBIGNORE+x}" ]] || shopt -q dotglob || shopt -q nocaseglob; then glob_shell_safe=0; fi
2580
- 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
2660
+ findings="$(run_parser)" || parser_failed
2581
2661
  [ -n "$findings" ] || exit 0
2582
2662
 
2583
2663
  {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cratis/pi",
3
- "version": "8.9.0",
3
+ "version": "8.9.2",
4
4
  "description": "Configuration-aware Cratis AI integration for Pi",
5
5
  "type": "module",
6
6
  "files": [