@node9/proxy 1.14.0 → 1.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -288,8 +288,8 @@ function sanitizeConfig(raw) {
288
288
  }
289
289
  }
290
290
  const lines = result.error.issues.map((issue) => {
291
- const path15 = issue.path.length > 0 ? issue.path.join(".") : "root";
292
- return ` \u2022 ${path15}: ${issue.message}`;
291
+ const path13 = issue.path.length > 0 ? issue.path.join(".") : "root";
292
+ return ` \u2022 ${path13}: ${issue.message}`;
293
293
  });
294
294
  return {
295
295
  sanitized,
@@ -302,783 +302,101 @@ ${lines.join("\n")}`
302
302
  var import_fs2 = __toESM(require("fs"));
303
303
  var import_path2 = __toESM(require("path"));
304
304
  var import_os2 = __toESM(require("os"));
305
- var BUILTIN_DIR = import_path2.default.join(__dirname, "shields", "builtin");
306
- var USER_SHIELDS_DIR = import_path2.default.join(import_os2.default.homedir(), ".node9", "shields");
307
- function validateShieldDefinition(raw, filePath) {
308
- if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
309
- process.stderr.write(`[node9] Shield file is not an object: ${filePath}
310
- `);
311
- return null;
312
- }
313
- const r = raw;
314
- if (typeof r.name !== "string" || !r.name) {
315
- process.stderr.write(`[node9] Shield file missing 'name': ${filePath}
316
- `);
317
- return null;
318
- }
319
- if (typeof r.description !== "string") {
320
- process.stderr.write(`[node9] Shield file missing 'description': ${filePath}
321
- `);
322
- return null;
323
- }
324
- if (!Array.isArray(r.aliases)) {
325
- process.stderr.write(`[node9] Shield file missing 'aliases' array: ${filePath}
326
- `);
327
- return null;
328
- }
329
- if (!Array.isArray(r.smartRules)) {
330
- process.stderr.write(`[node9] Shield file missing 'smartRules' array: ${filePath}
331
- `);
332
- return null;
333
- }
334
- if (!Array.isArray(r.dangerousWords)) {
335
- process.stderr.write(`[node9] Shield file missing 'dangerousWords' array: ${filePath}
336
- `);
337
- return null;
338
- }
339
- return r;
340
- }
341
- function loadShieldsFromDir(dir, label) {
342
- const result = {};
343
- let entries;
344
- try {
345
- entries = import_fs2.default.readdirSync(dir).filter((f) => f.endsWith(".json"));
346
- } catch (err) {
347
- if (err.code !== "ENOENT") {
348
- process.stderr.write(`[node9] Could not read ${label} shields dir ${dir}: ${String(err)}
349
- `);
350
- }
351
- return result;
352
- }
353
- for (const file of entries) {
354
- const filePath = import_path2.default.join(dir, file);
355
- try {
356
- const raw = JSON.parse(import_fs2.default.readFileSync(filePath, "utf-8"));
357
- const shield = validateShieldDefinition(raw, filePath);
358
- if (shield) result[shield.name] = shield;
359
- } catch (err) {
360
- process.stderr.write(`[node9] Failed to load ${label} shield ${file}: ${String(err)}
361
- `);
362
- }
363
- }
364
- return result;
365
- }
366
- function buildSHIELDS() {
367
- const builtins = loadShieldsFromDir(BUILTIN_DIR, "builtin");
368
- const userShields = loadShieldsFromDir(USER_SHIELDS_DIR, "user");
369
- return { ...builtins, ...userShields };
370
- }
371
- var SHIELDS = buildSHIELDS();
372
- function resolveShieldName(input) {
373
- const lower = input.toLowerCase();
374
- if (SHIELDS[lower]) return lower;
375
- for (const [name, def] of Object.entries(SHIELDS)) {
376
- if (def.aliases.includes(lower)) return name;
377
- }
378
- return null;
379
- }
380
- function getShield(name) {
381
- const resolved = resolveShieldName(name);
382
- return resolved ? SHIELDS[resolved] : null;
383
- }
384
- var SHIELDS_STATE_FILE = import_path2.default.join(import_os2.default.homedir(), ".node9", "shields.json");
385
- function isShieldVerdict(v) {
386
- return v === "allow" || v === "review" || v === "block";
387
- }
388
- function validateOverrides(raw) {
389
- if (!raw || typeof raw !== "object" || Array.isArray(raw)) return {};
390
- const result = {};
391
- for (const [shieldName, rules] of Object.entries(raw)) {
392
- if (!rules || typeof rules !== "object" || Array.isArray(rules)) continue;
393
- const validRules = {};
394
- for (const [ruleName, verdict] of Object.entries(rules)) {
395
- if (isShieldVerdict(verdict)) {
396
- validRules[ruleName] = verdict;
397
- } else {
398
- process.stderr.write(
399
- `[node9] Warning: shields.json contains invalid verdict "${String(verdict)}" for ${shieldName}/${ruleName} \u2014 entry ignored. File may be corrupted or tampered with.
400
- `
401
- );
402
- }
403
- }
404
- if (Object.keys(validRules).length > 0) result[shieldName] = validRules;
405
- }
406
- return result;
305
+
306
+ // packages/policy-engine/dist/index.mjs
307
+ var import_safe_regex2 = __toESM(require("safe-regex2"), 1);
308
+ var import_mvdan_sh = __toESM(require("mvdan-sh"), 1);
309
+ var import_picomatch = __toESM(require("picomatch"), 1);
310
+ var import_safe_regex22 = __toESM(require("safe-regex2"), 1);
311
+ var import_safe_regex23 = __toESM(require("safe-regex2"), 1);
312
+ var import_crypto2 = __toESM(require("crypto"), 1);
313
+ var ASSIGNMENT_CONTEXT_RE = /\b(?:password|passwd|secret|token|api[_-]?key|auth(?:_key|_token)?|credential|private[_-]?key|access[_-]?key|client[_-]?secret)\s*[=:]\s*/i;
314
+ function isAssignmentContext(text) {
315
+ return ASSIGNMENT_CONTEXT_RE.test(text);
407
316
  }
408
- function readShieldsFile() {
409
- try {
410
- const raw = import_fs2.default.readFileSync(SHIELDS_STATE_FILE, "utf-8");
411
- if (!raw.trim()) return { active: [] };
412
- const parsed = JSON.parse(raw);
413
- const active = Array.isArray(parsed.active) ? parsed.active.filter(
414
- (e) => typeof e === "string" && e.length > 0 && e in SHIELDS
415
- ) : [];
416
- return { active, overrides: validateOverrides(parsed.overrides) };
417
- } catch (err) {
418
- if (err.code !== "ENOENT") {
419
- process.stderr.write(`[node9] Warning: could not read shields state: ${String(err)}
420
- `);
421
- }
422
- return { active: [] };
317
+ function shannonEntropy(s) {
318
+ if (s.length === 0) return 0;
319
+ const freq = /* @__PURE__ */ new Map();
320
+ for (const ch of s) freq.set(ch, (freq.get(ch) ?? 0) + 1);
321
+ let h = 0;
322
+ for (const count of freq.values()) {
323
+ const p = count / s.length;
324
+ h -= p * Math.log2(p);
423
325
  }
326
+ return h;
424
327
  }
425
- function readActiveShields() {
426
- return readShieldsFile().active;
427
- }
428
- function readShieldOverrides() {
429
- return readShieldsFile().overrides ?? {};
430
- }
431
-
432
- // src/config/index.ts
433
- var DANGEROUS_WORDS = [
434
- "mkfs",
435
- // formats/wipes a filesystem partition
436
- "shred"
437
- // permanently overwrites file contents (unrecoverable)
328
+ var DLP_STOPWORDS = [
329
+ "example",
330
+ "placeholder",
331
+ "changeme",
332
+ "your_key",
333
+ "your_token",
334
+ "your_secret",
335
+ "replace_me",
336
+ "insert_key",
337
+ "put_your",
338
+ "fake",
339
+ "dummy",
340
+ "sample",
341
+ "xxxxxxxx",
342
+ "aaaaaa",
343
+ "bbbbbb",
344
+ "00000000",
345
+ "${",
346
+ "{{",
347
+ "%{",
348
+ "<your",
349
+ "test_key",
350
+ "test_token",
351
+ "your",
352
+ "here"
438
353
  ];
439
- var DEFAULT_CONFIG = {
440
- version: "1.0",
441
- settings: {
442
- mode: "standard",
443
- autoStartDaemon: true,
444
- enableUndo: true,
445
- // 🔥 ALWAYS TRUE BY DEFAULT for the safety net
446
- enableHookLogDebug: true,
447
- approvalTimeoutMs: 12e4,
448
- // 120-second auto-deny timeout
449
- flightRecorder: true,
450
- auditHashArgs: true,
451
- approvers: { native: true, browser: false, cloud: false, terminal: true },
452
- cloudSyncIntervalHours: 5
354
+ var DLP_PATTERNS = [
355
+ // ── AWS ───────────────────────────────────────────────────────────────────
356
+ {
357
+ name: "AWS Access Key ID",
358
+ regex: /\b(?:A3T[A-Z0-9]|AKIA|ASIA|ABIA|ACCA)[A-Z2-7]{16}\b/,
359
+ severity: "block",
360
+ keywords: ["akia", "asia", "abia", "acca", "a3t"]
453
361
  },
454
- policy: {
455
- sandboxPaths: ["/tmp/**", "**/sandbox/**", "**/test-results/**"],
456
- dangerousWords: DANGEROUS_WORDS,
457
- ignoredTools: [
458
- "list_*",
459
- "get_*",
460
- "read_*",
461
- "describe_*",
462
- "read",
463
- "glob",
464
- "grep",
465
- "ls",
466
- "notebookread",
467
- "notebookedit",
468
- "webfetch",
469
- "websearch",
470
- "exitplanmode",
471
- "askuserquestion",
472
- "agent",
473
- "task*",
474
- "toolsearch",
475
- "mcp__ide__*",
476
- "getDiagnostics"
477
- ],
478
- toolInspection: {
479
- bash: "command",
480
- shell: "command",
481
- run_shell_command: "command",
482
- "terminal.execute": "command",
483
- "postgres:query": "sql"
484
- },
485
- snapshot: {
486
- tools: [
487
- "str_replace_based_edit_tool",
488
- "write_file",
489
- "edit_file",
490
- "create_file",
491
- "edit",
492
- "replace"
493
- ],
494
- onlyPaths: [],
495
- ignorePaths: ["**/node_modules/**", "dist/**", "build/**", ".next/**", "**/*.log"]
496
- },
497
- smartRules: [
498
- // ── rm safety (critical — always evaluated first) ──────────────────────
499
- {
500
- name: "block-rm-rf-home",
501
- tool: "bash",
502
- conditionMode: "all",
503
- conditions: [
504
- {
505
- field: "command",
506
- op: "matches",
507
- // Anchor rm as a shell command (not inside a string arg like a git commit message).
508
- value: "(^|&&|\\|\\||;)\\s*rm\\b[^;&|]*\\s(-[rRfF]*[rR][rRfF]*|--recursive)(\\s|$)"
509
- },
510
- {
511
- field: "command",
512
- op: "matches",
513
- value: "(~|\\/root(\\/|$)|\\$HOME|\\/home\\/)"
514
- }
515
- ],
516
- verdict: "block",
517
- reason: "Recursive delete of home directory is irreversible",
518
- description: "The AI wants to recursively delete your home directory. This will permanently destroy all your personal files and cannot be undone."
519
- },
520
- // ── SQL safety ────────────────────────────────────────────────────────
521
- {
522
- name: "no-delete-without-where",
523
- tool: "*",
524
- conditions: [
525
- { field: "sql", op: "matches", value: "^(DELETE|UPDATE)\\s", flags: "i" },
526
- { field: "sql", op: "notMatches", value: "\\bWHERE\\b", flags: "i" }
527
- ],
528
- conditionMode: "all",
529
- verdict: "review",
530
- reason: "DELETE/UPDATE without WHERE clause \u2014 would affect every row in the table",
531
- description: "The AI is running a SQL statement that will modify every row in the table \u2014 no WHERE filter was found. This could wipe or corrupt all your data."
532
- },
533
- {
534
- name: "review-drop-truncate-shell",
535
- tool: "bash",
536
- conditions: [
537
- {
538
- field: "command",
539
- op: "matches",
540
- // Require a DB CLI in the command so grep/cat/echo of SQL strings don't trigger.
541
- value: "(^|&&|\\|\\||;|\\|)\\s*(psql|mysql|sqlite3|sqlplus|cockroach|clickhouse-client|mongo)\\b",
542
- flags: "i"
543
- },
544
- {
545
- field: "command",
546
- op: "matches",
547
- value: "\\b(DROP|TRUNCATE)\\s+(TABLE|DATABASE|SCHEMA|INDEX)",
548
- flags: "i"
549
- }
550
- ],
551
- conditionMode: "all",
552
- verdict: "review",
553
- reason: "SQL DDL destructive statement inside a shell command",
554
- description: "The AI wants to drop or truncate a database table via the shell. This permanently deletes the table structure or all its data."
555
- },
556
- // ── Git safety ────────────────────────────────────────────────────────
557
- {
558
- name: "review-force-push",
559
- tool: "bash",
560
- conditions: [
561
- {
562
- field: "command",
563
- op: "matches",
564
- // Anchor git as a shell command so node -e / python -c scripts containing
565
- // "git push --force" as a string don't false-positive.
566
- value: "(^|&&|\\|\\||;)\\s*git\\s+push[^;&|]*(--force|--force-with-lease|-f\\b)",
567
- flags: "i"
568
- }
569
- ],
570
- conditionMode: "all",
571
- verdict: "review",
572
- reason: "Force push rewrites remote history \u2014 confirm this is intentional",
573
- description: "The AI wants to force push to a remote git branch. This rewrites shared history and can permanently destroy commits that teammates have already pulled."
574
- },
575
- {
576
- name: "review-git-destructive",
577
- tool: "bash",
578
- conditions: [
579
- {
580
- field: "command",
581
- op: "matches",
582
- // Anchor git as a shell command so node -e / python -c scripts containing
583
- // "git reset --hard" as a string don't false-positive.
584
- value: "(^|&&|\\|\\||;)\\s*git\\s+(reset\\s+--hard|clean\\s+-[fdxX]|rebase\\b|tag\\s+-d|branch\\s+-[dD])",
585
- flags: "i"
586
- },
587
- {
588
- field: "command",
589
- op: "notMatches",
590
- // Exclude recovery ops and routine branch-surgery (--onto) — these are not destructive.
591
- value: "\\bgit\\s+rebase\\s+--(abort|continue|skip|onto)\\b",
592
- flags: "i"
593
- }
594
- ],
595
- conditionMode: "all",
596
- verdict: "review",
597
- reason: "Destructive git operation \u2014 discards history or working-tree changes",
598
- description: "The AI wants to run a destructive git operation (reset, rebase, clean, or branch delete) that can permanently discard commits or uncommitted work."
599
- },
600
- // ── Shell safety ──────────────────────────────────────────────────────
601
- {
602
- name: "review-sudo",
603
- tool: "bash",
604
- conditions: [{ field: "command", op: "matches", value: "\\bsudo\\s", flags: "i" }],
605
- conditionMode: "all",
606
- verdict: "review",
607
- reason: "Command requires elevated privileges",
608
- description: "The AI wants to run a command as root (sudo). Commands with root access can modify system files, install software, or change security settings."
609
- },
610
- {
611
- name: "review-curl-pipe-shell",
612
- tool: "bash",
613
- conditions: [
614
- {
615
- field: "command",
616
- op: "matches",
617
- // Anchor curl/wget as a shell command so node -e scripts testing this
618
- // regex pattern don't self-match as a false positive.
619
- value: "(^|&&|\\|\\||;)\\s*(curl|wget)[^|]*\\|\\s*(ba|z|da|fi|c|k)?sh",
620
- flags: "i"
621
- }
622
- ],
623
- conditionMode: "all",
624
- verdict: "block",
625
- reason: "Piping remote script into a shell is a supply-chain attack vector",
626
- description: "The AI wants to download a script from the internet and run it immediately, without you seeing what it contains. This is one of the most common ways malware gets installed."
627
- }
628
- ],
629
- dlp: { enabled: true, scanIgnoredTools: true },
630
- loopDetection: { enabled: true, threshold: 5, windowSeconds: 120 },
631
- skillPinning: { enabled: false, mode: "warn", roots: [] }
362
+ // ── GitHub ────────────────────────────────────────────────────────────────
363
+ {
364
+ name: "GitHub Token",
365
+ regex: /\bgh[pous]_[A-Za-z0-9]{36}\b/,
366
+ severity: "block",
367
+ keywords: ["ghp_", "gho_", "ghu_", "ghs_"],
368
+ minEntropy: 3
632
369
  },
633
- environments: {}
634
- };
635
- var ADVISORY_SMART_RULES = [
636
- // ── rm safety ─────────────────────────────────────────────────────────────
637
- // tool: '*' so they cover bash, shell, run_shell_command, and Gemini's Shell.
638
- // Pattern '(^|&&|\|\||;)\s*rm\b' matches rm as a shell command (including in
639
- // chained commands like 'cat foo && rm bar') but avoids false-positives on 'docker rm'.
640
370
  {
641
- name: "allow-rm-safe-paths",
642
- tool: "*",
643
- conditionMode: "all",
644
- conditions: [
645
- { field: "command", op: "matches", value: "(^|&&|\\|\\||;)\\s*rm\\b" },
646
- {
647
- field: "command",
648
- op: "matches",
649
- // Matches known-safe build artifact paths in the command.
650
- value: "(node_modules|\\bdist\\b|\\.next|\\bcoverage\\b|\\.cache|\\btmp\\b|\\btemp\\b|\\.DS_Store)(\\/|\\s|$)"
651
- }
652
- ],
653
- verdict: "allow",
654
- reason: "Deleting a known-safe build artifact path"
371
+ name: "GitHub Fine-Grained PAT",
372
+ regex: /\bgithub_pat_\w{82}\b/,
373
+ severity: "block",
374
+ keywords: ["github_pat_"]
655
375
  },
376
+ // ── Slack ─────────────────────────────────────────────────────────────────
656
377
  {
657
- name: "review-rm",
658
- tool: "*",
659
- conditions: [{ field: "command", op: "matches", value: "(^|&&|\\|\\||;)\\s*rm\\b" }],
660
- verdict: "review",
661
- reason: "rm can permanently delete files \u2014 confirm the target path",
662
- description: "The AI wants to delete files. Unlike moving to trash, rm is permanent \u2014 the files cannot be recovered without a backup."
378
+ name: "Slack Bot Token",
379
+ // Real tokens are ~50–80 chars; lower bound 20 avoids false negatives on partial tokens
380
+ regex: /\bxoxb-[0-9A-Za-z-]{20,100}\b/,
381
+ severity: "block",
382
+ keywords: ["xoxb-"]
663
383
  },
664
- // ── SQL safety (Safe by Default) ──────────────────────────────────────────
665
- // These rules fire when an AI calls a database tool directly (e.g. MCP postgres,
666
- // mcp__postgres__query) with a destructive SQL statement in the 'sql' field.
667
- // The postgres shield upgrades these from 'review' → 'block' for stricter teams;
668
- // without a shield, users still get a human-approval gate on every destructive op.
384
+ // ── Anthropic ─────────────────────────────────────────────────────────────
385
+ // Listed before OpenAI Anthropic keys start with sk-ant- which would also
386
+ // match the broader OpenAI sk- pattern; more specific rules must come first.
669
387
  {
670
- name: "review-drop-table-sql",
671
- tool: "*",
672
- conditions: [{ field: "sql", op: "matches", value: "DROP\\s+TABLE", flags: "i" }],
673
- verdict: "review",
674
- reason: "DROP TABLE is irreversible \u2014 enable the postgres shield to block instead",
675
- description: "The AI wants to drop a database table. This permanently deletes the table and all its data \u2014 there is no undo."
388
+ name: "Anthropic API Key",
389
+ regex: /\bsk-ant-api03-[a-zA-Z0-9_-]{93}AA\b/,
390
+ severity: "block",
391
+ keywords: ["sk-ant-api03"]
676
392
  },
677
393
  {
678
- name: "review-truncate-sql",
679
- tool: "*",
680
- conditions: [{ field: "sql", op: "matches", value: "TRUNCATE\\s+TABLE", flags: "i" }],
681
- verdict: "review",
682
- reason: "TRUNCATE removes all rows \u2014 enable the postgres shield to block instead",
683
- description: "The AI wants to truncate a database table, which instantly deletes every row. The table structure remains but all data is gone."
394
+ name: "Anthropic Admin Key",
395
+ regex: /\bsk-ant-admin01-[a-zA-Z0-9_-]{93}AA\b/,
396
+ severity: "block",
397
+ keywords: ["sk-ant-admin01"]
684
398
  },
685
- {
686
- name: "review-drop-column-sql",
687
- tool: "*",
688
- conditions: [
689
- { field: "sql", op: "matches", value: "ALTER\\s+TABLE.*DROP\\s+COLUMN", flags: "i" }
690
- ],
691
- verdict: "review",
692
- reason: "DROP COLUMN is irreversible \u2014 enable the postgres shield to block instead",
693
- description: "The AI wants to drop a column from a database table. This permanently removes the column and all its data from every row."
694
- }
695
- ];
696
- var cachedConfig = null;
697
- function getCredentials() {
698
- const DEFAULT_API_URL = "https://api.node9.ai/api/v1/intercept";
699
- if (process.env.NODE9_API_KEY) {
700
- return {
701
- apiKey: process.env.NODE9_API_KEY,
702
- apiUrl: process.env.NODE9_API_URL || DEFAULT_API_URL
703
- };
704
- }
705
- try {
706
- const credPath = import_path3.default.join(import_os3.default.homedir(), ".node9", "credentials.json");
707
- if (import_fs3.default.existsSync(credPath)) {
708
- const creds = JSON.parse(import_fs3.default.readFileSync(credPath, "utf-8"));
709
- const profileName = process.env.NODE9_PROFILE || "default";
710
- const profile = creds[profileName];
711
- if (profile?.apiKey) {
712
- return {
713
- apiKey: profile.apiKey,
714
- apiUrl: profile.apiUrl || DEFAULT_API_URL
715
- };
716
- }
717
- if (creds.apiKey) {
718
- return {
719
- apiKey: creds.apiKey,
720
- apiUrl: creds.apiUrl || DEFAULT_API_URL
721
- };
722
- }
723
- }
724
- } catch {
725
- }
726
- return null;
727
- }
728
- function getActiveEnvironment(config) {
729
- const env = config.settings.environment || process.env.NODE_ENV || "development";
730
- return config.environments[env] ?? null;
731
- }
732
- function getConfig(cwd) {
733
- if (!cwd && cachedConfig) return cachedConfig;
734
- const globalPath = import_path3.default.join(import_os3.default.homedir(), ".node9", "config.json");
735
- const projectPath = import_path3.default.join(cwd ?? process.cwd(), "node9.config.json");
736
- const globalConfig = tryLoadConfig(globalPath);
737
- const projectConfig = tryLoadConfig(projectPath);
738
- const mergedSettings = {
739
- ...DEFAULT_CONFIG.settings,
740
- approvers: { ...DEFAULT_CONFIG.settings.approvers }
741
- };
742
- const mergedPolicy = {
743
- sandboxPaths: [...DEFAULT_CONFIG.policy.sandboxPaths],
744
- dangerousWords: [...DEFAULT_CONFIG.policy.dangerousWords],
745
- ignoredTools: [...DEFAULT_CONFIG.policy.ignoredTools],
746
- toolInspection: { ...DEFAULT_CONFIG.policy.toolInspection },
747
- smartRules: [...DEFAULT_CONFIG.policy.smartRules],
748
- snapshot: {
749
- tools: [...DEFAULT_CONFIG.policy.snapshot.tools],
750
- onlyPaths: [...DEFAULT_CONFIG.policy.snapshot.onlyPaths],
751
- ignorePaths: [...DEFAULT_CONFIG.policy.snapshot.ignorePaths]
752
- },
753
- dlp: { ...DEFAULT_CONFIG.policy.dlp },
754
- loopDetection: { ...DEFAULT_CONFIG.policy.loopDetection },
755
- skillPinning: {
756
- ...DEFAULT_CONFIG.policy.skillPinning,
757
- roots: [...DEFAULT_CONFIG.policy.skillPinning.roots]
758
- }
759
- };
760
- const mergedEnvironments = { ...DEFAULT_CONFIG.environments };
761
- const applyLayer = (source) => {
762
- if (!source) return;
763
- const s = source.settings || {};
764
- const p = source.policy || {};
765
- if (s.mode !== void 0) mergedSettings.mode = s.mode;
766
- if (s.autoStartDaemon !== void 0) mergedSettings.autoStartDaemon = s.autoStartDaemon;
767
- if (s.enableUndo !== void 0) mergedSettings.enableUndo = s.enableUndo;
768
- if (s.enableHookLogDebug !== void 0)
769
- mergedSettings.enableHookLogDebug = s.enableHookLogDebug;
770
- if (s.approvers) mergedSettings.approvers = { ...mergedSettings.approvers, ...s.approvers };
771
- if (s.approvalTimeoutMs !== void 0) mergedSettings.approvalTimeoutMs = s.approvalTimeoutMs;
772
- if (s.approvalTimeoutSeconds !== void 0 && s.approvalTimeoutMs === void 0)
773
- mergedSettings.approvalTimeoutMs = s.approvalTimeoutSeconds * 1e3;
774
- if (s.environment !== void 0) mergedSettings.environment = s.environment;
775
- if (s.cloudSyncIntervalHours !== void 0)
776
- mergedSettings.cloudSyncIntervalHours = s.cloudSyncIntervalHours;
777
- if (s.hud !== void 0) mergedSettings.hud = { ...mergedSettings.hud, ...s.hud };
778
- if (p.sandboxPaths) mergedPolicy.sandboxPaths.push(...p.sandboxPaths);
779
- if (p.ignoredTools) mergedPolicy.ignoredTools.push(...p.ignoredTools);
780
- if (p.dangerousWords) mergedPolicy.dangerousWords = [...p.dangerousWords];
781
- if (p.toolInspection)
782
- mergedPolicy.toolInspection = { ...mergedPolicy.toolInspection, ...p.toolInspection };
783
- if (p.smartRules) {
784
- const defaultBlocks = mergedPolicy.smartRules.filter((r) => r.verdict === "block");
785
- const defaultNonBlocks = mergedPolicy.smartRules.filter((r) => r.verdict !== "block");
786
- const userRuleNames = new Set(p.smartRules.filter((r) => r.name).map((r) => r.name));
787
- const filteredBlocks = defaultBlocks.filter((r) => !r.name || !userRuleNames.has(r.name));
788
- const filteredNonBlocks = defaultNonBlocks.filter(
789
- (r) => !r.name || !userRuleNames.has(r.name)
790
- );
791
- mergedPolicy.smartRules = [...filteredBlocks, ...p.smartRules, ...filteredNonBlocks];
792
- }
793
- if (p.snapshot) {
794
- const s2 = p.snapshot;
795
- if (s2.tools) mergedPolicy.snapshot.tools.push(...s2.tools);
796
- if (s2.onlyPaths) mergedPolicy.snapshot.onlyPaths.push(...s2.onlyPaths);
797
- if (s2.ignorePaths) mergedPolicy.snapshot.ignorePaths.push(...s2.ignorePaths);
798
- }
799
- if (p.dlp) {
800
- const d = p.dlp;
801
- if (d.enabled !== void 0) mergedPolicy.dlp.enabled = d.enabled;
802
- if (d.scanIgnoredTools !== void 0) mergedPolicy.dlp.scanIgnoredTools = d.scanIgnoredTools;
803
- }
804
- if (p.loopDetection) {
805
- const ld = p.loopDetection;
806
- if (ld.enabled !== void 0) mergedPolicy.loopDetection.enabled = ld.enabled;
807
- if (ld.threshold !== void 0) mergedPolicy.loopDetection.threshold = ld.threshold;
808
- if (ld.windowSeconds !== void 0)
809
- mergedPolicy.loopDetection.windowSeconds = ld.windowSeconds;
810
- }
811
- if (p.skillPinning && typeof p.skillPinning === "object") {
812
- const sp = p.skillPinning;
813
- if (sp.enabled !== void 0) mergedPolicy.skillPinning.enabled = sp.enabled;
814
- if (sp.mode !== void 0) mergedPolicy.skillPinning.mode = sp.mode;
815
- if (Array.isArray(sp.roots)) {
816
- for (const r of sp.roots) {
817
- if (typeof r === "string" && r.length > 0) mergedPolicy.skillPinning.roots.push(r);
818
- }
819
- }
820
- }
821
- const envs = source.environments || {};
822
- for (const [envName, envConfig] of Object.entries(envs)) {
823
- if (envConfig && typeof envConfig === "object") {
824
- const ec = envConfig;
825
- mergedEnvironments[envName] = {
826
- ...mergedEnvironments[envName],
827
- // Validate field types before merging — do not blindly spread user input
828
- ...typeof ec.requireApproval === "boolean" ? { requireApproval: ec.requireApproval } : {}
829
- };
830
- }
831
- }
832
- };
833
- applyLayer(globalConfig);
834
- applyLayer(projectConfig);
835
- {
836
- const cacheFile = import_path3.default.join(import_os3.default.homedir(), ".node9", "rules-cache.json");
837
- try {
838
- const raw = JSON.parse(import_fs3.default.readFileSync(cacheFile, "utf-8"));
839
- if (Array.isArray(raw.rules) && raw.rules.length > 0) {
840
- applyLayer({ policy: { smartRules: raw.rules } });
841
- }
842
- } catch {
843
- }
844
- }
845
- const shieldOverrides = readShieldOverrides();
846
- for (const shieldName of readActiveShields()) {
847
- const shield = getShield(shieldName);
848
- if (!shield) continue;
849
- const existingRuleNames = new Set(mergedPolicy.smartRules.map((r) => r.name));
850
- const ruleOverrides = shieldOverrides[shieldName] ?? {};
851
- for (const rule of shield.smartRules) {
852
- if (!existingRuleNames.has(rule.name)) {
853
- const overrideVerdict = rule.name ? ruleOverrides[rule.name] : void 0;
854
- mergedPolicy.smartRules.push(
855
- overrideVerdict !== void 0 ? { ...rule, verdict: overrideVerdict } : rule
856
- );
857
- }
858
- }
859
- const existingWords = new Set(mergedPolicy.dangerousWords);
860
- for (const word of shield.dangerousWords) {
861
- if (!existingWords.has(word)) mergedPolicy.dangerousWords.push(word);
862
- }
863
- }
864
- const existingAdvisoryNames = new Set(mergedPolicy.smartRules.map((r) => r.name));
865
- for (const rule of ADVISORY_SMART_RULES) {
866
- if (!existingAdvisoryNames.has(rule.name)) mergedPolicy.smartRules.push(rule);
867
- }
868
- if (process.env.NODE9_MODE) mergedSettings.mode = process.env.NODE9_MODE;
869
- mergedPolicy.sandboxPaths = [...new Set(mergedPolicy.sandboxPaths)];
870
- mergedPolicy.dangerousWords = [...new Set(mergedPolicy.dangerousWords)];
871
- mergedPolicy.ignoredTools = [...new Set(mergedPolicy.ignoredTools)];
872
- mergedPolicy.skillPinning.roots = [...new Set(mergedPolicy.skillPinning.roots)];
873
- mergedPolicy.snapshot.tools = [...new Set(mergedPolicy.snapshot.tools)];
874
- mergedPolicy.snapshot.onlyPaths = [...new Set(mergedPolicy.snapshot.onlyPaths)];
875
- mergedPolicy.snapshot.ignorePaths = [...new Set(mergedPolicy.snapshot.ignorePaths)];
876
- const result = {
877
- settings: mergedSettings,
878
- policy: mergedPolicy,
879
- environments: mergedEnvironments
880
- };
881
- if (!cwd) cachedConfig = result;
882
- return result;
883
- }
884
- function tryLoadConfig(filePath) {
885
- if (!import_fs3.default.existsSync(filePath)) return null;
886
- let raw;
887
- try {
888
- raw = JSON.parse(import_fs3.default.readFileSync(filePath, "utf-8"));
889
- } catch (err) {
890
- const msg = err instanceof Error ? err.message : String(err);
891
- process.stderr.write(
892
- `
893
- \u26A0\uFE0F Node9: Failed to parse ${filePath}
894
- ${msg}
895
- \u2192 Using default config
896
-
897
- `
898
- );
899
- return null;
900
- }
901
- const SUPPORTED_VERSION = "1.0";
902
- const SUPPORTED_MAJOR = SUPPORTED_VERSION.split(".")[0];
903
- const fileVersion = raw?.version;
904
- if (fileVersion !== void 0) {
905
- const vStr = String(fileVersion);
906
- const fileMajor = vStr.split(".")[0];
907
- if (fileMajor !== SUPPORTED_MAJOR) {
908
- process.stderr.write(
909
- `
910
- \u274C Node9: Config at ${filePath} has version "${vStr}" \u2014 major version is incompatible with this release (expected "${SUPPORTED_VERSION}"). Config will not be loaded.
911
-
912
- `
913
- );
914
- return null;
915
- } else if (vStr !== SUPPORTED_VERSION) {
916
- process.stderr.write(
917
- `
918
- \u26A0\uFE0F Node9: Config at ${filePath} declares version "${vStr}" \u2014 expected "${SUPPORTED_VERSION}". Continuing with best-effort parsing.
919
-
920
- `
921
- );
922
- }
923
- }
924
- const { sanitized, error } = sanitizeConfig(raw);
925
- if (error) {
926
- process.stderr.write(
927
- `
928
- \u26A0\uFE0F Node9: Invalid config at ${filePath}:
929
- ${error.replace("Invalid config:\n", "")}
930
- \u2192 Invalid fields ignored, using defaults for those keys
931
-
932
- `
933
- );
934
- }
935
- return sanitized;
936
- }
937
-
938
- // src/utils/regex.ts
939
- var import_safe_regex2 = __toESM(require("safe-regex2"));
940
- var MAX_REGEX_LENGTH = 100;
941
- var REGEX_CACHE_MAX = 500;
942
- var regexCache = /* @__PURE__ */ new Map();
943
- function validateRegex(pattern) {
944
- if (!pattern) return "Pattern is required";
945
- if (pattern.length > MAX_REGEX_LENGTH) return `Pattern exceeds max length of ${MAX_REGEX_LENGTH}`;
946
- try {
947
- new RegExp(pattern);
948
- } catch (e) {
949
- return `Invalid regex syntax: ${e.message}`;
950
- }
951
- if (/\\\d+[*+{]/.test(pattern)) return "Quantified backreferences are forbidden (ReDoS risk)";
952
- if (!(0, import_safe_regex2.default)(pattern)) return "Pattern rejected: potential ReDoS vulnerability detected";
953
- return null;
954
- }
955
- function getCompiledRegex(pattern, flags = "") {
956
- if (flags && !/^[gimsuy]+$/.test(flags)) {
957
- if (process.env.NODE9_DEBUG === "1") console.error(`[Node9] Invalid regex flags: "${flags}"`);
958
- return null;
959
- }
960
- const key = `${pattern}\0${flags}`;
961
- if (regexCache.has(key)) {
962
- const cached = regexCache.get(key);
963
- regexCache.delete(key);
964
- regexCache.set(key, cached);
965
- return cached;
966
- }
967
- const err = validateRegex(pattern);
968
- if (err) {
969
- if (process.env.NODE9_DEBUG === "1")
970
- console.error(`[Node9] Regex blocked: ${err} \u2014 pattern: "${pattern}"`);
971
- return null;
972
- }
973
- try {
974
- const re = new RegExp(pattern, flags);
975
- if (regexCache.size >= REGEX_CACHE_MAX) {
976
- const oldest = regexCache.keys().next().value;
977
- if (oldest) regexCache.delete(oldest);
978
- }
979
- regexCache.set(key, re);
980
- return re;
981
- } catch (e) {
982
- if (process.env.NODE9_DEBUG === "1") console.error(`[Node9] Regex compile failed:`, e);
983
- return null;
984
- }
985
- }
986
-
987
- // src/policy/index.ts
988
- var import_path8 = __toESM(require("path"));
989
- var import_picomatch = __toESM(require("picomatch"));
990
- var import_mvdan_sh = __toESM(require("mvdan-sh"));
991
-
992
- // src/dlp.ts
993
- var import_fs4 = __toESM(require("fs"));
994
- var import_path4 = __toESM(require("path"));
995
- var ASSIGNMENT_CONTEXT_RE = /\b(?:password|passwd|secret|token|api[_-]?key|auth(?:_key|_token)?|credential|private[_-]?key|access[_-]?key|client[_-]?secret)\s*[=:]\s*/i;
996
- function isAssignmentContext(text) {
997
- return ASSIGNMENT_CONTEXT_RE.test(text);
998
- }
999
- function shannonEntropy(s) {
1000
- if (s.length === 0) return 0;
1001
- const freq = /* @__PURE__ */ new Map();
1002
- for (const ch of s) freq.set(ch, (freq.get(ch) ?? 0) + 1);
1003
- let h = 0;
1004
- for (const count of freq.values()) {
1005
- const p = count / s.length;
1006
- h -= p * Math.log2(p);
1007
- }
1008
- return h;
1009
- }
1010
- var DLP_STOPWORDS = [
1011
- "example",
1012
- "placeholder",
1013
- "changeme",
1014
- "your_key",
1015
- "your_token",
1016
- "your_secret",
1017
- "replace_me",
1018
- "insert_key",
1019
- "put_your",
1020
- "fake",
1021
- "dummy",
1022
- "sample",
1023
- "xxxxxxxx",
1024
- "aaaaaa",
1025
- "bbbbbb",
1026
- "00000000",
1027
- "${",
1028
- "{{",
1029
- "%{",
1030
- "<your",
1031
- "test_key",
1032
- "test_token",
1033
- "your",
1034
- "here"
1035
- ];
1036
- var DLP_PATTERNS = [
1037
- // ── AWS ───────────────────────────────────────────────────────────────────
1038
- {
1039
- name: "AWS Access Key ID",
1040
- regex: /\b(?:A3T[A-Z0-9]|AKIA|ASIA|ABIA|ACCA)[A-Z2-7]{16}\b/,
1041
- severity: "block",
1042
- keywords: ["akia", "asia", "abia", "acca", "a3t"]
1043
- },
1044
- // ── GitHub ────────────────────────────────────────────────────────────────
1045
- {
1046
- name: "GitHub Token",
1047
- regex: /\bgh[pous]_[A-Za-z0-9]{36}\b/,
1048
- severity: "block",
1049
- keywords: ["ghp_", "gho_", "ghu_", "ghs_"],
1050
- minEntropy: 3
1051
- },
1052
- {
1053
- name: "GitHub Fine-Grained PAT",
1054
- regex: /\bgithub_pat_\w{82}\b/,
1055
- severity: "block",
1056
- keywords: ["github_pat_"]
1057
- },
1058
- // ── Slack ─────────────────────────────────────────────────────────────────
1059
- {
1060
- name: "Slack Bot Token",
1061
- // Real tokens are ~50–80 chars; lower bound 20 avoids false negatives on partial tokens
1062
- regex: /\bxoxb-[0-9A-Za-z-]{20,100}\b/,
1063
- severity: "block",
1064
- keywords: ["xoxb-"]
1065
- },
1066
- // ── Anthropic ─────────────────────────────────────────────────────────────
1067
- // Listed before OpenAI — Anthropic keys start with sk-ant- which would also
1068
- // match the broader OpenAI sk- pattern; more specific rules must come first.
1069
- {
1070
- name: "Anthropic API Key",
1071
- regex: /\bsk-ant-api03-[a-zA-Z0-9_-]{93}AA\b/,
1072
- severity: "block",
1073
- keywords: ["sk-ant-api03"]
1074
- },
1075
- {
1076
- name: "Anthropic Admin Key",
1077
- regex: /\bsk-ant-admin01-[a-zA-Z0-9_-]{93}AA\b/,
1078
- severity: "block",
1079
- keywords: ["sk-ant-admin01"]
1080
- },
1081
- // ── OpenAI ────────────────────────────────────────────────────────────────
399
+ // ── OpenAI ────────────────────────────────────────────────────────────────
1082
400
  {
1083
401
  name: "OpenAI API Key",
1084
402
  regex: /\bsk-[a-zA-Z0-9_-]{20,}\b/,
@@ -1452,1014 +770,2450 @@ var SENSITIVE_PATH_PATTERNS = [
1452
770
  /[/\\]id_ed25519$/i,
1453
771
  /[/\\]id_ecdsa$/i
1454
772
  ];
1455
- function scanFilePath(filePath, cwd = process.cwd()) {
1456
- if (!filePath) return null;
1457
- let resolved;
1458
- try {
1459
- const absolute = import_path4.default.resolve(cwd, filePath);
1460
- resolved = import_fs4.default.realpathSync.native(absolute);
1461
- } catch (err) {
1462
- const code = err.code;
1463
- if (code === "ENOENT" || code === "ENOTDIR") {
1464
- resolved = import_path4.default.resolve(cwd, filePath);
1465
- } else {
773
+ function matchSensitivePath(resolvedPath, originalPath) {
774
+ const normalised = resolvedPath.replace(/\\/g, "/");
775
+ for (const pattern of SENSITIVE_PATH_PATTERNS) {
776
+ if (pattern.test(normalised)) {
1466
777
  return {
1467
778
  patternName: "Sensitive File Path",
1468
779
  fieldPath: "file_path",
1469
- redactedSample: filePath,
780
+ redactedSample: originalPath,
1470
781
  severity: "block"
1471
782
  };
1472
783
  }
1473
784
  }
1474
- const normalised = resolved.replace(/\\/g, "/");
1475
- for (const pattern of SENSITIVE_PATH_PATTERNS) {
1476
- if (pattern.test(normalised)) {
1477
- return {
1478
- patternName: "Sensitive File Path",
1479
- fieldPath: "file_path",
1480
- redactedSample: filePath,
1481
- // show original path in alert, not resolved
1482
- severity: "block"
1483
- };
785
+ return null;
786
+ }
787
+ function sensitivePathMatch(originalPath) {
788
+ return {
789
+ patternName: "Sensitive File Path",
790
+ fieldPath: "file_path",
791
+ redactedSample: originalPath,
792
+ severity: "block"
793
+ };
794
+ }
795
+ function assertBuiltinPatternsAreSafe() {
796
+ for (const p of DLP_PATTERNS) {
797
+ if (!(0, import_safe_regex2.default)(p.regex.source)) {
798
+ throw new Error(
799
+ `[node9 engine] Builtin DLP pattern '${p.name}' is vulnerable to ReDoS: ${p.regex.source}`
800
+ );
801
+ }
802
+ }
803
+ for (const re of SENSITIVE_PATH_PATTERNS) {
804
+ if (!(0, import_safe_regex2.default)(re.source)) {
805
+ throw new Error(
806
+ `[node9 engine] Builtin sensitive-path pattern is vulnerable to ReDoS: ${re.source}`
807
+ );
808
+ }
809
+ }
810
+ }
811
+ assertBuiltinPatternsAreSafe();
812
+ function maskSecret(raw, pattern) {
813
+ const match = raw.match(pattern);
814
+ if (!match) return "****";
815
+ const secret = match[0];
816
+ if (secret.length < 8) return "****";
817
+ const prefix = secret.slice(0, 4);
818
+ const suffix = secret.slice(-4);
819
+ const stars = "*".repeat(Math.min(secret.length - 8, 12));
820
+ return `${prefix}${stars}${suffix}`;
821
+ }
822
+ var MAX_DEPTH = 5;
823
+ var MAX_STRING_BYTES = 1e5;
824
+ var MAX_JSON_PARSE_BYTES = 1e4;
825
+ function scanArgs(args, depth = 0, fieldPath = "args") {
826
+ if (depth > MAX_DEPTH || args === null || args === void 0) return null;
827
+ if (Array.isArray(args)) {
828
+ for (let i = 0; i < args.length; i++) {
829
+ const match = scanArgs(args[i], depth + 1, `${fieldPath}[${i}]`);
830
+ if (match) return match;
831
+ }
832
+ return null;
833
+ }
834
+ if (typeof args === "object") {
835
+ for (const [key, value] of Object.entries(args)) {
836
+ const match = scanArgs(value, depth + 1, `${fieldPath}.${key}`);
837
+ if (match) return match;
838
+ }
839
+ return null;
840
+ }
841
+ if (typeof args === "string") {
842
+ const text = args.length > MAX_STRING_BYTES ? args.slice(0, MAX_STRING_BYTES) : args;
843
+ const textLower = text.toLowerCase();
844
+ const assignmentCtx = isAssignmentContext(text);
845
+ for (const pattern of DLP_PATTERNS) {
846
+ if (pattern.keywords && !pattern.keywords.some((kw) => textLower.includes(kw.toLowerCase()))) {
847
+ continue;
848
+ }
849
+ if (pattern.regex.test(text)) {
850
+ const raw = text.match(pattern.regex)?.[0] ?? "";
851
+ if (DLP_STOPWORDS.some((sw) => raw.toLowerCase().includes(sw))) continue;
852
+ if (pattern.minEntropy !== void 0 && shannonEntropy(raw) < pattern.minEntropy) continue;
853
+ const severity = pattern.contextBoost && assignmentCtx ? "block" : pattern.severity;
854
+ return {
855
+ patternName: pattern.name,
856
+ fieldPath,
857
+ redactedSample: maskSecret(text, pattern.regex),
858
+ severity
859
+ };
860
+ }
861
+ }
862
+ if (text.length < MAX_JSON_PARSE_BYTES) {
863
+ const trimmed = text.trim();
864
+ if (trimmed.startsWith("{") || trimmed.startsWith("[")) {
865
+ try {
866
+ const parsed = JSON.parse(text);
867
+ const inner = scanArgs(parsed, depth + 1, fieldPath);
868
+ if (inner) return inner;
869
+ } catch {
870
+ }
871
+ }
872
+ }
873
+ }
874
+ return null;
875
+ }
876
+ var { syntax } = import_mvdan_sh.default;
877
+ var sharedParser = syntax.NewParser();
878
+ var MESSAGE_FLAGS = /* @__PURE__ */ new Set([
879
+ "-m",
880
+ "--message",
881
+ "--body",
882
+ "--title",
883
+ "--description",
884
+ "--comment",
885
+ "--subject",
886
+ "--summary"
887
+ ]);
888
+ var SHELL_INTERPRETERS = /* @__PURE__ */ new Set(["bash", "sh", "zsh", "fish", "dash", "ksh"]);
889
+ var DOWNLOAD_CMDS = /* @__PURE__ */ new Set(["curl", "wget"]);
890
+ function normalizeCommandForPolicy(command) {
891
+ try {
892
+ const f = sharedParser.Parse(command, "cmd");
893
+ const strips = [];
894
+ syntax.Walk(f, (node) => {
895
+ if (!node) return false;
896
+ const n = node;
897
+ if (syntax.NodeType(n) !== "CallExpr") return true;
898
+ const args = n.Args || [];
899
+ for (let i = 0; i < args.length - 1; i++) {
900
+ const argParts = args[i].Parts || [];
901
+ if (argParts.length !== 1 || syntax.NodeType(argParts[0]) !== "Lit") continue;
902
+ const flagVal = argParts[0].Value || "";
903
+ if (!MESSAGE_FLAGS.has(flagVal.toLowerCase())) continue;
904
+ const next = args[i + 1];
905
+ const nextParts = next.Parts || [];
906
+ if (nextParts.length !== 1) continue;
907
+ const quotedNode = nextParts[0];
908
+ const nt = syntax.NodeType(quotedNode);
909
+ if (nt === "SglQuoted") {
910
+ strips.push([next.Pos().Offset(), next.End().Offset()]);
911
+ } else if (nt === "DblQuoted") {
912
+ const innerParts = quotedNode.Parts || [];
913
+ const allLit = innerParts.length === 0 || innerParts.every((p) => syntax.NodeType(p) === "Lit");
914
+ if (allLit) strips.push([next.Pos().Offset(), next.End().Offset()]);
915
+ }
916
+ }
917
+ return true;
918
+ });
919
+ if (strips.length === 0) return command;
920
+ strips.sort((a, b) => b[0] - a[0]);
921
+ let result = command;
922
+ for (const [start, end] of strips) {
923
+ result = result.slice(0, start) + '""' + result.slice(end);
1484
924
  }
925
+ return result;
926
+ } catch {
927
+ return command;
928
+ }
929
+ }
930
+ function scanArgsForDynamicExec(args, startIdx) {
931
+ let hasCmdSubst = false;
932
+ let hasParamExp = false;
933
+ let hasCurl = false;
934
+ for (let i = startIdx; i < args.length; i++) {
935
+ syntax.Walk(args[i], (inner) => {
936
+ if (!inner) return false;
937
+ const inn = inner;
938
+ const it = syntax.NodeType(inn);
939
+ if (it === "CmdSubst") hasCmdSubst = true;
940
+ if (it === "ParamExp") hasParamExp = true;
941
+ if (it === "Lit" && DOWNLOAD_CMDS.has(inn.Value?.toLowerCase())) hasCurl = true;
942
+ return true;
943
+ });
1485
944
  }
945
+ if (hasCmdSubst && hasCurl) return "block";
946
+ if (hasCmdSubst || hasParamExp) return "review";
1486
947
  return null;
1487
948
  }
1488
- function maskSecret(raw, pattern) {
1489
- const match = raw.match(pattern);
1490
- if (!match) return "****";
1491
- const secret = match[0];
1492
- if (secret.length < 8) return "****";
1493
- const prefix = secret.slice(0, 4);
1494
- const suffix = secret.slice(-4);
1495
- const stars = "*".repeat(Math.min(secret.length - 8, 12));
1496
- return `${prefix}${stars}${suffix}`;
949
+ function detectDangerousShellExec(command) {
950
+ try {
951
+ const f = sharedParser.Parse(command, "cmd");
952
+ let result = null;
953
+ syntax.Walk(f, (node) => {
954
+ if (!node || result === "block") return false;
955
+ const n = node;
956
+ if (syntax.NodeType(n) !== "CallExpr") return true;
957
+ const args = n.Args || [];
958
+ if (args.length === 0) return true;
959
+ const firstParts = args[0].Parts || [];
960
+ if (firstParts.length !== 1 || syntax.NodeType(firstParts[0]) !== "Lit") return true;
961
+ const cmdName = firstParts[0].Value?.toLowerCase() ?? "";
962
+ if (cmdName === "eval") {
963
+ const v = scanArgsForDynamicExec(args, 1);
964
+ if (v === "block" || v === "review" && result === null) result = v;
965
+ } else if (SHELL_INTERPRETERS.has(cmdName)) {
966
+ for (let i = 1; i < args.length - 1; i++) {
967
+ const flagParts = args[i].Parts || [];
968
+ if (flagParts.length !== 1 || syntax.NodeType(flagParts[0]) !== "Lit" || flagParts[0].Value !== "-c")
969
+ continue;
970
+ const v = scanArgsForDynamicExec(args, i + 1);
971
+ if (v === "block" || v === "review" && result === null) result = v;
972
+ break;
973
+ }
974
+ }
975
+ return true;
976
+ });
977
+ return result;
978
+ } catch {
979
+ return null;
980
+ }
1497
981
  }
1498
- var MAX_DEPTH = 5;
1499
- var MAX_STRING_BYTES = 1e5;
1500
- var MAX_JSON_PARSE_BYTES = 1e4;
1501
- function scanArgs(args, depth = 0, fieldPath = "args") {
1502
- if (depth > MAX_DEPTH || args === null || args === void 0) return null;
1503
- if (Array.isArray(args)) {
1504
- for (let i = 0; i < args.length; i++) {
1505
- const match = scanArgs(args[i], depth + 1, `${fieldPath}[${i}]`);
1506
- if (match) return match;
982
+ function analyzeShellCommand(command) {
983
+ const actions = [];
984
+ const paths = [];
985
+ const allTokens = [];
986
+ const addToken = (token) => {
987
+ const lower = token.toLowerCase();
988
+ allTokens.push(lower);
989
+ if (lower.includes("/")) allTokens.push(...lower.split("/").filter(Boolean));
990
+ if (lower.startsWith("-")) allTokens.push(lower.replace(/^-+/, ""));
991
+ };
992
+ try {
993
+ const f = sharedParser.Parse(command, "cmd");
994
+ syntax.Walk(f, (node) => {
995
+ if (!node) return false;
996
+ const n = node;
997
+ if (syntax.NodeType(n) !== "CallExpr") return true;
998
+ const wordValues = (n.Args || []).map((arg) => {
999
+ return (arg.Parts || []).map((p) => (p.Value ?? "").replace(/\\(.)/g, "$1")).join("");
1000
+ }).filter((s) => s.length > 0);
1001
+ if (wordValues.length > 0) {
1002
+ const cmd = wordValues[0].toLowerCase();
1003
+ if (!actions.includes(cmd)) actions.push(cmd);
1004
+ wordValues.forEach((w) => addToken(w));
1005
+ wordValues.slice(1).forEach((w) => {
1006
+ if (!w.startsWith("-")) paths.push(w);
1007
+ });
1008
+ }
1009
+ return true;
1010
+ });
1011
+ } catch {
1012
+ }
1013
+ if (allTokens.length === 0) {
1014
+ const normalized = command.replace(/\\(.)/g, "$1");
1015
+ const sanitized = normalized.replace(/["'<>]/g, " ");
1016
+ const segments = sanitized.split(/[|;&]|\$\(|\)|`/);
1017
+ segments.forEach((segment) => {
1018
+ const tokens = segment.trim().split(/\s+/).filter(Boolean);
1019
+ if (tokens.length > 0) {
1020
+ const action = tokens[0].toLowerCase();
1021
+ if (!actions.includes(action)) actions.push(action);
1022
+ tokens.forEach((t) => {
1023
+ addToken(t);
1024
+ if (t !== tokens[0] && !t.startsWith("-")) {
1025
+ if (!paths.includes(t)) paths.push(t);
1026
+ }
1027
+ });
1028
+ }
1029
+ });
1030
+ }
1031
+ return { actions, paths, allTokens };
1032
+ }
1033
+ var SOURCE_COMMANDS = /* @__PURE__ */ new Set([
1034
+ "cat",
1035
+ "head",
1036
+ "tail",
1037
+ "grep",
1038
+ "awk",
1039
+ "sed",
1040
+ "cut",
1041
+ "sort",
1042
+ "tee",
1043
+ "less",
1044
+ "more",
1045
+ "strings",
1046
+ "xxd"
1047
+ ]);
1048
+ var SINK_COMMANDS = /* @__PURE__ */ new Set([
1049
+ "curl",
1050
+ "wget",
1051
+ "nc",
1052
+ "ncat",
1053
+ "netcat",
1054
+ "ssh",
1055
+ "scp",
1056
+ "rsync",
1057
+ "socat",
1058
+ "ftp",
1059
+ "sftp",
1060
+ "telnet"
1061
+ ]);
1062
+ var OBFUSCATORS = /* @__PURE__ */ new Set([
1063
+ "base64",
1064
+ "gzip",
1065
+ "gunzip",
1066
+ "bzip2",
1067
+ "xz",
1068
+ "zstd",
1069
+ "openssl",
1070
+ "gpg",
1071
+ "python",
1072
+ "python3",
1073
+ "perl",
1074
+ "ruby",
1075
+ "node"
1076
+ ]);
1077
+ var SENSITIVE_PATTERNS = [
1078
+ /(?:^|\/)\.env(?:\.|$)/i,
1079
+ // .env, .env.local, .env.production
1080
+ /id_rsa|id_ed25519|id_ecdsa|id_dsa/i,
1081
+ // SSH private keys
1082
+ /\.pem$|\.key$|\.p12$|\.pfx$/i,
1083
+ // certificate files
1084
+ /(?:^|\/)\.ssh\//i,
1085
+ // ~/.ssh/ directory
1086
+ /(?:^|\/)\.aws\/credentials/i,
1087
+ // AWS credentials
1088
+ /(?:^|\/)\.netrc$/i,
1089
+ // netrc (stores HTTP credentials)
1090
+ /(?:^|\/)(passwd|shadow|sudoers)$/i,
1091
+ // /etc/passwd, /etc/shadow
1092
+ /(?:^|\/)credentials(?:\.json)?$/i
1093
+ // generic credentials files
1094
+ ];
1095
+ function isSensitivePath(p) {
1096
+ return SENSITIVE_PATTERNS.some((re) => re.test(p));
1097
+ }
1098
+ function splitOnPipe(cmd) {
1099
+ const segments = [];
1100
+ let current = "";
1101
+ let inSingle = false;
1102
+ let inDouble = false;
1103
+ for (let i = 0; i < cmd.length; i++) {
1104
+ const ch = cmd[i];
1105
+ if (ch === "'" && !inDouble) {
1106
+ inSingle = !inSingle;
1107
+ current += ch;
1108
+ } else if (ch === '"' && !inSingle) {
1109
+ inDouble = !inDouble;
1110
+ current += ch;
1111
+ } else if (ch === "|" && !inSingle && !inDouble && cmd[i + 1] !== "|" && (i === 0 || cmd[i - 1] !== "|")) {
1112
+ segments.push(current.trim());
1113
+ current = "";
1114
+ } else {
1115
+ current += ch;
1507
1116
  }
1508
- return null;
1509
1117
  }
1510
- if (typeof args === "object") {
1511
- for (const [key, value] of Object.entries(args)) {
1512
- const match = scanArgs(value, depth + 1, `${fieldPath}.${key}`);
1513
- if (match) return match;
1514
- }
1515
- return null;
1118
+ if (current.trim()) segments.push(current.trim());
1119
+ return segments.filter(Boolean);
1120
+ }
1121
+ function positionalTokens(segment) {
1122
+ return segment.split(/\s+/).slice(1).filter((t) => !t.startsWith("-") && !t.startsWith("@") && t.length > 0);
1123
+ }
1124
+ function analyzePipeChain(command) {
1125
+ const segments = splitOnPipe(command);
1126
+ if (segments.length < 2) {
1127
+ return {
1128
+ isPipeline: false,
1129
+ hasSensitiveSource: false,
1130
+ hasExternalSink: false,
1131
+ hasObfuscation: false,
1132
+ sourceFiles: [],
1133
+ sinkTargets: [],
1134
+ risk: "none"
1135
+ };
1516
1136
  }
1517
- if (typeof args === "string") {
1518
- const text = args.length > MAX_STRING_BYTES ? args.slice(0, MAX_STRING_BYTES) : args;
1519
- const textLower = text.toLowerCase();
1520
- const assignmentCtx = isAssignmentContext(text);
1521
- for (const pattern of DLP_PATTERNS) {
1522
- if (pattern.keywords && !pattern.keywords.some((kw) => textLower.includes(kw.toLowerCase()))) {
1523
- continue;
1524
- }
1525
- if (pattern.regex.test(text)) {
1526
- const raw = text.match(pattern.regex)?.[0] ?? "";
1527
- if (DLP_STOPWORDS.some((sw) => raw.toLowerCase().includes(sw))) continue;
1528
- if (pattern.minEntropy !== void 0 && shannonEntropy(raw) < pattern.minEntropy) continue;
1529
- const severity = pattern.contextBoost && assignmentCtx ? "block" : pattern.severity;
1530
- return {
1531
- patternName: pattern.name,
1532
- fieldPath,
1533
- redactedSample: maskSecret(text, pattern.regex),
1534
- severity
1535
- };
1536
- }
1137
+ const sourceFiles = [];
1138
+ const sinkTargets = [];
1139
+ let hasSensitiveSource = false;
1140
+ let hasExternalSink = false;
1141
+ let hasObfuscation = false;
1142
+ for (const segment of segments) {
1143
+ const tokens = segment.split(/\s+/).filter(Boolean);
1144
+ if (tokens.length === 0) continue;
1145
+ const binary = tokens[0].toLowerCase();
1146
+ const args = positionalTokens(segment);
1147
+ if (SOURCE_COMMANDS.has(binary)) {
1148
+ sourceFiles.push(...args);
1149
+ if (args.some(isSensitivePath)) hasSensitiveSource = true;
1537
1150
  }
1538
- if (text.length < MAX_JSON_PARSE_BYTES) {
1539
- const trimmed = text.trim();
1540
- if (trimmed.startsWith("{") || trimmed.startsWith("[")) {
1541
- try {
1542
- const parsed = JSON.parse(text);
1543
- const inner = scanArgs(parsed, depth + 1, fieldPath);
1544
- if (inner) return inner;
1545
- } catch {
1546
- }
1547
- }
1151
+ if (OBFUSCATORS.has(binary)) hasObfuscation = true;
1152
+ if (SINK_COMMANDS.has(binary)) {
1153
+ const targets = args.filter(
1154
+ (a) => a.includes(".") || a.includes("://") || /^\d+\.\d+/.test(a)
1155
+ );
1156
+ sinkTargets.push(...targets);
1157
+ if (targets.length > 0) hasExternalSink = true;
1548
1158
  }
1549
1159
  }
1550
- return null;
1551
- }
1552
-
1553
- // src/utils/provenance.ts
1554
- var import_fs5 = __toESM(require("fs"));
1555
- var import_path5 = __toESM(require("path"));
1556
- var import_os4 = __toESM(require("os"));
1557
- var SYSTEM_PREFIXES = ["/usr/bin", "/usr/sbin", "/bin", "/sbin"];
1558
- var MANAGED_PREFIXES = ["/usr/local/bin", "/opt/homebrew", "/home/linuxbrew", "/nix/store"];
1559
- var USER_PREFIXES = [
1560
- import_path5.default.join(import_os4.default.homedir(), "bin"),
1561
- import_path5.default.join(import_os4.default.homedir(), ".local", "bin"),
1562
- import_path5.default.join(import_os4.default.homedir(), ".cargo", "bin"),
1563
- import_path5.default.join(import_os4.default.homedir(), ".npm-global", "bin"),
1564
- import_path5.default.join(import_os4.default.homedir(), ".volta", "bin")
1565
- ];
1566
- var SUSPECT_PREFIXES = ["/tmp", "/var/tmp", "/dev/shm"];
1567
- function findInPath(cmd) {
1568
- if (import_path5.default.posix.isAbsolute(cmd)) return cmd;
1569
- const pathEnv = process.env.PATH ?? "";
1570
- for (const dir of pathEnv.split(import_path5.default.delimiter)) {
1571
- if (!dir) continue;
1572
- const full = import_path5.default.join(dir, cmd);
1573
- try {
1574
- import_fs5.default.accessSync(full, import_fs5.default.constants.X_OK);
1575
- return full;
1576
- } catch {
1160
+ const fullCmd = command.toLowerCase();
1161
+ if (!hasSensitiveSource) {
1162
+ const redirMatch = fullCmd.match(/<\s*(\S+)/);
1163
+ if (redirMatch && isSensitivePath(redirMatch[1])) {
1164
+ hasSensitiveSource = true;
1165
+ sourceFiles.push(redirMatch[1]);
1577
1166
  }
1578
1167
  }
1579
- return null;
1168
+ const risk = hasSensitiveSource && hasExternalSink && hasObfuscation ? "critical" : hasSensitiveSource && hasExternalSink ? "high" : hasExternalSink ? "medium" : "none";
1169
+ return {
1170
+ isPipeline: true,
1171
+ hasSensitiveSource,
1172
+ hasExternalSink,
1173
+ hasObfuscation,
1174
+ sourceFiles,
1175
+ sinkTargets,
1176
+ risk
1177
+ };
1580
1178
  }
1581
- function _classifyPath(resolved, cwd) {
1582
- if (cwd && resolved.startsWith(cwd + "/")) {
1583
- return { trustLevel: "user", reason: "binary in project directory" };
1584
- }
1585
- const osTmp = import_os4.default.tmpdir();
1586
- const allSuspect = osTmp ? [...SUSPECT_PREFIXES, osTmp] : SUSPECT_PREFIXES;
1587
- if (allSuspect.some((p) => resolved === p || resolved.startsWith(p + "/"))) {
1588
- return { trustLevel: "suspect", reason: `binary in temp directory: ${resolved}` };
1589
- }
1590
- if (SYSTEM_PREFIXES.some((p) => resolved === p || resolved.startsWith(p + "/"))) {
1591
- return { trustLevel: "system", reason: "" };
1592
- }
1593
- if (MANAGED_PREFIXES.some((p) => resolved === p || resolved.startsWith(p + "/"))) {
1594
- return { trustLevel: "managed", reason: "" };
1595
- }
1596
- if (USER_PREFIXES.some((p) => resolved === p || resolved.startsWith(p + "/"))) {
1597
- return { trustLevel: "user", reason: "" };
1598
- }
1599
- return { trustLevel: "unknown", reason: "binary in unrecognized location" };
1179
+ function basename(p) {
1180
+ const segments = p.split(/[\\/]/);
1181
+ return segments[segments.length - 1] || "";
1600
1182
  }
1601
- function checkProvenance(cmd, cwd) {
1602
- const bare = cmd.startsWith("./") ? cmd.slice(2) : cmd;
1603
- if (import_path5.default.posix.isAbsolute(bare)) {
1604
- const early = _classifyPath(bare, cwd);
1605
- if (early.trustLevel === "suspect") {
1606
- return { resolvedPath: bare, ...early };
1607
- }
1608
- }
1609
- let resolved;
1610
- try {
1611
- const found = findInPath(bare);
1612
- if (!found) {
1613
- return {
1614
- resolvedPath: cmd,
1615
- trustLevel: "unknown",
1616
- reason: "binary not found in PATH"
1617
- };
1183
+ var FLAGS_WITH_VALUES = {
1184
+ curl: /* @__PURE__ */ new Set([
1185
+ "-H",
1186
+ "--header",
1187
+ "-A",
1188
+ "--user-agent",
1189
+ "-e",
1190
+ "--referer",
1191
+ "-x",
1192
+ "--proxy",
1193
+ "-u",
1194
+ "--user",
1195
+ "-d",
1196
+ "--data",
1197
+ "--data-raw",
1198
+ "--data-binary",
1199
+ "-o",
1200
+ "--output",
1201
+ "-F",
1202
+ "--form",
1203
+ "--connect-to",
1204
+ "--resolve",
1205
+ "--cacert",
1206
+ "--cert",
1207
+ "--key",
1208
+ "-m",
1209
+ "--max-time"
1210
+ ]),
1211
+ wget: /* @__PURE__ */ new Set([
1212
+ "-O",
1213
+ "--output-document",
1214
+ "-P",
1215
+ "--directory-prefix",
1216
+ "-U",
1217
+ "--user-agent",
1218
+ "-e",
1219
+ "--execute",
1220
+ "--proxy",
1221
+ "--ca-certificate"
1222
+ ]),
1223
+ nc: /* @__PURE__ */ new Set(["-x", "-p", "-s", "-w", "-W", "-I", "-O"]),
1224
+ ncat: /* @__PURE__ */ new Set(["-x", "-p", "-s", "--proxy", "--proxy-auth", "-w", "--wait"]),
1225
+ netcat: /* @__PURE__ */ new Set(["-x", "-p", "-s", "-w"]),
1226
+ ssh: /* @__PURE__ */ new Set([
1227
+ "-i",
1228
+ "-l",
1229
+ "-p",
1230
+ "-o",
1231
+ "-E",
1232
+ "-F",
1233
+ "-J",
1234
+ "-L",
1235
+ "-R",
1236
+ "-W",
1237
+ "-b",
1238
+ "-c",
1239
+ "-D",
1240
+ "-e",
1241
+ "-I",
1242
+ "-S"
1243
+ ]),
1244
+ scp: /* @__PURE__ */ new Set(["-i", "-o", "-P", "-S"]),
1245
+ rsync: /* @__PURE__ */ new Set(["-e", "--rsh", "--rsync-path", "--password-file", "--log-file"]),
1246
+ socat: /* @__PURE__ */ new Set([])
1247
+ // socat uses address syntax, not flags — no value-flags
1248
+ };
1249
+ function extractPositionalArgs(tokens, binary) {
1250
+ const binaryName = basename(binary).replace(/\.exe$/i, "");
1251
+ const flagsWithValues = FLAGS_WITH_VALUES[binaryName] ?? /* @__PURE__ */ new Set();
1252
+ const positional = [];
1253
+ let skipNext = false;
1254
+ for (const token of tokens) {
1255
+ if (skipNext) {
1256
+ skipNext = false;
1257
+ continue;
1618
1258
  }
1619
- resolved = import_fs5.default.realpathSync(found);
1620
- } catch {
1621
- return {
1622
- resolvedPath: cmd,
1623
- trustLevel: "unknown",
1624
- reason: "binary not found in PATH"
1625
- };
1626
- }
1627
- try {
1628
- const stat = import_fs5.default.statSync(resolved);
1629
- if (stat.mode & 2) {
1630
- return {
1631
- resolvedPath: resolved,
1632
- trustLevel: "suspect",
1633
- reason: "binary is world-writable"
1634
- };
1259
+ if (token.startsWith("--") && token.includes("=")) continue;
1260
+ if (token.startsWith("-") && token.length === 2 && flagsWithValues.has(token)) {
1261
+ skipNext = true;
1262
+ continue;
1635
1263
  }
1636
- } catch {
1637
- return {
1638
- resolvedPath: resolved,
1639
- trustLevel: "unknown",
1640
- reason: "could not stat binary"
1641
- };
1264
+ if (token.startsWith("--") && flagsWithValues.has(token)) {
1265
+ skipNext = true;
1266
+ continue;
1267
+ }
1268
+ const shortFlag = token.slice(0, 2);
1269
+ if (token.startsWith("-") && token.length > 2 && flagsWithValues.has(shortFlag)) continue;
1270
+ if (token.startsWith("-")) continue;
1271
+ if (token.startsWith("@")) continue;
1272
+ positional.push(token);
1642
1273
  }
1643
- const classify = _classifyPath(resolved, cwd);
1644
- return { resolvedPath: resolved, ...classify };
1274
+ return positional;
1645
1275
  }
1646
-
1647
- // src/policy/pipe-chain.ts
1648
- var SOURCE_COMMANDS = /* @__PURE__ */ new Set([
1649
- "cat",
1650
- "head",
1651
- "tail",
1652
- "grep",
1653
- "awk",
1654
- "sed",
1655
- "cut",
1656
- "sort",
1657
- "tee",
1658
- "less",
1659
- "more",
1660
- "strings",
1661
- "xxd"
1662
- ]);
1663
- var SINK_COMMANDS = /* @__PURE__ */ new Set([
1664
- "curl",
1665
- "wget",
1666
- "nc",
1667
- "ncat",
1668
- "netcat",
1669
- "ssh",
1670
- "scp",
1671
- "rsync",
1672
- "socat",
1673
- "ftp",
1674
- "sftp",
1675
- "telnet"
1676
- ]);
1677
- var OBFUSCATORS = /* @__PURE__ */ new Set([
1678
- "base64",
1679
- "gzip",
1680
- "gunzip",
1681
- "bzip2",
1682
- "xz",
1683
- "zstd",
1684
- "openssl",
1685
- "gpg",
1686
- "python",
1687
- "python3",
1688
- "perl",
1689
- "ruby",
1690
- "node"
1691
- ]);
1692
- var SENSITIVE_PATTERNS = [
1693
- /(?:^|\/)\.env(?:\.|$)/i,
1694
- // .env, .env.local, .env.production
1695
- /id_rsa|id_ed25519|id_ecdsa|id_dsa/i,
1696
- // SSH private keys
1697
- /\.pem$|\.key$|\.p12$|\.pfx$/i,
1698
- // certificate files
1699
- /(?:^|\/)\.ssh\//i,
1700
- // ~/.ssh/ directory
1701
- /(?:^|\/)\.aws\/credentials/i,
1702
- // AWS credentials
1703
- /(?:^|\/)\.netrc$/i,
1704
- // netrc (stores HTTP credentials)
1705
- /(?:^|\/)(passwd|shadow|sudoers)$/i,
1706
- // /etc/passwd, /etc/shadow
1707
- /(?:^|\/)credentials(?:\.json)?$/i
1708
- // generic credentials files
1709
- ];
1710
- function isSensitivePath(p) {
1711
- return SENSITIVE_PATTERNS.some((re) => re.test(p));
1276
+ function extractNetworkTargets(tokens, binary) {
1277
+ return extractPositionalArgs(tokens, binary).map((t) => t.includes("@") ? t.split("@")[1] : t).map((t) => {
1278
+ const colonIdx = t.indexOf(":");
1279
+ if (colonIdx === -1) return t;
1280
+ const afterColon = t.slice(colonIdx + 1);
1281
+ if (/^\d+$/.test(afterColon)) return t.slice(0, colonIdx);
1282
+ return t;
1283
+ }).filter(Boolean);
1712
1284
  }
1713
- function splitOnPipe(cmd) {
1714
- const segments = [];
1285
+ function tokenize(cmd) {
1286
+ const tokens = [];
1715
1287
  let current = "";
1716
1288
  let inSingle = false;
1717
1289
  let inDouble = false;
1718
- for (let i = 0; i < cmd.length; i++) {
1719
- const ch = cmd[i];
1290
+ for (const ch of cmd) {
1720
1291
  if (ch === "'" && !inDouble) {
1721
1292
  inSingle = !inSingle;
1722
- current += ch;
1723
1293
  } else if (ch === '"' && !inSingle) {
1724
1294
  inDouble = !inDouble;
1725
- current += ch;
1726
- } else if (ch === "|" && !inSingle && !inDouble && cmd[i + 1] !== "|" && (i === 0 || cmd[i - 1] !== "|")) {
1727
- segments.push(current.trim());
1728
- current = "";
1295
+ } else if ((ch === " " || ch === " ") && !inSingle && !inDouble) {
1296
+ if (current) {
1297
+ tokens.push(current);
1298
+ current = "";
1299
+ }
1729
1300
  } else {
1730
1301
  current += ch;
1731
1302
  }
1732
1303
  }
1733
- if (current.trim()) segments.push(current.trim());
1734
- return segments.filter(Boolean);
1735
- }
1736
- function positionalTokens(segment) {
1737
- return segment.split(/\s+/).slice(1).filter((t) => !t.startsWith("-") && !t.startsWith("@") && t.length > 0);
1738
- }
1739
- function analyzePipeChain(command) {
1740
- const segments = splitOnPipe(command);
1741
- if (segments.length < 2) {
1742
- return {
1743
- isPipeline: false,
1744
- hasSensitiveSource: false,
1745
- hasExternalSink: false,
1746
- hasObfuscation: false,
1747
- sourceFiles: [],
1748
- sinkTargets: [],
1749
- risk: "none"
1750
- };
1304
+ if (current) tokens.push(current);
1305
+ return tokens;
1306
+ }
1307
+ function parseHost(raw) {
1308
+ return raw.split("@").pop().split(":")[0];
1309
+ }
1310
+ function extractAllSshHosts(tokens) {
1311
+ const hosts = /* @__PURE__ */ new Set();
1312
+ for (let i = 0; i < tokens.length; i++) {
1313
+ const t = tokens[i];
1314
+ if (t === "-J" && tokens[i + 1]) {
1315
+ for (const hop of tokens[++i].split(",")) {
1316
+ const h = parseHost(hop);
1317
+ if (h) hosts.add(h);
1318
+ }
1319
+ continue;
1320
+ }
1321
+ if (t === "-o" && tokens[i + 1]?.toLowerCase().startsWith("proxyjump=")) {
1322
+ const val = tokens[++i].split("=").slice(1).join("=");
1323
+ for (const hop of val.split(",")) {
1324
+ const h = parseHost(hop);
1325
+ if (h) hosts.add(h);
1326
+ }
1327
+ continue;
1328
+ }
1329
+ if (t === "-o" && tokens[i + 1]?.toLowerCase().startsWith("proxycommand=")) {
1330
+ const raw = tokens[++i].split("=").slice(1).join("=").replace(/^['"]|['"]$/g, "");
1331
+ const subTokens = tokenize(raw);
1332
+ const binary = subTokens[0] ?? "";
1333
+ extractNetworkTargets(subTokens.slice(1), binary).forEach((h) => hosts.add(h));
1334
+ extractAllSshHosts(subTokens.slice(1)).forEach((h) => hosts.add(h));
1335
+ continue;
1336
+ }
1337
+ if (!t.startsWith("-")) {
1338
+ const h = parseHost(t);
1339
+ if (h) hosts.add(h);
1340
+ }
1341
+ }
1342
+ return [...hosts].filter(Boolean);
1343
+ }
1344
+ var MAX_REGEX_LENGTH = 100;
1345
+ var REGEX_CACHE_MAX = 500;
1346
+ var regexCache = /* @__PURE__ */ new Map();
1347
+ function validateRegex(pattern) {
1348
+ if (!pattern) return "Pattern is required";
1349
+ if (pattern.length > MAX_REGEX_LENGTH) return `Pattern exceeds max length of ${MAX_REGEX_LENGTH}`;
1350
+ try {
1351
+ new RegExp(pattern);
1352
+ } catch (e) {
1353
+ return `Invalid regex syntax: ${e.message}`;
1354
+ }
1355
+ if (/\\\d+[*+{]/.test(pattern)) return "Quantified backreferences are forbidden (ReDoS risk)";
1356
+ if (!(0, import_safe_regex22.default)(pattern)) return "Pattern rejected: potential ReDoS vulnerability detected";
1357
+ return null;
1358
+ }
1359
+ function getCompiledRegex(pattern, flags = "") {
1360
+ if (flags && !/^[gimsuy]+$/.test(flags)) return null;
1361
+ const key = `${pattern}\0${flags}`;
1362
+ if (regexCache.has(key)) {
1363
+ const cached = regexCache.get(key);
1364
+ regexCache.delete(key);
1365
+ regexCache.set(key, cached);
1366
+ return cached;
1367
+ }
1368
+ if (validateRegex(pattern) !== null) return null;
1369
+ try {
1370
+ const re = new RegExp(pattern, flags);
1371
+ if (regexCache.size >= REGEX_CACHE_MAX) {
1372
+ const oldest = regexCache.keys().next().value;
1373
+ if (oldest) regexCache.delete(oldest);
1374
+ }
1375
+ regexCache.set(key, re);
1376
+ return re;
1377
+ } catch {
1378
+ return null;
1379
+ }
1380
+ }
1381
+ function matchesPattern(text, patterns) {
1382
+ const p = Array.isArray(patterns) ? patterns : [patterns];
1383
+ if (p.length === 0) return false;
1384
+ const isMatch = (0, import_picomatch.default)(p, { nocase: true, dot: true });
1385
+ const target = text.toLowerCase();
1386
+ const directMatch = isMatch(target);
1387
+ if (directMatch) return true;
1388
+ const withoutDotSlash = text.replace(/^\.\//, "");
1389
+ return isMatch(withoutDotSlash) || isMatch(`./${withoutDotSlash}`);
1390
+ }
1391
+ var FORBIDDEN_PATH_SEGMENTS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
1392
+ function getNestedValue(obj, path13) {
1393
+ if (!obj || typeof obj !== "object") return null;
1394
+ const segments = path13.split(".");
1395
+ for (const seg of segments) {
1396
+ if (FORBIDDEN_PATH_SEGMENTS.has(seg)) return null;
1397
+ }
1398
+ return segments.reduce((prev, curr) => prev?.[curr], obj);
1399
+ }
1400
+ function evaluateSmartConditions(args, rule) {
1401
+ if (!rule.conditions || rule.conditions.length === 0) return true;
1402
+ const mode = rule.conditionMode ?? "all";
1403
+ const results = rule.conditions.map((cond) => {
1404
+ const rawVal = getNestedValue(args, cond.field);
1405
+ const normalized = rawVal !== null && rawVal !== void 0 ? String(rawVal).replace(/\s+/g, " ").trim() : null;
1406
+ const val = cond.field === "command" && normalized !== null ? normalizeCommandForPolicy(normalized) : normalized;
1407
+ switch (cond.op) {
1408
+ case "exists":
1409
+ return val !== null && val !== "";
1410
+ case "notExists":
1411
+ return val === null || val === "";
1412
+ case "contains":
1413
+ return val !== null && cond.value ? val.includes(cond.value) : false;
1414
+ case "notContains":
1415
+ return val !== null && cond.value ? !val.includes(cond.value) : true;
1416
+ case "matches": {
1417
+ if (val === null || !cond.value) return false;
1418
+ const reM = getCompiledRegex(cond.value, cond.flags ?? "");
1419
+ if (!reM) return false;
1420
+ return reM.test(val);
1421
+ }
1422
+ case "notMatches": {
1423
+ if (!cond.value) return false;
1424
+ if (val === null) return true;
1425
+ const reN = getCompiledRegex(cond.value, cond.flags ?? "");
1426
+ if (!reN) return false;
1427
+ return !reN.test(val);
1428
+ }
1429
+ case "matchesGlob":
1430
+ return val !== null && cond.value ? import_picomatch.default.isMatch(val, cond.value) : false;
1431
+ case "notMatchesGlob":
1432
+ return val !== null && cond.value ? !import_picomatch.default.isMatch(val, cond.value) : false;
1433
+ default:
1434
+ return false;
1435
+ }
1436
+ });
1437
+ return mode === "any" ? results.some((r) => r) : results.every((r) => r);
1438
+ }
1439
+ function tokenize2(toolName) {
1440
+ return toolName.toLowerCase().split(/[_.\-\s]+/).filter(Boolean);
1441
+ }
1442
+ function extractShellCommand(toolName, args, toolInspection) {
1443
+ const patterns = Object.keys(toolInspection);
1444
+ const matchingPattern = patterns.find((p) => matchesPattern(toolName, p));
1445
+ if (!matchingPattern) return null;
1446
+ const fieldPath = toolInspection[matchingPattern];
1447
+ const value = getNestedValue(args, fieldPath);
1448
+ return typeof value === "string" ? value : null;
1449
+ }
1450
+ function isSqlTool(toolName, toolInspection) {
1451
+ const patterns = Object.keys(toolInspection);
1452
+ const matchingPattern = patterns.find((p) => matchesPattern(toolName, p));
1453
+ if (!matchingPattern) return false;
1454
+ const fieldName = toolInspection[matchingPattern];
1455
+ return fieldName === "sql" || fieldName === "query";
1456
+ }
1457
+ var SQL_DML_KEYWORDS = /* @__PURE__ */ new Set(["select", "insert", "update", "delete", "merge", "upsert"]);
1458
+ async function evaluatePolicy(config, toolName, args, context = {}, hooks = {}) {
1459
+ const { agent, cwd, activeEnvironment } = context;
1460
+ const { checkProvenance: checkProvenance2, isTrustedHost: isTrustedHost2 } = hooks;
1461
+ const wouldBeIgnored = matchesPattern(toolName, config.policy.ignoredTools);
1462
+ if (config.policy.dlp.enabled && (!wouldBeIgnored || config.policy.dlp.scanIgnoredTools)) {
1463
+ const dlpMatch = args !== void 0 ? scanArgs(args) : null;
1464
+ if (dlpMatch) {
1465
+ return {
1466
+ decision: dlpMatch.severity,
1467
+ blockedByLabel: `DLP: ${dlpMatch.patternName}`,
1468
+ reason: `${dlpMatch.patternName} detected in ${dlpMatch.fieldPath}`
1469
+ };
1470
+ }
1471
+ }
1472
+ if (wouldBeIgnored) return { decision: "allow" };
1473
+ if (config.policy.smartRules.length > 0) {
1474
+ const matchedRule = config.policy.smartRules.find(
1475
+ (rule) => matchesPattern(toolName, rule.tool) && evaluateSmartConditions(args, rule)
1476
+ );
1477
+ if (matchedRule) {
1478
+ if (matchedRule.verdict === "allow")
1479
+ return { decision: "allow", ruleName: matchedRule.name ?? matchedRule.tool };
1480
+ return {
1481
+ decision: matchedRule.verdict,
1482
+ blockedByLabel: `Smart Rule: ${matchedRule.name ?? matchedRule.tool}`,
1483
+ reason: matchedRule.reason,
1484
+ tier: 2,
1485
+ ruleName: matchedRule.name ?? matchedRule.tool,
1486
+ ...(matchedRule.description ?? matchedRule.reason) && {
1487
+ ruleDescription: matchedRule.description ?? matchedRule.reason
1488
+ },
1489
+ ...matchedRule.verdict === "block" && matchedRule.dependsOnState?.length && {
1490
+ dependsOnStatePredicates: matchedRule.dependsOnState
1491
+ },
1492
+ ...matchedRule.verdict === "block" && matchedRule.recoveryCommand && {
1493
+ recoveryCommand: matchedRule.recoveryCommand
1494
+ }
1495
+ };
1496
+ }
1751
1497
  }
1752
- const sourceFiles = [];
1753
- const sinkTargets = [];
1754
- let hasSensitiveSource = false;
1755
- let hasExternalSink = false;
1756
- let hasObfuscation = false;
1757
- for (const segment of segments) {
1758
- const tokens = segment.split(/\s+/).filter(Boolean);
1759
- if (tokens.length === 0) continue;
1760
- const binary = tokens[0].toLowerCase();
1761
- const args = positionalTokens(segment);
1762
- if (SOURCE_COMMANDS.has(binary)) {
1763
- sourceFiles.push(...args);
1764
- if (args.some(isSensitivePath)) hasSensitiveSource = true;
1498
+ let allTokens = [];
1499
+ let pathTokens = [];
1500
+ const shellCommand = extractShellCommand(toolName, args, config.policy.toolInspection);
1501
+ if (shellCommand) {
1502
+ const analyzed = analyzeShellCommand(shellCommand);
1503
+ allTokens = analyzed.allTokens;
1504
+ pathTokens = analyzed.paths;
1505
+ const INLINE_EXEC_PATTERN = /^(python3?|bash|sh|zsh|perl|ruby|node|php|lua)\s+(-c|-e|-eval)\s/i;
1506
+ if (INLINE_EXEC_PATTERN.test(shellCommand.trim())) {
1507
+ return {
1508
+ decision: "review",
1509
+ blockedByLabel: "Node9 Standard (Inline Execution)",
1510
+ ruleDescription: "The AI is running code directly from the command line. Review the full script below before allowing it to execute.",
1511
+ tier: 3
1512
+ };
1765
1513
  }
1766
- if (OBFUSCATORS.has(binary)) hasObfuscation = true;
1767
- if (SINK_COMMANDS.has(binary)) {
1768
- const targets = args.filter(
1769
- (a) => a.includes(".") || a.includes("://") || /^\d+\.\d+/.test(a)
1770
- );
1771
- sinkTargets.push(...targets);
1772
- if (targets.length > 0) hasExternalSink = true;
1514
+ const evalVerdict = detectDangerousShellExec(shellCommand);
1515
+ if (evalVerdict === "block") {
1516
+ return {
1517
+ decision: "block",
1518
+ blockedByLabel: "Node9: Eval Remote Execution",
1519
+ reason: "eval of remote download (curl/wget) is a near-certain supply-chain attack",
1520
+ ruleDescription: "The AI is downloading a script from the internet and running it immediately without inspection. This is a common way malware gets installed.",
1521
+ tier: 3
1522
+ };
1773
1523
  }
1774
- }
1775
- const fullCmd = command.toLowerCase();
1776
- if (!hasSensitiveSource) {
1777
- const redirMatch = fullCmd.match(/<\s*(\S+)/);
1778
- if (redirMatch && isSensitivePath(redirMatch[1])) {
1779
- hasSensitiveSource = true;
1780
- sourceFiles.push(redirMatch[1]);
1524
+ if (evalVerdict === "review") {
1525
+ return {
1526
+ decision: "review",
1527
+ blockedByLabel: "Node9: Eval Dynamic Content",
1528
+ reason: "eval of dynamic content (variable or subshell expansion) requires approval",
1529
+ ruleDescription: "The AI is running a command that includes a variable or subshell expansion. The actual command executed at runtime may differ from what is shown here.",
1530
+ tier: 3
1531
+ };
1781
1532
  }
1782
- }
1783
- const risk = hasSensitiveSource && hasExternalSink && hasObfuscation ? "critical" : hasSensitiveSource && hasExternalSink ? "high" : hasExternalSink ? "medium" : "none";
1784
- return {
1785
- isPipeline: true,
1786
- hasSensitiveSource,
1787
- hasExternalSink,
1788
- hasObfuscation,
1789
- sourceFiles,
1790
- sinkTargets,
1791
- risk
1792
- };
1793
- }
1794
-
1795
- // src/policy/flag-tables.ts
1796
- var import_path6 = __toESM(require("path"));
1797
- var FLAGS_WITH_VALUES = {
1798
- curl: /* @__PURE__ */ new Set([
1799
- "-H",
1800
- "--header",
1801
- "-A",
1802
- "--user-agent",
1803
- "-e",
1804
- "--referer",
1805
- "-x",
1806
- "--proxy",
1807
- "-u",
1808
- "--user",
1809
- "-d",
1810
- "--data",
1811
- "--data-raw",
1812
- "--data-binary",
1813
- "-o",
1814
- "--output",
1815
- "-F",
1816
- "--form",
1817
- "--connect-to",
1818
- "--resolve",
1819
- "--cacert",
1820
- "--cert",
1821
- "--key",
1822
- "-m",
1823
- "--max-time"
1824
- ]),
1825
- wget: /* @__PURE__ */ new Set([
1826
- "-O",
1827
- "--output-document",
1828
- "-P",
1829
- "--directory-prefix",
1830
- "-U",
1831
- "--user-agent",
1832
- "-e",
1833
- "--execute",
1834
- "--proxy",
1835
- "--ca-certificate"
1836
- ]),
1837
- nc: /* @__PURE__ */ new Set(["-x", "-p", "-s", "-w", "-W", "-I", "-O"]),
1838
- ncat: /* @__PURE__ */ new Set(["-x", "-p", "-s", "--proxy", "--proxy-auth", "-w", "--wait"]),
1839
- netcat: /* @__PURE__ */ new Set(["-x", "-p", "-s", "-w"]),
1840
- ssh: /* @__PURE__ */ new Set([
1841
- "-i",
1842
- "-l",
1843
- "-p",
1844
- "-o",
1845
- "-E",
1846
- "-F",
1847
- "-J",
1848
- "-L",
1849
- "-R",
1850
- "-W",
1851
- "-b",
1852
- "-c",
1853
- "-D",
1854
- "-e",
1855
- "-I",
1856
- "-S"
1857
- ]),
1858
- scp: /* @__PURE__ */ new Set(["-i", "-o", "-P", "-S"]),
1859
- rsync: /* @__PURE__ */ new Set(["-e", "--rsh", "--rsync-path", "--password-file", "--log-file"]),
1860
- socat: /* @__PURE__ */ new Set([])
1861
- // socat uses address syntax, not flags — no value-flags
1862
- };
1863
- function extractPositionalArgs(tokens, binary) {
1864
- const binaryName = import_path6.default.basename(binary).replace(/\.exe$/i, "");
1865
- const flagsWithValues = FLAGS_WITH_VALUES[binaryName] ?? /* @__PURE__ */ new Set();
1866
- const positional = [];
1867
- let skipNext = false;
1868
- for (const token of tokens) {
1869
- if (skipNext) {
1870
- skipNext = false;
1871
- continue;
1533
+ const pipeAnalysis = analyzePipeChain(shellCommand);
1534
+ if (pipeAnalysis.isPipeline && (pipeAnalysis.risk === "critical" || pipeAnalysis.risk === "high")) {
1535
+ const sinks = pipeAnalysis.sinkTargets;
1536
+ const allTrusted = sinks.length > 0 && sinks.every((host) => isTrustedHost2 ? isTrustedHost2(host) : false);
1537
+ if (pipeAnalysis.risk === "critical") {
1538
+ if (allTrusted) {
1539
+ return {
1540
+ decision: "review",
1541
+ blockedByLabel: "Node9: Pipe-Chain to Trusted Host (obfuscated)",
1542
+ reason: `Obfuscated pipe to trusted host(s): ${sinks.join(", ")} \u2014 requires approval`,
1543
+ tier: 3
1544
+ };
1545
+ }
1546
+ return {
1547
+ decision: "block",
1548
+ blockedByLabel: "Node9: Pipe-Chain Exfiltration (critical)",
1549
+ reason: `Sensitive file piped through obfuscator to network sink: ${pipeAnalysis.sourceFiles.join(", ")} \u2192 ${sinks.join(", ")}`,
1550
+ tier: 3
1551
+ };
1552
+ }
1553
+ if (allTrusted) {
1554
+ return {
1555
+ decision: "allow",
1556
+ blockedByLabel: "Node9: Pipe-Chain to Trusted Host",
1557
+ reason: `Sensitive file piped to trusted host(s): ${sinks.join(", ")}`,
1558
+ tier: 3
1559
+ };
1560
+ }
1561
+ return {
1562
+ decision: "review",
1563
+ blockedByLabel: "Node9: Pipe-Chain Exfiltration (high)",
1564
+ reason: `Sensitive file piped to network sink: ${pipeAnalysis.sourceFiles.join(", ")} \u2192 ${sinks.join(", ")}`,
1565
+ tier: 3
1566
+ };
1567
+ }
1568
+ const firstToken = analyzed.actions[0] ?? "";
1569
+ if (["ssh", "scp", "rsync"].includes(firstToken)) {
1570
+ const rawTokens = shellCommand.trim().split(/\s+/);
1571
+ const sshHosts = extractAllSshHosts(rawTokens.slice(1));
1572
+ allTokens.push(...sshHosts);
1573
+ }
1574
+ if (firstToken && firstToken.startsWith("/") && checkProvenance2) {
1575
+ const prov = checkProvenance2(firstToken, cwd);
1576
+ if (prov.trustLevel === "suspect") {
1577
+ return {
1578
+ decision: config.settings.mode === "strict" ? "block" : "review",
1579
+ blockedByLabel: "Node9: Suspect Binary",
1580
+ reason: `Binary "${firstToken}" resolved to ${prov.resolvedPath} \u2014 ${prov.reason}`,
1581
+ tier: 3
1582
+ };
1583
+ }
1584
+ if (prov.trustLevel === "unknown" && config.settings.mode === "strict") {
1585
+ return {
1586
+ decision: "review",
1587
+ blockedByLabel: "Node9: Unknown Binary (strict mode)",
1588
+ reason: `Binary "${firstToken}" \u2014 ${prov.reason}`,
1589
+ tier: 3
1590
+ };
1591
+ }
1872
1592
  }
1873
- if (token.startsWith("--") && token.includes("=")) continue;
1874
- if (token.startsWith("-") && token.length === 2 && flagsWithValues.has(token)) {
1875
- skipNext = true;
1876
- continue;
1593
+ if (isSqlTool(toolName, config.policy.toolInspection)) {
1594
+ allTokens = allTokens.filter((t) => !SQL_DML_KEYWORDS.has(t.toLowerCase()));
1877
1595
  }
1878
- if (token.startsWith("--") && flagsWithValues.has(token)) {
1879
- skipNext = true;
1880
- continue;
1596
+ } else {
1597
+ allTokens = tokenize2(toolName);
1598
+ if (args && typeof args === "object") {
1599
+ const flattenedArgs = JSON.stringify(args).toLowerCase();
1600
+ const extraTokens = flattenedArgs.split(/[^a-zA-Z0-9]+/).filter((t) => t.length > 1);
1601
+ allTokens.push(...extraTokens);
1881
1602
  }
1882
- const shortFlag = token.slice(0, 2);
1883
- if (token.startsWith("-") && token.length > 2 && flagsWithValues.has(shortFlag)) continue;
1884
- if (token.startsWith("-")) continue;
1885
- if (token.startsWith("@")) continue;
1886
- positional.push(token);
1887
1603
  }
1888
- return positional;
1889
- }
1890
- function extractNetworkTargets(tokens, binary) {
1891
- return extractPositionalArgs(tokens, binary).map((t) => t.includes("@") ? t.split("@")[1] : t).map((t) => {
1892
- const colonIdx = t.indexOf(":");
1893
- if (colonIdx === -1) return t;
1894
- const afterColon = t.slice(colonIdx + 1);
1895
- if (/^\d+$/.test(afterColon)) return t.slice(0, colonIdx);
1896
- return t;
1897
- }).filter(Boolean);
1898
- }
1899
-
1900
- // src/policy/ssh-parser.ts
1901
- function tokenize(cmd) {
1902
- const tokens = [];
1903
- let current = "";
1904
- let inSingle = false;
1905
- let inDouble = false;
1906
- for (const ch of cmd) {
1907
- if (ch === "'" && !inDouble) {
1908
- inSingle = !inSingle;
1909
- } else if (ch === '"' && !inSingle) {
1910
- inDouble = !inDouble;
1911
- } else if ((ch === " " || ch === " ") && !inSingle && !inDouble) {
1912
- if (current) {
1913
- tokens.push(current);
1914
- current = "";
1604
+ const isManual = agent === "Terminal";
1605
+ if (isManual) {
1606
+ const SYSTEM_DISASTER_COMMANDS = ["mkfs", "shred", "dd", "drop", "truncate", "purge"];
1607
+ const hasSystemDisaster = allTokens.some(
1608
+ (t) => SYSTEM_DISASTER_COMMANDS.includes(t.toLowerCase())
1609
+ );
1610
+ const isRootWipe = allTokens.includes("rm") && (allTokens.includes("/") || allTokens.includes("/*"));
1611
+ if (hasSystemDisaster || isRootWipe) {
1612
+ return { decision: "review", blockedByLabel: "Manual Nuclear Protection", tier: 3 };
1613
+ }
1614
+ return { decision: "allow" };
1615
+ }
1616
+ if (pathTokens.length > 0 && config.policy.sandboxPaths.length > 0) {
1617
+ const allInSandbox = pathTokens.every((p) => matchesPattern(p, config.policy.sandboxPaths));
1618
+ if (allInSandbox) return { decision: "allow" };
1619
+ }
1620
+ let matchedDangerousWord;
1621
+ const isDangerous = allTokens.some(
1622
+ (token) => config.policy.dangerousWords.some((word) => {
1623
+ const w = word.toLowerCase();
1624
+ const hit = token === w || (() => {
1625
+ try {
1626
+ return new RegExp(`\\b${w}\\b`, "i").test(token);
1627
+ } catch {
1628
+ return false;
1629
+ }
1630
+ })();
1631
+ if (hit && !matchedDangerousWord) matchedDangerousWord = word;
1632
+ return hit;
1633
+ })
1634
+ );
1635
+ if (isDangerous) {
1636
+ let matchedField;
1637
+ if (matchedDangerousWord && args && typeof args === "object" && !Array.isArray(args)) {
1638
+ const obj = args;
1639
+ for (const [key, value] of Object.entries(obj)) {
1640
+ if (typeof value === "string") {
1641
+ try {
1642
+ if (new RegExp(
1643
+ `\\b${matchedDangerousWord.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}\\b`,
1644
+ "i"
1645
+ ).test(value)) {
1646
+ matchedField = key;
1647
+ break;
1648
+ }
1649
+ } catch {
1650
+ }
1651
+ }
1915
1652
  }
1916
- } else {
1917
- current += ch;
1918
1653
  }
1654
+ return {
1655
+ decision: "review",
1656
+ blockedByLabel: `Project/Global Config \u2014 dangerous word: "${matchedDangerousWord}"`,
1657
+ matchedWord: matchedDangerousWord,
1658
+ matchedField,
1659
+ ruleDescription: `This command contains a flagged keyword ("${matchedDangerousWord}") from your node9 config. Review it before allowing.`,
1660
+ tier: 6
1661
+ };
1919
1662
  }
1920
- if (current) tokens.push(current);
1921
- return tokens;
1663
+ if (config.settings.mode === "strict") {
1664
+ if (activeEnvironment?.requireApproval === false) return { decision: "allow" };
1665
+ return { decision: "review", blockedByLabel: "Global Config (Strict Mode Active)", tier: 7 };
1666
+ }
1667
+ return { decision: "allow" };
1922
1668
  }
1923
- function parseHost(raw) {
1924
- return raw.split("@").pop().split(":")[0];
1669
+ function isIgnoredTool(toolName, config) {
1670
+ return matchesPattern(toolName, config.policy.ignoredTools);
1925
1671
  }
1926
- function extractAllSshHosts(tokens) {
1927
- const hosts = /* @__PURE__ */ new Set();
1928
- for (let i = 0; i < tokens.length; i++) {
1929
- const t = tokens[i];
1930
- if (t === "-J" && tokens[i + 1]) {
1931
- for (const hop of tokens[++i].split(",")) {
1932
- const h = parseHost(hop);
1933
- if (h) hosts.add(h);
1934
- }
1935
- continue;
1672
+ var aws_default = {
1673
+ name: "aws",
1674
+ description: "Protects AWS infrastructure from destructive AI operations",
1675
+ aliases: ["amazon"],
1676
+ smartRules: [
1677
+ {
1678
+ name: "shield:aws:block-delete-s3-bucket",
1679
+ tool: "*",
1680
+ conditions: [
1681
+ {
1682
+ field: "command",
1683
+ op: "matches",
1684
+ value: "aws\\s+s3.*rb\\s|aws\\s+s3api\\s+delete-bucket",
1685
+ flags: "i"
1686
+ }
1687
+ ],
1688
+ verdict: "block",
1689
+ reason: "S3 bucket deletion is irreversible \u2014 blocked by AWS shield"
1690
+ },
1691
+ {
1692
+ name: "shield:aws:review-iam-changes",
1693
+ tool: "*",
1694
+ conditions: [
1695
+ {
1696
+ field: "command",
1697
+ op: "matches",
1698
+ value: "aws\\s+iam\\s+(create|delete|attach|detach|put|remove)",
1699
+ flags: "i"
1700
+ }
1701
+ ],
1702
+ verdict: "review",
1703
+ reason: "IAM changes require human approval (AWS shield)"
1704
+ },
1705
+ {
1706
+ name: "shield:aws:block-ec2-terminate",
1707
+ tool: "*",
1708
+ conditions: [
1709
+ {
1710
+ field: "command",
1711
+ op: "matches",
1712
+ value: "aws\\s+ec2\\s+terminate-instances",
1713
+ flags: "i"
1714
+ }
1715
+ ],
1716
+ verdict: "block",
1717
+ reason: "EC2 instance termination is irreversible \u2014 blocked by AWS shield"
1718
+ },
1719
+ {
1720
+ name: "shield:aws:review-rds-delete",
1721
+ tool: "*",
1722
+ conditions: [
1723
+ { field: "command", op: "matches", value: "aws\\s+rds\\s+delete-", flags: "i" }
1724
+ ],
1725
+ verdict: "review",
1726
+ reason: "RDS deletion requires human approval (AWS shield)"
1936
1727
  }
1937
- if (t === "-o" && tokens[i + 1]?.toLowerCase().startsWith("proxyjump=")) {
1938
- const val = tokens[++i].split("=").slice(1).join("=");
1939
- for (const hop of val.split(",")) {
1940
- const h = parseHost(hop);
1941
- if (h) hosts.add(h);
1942
- }
1943
- continue;
1728
+ ],
1729
+ dangerousWords: []
1730
+ };
1731
+ var bash_safe_default = {
1732
+ name: "bash-safe",
1733
+ description: "Blocks high-risk bash patterns: pipe-to-shell, rm -rf /, disk overwrites, eval",
1734
+ aliases: ["bash", "shell"],
1735
+ smartRules: [
1736
+ {
1737
+ name: "shield:bash-safe:block-pipe-to-shell",
1738
+ tool: "bash",
1739
+ conditions: [
1740
+ {
1741
+ field: "command",
1742
+ op: "matches",
1743
+ value: "(^|&&|\\|\\||;)\\s*(curl|wget)\\s+[^|]*\\|\\s*(?:(bash|sh|zsh|fish)|(python3?|ruby|perl|node)\\b(?!\\s+-[cem]\\b))",
1744
+ flags: "i"
1745
+ }
1746
+ ],
1747
+ verdict: "block",
1748
+ reason: "Pipe-to-shell is a common supply-chain attack vector \u2014 blocked by bash-safe shield"
1749
+ },
1750
+ {
1751
+ name: "shield:bash-safe:block-obfuscated-exec",
1752
+ tool: "bash",
1753
+ conditions: [
1754
+ {
1755
+ field: "command",
1756
+ op: "matches",
1757
+ value: "\\bbase64\\s+(-d|--decode)[^|;&]*\\|\\s*(bash|sh|zsh)",
1758
+ flags: "i"
1759
+ }
1760
+ ],
1761
+ verdict: "block",
1762
+ reason: "Obfuscated execution via base64 decode \u2014 blocked by bash-safe shield"
1763
+ },
1764
+ {
1765
+ name: "shield:bash-safe:block-rm-root",
1766
+ tool: "bash",
1767
+ conditions: [
1768
+ {
1769
+ field: "command",
1770
+ op: "matches",
1771
+ value: "rm\\s+(-[a-zA-Z]*r[a-zA-Z]*f|-[a-zA-Z]*f[a-zA-Z]*r)[a-zA-Z]*\\s+(\\/|~|\\$HOME|\\$\\{HOME\\})\\s*$",
1772
+ flags: "i"
1773
+ }
1774
+ ],
1775
+ verdict: "block",
1776
+ reason: "rm -rf of root or home directory is catastrophic \u2014 blocked by bash-safe shield"
1777
+ },
1778
+ {
1779
+ name: "shield:bash-safe:block-disk-overwrite",
1780
+ tool: "bash",
1781
+ conditions: [
1782
+ {
1783
+ field: "command",
1784
+ op: "matches",
1785
+ value: "(^|&&|\\|\\||;)\\s*dd\\s+.*of=\\/dev\\/(sd|nvme|hd|vd|xvd)",
1786
+ flags: "i"
1787
+ }
1788
+ ],
1789
+ verdict: "block",
1790
+ reason: "Writing directly to a block device is irreversible \u2014 blocked by bash-safe shield"
1791
+ },
1792
+ {
1793
+ name: "shield:bash-safe:block-eval-remote",
1794
+ tool: "bash",
1795
+ conditions: [
1796
+ {
1797
+ field: "command",
1798
+ op: "matches",
1799
+ value: "(^|&&|\\|\\||;)\\s*eval\\s+.*\\$\\((curl|wget)\\b",
1800
+ flags: "i"
1801
+ }
1802
+ ],
1803
+ verdict: "block",
1804
+ reason: "eval of remote download is a near-certain supply-chain attack \u2014 blocked by bash-safe shield"
1805
+ },
1806
+ {
1807
+ name: "shield:bash-safe:review-eval-dynamic",
1808
+ tool: "bash",
1809
+ conditions: [
1810
+ {
1811
+ field: "command",
1812
+ op: "matches",
1813
+ value: '(^|&&|\\|\\||[;|\\n{(`])\\s*eval\\s+([\\$`(]|"[^"]*\\$)',
1814
+ flags: "i"
1815
+ }
1816
+ ],
1817
+ verdict: "review",
1818
+ reason: "eval of dynamic content \u2014 backup regex rule for scan path (real-time uses AST detection)"
1944
1819
  }
1945
- if (t === "-o" && tokens[i + 1]?.toLowerCase().startsWith("proxycommand=")) {
1946
- const raw = tokens[++i].split("=").slice(1).join("=").replace(/^['"]|['"]$/g, "");
1947
- const subTokens = tokenize(raw);
1948
- const binary = subTokens[0] ?? "";
1949
- extractNetworkTargets(subTokens.slice(1), binary).forEach((h) => hosts.add(h));
1950
- extractAllSshHosts(subTokens.slice(1)).forEach((h) => hosts.add(h));
1951
- continue;
1820
+ ],
1821
+ dangerousWords: []
1822
+ };
1823
+ var docker_default = {
1824
+ name: "docker",
1825
+ description: "Protects Docker environments from destructive AI operations",
1826
+ aliases: [],
1827
+ smartRules: [
1828
+ {
1829
+ name: "shield:docker:block-system-prune",
1830
+ tool: "*",
1831
+ conditions: [
1832
+ {
1833
+ field: "command",
1834
+ op: "matches",
1835
+ value: "docker\\s+system\\s+prune",
1836
+ flags: "i"
1837
+ }
1838
+ ],
1839
+ verdict: "block",
1840
+ reason: "docker system prune removes all unused containers, images, and volumes \u2014 blocked by Docker shield"
1841
+ },
1842
+ {
1843
+ name: "shield:docker:block-volume-prune",
1844
+ tool: "*",
1845
+ conditions: [
1846
+ {
1847
+ field: "command",
1848
+ op: "matches",
1849
+ value: "docker\\s+volume\\s+prune",
1850
+ flags: "i"
1851
+ }
1852
+ ],
1853
+ verdict: "block",
1854
+ reason: "docker volume prune destroys all unused volumes and their data \u2014 blocked by Docker shield"
1855
+ },
1856
+ {
1857
+ name: "shield:docker:block-rm-force",
1858
+ tool: "*",
1859
+ conditionMode: "all",
1860
+ conditions: [
1861
+ {
1862
+ field: "command",
1863
+ op: "matches",
1864
+ value: "docker\\s+rm\\b",
1865
+ flags: "i"
1866
+ },
1867
+ {
1868
+ field: "command",
1869
+ op: "matches",
1870
+ value: "(^|\\s)(-f|--force)(\\s|$)",
1871
+ flags: "i"
1872
+ }
1873
+ ],
1874
+ verdict: "block",
1875
+ reason: "Force-removing running containers is destructive \u2014 blocked by Docker shield"
1876
+ },
1877
+ {
1878
+ name: "shield:docker:review-volume-rm",
1879
+ tool: "*",
1880
+ conditions: [
1881
+ {
1882
+ field: "command",
1883
+ op: "matches",
1884
+ value: "docker\\s+volume\\s+rm\\s+",
1885
+ flags: "i"
1886
+ }
1887
+ ],
1888
+ verdict: "review",
1889
+ reason: "Volume removal deletes persistent data and requires human approval (Docker shield)"
1890
+ },
1891
+ {
1892
+ name: "shield:docker:review-stop-kill",
1893
+ tool: "*",
1894
+ conditions: [
1895
+ {
1896
+ field: "command",
1897
+ op: "matches",
1898
+ value: "docker\\s+(stop|kill)\\s+",
1899
+ flags: "i"
1900
+ }
1901
+ ],
1902
+ verdict: "review",
1903
+ reason: "Stopping or killing containers requires human approval (Docker shield)"
1904
+ },
1905
+ {
1906
+ name: "shield:docker:review-image-rm",
1907
+ tool: "*",
1908
+ conditions: [
1909
+ {
1910
+ field: "command",
1911
+ op: "matches",
1912
+ value: "docker\\s+image\\s+rm\\b",
1913
+ flags: "i"
1914
+ }
1915
+ ],
1916
+ verdict: "review",
1917
+ reason: "Image removal requires human approval (Docker shield)"
1918
+ },
1919
+ {
1920
+ name: "shield:docker:review-rmi-force",
1921
+ tool: "*",
1922
+ conditionMode: "all",
1923
+ conditions: [
1924
+ {
1925
+ field: "command",
1926
+ op: "matches",
1927
+ value: "docker\\s+rmi\\b",
1928
+ flags: "i"
1929
+ },
1930
+ {
1931
+ field: "command",
1932
+ op: "matches",
1933
+ value: "(^|\\s)(-f|--force)(\\s|$)",
1934
+ flags: "i"
1935
+ }
1936
+ ],
1937
+ verdict: "review",
1938
+ reason: "Force image removal requires human approval (Docker shield)"
1939
+ }
1940
+ ],
1941
+ dangerousWords: []
1942
+ };
1943
+ var filesystem_default = {
1944
+ name: "filesystem",
1945
+ description: "Protects the local filesystem from dangerous AI operations",
1946
+ aliases: ["fs"],
1947
+ smartRules: [
1948
+ {
1949
+ name: "shield:filesystem:review-chmod-777",
1950
+ tool: "bash",
1951
+ conditions: [
1952
+ { field: "command", op: "matches", value: "chmod\\s+(777|a\\+rwx)", flags: "i" }
1953
+ ],
1954
+ verdict: "review",
1955
+ reason: "chmod 777 requires human approval (filesystem shield)"
1956
+ },
1957
+ {
1958
+ name: "shield:filesystem:review-write-etc",
1959
+ tool: "bash",
1960
+ conditions: [
1961
+ {
1962
+ field: "command",
1963
+ op: "matches",
1964
+ value: "(tee|\\bcp\\b|\\bmv\\b|install|>+)\\s+.*\\/etc\\/"
1965
+ }
1966
+ ],
1967
+ verdict: "review",
1968
+ reason: "Writing to /etc requires human approval (filesystem shield)"
1969
+ }
1970
+ ],
1971
+ dangerousWords: ["wipefs"]
1972
+ };
1973
+ var github_default = {
1974
+ name: "github",
1975
+ description: "Protects GitHub repositories from destructive AI operations",
1976
+ aliases: ["git"],
1977
+ smartRules: [
1978
+ {
1979
+ name: "shield:github:review-delete-branch-remote",
1980
+ tool: "bash",
1981
+ conditions: [
1982
+ { field: "command", op: "matches", value: "git\\s+push\\s+.*--delete", flags: "i" }
1983
+ ],
1984
+ verdict: "review",
1985
+ reason: "Remote branch deletion requires human approval (GitHub shield)"
1986
+ },
1987
+ {
1988
+ name: "shield:github:block-delete-repo",
1989
+ tool: "*",
1990
+ conditions: [
1991
+ { field: "command", op: "matches", value: "gh\\s+repo\\s+delete", flags: "i" }
1992
+ ],
1993
+ verdict: "block",
1994
+ reason: "Repository deletion is irreversible \u2014 blocked by GitHub shield"
1995
+ }
1996
+ ],
1997
+ dangerousWords: []
1998
+ };
1999
+ var k8s_default = {
2000
+ name: "k8s",
2001
+ description: "Protects Kubernetes clusters from destructive AI operations",
2002
+ aliases: ["kubernetes", "kubectl"],
2003
+ smartRules: [
2004
+ {
2005
+ name: "shield:k8s:block-delete-namespace",
2006
+ tool: "*",
2007
+ conditions: [
2008
+ {
2009
+ field: "command",
2010
+ op: "matches",
2011
+ value: "kubectl\\s+delete\\s+(ns|namespace)\\s+",
2012
+ flags: "i"
2013
+ }
2014
+ ],
2015
+ verdict: "block",
2016
+ reason: "Deleting a namespace destroys all resources inside it \u2014 blocked by k8s shield"
2017
+ },
2018
+ {
2019
+ name: "shield:k8s:block-delete-all",
2020
+ tool: "*",
2021
+ conditions: [
2022
+ {
2023
+ field: "command",
2024
+ op: "matches",
2025
+ value: "kubectl\\s+delete\\s+.*--all\\b",
2026
+ flags: "i"
2027
+ }
2028
+ ],
2029
+ verdict: "block",
2030
+ reason: "kubectl delete --all is irreversible \u2014 blocked by k8s shield"
2031
+ },
2032
+ {
2033
+ name: "shield:k8s:block-helm-uninstall",
2034
+ tool: "*",
2035
+ conditions: [
2036
+ {
2037
+ field: "command",
2038
+ op: "matches",
2039
+ value: "helm\\s+(uninstall|delete|del)\\s+",
2040
+ flags: "i"
2041
+ }
2042
+ ],
2043
+ verdict: "block",
2044
+ reason: "helm uninstall removes a release and its resources \u2014 blocked by k8s shield"
2045
+ },
2046
+ {
2047
+ name: "shield:k8s:review-scale-zero",
2048
+ tool: "*",
2049
+ conditions: [
2050
+ {
2051
+ field: "command",
2052
+ op: "matches",
2053
+ value: "kubectl\\s+scale\\s+.*--replicas=0",
2054
+ flags: "i"
2055
+ }
2056
+ ],
2057
+ verdict: "review",
2058
+ reason: "Scaling to zero takes down a workload and requires human approval (k8s shield)"
2059
+ },
2060
+ {
2061
+ name: "shield:k8s:review-delete-deployment",
2062
+ tool: "*",
2063
+ conditions: [
2064
+ {
2065
+ field: "command",
2066
+ op: "matches",
2067
+ value: "kubectl\\s+delete\\s+(deployment|deploy|statefulset|sts|daemonset|ds)\\s+",
2068
+ flags: "i"
2069
+ }
2070
+ ],
2071
+ verdict: "review",
2072
+ reason: "Deleting a workload requires human approval (k8s shield)"
2073
+ },
2074
+ {
2075
+ name: "shield:k8s:review-apply-force",
2076
+ tool: "*",
2077
+ conditions: [
2078
+ {
2079
+ field: "command",
2080
+ op: "matches",
2081
+ value: "kubectl\\s+(apply|replace)\\s+.*--force",
2082
+ flags: "i"
2083
+ }
2084
+ ],
2085
+ verdict: "review",
2086
+ reason: "Force-apply overwrites live resources and requires human approval (k8s shield)"
2087
+ }
2088
+ ],
2089
+ dangerousWords: []
2090
+ };
2091
+ var mcp_tool_gating_default = {
2092
+ name: "mcp-tool-gating",
2093
+ description: "Intercept MCP tool lists and require user approval before the agent can use any tools from a new server",
2094
+ aliases: ["mcp-gating", "mcp-tools"],
2095
+ smartRules: [],
2096
+ dangerousWords: []
2097
+ };
2098
+ var mongodb_default = {
2099
+ name: "mongodb",
2100
+ description: "Protects MongoDB databases from destructive AI operations",
2101
+ aliases: ["mongo"],
2102
+ smartRules: [
2103
+ {
2104
+ name: "shield:mongodb:block-drop-database",
2105
+ tool: "*",
2106
+ conditions: [
2107
+ {
2108
+ field: "command",
2109
+ op: "matches",
2110
+ value: "\\.dropDatabase\\s*\\(",
2111
+ flags: "i"
2112
+ }
2113
+ ],
2114
+ verdict: "block",
2115
+ reason: "dropDatabase is irreversible \u2014 blocked by MongoDB shield"
2116
+ },
2117
+ {
2118
+ name: "shield:mongodb:block-drop-collection",
2119
+ tool: "*",
2120
+ conditions: [
2121
+ {
2122
+ field: "command",
2123
+ op: "matches",
2124
+ value: "\\.drop\\s*\\(|db\\.getCollection\\([^)]+\\)\\.drop\\s*\\(",
2125
+ flags: "i"
2126
+ }
2127
+ ],
2128
+ verdict: "block",
2129
+ reason: "Collection drop is irreversible \u2014 blocked by MongoDB shield"
2130
+ },
2131
+ {
2132
+ name: "shield:mongodb:block-delete-many-empty-filter",
2133
+ tool: "*",
2134
+ conditions: [
2135
+ {
2136
+ field: "command",
2137
+ op: "matches",
2138
+ value: "\\.deleteMany\\s*\\(\\s*\\{\\s*\\}\\s*\\)",
2139
+ flags: "i"
2140
+ }
2141
+ ],
2142
+ verdict: "block",
2143
+ reason: "deleteMany({}) with empty filter wipes the entire collection \u2014 blocked by MongoDB shield"
2144
+ },
2145
+ {
2146
+ name: "shield:mongodb:review-delete-many",
2147
+ tool: "*",
2148
+ conditions: [
2149
+ {
2150
+ field: "command",
2151
+ op: "matches",
2152
+ value: "\\.deleteMany\\s*\\(",
2153
+ flags: "i"
2154
+ }
2155
+ ],
2156
+ verdict: "review",
2157
+ reason: "deleteMany requires human approval (MongoDB shield)"
2158
+ },
2159
+ {
2160
+ name: "shield:mongodb:review-drop-index",
2161
+ tool: "*",
2162
+ conditions: [
2163
+ {
2164
+ field: "command",
2165
+ op: "matches",
2166
+ value: "\\.dropIndex\\s*\\(|\\.dropIndexes\\s*\\(",
2167
+ flags: "i"
2168
+ }
2169
+ ],
2170
+ verdict: "review",
2171
+ reason: "Index drops affect query performance and require human approval (MongoDB shield)"
1952
2172
  }
1953
- if (!t.startsWith("-")) {
1954
- const h = parseHost(t);
1955
- if (h) hosts.add(h);
2173
+ ],
2174
+ dangerousWords: ["dropDatabase", "dropCollection", "mongodrop"]
2175
+ };
2176
+ var postgres_default = {
2177
+ name: "postgres",
2178
+ description: "Protects PostgreSQL databases from destructive AI operations",
2179
+ aliases: ["pg", "postgresql"],
2180
+ smartRules: [
2181
+ {
2182
+ name: "shield:postgres:block-drop-table",
2183
+ tool: "*",
2184
+ conditions: [{ field: "sql", op: "matches", value: "DROP\\s+TABLE", flags: "i" }],
2185
+ verdict: "block",
2186
+ reason: "DROP TABLE is irreversible \u2014 blocked by Postgres shield"
2187
+ },
2188
+ {
2189
+ name: "shield:postgres:block-truncate",
2190
+ tool: "*",
2191
+ conditions: [
2192
+ { field: "sql", op: "matches", value: "TRUNCATE\\s+TABLE", flags: "i" }
2193
+ ],
2194
+ verdict: "block",
2195
+ reason: "TRUNCATE is irreversible \u2014 blocked by Postgres shield"
2196
+ },
2197
+ {
2198
+ name: "shield:postgres:block-drop-column",
2199
+ tool: "*",
2200
+ conditions: [
2201
+ { field: "sql", op: "matches", value: "ALTER\\s+TABLE.*DROP\\s+COLUMN", flags: "i" }
2202
+ ],
2203
+ verdict: "block",
2204
+ reason: "DROP COLUMN is irreversible \u2014 blocked by Postgres shield"
2205
+ },
2206
+ {
2207
+ name: "shield:postgres:review-grant-revoke",
2208
+ tool: "*",
2209
+ conditions: [
2210
+ { field: "sql", op: "matches", value: "\\b(GRANT|REVOKE)\\b", flags: "i" }
2211
+ ],
2212
+ verdict: "review",
2213
+ reason: "Permission changes require human approval (Postgres shield)"
1956
2214
  }
1957
- }
1958
- return [...hosts].filter(Boolean);
1959
- }
1960
-
1961
- // src/auth/trusted-hosts.ts
1962
- var import_fs6 = __toESM(require("fs"));
1963
- var import_path7 = __toESM(require("path"));
1964
- var import_os5 = __toESM(require("os"));
1965
- function getTrustedHostsPath() {
1966
- return import_path7.default.join(import_os5.default.homedir(), ".node9", "trusted-hosts.json");
1967
- }
1968
- function readTrustedHosts() {
1969
- try {
1970
- const raw = import_fs6.default.readFileSync(getTrustedHostsPath(), "utf8");
1971
- const parsed = JSON.parse(raw);
1972
- return Array.isArray(parsed.hosts) ? parsed.hosts : [];
1973
- } catch {
1974
- return [];
1975
- }
1976
- }
1977
- var _cache = null;
1978
- var CACHE_TTL_MS = 5e3;
1979
- function getFileMtime() {
1980
- try {
1981
- return import_fs6.default.statSync(getTrustedHostsPath()).mtimeMs;
1982
- } catch {
1983
- return 0;
1984
- }
1985
- }
1986
- function getCachedHosts() {
1987
- const now = Date.now();
1988
- if (_cache && now < _cache.expiry) {
1989
- const mtime = getFileMtime();
1990
- if (mtime === _cache.mtime) return _cache.hosts;
1991
- }
1992
- const hosts = readTrustedHosts();
1993
- _cache = { hosts, expiry: now + CACHE_TTL_MS, mtime: getFileMtime() };
1994
- return hosts;
1995
- }
1996
- function normalizeHost(raw) {
1997
- return raw.toLowerCase().replace(/^https?:\/\//, "").replace(/\/.*$/, "").replace(/^[^@]+@/, "").replace(/:\d+$/, "");
1998
- }
1999
- function isTrustedHost(host) {
2000
- const normalized = normalizeHost(host);
2001
- return getCachedHosts().some((entry) => {
2002
- const entryHost = entry.host.toLowerCase();
2003
- if (entryHost.startsWith("*.")) {
2004
- const domain = entryHost.slice(2);
2005
- return normalized.endsWith("." + domain);
2215
+ ],
2216
+ dangerousWords: ["dropdb", "pg_dropcluster"]
2217
+ };
2218
+ var project_jail_default = {
2219
+ name: "project-jail",
2220
+ description: "Restricts AI agents from reading sensitive credential files outside the current project",
2221
+ aliases: ["jail"],
2222
+ smartRules: [
2223
+ {
2224
+ name: "shield:project-jail:block-read-ssh",
2225
+ tool: "bash",
2226
+ conditions: [
2227
+ {
2228
+ field: "command",
2229
+ op: "matches",
2230
+ value: "(cat|less|head|tail|bat|more|open|print|nano|vim|vi|emacs|code|type)\\s+.*[\\/\\\\]\\.ssh[\\/\\\\]",
2231
+ flags: "i"
2232
+ }
2233
+ ],
2234
+ verdict: "block",
2235
+ reason: "Reading SSH private keys is blocked by project-jail shield"
2236
+ },
2237
+ {
2238
+ name: "shield:project-jail:block-read-aws",
2239
+ tool: "bash",
2240
+ conditions: [
2241
+ {
2242
+ field: "command",
2243
+ op: "matches",
2244
+ value: "(cat|less|head|tail|bat|more|open|print|nano|vim|vi|emacs|code|type)\\s+.*[\\/\\\\]\\.aws[\\/\\\\]",
2245
+ flags: "i"
2246
+ }
2247
+ ],
2248
+ verdict: "block",
2249
+ reason: "Reading AWS credentials is blocked by project-jail shield"
2250
+ },
2251
+ {
2252
+ name: "shield:project-jail:block-read-env",
2253
+ tool: "bash",
2254
+ conditions: [
2255
+ {
2256
+ field: "command",
2257
+ op: "matches",
2258
+ value: "(cat|less|head|tail|bat|more|open|print|nano|vim|vi|emacs|code|type)\\s+.*\\.env(\\.local|\\.production|\\.staging)?\\b",
2259
+ flags: "i"
2260
+ }
2261
+ ],
2262
+ verdict: "block",
2263
+ reason: "Reading .env files is blocked by project-jail shield"
2264
+ },
2265
+ {
2266
+ name: "shield:project-jail:block-read-credentials",
2267
+ tool: "bash",
2268
+ conditions: [
2269
+ {
2270
+ field: "command",
2271
+ op: "matches",
2272
+ value: "(cat|less|head|tail|bat|more|open|print|nano|vim|vi|emacs|code|type)\\s+.*(credentials\\.json|\\.netrc|\\.npmrc|\\.docker[\\/\\\\]config\\.json|gcloud[\\/\\\\]credentials)",
2273
+ flags: "i"
2274
+ }
2275
+ ],
2276
+ verdict: "block",
2277
+ reason: "Reading credential files is blocked by project-jail shield"
2006
2278
  }
2007
- return normalized === entryHost;
2008
- });
2009
- }
2010
-
2011
- // src/policy/index.ts
2012
- function tokenize2(toolName) {
2013
- return toolName.toLowerCase().split(/[_.\-\s]+/).filter(Boolean);
2014
- }
2015
- function matchesPattern(text, patterns) {
2016
- const p = Array.isArray(patterns) ? patterns : [patterns];
2017
- if (p.length === 0) return false;
2018
- const isMatch = (0, import_picomatch.default)(p, { nocase: true, dot: true });
2019
- const target = text.toLowerCase();
2020
- const directMatch = isMatch(target);
2021
- if (directMatch) return true;
2022
- const withoutDotSlash = text.replace(/^\.\//, "");
2023
- return isMatch(withoutDotSlash) || isMatch(`./${withoutDotSlash}`);
2024
- }
2025
- function getNestedValue(obj, path15) {
2026
- if (!obj || typeof obj !== "object") return null;
2027
- return path15.split(".").reduce((prev, curr) => prev?.[curr], obj);
2028
- }
2029
- var { syntax } = import_mvdan_sh.default;
2030
- var sharedParser = syntax.NewParser();
2031
- var MESSAGE_FLAGS = /* @__PURE__ */ new Set([
2032
- "-m",
2033
- "--message",
2034
- "--body",
2035
- "--title",
2036
- "--description",
2037
- "--comment",
2038
- "--subject",
2039
- "--summary"
2040
- ]);
2041
- function normalizeCommandForPolicy(command) {
2042
- try {
2043
- const f = sharedParser.Parse(command, "cmd");
2044
- const strips = [];
2045
- syntax.Walk(f, (node) => {
2046
- if (!node) return false;
2047
- const n = node;
2048
- if (syntax.NodeType(n) !== "CallExpr") return true;
2049
- const args = n.Args || [];
2050
- for (let i = 0; i < args.length - 1; i++) {
2051
- const argParts = args[i].Parts || [];
2052
- if (argParts.length !== 1 || syntax.NodeType(argParts[0]) !== "Lit") continue;
2053
- const flagVal = argParts[0].Value || "";
2054
- if (!MESSAGE_FLAGS.has(flagVal.toLowerCase())) continue;
2055
- const next = args[i + 1];
2056
- const nextParts = next.Parts || [];
2057
- if (nextParts.length !== 1) continue;
2058
- const quotedNode = nextParts[0];
2059
- const nt = syntax.NodeType(quotedNode);
2060
- if (nt === "SglQuoted") {
2061
- strips.push([next.Pos().Offset(), next.End().Offset()]);
2062
- } else if (nt === "DblQuoted") {
2063
- const innerParts = quotedNode.Parts || [];
2064
- const allLit = innerParts.length === 0 || innerParts.every((p) => syntax.NodeType(p) === "Lit");
2065
- if (allLit) strips.push([next.Pos().Offset(), next.End().Offset()]);
2279
+ ],
2280
+ dangerousWords: []
2281
+ };
2282
+ var redis_default = {
2283
+ name: "redis",
2284
+ description: "Protects Redis instances from destructive AI operations",
2285
+ aliases: [],
2286
+ smartRules: [
2287
+ {
2288
+ name: "shield:redis:block-flushall",
2289
+ tool: "*",
2290
+ conditions: [
2291
+ {
2292
+ field: "command",
2293
+ op: "matches",
2294
+ value: "\\bFLUSHALL\\b",
2295
+ flags: "i"
2066
2296
  }
2067
- }
2068
- return true;
2069
- });
2070
- if (strips.length === 0) return command;
2071
- strips.sort((a, b) => b[0] - a[0]);
2072
- let result = command;
2073
- for (const [start, end] of strips) {
2074
- result = result.slice(0, start) + '""' + result.slice(end);
2297
+ ],
2298
+ verdict: "block",
2299
+ reason: "FLUSHALL deletes every key in every database \u2014 blocked by Redis shield"
2300
+ },
2301
+ {
2302
+ name: "shield:redis:block-flushdb",
2303
+ tool: "*",
2304
+ conditions: [
2305
+ {
2306
+ field: "command",
2307
+ op: "matches",
2308
+ value: "\\bFLUSHDB\\b",
2309
+ flags: "i"
2310
+ }
2311
+ ],
2312
+ verdict: "block",
2313
+ reason: "FLUSHDB deletes all keys in the current database \u2014 blocked by Redis shield"
2314
+ },
2315
+ {
2316
+ name: "shield:redis:block-config-resetstat",
2317
+ tool: "*",
2318
+ conditions: [
2319
+ {
2320
+ field: "command",
2321
+ op: "matches",
2322
+ value: "\\bCONFIG\\s+RESETSTAT\\b",
2323
+ flags: "i"
2324
+ }
2325
+ ],
2326
+ verdict: "block",
2327
+ reason: "CONFIG RESETSTAT resets server statistics irreversibly \u2014 blocked by Redis shield"
2328
+ },
2329
+ {
2330
+ name: "shield:redis:review-config-set",
2331
+ tool: "*",
2332
+ conditions: [
2333
+ {
2334
+ field: "command",
2335
+ op: "matches",
2336
+ value: "\\bCONFIG\\s+SET\\b",
2337
+ flags: "i"
2338
+ }
2339
+ ],
2340
+ verdict: "review",
2341
+ reason: "CONFIG SET changes live server configuration and requires human approval (Redis shield)"
2342
+ },
2343
+ {
2344
+ name: "shield:redis:review-del-wildcard",
2345
+ tool: "*",
2346
+ conditions: [
2347
+ {
2348
+ field: "command",
2349
+ op: "matches",
2350
+ value: "\\bDEL\\b.*[*?\\[]|redis-cli.*--scan.*\\|.*xargs.*del",
2351
+ flags: "i"
2352
+ }
2353
+ ],
2354
+ verdict: "review",
2355
+ reason: "Wildcard key deletion requires human approval (Redis shield)"
2075
2356
  }
2076
- return result;
2077
- } catch {
2078
- return command;
2079
- }
2080
- }
2081
- var SHELL_INTERPRETERS = /* @__PURE__ */ new Set(["bash", "sh", "zsh", "fish", "dash", "ksh"]);
2082
- var DOWNLOAD_CMDS = /* @__PURE__ */ new Set(["curl", "wget"]);
2083
- function scanArgsForDynamicExec(args, startIdx) {
2084
- let hasCmdSubst = false;
2085
- let hasParamExp = false;
2086
- let hasCurl = false;
2087
- for (let i = startIdx; i < args.length; i++) {
2088
- syntax.Walk(args[i], (inner) => {
2089
- if (!inner) return false;
2090
- const inn = inner;
2091
- const it = syntax.NodeType(inn);
2092
- if (it === "CmdSubst") hasCmdSubst = true;
2093
- if (it === "ParamExp") hasParamExp = true;
2094
- if (it === "Lit" && DOWNLOAD_CMDS.has(inn.Value?.toLowerCase())) hasCurl = true;
2095
- return true;
2096
- });
2097
- }
2098
- if (hasCmdSubst && hasCurl) return "block";
2099
- if (hasCmdSubst || hasParamExp) return "review";
2100
- return null;
2357
+ ],
2358
+ dangerousWords: ["FLUSHALL", "FLUSHDB"]
2359
+ };
2360
+ function isShieldVerdict(v) {
2361
+ return v === "allow" || v === "review" || v === "block";
2101
2362
  }
2102
- function detectDangerousShellExec(command) {
2103
- try {
2104
- const f = sharedParser.Parse(command, "cmd");
2105
- let result = null;
2106
- syntax.Walk(f, (node) => {
2107
- if (!node || result === "block") return false;
2108
- const n = node;
2109
- if (syntax.NodeType(n) !== "CallExpr") return true;
2110
- const args = n.Args || [];
2111
- if (args.length === 0) return true;
2112
- const firstParts = args[0].Parts || [];
2113
- if (firstParts.length !== 1 || syntax.NodeType(firstParts[0]) !== "Lit") return true;
2114
- const cmdName = firstParts[0].Value?.toLowerCase() ?? "";
2115
- if (cmdName === "eval") {
2116
- const v = scanArgsForDynamicExec(args, 1);
2117
- if (v === "block" || v === "review" && result === null) result = v;
2118
- } else if (SHELL_INTERPRETERS.has(cmdName)) {
2119
- for (let i = 1; i < args.length - 1; i++) {
2120
- const flagParts = args[i].Parts || [];
2121
- if (flagParts.length !== 1 || syntax.NodeType(flagParts[0]) !== "Lit" || flagParts[0].Value !== "-c")
2122
- continue;
2123
- const v = scanArgsForDynamicExec(args, i + 1);
2124
- if (v === "block" || v === "review" && result === null) result = v;
2125
- break;
2126
- }
2127
- }
2128
- return true;
2129
- });
2130
- return result;
2131
- } catch {
2132
- return null;
2363
+ function validateShieldDefinition(raw) {
2364
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
2365
+ return { error: "Shield file is not an object" };
2133
2366
  }
2367
+ const r = raw;
2368
+ if (typeof r.name !== "string" || !r.name) return { error: "Shield file missing 'name'" };
2369
+ if (typeof r.description !== "string") return { error: "Shield file missing 'description'" };
2370
+ if (!Array.isArray(r.aliases)) return { error: "Shield file missing 'aliases' array" };
2371
+ if (!Array.isArray(r.smartRules)) return { error: "Shield file missing 'smartRules' array" };
2372
+ if (!Array.isArray(r.dangerousWords))
2373
+ return { error: "Shield file missing 'dangerousWords' array" };
2374
+ return { ok: r };
2134
2375
  }
2135
- function evaluateSmartConditions(args, rule) {
2136
- if (!rule.conditions || rule.conditions.length === 0) return true;
2137
- const mode = rule.conditionMode ?? "all";
2138
- const results = rule.conditions.map((cond) => {
2139
- const rawVal = getNestedValue(args, cond.field);
2140
- const normalized = rawVal !== null && rawVal !== void 0 ? String(rawVal).replace(/\s+/g, " ").trim() : null;
2141
- const val = cond.field === "command" && normalized !== null ? normalizeCommandForPolicy(normalized) : normalized;
2142
- switch (cond.op) {
2143
- case "exists":
2144
- return val !== null && val !== "";
2145
- case "notExists":
2146
- return val === null || val === "";
2147
- case "contains":
2148
- return val !== null && cond.value ? val.includes(cond.value) : false;
2149
- case "notContains":
2150
- return val !== null && cond.value ? !val.includes(cond.value) : true;
2151
- case "matches": {
2152
- if (val === null || !cond.value) return false;
2153
- const reM = getCompiledRegex(cond.value, cond.flags ?? "");
2154
- if (!reM) return false;
2155
- return reM.test(val);
2376
+ function validateOverrides(raw) {
2377
+ const warnings = [];
2378
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) return { overrides: {}, warnings };
2379
+ const result = {};
2380
+ for (const [shieldName, rules] of Object.entries(raw)) {
2381
+ if (!rules || typeof rules !== "object" || Array.isArray(rules)) continue;
2382
+ const validRules = {};
2383
+ for (const [ruleName, verdict] of Object.entries(rules)) {
2384
+ if (isShieldVerdict(verdict)) {
2385
+ validRules[ruleName] = verdict;
2386
+ } else {
2387
+ warnings.push(
2388
+ `shields.json contains invalid verdict "${String(verdict)}" for ${shieldName}/${ruleName} \u2014 entry ignored. File may be corrupted or tampered with.`
2389
+ );
2156
2390
  }
2157
- case "notMatches": {
2158
- if (!cond.value) return false;
2159
- if (val === null) return true;
2160
- const reN = getCompiledRegex(cond.value, cond.flags ?? "");
2161
- if (!reN) return false;
2162
- return !reN.test(val);
2391
+ }
2392
+ if (Object.keys(validRules).length > 0) result[shieldName] = validRules;
2393
+ }
2394
+ return { overrides: result, warnings };
2395
+ }
2396
+ var BUILTIN_SHIELDS = {
2397
+ [aws_default.name]: aws_default,
2398
+ [bash_safe_default.name]: bash_safe_default,
2399
+ [docker_default.name]: docker_default,
2400
+ [filesystem_default.name]: filesystem_default,
2401
+ [github_default.name]: github_default,
2402
+ [k8s_default.name]: k8s_default,
2403
+ [mcp_tool_gating_default.name]: mcp_tool_gating_default,
2404
+ [mongodb_default.name]: mongodb_default,
2405
+ [postgres_default.name]: postgres_default,
2406
+ [project_jail_default.name]: project_jail_default,
2407
+ [redis_default.name]: redis_default
2408
+ };
2409
+ function assertBuiltinShieldRegexesAreSafe() {
2410
+ for (const shield of Object.values(BUILTIN_SHIELDS)) {
2411
+ for (const rule of shield.smartRules) {
2412
+ const conditions = rule.conditions ?? [];
2413
+ for (const cond of conditions) {
2414
+ if (cond.op !== "matches" && cond.op !== "notMatches") continue;
2415
+ const pattern = cond.value;
2416
+ if (!pattern) continue;
2417
+ if (!(0, import_safe_regex23.default)(pattern)) {
2418
+ throw new Error(
2419
+ `[node9 engine] Shield '${shield.name}' rule '${rule.name ?? rule.tool}' has unsafe regex: ${pattern}`
2420
+ );
2421
+ }
2163
2422
  }
2164
- case "matchesGlob":
2165
- return val !== null && cond.value ? import_picomatch.default.isMatch(val, cond.value) : false;
2166
- case "notMatchesGlob":
2167
- return val !== null && cond.value ? !import_picomatch.default.isMatch(val, cond.value) : false;
2168
- default:
2169
- return false;
2170
2423
  }
2171
- });
2172
- return mode === "any" ? results.some((r) => r) : results.every((r) => r);
2424
+ }
2173
2425
  }
2174
- function extractShellCommand(toolName, args, toolInspection) {
2175
- const patterns = Object.keys(toolInspection);
2176
- const matchingPattern = patterns.find((p) => matchesPattern(toolName, p));
2177
- if (!matchingPattern) return null;
2178
- const fieldPath = toolInspection[matchingPattern];
2179
- const value = getNestedValue(args, fieldPath);
2180
- return typeof value === "string" ? value : null;
2426
+ assertBuiltinShieldRegexesAreSafe();
2427
+ var LOOP_MAX_RECORDS = 500;
2428
+ function computeArgsHash(args) {
2429
+ const str = JSON.stringify(args ?? "");
2430
+ return import_crypto2.default.createHash("sha256").update(str).digest("hex").slice(0, 16);
2181
2431
  }
2182
- function isSqlTool(toolName, toolInspection) {
2183
- const patterns = Object.keys(toolInspection);
2184
- const matchingPattern = patterns.find((p) => matchesPattern(toolName, p));
2185
- if (!matchingPattern) return false;
2186
- const fieldName = toolInspection[matchingPattern];
2187
- return fieldName === "sql" || fieldName === "query";
2432
+ function evaluateLoopWindow(records, tool, args, threshold, windowMs, now) {
2433
+ const hash = computeArgsHash(args);
2434
+ const cutoff = now - windowMs;
2435
+ const fresh = records.filter((r) => r.ts >= cutoff);
2436
+ fresh.push({ t: tool, h: hash, ts: now });
2437
+ const count = fresh.filter((r) => r.t === tool && r.h === hash).length;
2438
+ const nextRecords = fresh.slice(-LOOP_MAX_RECORDS);
2439
+ return { nextRecords, count, looping: count >= threshold };
2188
2440
  }
2189
- var SQL_DML_KEYWORDS = /* @__PURE__ */ new Set(["select", "insert", "update", "delete", "merge", "upsert"]);
2190
- function analyzeShellCommand(command) {
2191
- const actions = [];
2192
- const paths = [];
2193
- const allTokens = [];
2194
- const addToken = (token) => {
2195
- const lower = token.toLowerCase();
2196
- allTokens.push(lower);
2197
- if (lower.includes("/")) allTokens.push(...lower.split("/").filter(Boolean));
2198
- if (lower.startsWith("-")) allTokens.push(lower.replace(/^-+/, ""));
2199
- };
2441
+
2442
+ // src/shields.ts
2443
+ var USER_SHIELDS_DIR = import_path2.default.join(import_os2.default.homedir(), ".node9", "shields");
2444
+ function loadUserShields() {
2445
+ const result = {};
2446
+ let entries;
2200
2447
  try {
2201
- const f = sharedParser.Parse(command, "cmd");
2202
- syntax.Walk(f, (node) => {
2203
- if (!node) return false;
2204
- const n = node;
2205
- if (syntax.NodeType(n) !== "CallExpr") return true;
2206
- const wordValues = (n.Args || []).map((arg) => {
2207
- return (arg.Parts || []).map((p) => (p.Value ?? "").replace(/\\(.)/g, "$1")).join("");
2208
- }).filter((s) => s.length > 0);
2209
- if (wordValues.length > 0) {
2210
- const cmd = wordValues[0].toLowerCase();
2211
- if (!actions.includes(cmd)) actions.push(cmd);
2212
- wordValues.forEach((w) => addToken(w));
2213
- wordValues.slice(1).forEach((w) => {
2214
- if (!w.startsWith("-")) paths.push(w);
2215
- });
2448
+ entries = import_fs2.default.readdirSync(USER_SHIELDS_DIR).filter((f) => f.endsWith(".json"));
2449
+ } catch (err) {
2450
+ if (err.code !== "ENOENT") {
2451
+ process.stderr.write(
2452
+ `[node9] Could not read user shields dir ${USER_SHIELDS_DIR}: ${String(err)}
2453
+ `
2454
+ );
2455
+ }
2456
+ return result;
2457
+ }
2458
+ for (const file of entries) {
2459
+ const filePath = import_path2.default.join(USER_SHIELDS_DIR, file);
2460
+ try {
2461
+ const raw = JSON.parse(import_fs2.default.readFileSync(filePath, "utf-8"));
2462
+ const v = validateShieldDefinition(raw);
2463
+ if ("error" in v) {
2464
+ process.stderr.write(`[node9] ${v.error}: ${filePath}
2465
+ `);
2466
+ continue;
2216
2467
  }
2217
- return true;
2218
- });
2219
- } catch {
2468
+ result[v.ok.name] = v.ok;
2469
+ } catch (err) {
2470
+ process.stderr.write(`[node9] Failed to load user shield ${file}: ${String(err)}
2471
+ `);
2472
+ }
2220
2473
  }
2221
- if (allTokens.length === 0) {
2222
- const normalized = command.replace(/\\(.)/g, "$1");
2223
- const sanitized = normalized.replace(/["'<>]/g, " ");
2224
- const segments = sanitized.split(/[|;&]|\$\(|\)|`/);
2225
- segments.forEach((segment) => {
2226
- const tokens = segment.trim().split(/\s+/).filter(Boolean);
2227
- if (tokens.length > 0) {
2228
- const action = tokens[0].toLowerCase();
2229
- if (!actions.includes(action)) actions.push(action);
2230
- tokens.forEach((t) => {
2231
- addToken(t);
2232
- if (t !== tokens[0] && !t.startsWith("-")) {
2233
- if (!paths.includes(t)) paths.push(t);
2474
+ return result;
2475
+ }
2476
+ function buildSHIELDS() {
2477
+ return { ...BUILTIN_SHIELDS, ...loadUserShields() };
2478
+ }
2479
+ var SHIELDS = buildSHIELDS();
2480
+ function resolveShieldName(input) {
2481
+ const lower = input.toLowerCase();
2482
+ if (SHIELDS[lower]) return lower;
2483
+ for (const [name, def] of Object.entries(SHIELDS)) {
2484
+ if (def.aliases.includes(lower)) return name;
2485
+ }
2486
+ return null;
2487
+ }
2488
+ function getShield(name) {
2489
+ const resolved = resolveShieldName(name);
2490
+ return resolved ? SHIELDS[resolved] : null;
2491
+ }
2492
+ var SHIELDS_STATE_FILE = import_path2.default.join(import_os2.default.homedir(), ".node9", "shields.json");
2493
+ function readShieldsFile() {
2494
+ try {
2495
+ const raw = import_fs2.default.readFileSync(SHIELDS_STATE_FILE, "utf-8");
2496
+ if (!raw.trim()) return { active: [] };
2497
+ const parsed = JSON.parse(raw);
2498
+ const active = Array.isArray(parsed.active) ? parsed.active.filter(
2499
+ (e) => typeof e === "string" && e.length > 0 && e in SHIELDS
2500
+ ) : [];
2501
+ const { overrides, warnings } = validateOverrides(parsed.overrides);
2502
+ for (const w of warnings) {
2503
+ process.stderr.write(`[node9] Warning: ${w}
2504
+ `);
2505
+ }
2506
+ return { active, overrides };
2507
+ } catch (err) {
2508
+ if (err.code !== "ENOENT") {
2509
+ process.stderr.write(`[node9] Warning: could not read shields state: ${String(err)}
2510
+ `);
2511
+ }
2512
+ return { active: [] };
2513
+ }
2514
+ }
2515
+ function readActiveShields() {
2516
+ return readShieldsFile().active;
2517
+ }
2518
+ function readShieldOverrides() {
2519
+ return readShieldsFile().overrides ?? {};
2520
+ }
2521
+
2522
+ // src/config/index.ts
2523
+ var DANGEROUS_WORDS = [
2524
+ "mkfs",
2525
+ // formats/wipes a filesystem partition
2526
+ "shred"
2527
+ // permanently overwrites file contents (unrecoverable)
2528
+ ];
2529
+ var DEFAULT_CONFIG = {
2530
+ version: "1.0",
2531
+ settings: {
2532
+ mode: "standard",
2533
+ autoStartDaemon: true,
2534
+ enableUndo: true,
2535
+ // 🔥 ALWAYS TRUE BY DEFAULT for the safety net
2536
+ enableHookLogDebug: true,
2537
+ approvalTimeoutMs: 12e4,
2538
+ // 120-second auto-deny timeout
2539
+ flightRecorder: true,
2540
+ auditHashArgs: true,
2541
+ approvers: { native: true, browser: false, cloud: false, terminal: true },
2542
+ cloudSyncIntervalHours: 5
2543
+ },
2544
+ policy: {
2545
+ sandboxPaths: ["/tmp/**", "**/sandbox/**", "**/test-results/**"],
2546
+ dangerousWords: DANGEROUS_WORDS,
2547
+ ignoredTools: [
2548
+ "list_*",
2549
+ "get_*",
2550
+ "read_*",
2551
+ "describe_*",
2552
+ "read",
2553
+ "glob",
2554
+ "grep",
2555
+ "ls",
2556
+ "notebookread",
2557
+ "notebookedit",
2558
+ "webfetch",
2559
+ "websearch",
2560
+ "exitplanmode",
2561
+ "askuserquestion",
2562
+ "agent",
2563
+ "task*",
2564
+ "toolsearch",
2565
+ "mcp__ide__*",
2566
+ "getDiagnostics"
2567
+ ],
2568
+ toolInspection: {
2569
+ bash: "command",
2570
+ shell: "command",
2571
+ run_shell_command: "command",
2572
+ "terminal.execute": "command",
2573
+ "postgres:query": "sql"
2574
+ },
2575
+ snapshot: {
2576
+ tools: [
2577
+ "str_replace_based_edit_tool",
2578
+ "write_file",
2579
+ "edit_file",
2580
+ "create_file",
2581
+ "edit",
2582
+ "replace"
2583
+ ],
2584
+ onlyPaths: [],
2585
+ ignorePaths: ["**/node_modules/**", "dist/**", "build/**", ".next/**", "**/*.log"]
2586
+ },
2587
+ smartRules: [
2588
+ // ── rm safety (critical — always evaluated first) ──────────────────────
2589
+ {
2590
+ name: "block-rm-rf-home",
2591
+ tool: "bash",
2592
+ conditionMode: "all",
2593
+ conditions: [
2594
+ {
2595
+ field: "command",
2596
+ op: "matches",
2597
+ // Anchor rm as a shell command (not inside a string arg like a git commit message).
2598
+ value: "(^|&&|\\|\\||;)\\s*rm\\b[^;&|]*\\s(-[rRfF]*[rR][rRfF]*|--recursive)(\\s|$)"
2599
+ },
2600
+ {
2601
+ field: "command",
2602
+ op: "matches",
2603
+ value: "(~|\\/root(\\/|$)|\\$HOME|\\/home\\/)"
2604
+ }
2605
+ ],
2606
+ verdict: "block",
2607
+ reason: "Recursive delete of home directory is irreversible",
2608
+ description: "The AI wants to recursively delete your home directory. This will permanently destroy all your personal files and cannot be undone."
2609
+ },
2610
+ // ── SQL safety ────────────────────────────────────────────────────────
2611
+ {
2612
+ name: "no-delete-without-where",
2613
+ tool: "*",
2614
+ conditions: [
2615
+ { field: "sql", op: "matches", value: "^(DELETE|UPDATE)\\s", flags: "i" },
2616
+ { field: "sql", op: "notMatches", value: "\\bWHERE\\b", flags: "i" }
2617
+ ],
2618
+ conditionMode: "all",
2619
+ verdict: "review",
2620
+ reason: "DELETE/UPDATE without WHERE clause \u2014 would affect every row in the table",
2621
+ description: "The AI is running a SQL statement that will modify every row in the table \u2014 no WHERE filter was found. This could wipe or corrupt all your data."
2622
+ },
2623
+ {
2624
+ name: "review-drop-truncate-shell",
2625
+ tool: "bash",
2626
+ conditions: [
2627
+ {
2628
+ field: "command",
2629
+ op: "matches",
2630
+ // Require a DB CLI in the command so grep/cat/echo of SQL strings don't trigger.
2631
+ value: "(^|&&|\\|\\||;|\\|)\\s*(psql|mysql|sqlite3|sqlplus|cockroach|clickhouse-client|mongo)\\b",
2632
+ flags: "i"
2633
+ },
2634
+ {
2635
+ field: "command",
2636
+ op: "matches",
2637
+ value: "\\b(DROP|TRUNCATE)\\s+(TABLE|DATABASE|SCHEMA|INDEX)",
2638
+ flags: "i"
2234
2639
  }
2235
- });
2640
+ ],
2641
+ conditionMode: "all",
2642
+ verdict: "review",
2643
+ reason: "SQL DDL destructive statement inside a shell command",
2644
+ description: "The AI wants to drop or truncate a database table via the shell. This permanently deletes the table structure or all its data."
2645
+ },
2646
+ // ── Git safety ────────────────────────────────────────────────────────
2647
+ {
2648
+ name: "review-force-push",
2649
+ tool: "bash",
2650
+ conditions: [
2651
+ {
2652
+ field: "command",
2653
+ op: "matches",
2654
+ // Anchor git as a shell command so node -e / python -c scripts containing
2655
+ // "git push --force" as a string don't false-positive.
2656
+ value: "(^|&&|\\|\\||;)\\s*git\\s+push[^;&|]*(--force|--force-with-lease|-f\\b)",
2657
+ flags: "i"
2658
+ }
2659
+ ],
2660
+ conditionMode: "all",
2661
+ verdict: "review",
2662
+ reason: "Force push rewrites remote history \u2014 confirm this is intentional",
2663
+ description: "The AI wants to force push to a remote git branch. This rewrites shared history and can permanently destroy commits that teammates have already pulled."
2664
+ },
2665
+ {
2666
+ name: "review-git-destructive",
2667
+ tool: "bash",
2668
+ conditions: [
2669
+ {
2670
+ field: "command",
2671
+ op: "matches",
2672
+ // Anchor git as a shell command so node -e / python -c scripts containing
2673
+ // "git reset --hard" as a string don't false-positive.
2674
+ value: "(^|&&|\\|\\||;)\\s*git\\s+(reset\\s+--hard|clean\\s+-[fdxX]|rebase\\b|tag\\s+-d|branch\\s+-[dD])",
2675
+ flags: "i"
2676
+ },
2677
+ {
2678
+ field: "command",
2679
+ op: "notMatches",
2680
+ // Exclude recovery ops and routine branch-surgery (--onto) — these are not destructive.
2681
+ value: "\\bgit\\s+rebase\\s+--(abort|continue|skip|onto)\\b",
2682
+ flags: "i"
2683
+ }
2684
+ ],
2685
+ conditionMode: "all",
2686
+ verdict: "review",
2687
+ reason: "Destructive git operation \u2014 discards history or working-tree changes",
2688
+ description: "The AI wants to run a destructive git operation (reset, rebase, clean, or branch delete) that can permanently discard commits or uncommitted work."
2689
+ },
2690
+ // ── Shell safety ──────────────────────────────────────────────────────
2691
+ {
2692
+ name: "review-sudo",
2693
+ tool: "bash",
2694
+ conditions: [{ field: "command", op: "matches", value: "\\bsudo\\s", flags: "i" }],
2695
+ conditionMode: "all",
2696
+ verdict: "review",
2697
+ reason: "Command requires elevated privileges",
2698
+ description: "The AI wants to run a command as root (sudo). Commands with root access can modify system files, install software, or change security settings."
2699
+ },
2700
+ {
2701
+ name: "review-curl-pipe-shell",
2702
+ tool: "bash",
2703
+ conditions: [
2704
+ {
2705
+ field: "command",
2706
+ op: "matches",
2707
+ // Anchor curl/wget as a shell command so node -e scripts testing this
2708
+ // regex pattern don't self-match as a false positive.
2709
+ value: "(^|&&|\\|\\||;)\\s*(curl|wget)[^|]*\\|\\s*(ba|z|da|fi|c|k)?sh",
2710
+ flags: "i"
2711
+ }
2712
+ ],
2713
+ conditionMode: "all",
2714
+ verdict: "block",
2715
+ reason: "Piping remote script into a shell is a supply-chain attack vector",
2716
+ description: "The AI wants to download a script from the internet and run it immediately, without you seeing what it contains. This is one of the most common ways malware gets installed."
2236
2717
  }
2237
- });
2238
- }
2239
- return { actions, paths, allTokens };
2240
- }
2241
- async function evaluatePolicy(toolName, args, agent, cwd) {
2242
- const config = getConfig();
2243
- const wouldBeIgnored = matchesPattern(toolName, config.policy.ignoredTools);
2244
- if (config.policy.dlp.enabled && (!wouldBeIgnored || config.policy.dlp.scanIgnoredTools)) {
2245
- const dlpMatch = args !== void 0 ? scanArgs(args) : null;
2246
- if (dlpMatch) {
2247
- return {
2248
- decision: dlpMatch.severity,
2249
- blockedByLabel: `DLP: ${dlpMatch.patternName}`,
2250
- reason: `${dlpMatch.patternName} detected in ${dlpMatch.fieldPath}`
2251
- };
2252
- }
2718
+ ],
2719
+ dlp: { enabled: true, scanIgnoredTools: true },
2720
+ loopDetection: { enabled: true, threshold: 5, windowSeconds: 120 },
2721
+ skillPinning: { enabled: false, mode: "warn", roots: [] }
2722
+ },
2723
+ environments: {}
2724
+ };
2725
+ var ADVISORY_SMART_RULES = [
2726
+ // ── rm safety ─────────────────────────────────────────────────────────────
2727
+ // tool: '*' so they cover bash, shell, run_shell_command, and Gemini's Shell.
2728
+ // Pattern '(^|&&|\|\||;)\s*rm\b' matches rm as a shell command (including in
2729
+ // chained commands like 'cat foo && rm bar') but avoids false-positives on 'docker rm'.
2730
+ {
2731
+ name: "allow-rm-safe-paths",
2732
+ tool: "*",
2733
+ conditionMode: "all",
2734
+ conditions: [
2735
+ { field: "command", op: "matches", value: "(^|&&|\\|\\||;)\\s*rm\\b" },
2736
+ {
2737
+ field: "command",
2738
+ op: "matches",
2739
+ // Matches known-safe build artifact paths in the command.
2740
+ value: "(node_modules|\\bdist\\b|\\.next|\\bcoverage\\b|\\.cache|\\btmp\\b|\\btemp\\b|\\.DS_Store)(\\/|\\s|$)"
2741
+ }
2742
+ ],
2743
+ verdict: "allow",
2744
+ reason: "Deleting a known-safe build artifact path"
2745
+ },
2746
+ {
2747
+ name: "review-rm",
2748
+ tool: "*",
2749
+ conditions: [{ field: "command", op: "matches", value: "(^|&&|\\|\\||;)\\s*rm\\b" }],
2750
+ verdict: "review",
2751
+ reason: "rm can permanently delete files \u2014 confirm the target path",
2752
+ description: "The AI wants to delete files. Unlike moving to trash, rm is permanent \u2014 the files cannot be recovered without a backup."
2753
+ },
2754
+ // ── SQL safety (Safe by Default) ──────────────────────────────────────────
2755
+ // These rules fire when an AI calls a database tool directly (e.g. MCP postgres,
2756
+ // mcp__postgres__query) with a destructive SQL statement in the 'sql' field.
2757
+ // The postgres shield upgrades these from 'review' → 'block' for stricter teams;
2758
+ // without a shield, users still get a human-approval gate on every destructive op.
2759
+ {
2760
+ name: "review-drop-table-sql",
2761
+ tool: "*",
2762
+ conditions: [{ field: "sql", op: "matches", value: "DROP\\s+TABLE", flags: "i" }],
2763
+ verdict: "review",
2764
+ reason: "DROP TABLE is irreversible \u2014 enable the postgres shield to block instead",
2765
+ description: "The AI wants to drop a database table. This permanently deletes the table and all its data \u2014 there is no undo."
2766
+ },
2767
+ {
2768
+ name: "review-truncate-sql",
2769
+ tool: "*",
2770
+ conditions: [{ field: "sql", op: "matches", value: "TRUNCATE\\s+TABLE", flags: "i" }],
2771
+ verdict: "review",
2772
+ reason: "TRUNCATE removes all rows \u2014 enable the postgres shield to block instead",
2773
+ description: "The AI wants to truncate a database table, which instantly deletes every row. The table structure remains but all data is gone."
2774
+ },
2775
+ {
2776
+ name: "review-drop-column-sql",
2777
+ tool: "*",
2778
+ conditions: [
2779
+ { field: "sql", op: "matches", value: "ALTER\\s+TABLE.*DROP\\s+COLUMN", flags: "i" }
2780
+ ],
2781
+ verdict: "review",
2782
+ reason: "DROP COLUMN is irreversible \u2014 enable the postgres shield to block instead",
2783
+ description: "The AI wants to drop a column from a database table. This permanently removes the column and all its data from every row."
2253
2784
  }
2254
- if (wouldBeIgnored) return { decision: "allow" };
2255
- if (config.policy.smartRules.length > 0) {
2256
- const matchedRule = config.policy.smartRules.find(
2257
- (rule) => matchesPattern(toolName, rule.tool) && evaluateSmartConditions(args, rule)
2258
- );
2259
- if (matchedRule) {
2260
- if (matchedRule.verdict === "allow")
2261
- return { decision: "allow", ruleName: matchedRule.name ?? matchedRule.tool };
2262
- return {
2263
- decision: matchedRule.verdict,
2264
- blockedByLabel: `Smart Rule: ${matchedRule.name ?? matchedRule.tool}`,
2265
- reason: matchedRule.reason,
2266
- tier: 2,
2267
- ruleName: matchedRule.name ?? matchedRule.tool,
2268
- ...(matchedRule.description ?? matchedRule.reason) && {
2269
- ruleDescription: matchedRule.description ?? matchedRule.reason
2270
- },
2271
- ...matchedRule.verdict === "block" && matchedRule.dependsOnState?.length && {
2272
- dependsOnStatePredicates: matchedRule.dependsOnState
2273
- },
2274
- ...matchedRule.verdict === "block" && matchedRule.recoveryCommand && {
2275
- recoveryCommand: matchedRule.recoveryCommand
2276
- }
2277
- };
2278
- }
2785
+ ];
2786
+ var cachedConfig = null;
2787
+ function getCredentials() {
2788
+ const DEFAULT_API_URL = "https://api.node9.ai/api/v1/intercept";
2789
+ if (process.env.NODE9_API_KEY) {
2790
+ return {
2791
+ apiKey: process.env.NODE9_API_KEY,
2792
+ apiUrl: process.env.NODE9_API_URL || DEFAULT_API_URL
2793
+ };
2279
2794
  }
2280
- let allTokens = [];
2281
- let pathTokens = [];
2282
- const shellCommand = extractShellCommand(toolName, args, config.policy.toolInspection);
2283
- if (shellCommand) {
2284
- const analyzed = analyzeShellCommand(shellCommand);
2285
- allTokens = analyzed.allTokens;
2286
- pathTokens = analyzed.paths;
2287
- const INLINE_EXEC_PATTERN = /^(python3?|bash|sh|zsh|perl|ruby|node|php|lua)\s+(-c|-e|-eval)\s/i;
2288
- if (INLINE_EXEC_PATTERN.test(shellCommand.trim())) {
2289
- return {
2290
- decision: "review",
2291
- blockedByLabel: "Node9 Standard (Inline Execution)",
2292
- ruleDescription: "The AI is running code directly from the command line. Review the full script below before allowing it to execute.",
2293
- tier: 3
2294
- };
2295
- }
2296
- const evalVerdict = detectDangerousShellExec(shellCommand);
2297
- if (evalVerdict === "block") {
2298
- return {
2299
- decision: "block",
2300
- blockedByLabel: "Node9: Eval Remote Execution",
2301
- reason: "eval of remote download (curl/wget) is a near-certain supply-chain attack",
2302
- ruleDescription: "The AI is downloading a script from the internet and running it immediately without inspection. This is a common way malware gets installed.",
2303
- tier: 3
2304
- };
2305
- }
2306
- if (evalVerdict === "review") {
2307
- return {
2308
- decision: "review",
2309
- blockedByLabel: "Node9: Eval Dynamic Content",
2310
- reason: "eval of dynamic content (variable or subshell expansion) requires approval",
2311
- ruleDescription: "The AI is running a command that includes a variable or subshell expansion. The actual command executed at runtime may differ from what is shown here.",
2312
- tier: 3
2313
- };
2314
- }
2315
- const pipeAnalysis = analyzePipeChain(shellCommand);
2316
- if (pipeAnalysis.isPipeline && (pipeAnalysis.risk === "critical" || pipeAnalysis.risk === "high")) {
2317
- const sinks = pipeAnalysis.sinkTargets;
2318
- const allTrusted = sinks.length > 0 && sinks.every(isTrustedHost);
2319
- if (pipeAnalysis.risk === "critical") {
2320
- if (allTrusted) {
2321
- return {
2322
- decision: "review",
2323
- blockedByLabel: "Node9: Pipe-Chain to Trusted Host (obfuscated)",
2324
- reason: `Obfuscated pipe to trusted host(s): ${sinks.join(", ")} \u2014 requires approval`,
2325
- tier: 3
2326
- };
2327
- }
2795
+ try {
2796
+ const credPath = import_path3.default.join(import_os3.default.homedir(), ".node9", "credentials.json");
2797
+ if (import_fs3.default.existsSync(credPath)) {
2798
+ const creds = JSON.parse(import_fs3.default.readFileSync(credPath, "utf-8"));
2799
+ const profileName = process.env.NODE9_PROFILE || "default";
2800
+ const profile = creds[profileName];
2801
+ if (profile?.apiKey) {
2328
2802
  return {
2329
- decision: "block",
2330
- blockedByLabel: "Node9: Pipe-Chain Exfiltration (critical)",
2331
- reason: `Sensitive file piped through obfuscator to network sink: ${pipeAnalysis.sourceFiles.join(", ")} \u2192 ${sinks.join(", ")}`,
2332
- tier: 3
2803
+ apiKey: profile.apiKey,
2804
+ apiUrl: profile.apiUrl || DEFAULT_API_URL
2333
2805
  };
2334
2806
  }
2335
- if (allTrusted) {
2807
+ if (creds.apiKey) {
2336
2808
  return {
2337
- decision: "allow",
2338
- blockedByLabel: "Node9: Pipe-Chain to Trusted Host",
2339
- reason: `Sensitive file piped to trusted host(s): ${sinks.join(", ")}`,
2340
- tier: 3
2809
+ apiKey: creds.apiKey,
2810
+ apiUrl: creds.apiUrl || DEFAULT_API_URL
2341
2811
  };
2342
2812
  }
2343
- return {
2344
- decision: "review",
2345
- blockedByLabel: "Node9: Pipe-Chain Exfiltration (high)",
2346
- reason: `Sensitive file piped to network sink: ${pipeAnalysis.sourceFiles.join(", ")} \u2192 ${sinks.join(", ")}`,
2347
- tier: 3
2348
- };
2349
2813
  }
2350
- const firstToken = analyzed.actions[0] ?? "";
2351
- if (["ssh", "scp", "rsync"].includes(firstToken)) {
2352
- const rawTokens = shellCommand.trim().split(/\s+/);
2353
- const sshHosts = extractAllSshHosts(rawTokens.slice(1));
2354
- allTokens.push(...sshHosts);
2814
+ } catch {
2815
+ }
2816
+ return null;
2817
+ }
2818
+ function getActiveEnvironment(config) {
2819
+ const env = config.settings.environment || process.env.NODE_ENV || "development";
2820
+ return config.environments[env] ?? null;
2821
+ }
2822
+ function getConfig(cwd) {
2823
+ if (!cwd && cachedConfig) return cachedConfig;
2824
+ const globalPath = import_path3.default.join(import_os3.default.homedir(), ".node9", "config.json");
2825
+ const projectPath = import_path3.default.join(cwd ?? process.cwd(), "node9.config.json");
2826
+ const globalConfig = tryLoadConfig(globalPath);
2827
+ const projectConfig = tryLoadConfig(projectPath);
2828
+ const mergedSettings = {
2829
+ ...DEFAULT_CONFIG.settings,
2830
+ approvers: { ...DEFAULT_CONFIG.settings.approvers }
2831
+ };
2832
+ const mergedPolicy = {
2833
+ sandboxPaths: [...DEFAULT_CONFIG.policy.sandboxPaths],
2834
+ dangerousWords: [...DEFAULT_CONFIG.policy.dangerousWords],
2835
+ ignoredTools: [...DEFAULT_CONFIG.policy.ignoredTools],
2836
+ toolInspection: { ...DEFAULT_CONFIG.policy.toolInspection },
2837
+ smartRules: [...DEFAULT_CONFIG.policy.smartRules],
2838
+ snapshot: {
2839
+ tools: [...DEFAULT_CONFIG.policy.snapshot.tools],
2840
+ onlyPaths: [...DEFAULT_CONFIG.policy.snapshot.onlyPaths],
2841
+ ignorePaths: [...DEFAULT_CONFIG.policy.snapshot.ignorePaths]
2842
+ },
2843
+ dlp: { ...DEFAULT_CONFIG.policy.dlp },
2844
+ loopDetection: { ...DEFAULT_CONFIG.policy.loopDetection },
2845
+ skillPinning: {
2846
+ ...DEFAULT_CONFIG.policy.skillPinning,
2847
+ roots: [...DEFAULT_CONFIG.policy.skillPinning.roots]
2355
2848
  }
2356
- if (firstToken && import_path8.default.posix.isAbsolute(firstToken)) {
2357
- const prov = checkProvenance(firstToken, cwd);
2358
- if (prov.trustLevel === "suspect") {
2359
- return {
2360
- decision: config.settings.mode === "strict" ? "block" : "review",
2361
- blockedByLabel: "Node9: Suspect Binary",
2362
- reason: `Binary "${firstToken}" resolved to ${prov.resolvedPath} \u2014 ${prov.reason}`,
2363
- tier: 3
2364
- };
2849
+ };
2850
+ const mergedEnvironments = { ...DEFAULT_CONFIG.environments };
2851
+ const applyLayer = (source) => {
2852
+ if (!source) return;
2853
+ const s = source.settings || {};
2854
+ const p = source.policy || {};
2855
+ if (s.mode !== void 0) mergedSettings.mode = s.mode;
2856
+ if (s.autoStartDaemon !== void 0) mergedSettings.autoStartDaemon = s.autoStartDaemon;
2857
+ if (s.enableUndo !== void 0) mergedSettings.enableUndo = s.enableUndo;
2858
+ if (s.enableHookLogDebug !== void 0)
2859
+ mergedSettings.enableHookLogDebug = s.enableHookLogDebug;
2860
+ if (s.approvers) mergedSettings.approvers = { ...mergedSettings.approvers, ...s.approvers };
2861
+ if (s.approvalTimeoutMs !== void 0) mergedSettings.approvalTimeoutMs = s.approvalTimeoutMs;
2862
+ if (s.approvalTimeoutSeconds !== void 0 && s.approvalTimeoutMs === void 0)
2863
+ mergedSettings.approvalTimeoutMs = s.approvalTimeoutSeconds * 1e3;
2864
+ if (s.environment !== void 0) mergedSettings.environment = s.environment;
2865
+ if (s.cloudSyncIntervalHours !== void 0)
2866
+ mergedSettings.cloudSyncIntervalHours = s.cloudSyncIntervalHours;
2867
+ if (s.hud !== void 0) mergedSettings.hud = { ...mergedSettings.hud, ...s.hud };
2868
+ if (p.sandboxPaths) mergedPolicy.sandboxPaths.push(...p.sandboxPaths);
2869
+ if (p.ignoredTools) mergedPolicy.ignoredTools.push(...p.ignoredTools);
2870
+ if (p.dangerousWords) mergedPolicy.dangerousWords = [...p.dangerousWords];
2871
+ if (p.toolInspection)
2872
+ mergedPolicy.toolInspection = { ...mergedPolicy.toolInspection, ...p.toolInspection };
2873
+ if (p.smartRules) {
2874
+ const defaultBlocks = mergedPolicy.smartRules.filter((r) => r.verdict === "block");
2875
+ const defaultNonBlocks = mergedPolicy.smartRules.filter((r) => r.verdict !== "block");
2876
+ const userRuleNames = new Set(p.smartRules.filter((r) => r.name).map((r) => r.name));
2877
+ const filteredBlocks = defaultBlocks.filter((r) => !r.name || !userRuleNames.has(r.name));
2878
+ const filteredNonBlocks = defaultNonBlocks.filter(
2879
+ (r) => !r.name || !userRuleNames.has(r.name)
2880
+ );
2881
+ mergedPolicy.smartRules = [...filteredBlocks, ...p.smartRules, ...filteredNonBlocks];
2882
+ }
2883
+ if (p.snapshot) {
2884
+ const s2 = p.snapshot;
2885
+ if (s2.tools) mergedPolicy.snapshot.tools.push(...s2.tools);
2886
+ if (s2.onlyPaths) mergedPolicy.snapshot.onlyPaths.push(...s2.onlyPaths);
2887
+ if (s2.ignorePaths) mergedPolicy.snapshot.ignorePaths.push(...s2.ignorePaths);
2888
+ }
2889
+ if (p.dlp) {
2890
+ const d = p.dlp;
2891
+ if (d.enabled !== void 0) mergedPolicy.dlp.enabled = d.enabled;
2892
+ if (d.scanIgnoredTools !== void 0) mergedPolicy.dlp.scanIgnoredTools = d.scanIgnoredTools;
2893
+ }
2894
+ if (p.loopDetection) {
2895
+ const ld = p.loopDetection;
2896
+ if (ld.enabled !== void 0) mergedPolicy.loopDetection.enabled = ld.enabled;
2897
+ if (ld.threshold !== void 0) mergedPolicy.loopDetection.threshold = ld.threshold;
2898
+ if (ld.windowSeconds !== void 0)
2899
+ mergedPolicy.loopDetection.windowSeconds = ld.windowSeconds;
2900
+ }
2901
+ if (p.skillPinning && typeof p.skillPinning === "object") {
2902
+ const sp = p.skillPinning;
2903
+ if (sp.enabled !== void 0) mergedPolicy.skillPinning.enabled = sp.enabled;
2904
+ if (sp.mode !== void 0) mergedPolicy.skillPinning.mode = sp.mode;
2905
+ if (Array.isArray(sp.roots)) {
2906
+ for (const r of sp.roots) {
2907
+ if (typeof r === "string" && r.length > 0) mergedPolicy.skillPinning.roots.push(r);
2908
+ }
2365
2909
  }
2366
- if (prov.trustLevel === "unknown" && config.settings.mode === "strict") {
2367
- return {
2368
- decision: "review",
2369
- blockedByLabel: "Node9: Unknown Binary (strict mode)",
2370
- reason: `Binary "${firstToken}" \u2014 ${prov.reason}`,
2371
- tier: 3
2910
+ }
2911
+ const envs = source.environments || {};
2912
+ for (const [envName, envConfig] of Object.entries(envs)) {
2913
+ if (envConfig && typeof envConfig === "object") {
2914
+ const ec = envConfig;
2915
+ mergedEnvironments[envName] = {
2916
+ ...mergedEnvironments[envName],
2917
+ // Validate field types before merging — do not blindly spread user input
2918
+ ...typeof ec.requireApproval === "boolean" ? { requireApproval: ec.requireApproval } : {}
2372
2919
  };
2373
2920
  }
2374
2921
  }
2375
- if (isSqlTool(toolName, config.policy.toolInspection)) {
2376
- allTokens = allTokens.filter((t) => !SQL_DML_KEYWORDS.has(t.toLowerCase()));
2922
+ };
2923
+ applyLayer(globalConfig);
2924
+ applyLayer(projectConfig);
2925
+ {
2926
+ const cacheFile = import_path3.default.join(import_os3.default.homedir(), ".node9", "rules-cache.json");
2927
+ try {
2928
+ const raw = JSON.parse(import_fs3.default.readFileSync(cacheFile, "utf-8"));
2929
+ if (Array.isArray(raw.rules) && raw.rules.length > 0) {
2930
+ applyLayer({ policy: { smartRules: raw.rules } });
2931
+ }
2932
+ } catch {
2933
+ }
2934
+ }
2935
+ const shieldOverrides = readShieldOverrides();
2936
+ for (const shieldName of readActiveShields()) {
2937
+ const shield = getShield(shieldName);
2938
+ if (!shield) continue;
2939
+ const existingRuleNames = new Set(mergedPolicy.smartRules.map((r) => r.name));
2940
+ const ruleOverrides = shieldOverrides[shieldName] ?? {};
2941
+ for (const rule of shield.smartRules) {
2942
+ if (!existingRuleNames.has(rule.name)) {
2943
+ const overrideVerdict = rule.name ? ruleOverrides[rule.name] : void 0;
2944
+ mergedPolicy.smartRules.push(
2945
+ overrideVerdict !== void 0 ? { ...rule, verdict: overrideVerdict } : rule
2946
+ );
2947
+ }
2948
+ }
2949
+ const existingWords = new Set(mergedPolicy.dangerousWords);
2950
+ for (const word of shield.dangerousWords) {
2951
+ if (!existingWords.has(word)) mergedPolicy.dangerousWords.push(word);
2952
+ }
2953
+ }
2954
+ const existingAdvisoryNames = new Set(mergedPolicy.smartRules.map((r) => r.name));
2955
+ for (const rule of ADVISORY_SMART_RULES) {
2956
+ if (!existingAdvisoryNames.has(rule.name)) mergedPolicy.smartRules.push(rule);
2957
+ }
2958
+ if (process.env.NODE9_MODE) mergedSettings.mode = process.env.NODE9_MODE;
2959
+ mergedPolicy.sandboxPaths = [...new Set(mergedPolicy.sandboxPaths)];
2960
+ mergedPolicy.dangerousWords = [...new Set(mergedPolicy.dangerousWords)];
2961
+ mergedPolicy.ignoredTools = [...new Set(mergedPolicy.ignoredTools)];
2962
+ mergedPolicy.skillPinning.roots = [...new Set(mergedPolicy.skillPinning.roots)];
2963
+ mergedPolicy.snapshot.tools = [...new Set(mergedPolicy.snapshot.tools)];
2964
+ mergedPolicy.snapshot.onlyPaths = [...new Set(mergedPolicy.snapshot.onlyPaths)];
2965
+ mergedPolicy.snapshot.ignorePaths = [...new Set(mergedPolicy.snapshot.ignorePaths)];
2966
+ const result = {
2967
+ settings: mergedSettings,
2968
+ policy: mergedPolicy,
2969
+ environments: mergedEnvironments
2970
+ };
2971
+ if (!cwd) cachedConfig = result;
2972
+ return result;
2973
+ }
2974
+ function tryLoadConfig(filePath) {
2975
+ if (!import_fs3.default.existsSync(filePath)) return null;
2976
+ let raw;
2977
+ try {
2978
+ raw = JSON.parse(import_fs3.default.readFileSync(filePath, "utf-8"));
2979
+ } catch (err) {
2980
+ const msg = err instanceof Error ? err.message : String(err);
2981
+ process.stderr.write(
2982
+ `
2983
+ \u26A0\uFE0F Node9: Failed to parse ${filePath}
2984
+ ${msg}
2985
+ \u2192 Using default config
2986
+
2987
+ `
2988
+ );
2989
+ return null;
2990
+ }
2991
+ const SUPPORTED_VERSION = "1.0";
2992
+ const SUPPORTED_MAJOR = SUPPORTED_VERSION.split(".")[0];
2993
+ const fileVersion = raw?.version;
2994
+ if (fileVersion !== void 0) {
2995
+ const vStr = String(fileVersion);
2996
+ const fileMajor = vStr.split(".")[0];
2997
+ if (fileMajor !== SUPPORTED_MAJOR) {
2998
+ process.stderr.write(
2999
+ `
3000
+ \u274C Node9: Config at ${filePath} has version "${vStr}" \u2014 major version is incompatible with this release (expected "${SUPPORTED_VERSION}"). Config will not be loaded.
3001
+
3002
+ `
3003
+ );
3004
+ return null;
3005
+ } else if (vStr !== SUPPORTED_VERSION) {
3006
+ process.stderr.write(
3007
+ `
3008
+ \u26A0\uFE0F Node9: Config at ${filePath} declares version "${vStr}" \u2014 expected "${SUPPORTED_VERSION}". Continuing with best-effort parsing.
3009
+
3010
+ `
3011
+ );
3012
+ }
3013
+ }
3014
+ const { sanitized, error } = sanitizeConfig(raw);
3015
+ if (error) {
3016
+ process.stderr.write(
3017
+ `
3018
+ \u26A0\uFE0F Node9: Invalid config at ${filePath}:
3019
+ ${error.replace("Invalid config:\n", "")}
3020
+ \u2192 Invalid fields ignored, using defaults for those keys
3021
+
3022
+ `
3023
+ );
3024
+ }
3025
+ return sanitized;
3026
+ }
3027
+
3028
+ // src/policy/index.ts
3029
+ var import_picomatch2 = __toESM(require("picomatch"));
3030
+
3031
+ // src/dlp.ts
3032
+ var import_fs4 = __toESM(require("fs"));
3033
+ var import_path4 = __toESM(require("path"));
3034
+ function scanFilePath(filePath, cwd = process.cwd()) {
3035
+ if (!filePath) return null;
3036
+ let resolved;
3037
+ try {
3038
+ const absolute = import_path4.default.resolve(cwd, filePath);
3039
+ resolved = import_fs4.default.realpathSync.native(absolute);
3040
+ } catch (err) {
3041
+ const code = err.code;
3042
+ if (code === "ENOENT" || code === "ENOTDIR") {
3043
+ resolved = import_path4.default.resolve(cwd, filePath);
3044
+ } else {
3045
+ return sensitivePathMatch(filePath);
2377
3046
  }
2378
- } else {
2379
- allTokens = tokenize2(toolName);
2380
- if (args && typeof args === "object") {
2381
- const flattenedArgs = JSON.stringify(args).toLowerCase();
2382
- const extraTokens = flattenedArgs.split(/[^a-zA-Z0-9]+/).filter((t) => t.length > 1);
2383
- allTokens.push(...extraTokens);
3047
+ }
3048
+ return matchSensitivePath(resolved, filePath);
3049
+ }
3050
+
3051
+ // src/utils/provenance.ts
3052
+ var import_fs5 = __toESM(require("fs"));
3053
+ var import_path5 = __toESM(require("path"));
3054
+ var import_os4 = __toESM(require("os"));
3055
+ var SYSTEM_PREFIXES = ["/usr/bin", "/usr/sbin", "/bin", "/sbin"];
3056
+ var MANAGED_PREFIXES = ["/usr/local/bin", "/opt/homebrew", "/home/linuxbrew", "/nix/store"];
3057
+ var USER_PREFIXES = [
3058
+ import_path5.default.join(import_os4.default.homedir(), "bin"),
3059
+ import_path5.default.join(import_os4.default.homedir(), ".local", "bin"),
3060
+ import_path5.default.join(import_os4.default.homedir(), ".cargo", "bin"),
3061
+ import_path5.default.join(import_os4.default.homedir(), ".npm-global", "bin"),
3062
+ import_path5.default.join(import_os4.default.homedir(), ".volta", "bin")
3063
+ ];
3064
+ var SUSPECT_PREFIXES = ["/tmp", "/var/tmp", "/dev/shm"];
3065
+ function findInPath(cmd) {
3066
+ if (import_path5.default.posix.isAbsolute(cmd)) return cmd;
3067
+ const pathEnv = process.env.PATH ?? "";
3068
+ for (const dir of pathEnv.split(import_path5.default.delimiter)) {
3069
+ if (!dir) continue;
3070
+ const full = import_path5.default.join(dir, cmd);
3071
+ try {
3072
+ import_fs5.default.accessSync(full, import_fs5.default.constants.X_OK);
3073
+ return full;
3074
+ } catch {
2384
3075
  }
2385
3076
  }
2386
- const isManual = agent === "Terminal";
2387
- if (isManual) {
2388
- const SYSTEM_DISASTER_COMMANDS = ["mkfs", "shred", "dd", "drop", "truncate", "purge"];
2389
- const hasSystemDisaster = allTokens.some(
2390
- (t) => SYSTEM_DISASTER_COMMANDS.includes(t.toLowerCase())
2391
- );
2392
- const isRootWipe = allTokens.includes("rm") && (allTokens.includes("/") || allTokens.includes("/*"));
2393
- if (hasSystemDisaster || isRootWipe) {
2394
- return { decision: "review", blockedByLabel: "Manual Nuclear Protection", tier: 3 };
3077
+ return null;
3078
+ }
3079
+ function _classifyPath(resolved, cwd) {
3080
+ if (cwd && resolved.startsWith(cwd + "/")) {
3081
+ return { trustLevel: "user", reason: "binary in project directory" };
3082
+ }
3083
+ const osTmp = import_os4.default.tmpdir();
3084
+ const allSuspect = osTmp ? [...SUSPECT_PREFIXES, osTmp] : SUSPECT_PREFIXES;
3085
+ if (allSuspect.some((p) => resolved === p || resolved.startsWith(p + "/"))) {
3086
+ return { trustLevel: "suspect", reason: `binary in temp directory: ${resolved}` };
3087
+ }
3088
+ if (SYSTEM_PREFIXES.some((p) => resolved === p || resolved.startsWith(p + "/"))) {
3089
+ return { trustLevel: "system", reason: "" };
3090
+ }
3091
+ if (MANAGED_PREFIXES.some((p) => resolved === p || resolved.startsWith(p + "/"))) {
3092
+ return { trustLevel: "managed", reason: "" };
3093
+ }
3094
+ if (USER_PREFIXES.some((p) => resolved === p || resolved.startsWith(p + "/"))) {
3095
+ return { trustLevel: "user", reason: "" };
3096
+ }
3097
+ return { trustLevel: "unknown", reason: "binary in unrecognized location" };
3098
+ }
3099
+ function checkProvenance(cmd, cwd) {
3100
+ const bare = cmd.startsWith("./") ? cmd.slice(2) : cmd;
3101
+ if (import_path5.default.posix.isAbsolute(bare)) {
3102
+ const early = _classifyPath(bare, cwd);
3103
+ if (early.trustLevel === "suspect") {
3104
+ return { resolvedPath: bare, ...early };
2395
3105
  }
2396
- return { decision: "allow" };
2397
3106
  }
2398
- if (pathTokens.length > 0 && config.policy.sandboxPaths.length > 0) {
2399
- const allInSandbox = pathTokens.every((p) => matchesPattern(p, config.policy.sandboxPaths));
2400
- if (allInSandbox) return { decision: "allow" };
3107
+ let resolved;
3108
+ try {
3109
+ const found = findInPath(bare);
3110
+ if (!found) {
3111
+ return {
3112
+ resolvedPath: cmd,
3113
+ trustLevel: "unknown",
3114
+ reason: "binary not found in PATH"
3115
+ };
3116
+ }
3117
+ resolved = import_fs5.default.realpathSync(found);
3118
+ } catch {
3119
+ return {
3120
+ resolvedPath: cmd,
3121
+ trustLevel: "unknown",
3122
+ reason: "binary not found in PATH"
3123
+ };
2401
3124
  }
2402
- let matchedDangerousWord;
2403
- const isDangerous = allTokens.some(
2404
- (token) => config.policy.dangerousWords.some((word) => {
2405
- const w = word.toLowerCase();
2406
- const hit = token === w || (() => {
2407
- try {
2408
- return new RegExp(`\\b${w}\\b`, "i").test(token);
2409
- } catch {
2410
- return false;
2411
- }
2412
- })();
2413
- if (hit && !matchedDangerousWord) matchedDangerousWord = word;
2414
- return hit;
2415
- })
2416
- );
2417
- if (isDangerous) {
2418
- let matchedField;
2419
- if (matchedDangerousWord && args && typeof args === "object" && !Array.isArray(args)) {
2420
- const obj = args;
2421
- for (const [key, value] of Object.entries(obj)) {
2422
- if (typeof value === "string") {
2423
- try {
2424
- if (new RegExp(
2425
- `\\b${matchedDangerousWord.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}\\b`,
2426
- "i"
2427
- ).test(value)) {
2428
- matchedField = key;
2429
- break;
2430
- }
2431
- } catch {
2432
- }
2433
- }
2434
- }
3125
+ try {
3126
+ const stat = import_fs5.default.statSync(resolved);
3127
+ if (stat.mode & 2) {
3128
+ return {
3129
+ resolvedPath: resolved,
3130
+ trustLevel: "suspect",
3131
+ reason: "binary is world-writable"
3132
+ };
2435
3133
  }
3134
+ } catch {
2436
3135
  return {
2437
- decision: "review",
2438
- blockedByLabel: `Project/Global Config \u2014 dangerous word: "${matchedDangerousWord}"`,
2439
- matchedWord: matchedDangerousWord,
2440
- matchedField,
2441
- ruleDescription: `This command contains a flagged keyword ("${matchedDangerousWord}") from your node9 config. Review it before allowing.`,
2442
- tier: 6
3136
+ resolvedPath: resolved,
3137
+ trustLevel: "unknown",
3138
+ reason: "could not stat binary"
2443
3139
  };
2444
3140
  }
2445
- if (config.settings.mode === "strict") {
2446
- const envConfig = getActiveEnvironment(config);
2447
- if (envConfig?.requireApproval === false) return { decision: "allow" };
2448
- return { decision: "review", blockedByLabel: "Global Config (Strict Mode Active)", tier: 7 };
3141
+ const classify = _classifyPath(resolved, cwd);
3142
+ return { resolvedPath: resolved, ...classify };
3143
+ }
3144
+
3145
+ // src/auth/trusted-hosts.ts
3146
+ var import_fs6 = __toESM(require("fs"));
3147
+ var import_path6 = __toESM(require("path"));
3148
+ var import_os5 = __toESM(require("os"));
3149
+ function getTrustedHostsPath() {
3150
+ return import_path6.default.join(import_os5.default.homedir(), ".node9", "trusted-hosts.json");
3151
+ }
3152
+ function readTrustedHosts() {
3153
+ try {
3154
+ const raw = import_fs6.default.readFileSync(getTrustedHostsPath(), "utf8");
3155
+ const parsed = JSON.parse(raw);
3156
+ return Array.isArray(parsed.hosts) ? parsed.hosts : [];
3157
+ } catch {
3158
+ return [];
2449
3159
  }
2450
- return { decision: "allow" };
2451
3160
  }
2452
- function isIgnoredTool(toolName) {
3161
+ var _cache = null;
3162
+ var CACHE_TTL_MS = 5e3;
3163
+ function getFileMtime() {
3164
+ try {
3165
+ return import_fs6.default.statSync(getTrustedHostsPath()).mtimeMs;
3166
+ } catch {
3167
+ return 0;
3168
+ }
3169
+ }
3170
+ function getCachedHosts() {
3171
+ const now = Date.now();
3172
+ if (_cache && now < _cache.expiry) {
3173
+ const mtime = getFileMtime();
3174
+ if (mtime === _cache.mtime) return _cache.hosts;
3175
+ }
3176
+ const hosts = readTrustedHosts();
3177
+ _cache = { hosts, expiry: now + CACHE_TTL_MS, mtime: getFileMtime() };
3178
+ return hosts;
3179
+ }
3180
+ function normalizeHost(raw) {
3181
+ return raw.toLowerCase().replace(/^https?:\/\//, "").replace(/\/.*$/, "").replace(/^[^@]+@/, "").replace(/:\d+$/, "");
3182
+ }
3183
+ function isTrustedHost(host) {
3184
+ const normalized = normalizeHost(host);
3185
+ return getCachedHosts().some((entry) => {
3186
+ const entryHost = entry.host.toLowerCase();
3187
+ if (entryHost.startsWith("*.")) {
3188
+ const domain = entryHost.slice(2);
3189
+ return normalized.endsWith("." + domain);
3190
+ }
3191
+ return normalized === entryHost;
3192
+ });
3193
+ }
3194
+
3195
+ // src/policy/index.ts
3196
+ async function evaluatePolicy2(toolName, args, agent, cwd) {
2453
3197
  const config = getConfig();
2454
- return matchesPattern(toolName, config.policy.ignoredTools);
3198
+ const activeEnvironment = getActiveEnvironment(config) ?? void 0;
3199
+ return evaluatePolicy(
3200
+ config,
3201
+ toolName,
3202
+ args,
3203
+ { agent, cwd, activeEnvironment },
3204
+ { checkProvenance, isTrustedHost }
3205
+ );
3206
+ }
3207
+ function isIgnoredTool2(toolName) {
3208
+ return isIgnoredTool(toolName, getConfig());
2455
3209
  }
2456
3210
 
2457
3211
  // src/auth/state.ts
2458
3212
  var import_fs7 = __toESM(require("fs"));
2459
- var import_path9 = __toESM(require("path"));
3213
+ var import_path7 = __toESM(require("path"));
2460
3214
  var import_os6 = __toESM(require("os"));
2461
- var PAUSED_FILE = import_path9.default.join(import_os6.default.homedir(), ".node9", "PAUSED");
2462
- var TRUST_FILE = import_path9.default.join(import_os6.default.homedir(), ".node9", "trust.json");
3215
+ var PAUSED_FILE = import_path7.default.join(import_os6.default.homedir(), ".node9", "PAUSED");
3216
+ var TRUST_FILE = import_path7.default.join(import_os6.default.homedir(), ".node9", "trust.json");
2463
3217
  function extractCommandPattern(toolName, args) {
2464
3218
  const lower = toolName.toLowerCase();
2465
3219
  if (lower !== "bash" && lower !== "execute_bash" && lower !== "shell") return void 0;
@@ -2486,7 +3240,7 @@ function checkPause() {
2486
3240
  }
2487
3241
  }
2488
3242
  function atomicWriteSync(filePath, data, options) {
2489
- const dir = import_path9.default.dirname(filePath);
3243
+ const dir = import_path7.default.dirname(filePath);
2490
3244
  if (!import_fs7.default.existsSync(dir)) import_fs7.default.mkdirSync(dir, { recursive: true });
2491
3245
  const tmpPath = `${filePath}.${import_os6.default.hostname()}.${process.pid}.tmp`;
2492
3246
  import_fs7.default.writeFileSync(tmpPath, data, options);
@@ -2541,7 +3295,7 @@ function writeTrustSession(toolName, durationMs, args) {
2541
3295
  }
2542
3296
  function getPersistentDecision(toolName) {
2543
3297
  try {
2544
- const file = import_path9.default.join(import_os6.default.homedir(), ".node9", "decisions.json");
3298
+ const file = import_path7.default.join(import_os6.default.homedir(), ".node9", "decisions.json");
2545
3299
  if (!import_fs7.default.existsSync(file)) return null;
2546
3300
  const decisions = JSON.parse(import_fs7.default.readFileSync(file, "utf-8"));
2547
3301
  const d = decisions[toolName];
@@ -2554,10 +3308,10 @@ function getPersistentDecision(toolName) {
2554
3308
  // src/auth/daemon.ts
2555
3309
  var import_fs8 = __toESM(require("fs"));
2556
3310
  var import_net = __toESM(require("net"));
2557
- var import_path10 = __toESM(require("path"));
3311
+ var import_path8 = __toESM(require("path"));
2558
3312
  var import_os7 = __toESM(require("os"));
2559
3313
  var import_child_process = require("child_process");
2560
- var ACTIVITY_SOCKET_PATH = process.platform === "win32" ? "\\\\.\\pipe\\node9-activity" : import_path10.default.join(import_os7.default.tmpdir(), "node9-activity.sock");
3314
+ var ACTIVITY_SOCKET_PATH = process.platform === "win32" ? "\\\\.\\pipe\\node9-activity" : import_path8.default.join(import_os7.default.tmpdir(), "node9-activity.sock");
2561
3315
  function notifyActivitySocket(data) {
2562
3316
  return new Promise((resolve) => {
2563
3317
  try {
@@ -2590,7 +3344,7 @@ var DAEMON_PORT = 7391;
2590
3344
  var DAEMON_HOST = "127.0.0.1";
2591
3345
  function getInternalToken() {
2592
3346
  try {
2593
- const pidFile = import_path10.default.join(import_os7.default.homedir(), ".node9", "daemon.pid");
3347
+ const pidFile = import_path8.default.join(import_os7.default.homedir(), ".node9", "daemon.pid");
2594
3348
  if (!import_fs8.default.existsSync(pidFile)) return null;
2595
3349
  const data = JSON.parse(import_fs8.default.readFileSync(pidFile, "utf-8"));
2596
3350
  process.kill(data.pid, 0);
@@ -2600,7 +3354,7 @@ function getInternalToken() {
2600
3354
  }
2601
3355
  }
2602
3356
  function isDaemonRunning() {
2603
- const pidFile = import_path10.default.join(import_os7.default.homedir(), ".node9", "daemon.pid");
3357
+ const pidFile = import_path8.default.join(import_os7.default.homedir(), ".node9", "daemon.pid");
2604
3358
  if (import_fs8.default.existsSync(pidFile)) {
2605
3359
  let pid;
2606
3360
  let port;
@@ -2773,10 +3527,10 @@ var import_crypto3 = require("crypto");
2773
3527
 
2774
3528
  // src/ui/native.ts
2775
3529
  var import_child_process2 = require("child_process");
2776
- var import_path12 = __toESM(require("path"));
3530
+ var import_path10 = __toESM(require("path"));
2777
3531
 
2778
3532
  // src/context-sniper.ts
2779
- var import_path11 = __toESM(require("path"));
3533
+ var import_path9 = __toESM(require("path"));
2780
3534
  function smartTruncate(str, maxLen = 500) {
2781
3535
  if (str.length <= maxLen) return str;
2782
3536
  const edge = Math.floor(maxLen / 2) - 3;
@@ -2844,7 +3598,7 @@ function computeRiskMetadata(args, tier, blockedByLabel, matchedField, matchedWo
2844
3598
  intent = "EDIT";
2845
3599
  if (obj.file_path) {
2846
3600
  editFilePath = String(obj.file_path);
2847
- editFileName = import_path11.default.basename(editFilePath);
3601
+ editFileName = import_path9.default.basename(editFilePath);
2848
3602
  }
2849
3603
  const result = extractContext(String(obj.new_string), matchedWord);
2850
3604
  contextSnippet = result.snippet;
@@ -2899,7 +3653,7 @@ function formatArgs(args, matchedField, matchedWord) {
2899
3653
  if (typeof parsed === "object" && !Array.isArray(parsed)) {
2900
3654
  const obj = parsed;
2901
3655
  if (obj.old_string !== void 0 && obj.new_string !== void 0) {
2902
- const file = obj.file_path ? import_path12.default.basename(String(obj.file_path)) : "file";
3656
+ const file = obj.file_path ? import_path10.default.basename(String(obj.file_path)) : "file";
2903
3657
  const oldPreview = smartTruncate(String(obj.old_string), 120);
2904
3658
  const newPreview = extractContext(String(obj.new_string), matchedWord).snippet;
2905
3659
  return {
@@ -3100,16 +3854,63 @@ init_audit();
3100
3854
  // src/auth/cloud.ts
3101
3855
  var import_fs9 = __toESM(require("fs"));
3102
3856
  var import_os8 = __toESM(require("os"));
3103
- var import_path13 = __toESM(require("path"));
3857
+ var import_path11 = __toESM(require("path"));
3104
3858
  init_audit();
3105
- function auditLocalAllow(toolName, args, checkedBy, creds, meta) {
3106
- return fetch(`${creds.apiUrl}/audit`, {
3859
+ var DLP_SAMPLE_MAX_LEN = 200;
3860
+ var DLP_PATTERN_MAX_LEN = 100;
3861
+ var KNOWN_CHECKED_BY = /* @__PURE__ */ new Set([
3862
+ "dlp-block",
3863
+ "observe-mode-dlp-would-block",
3864
+ "dlp-review-flagged",
3865
+ "loop-detected",
3866
+ "audit-mode",
3867
+ "local-policy",
3868
+ "smart-rule-block",
3869
+ "persistent",
3870
+ "trust",
3871
+ "observe-mode",
3872
+ "observe-mode-would-block"
3873
+ ]);
3874
+ function validateApiUrl(raw) {
3875
+ let u;
3876
+ try {
3877
+ u = new URL(raw);
3878
+ } catch {
3879
+ return null;
3880
+ }
3881
+ if (u.username || u.password) return null;
3882
+ if (u.protocol === "https:") return u;
3883
+ if (u.protocol === "http:") {
3884
+ const h = u.hostname;
3885
+ if (h === "127.0.0.1" || h === "localhost" || h === "::1" || h === "[::1]") return u;
3886
+ }
3887
+ return null;
3888
+ }
3889
+ function auditLocalAllow(toolName, args, checkedBy, creds, meta, dlpInfo, containsSensitiveArgs = false) {
3890
+ const validated = validateApiUrl(creds.apiUrl);
3891
+ if (!validated) {
3892
+ try {
3893
+ import_fs9.default.appendFileSync(
3894
+ HOOK_DEBUG_LOG,
3895
+ `[audit] refused to send: invalid apiUrl scheme/host (got "${String(creds.apiUrl).slice(0, 200)}")
3896
+ `
3897
+ );
3898
+ } catch {
3899
+ }
3900
+ return Promise.resolve();
3901
+ }
3902
+ const safeArgs = containsSensitiveArgs ? { tool: toolName, redacted: true } : args;
3903
+ const dlpSample = dlpInfo && typeof dlpInfo.redactedSample === "string" ? dlpInfo.redactedSample.slice(0, DLP_SAMPLE_MAX_LEN) : void 0;
3904
+ const dlpPattern = dlpInfo && typeof dlpInfo.pattern === "string" ? dlpInfo.pattern.slice(0, DLP_PATTERN_MAX_LEN) : void 0;
3905
+ const safeCheckedBy = KNOWN_CHECKED_BY.has(checkedBy) ? checkedBy : "unknown";
3906
+ return fetch(`${validated.toString().replace(/\/$/, "")}/audit`, {
3107
3907
  method: "POST",
3108
3908
  headers: { "Content-Type": "application/json", Authorization: `Bearer ${creds.apiKey}` },
3109
3909
  body: JSON.stringify({
3110
3910
  toolName,
3111
- args,
3112
- checkedBy,
3911
+ args: safeArgs,
3912
+ checkedBy: safeCheckedBy,
3913
+ ...dlpInfo && { dlpPattern, dlpSample },
3113
3914
  context: {
3114
3915
  agent: meta?.agent,
3115
3916
  mcpServer: meta?.mcpServer,
@@ -3130,7 +3931,7 @@ async function initNode9SaaS(toolName, args, creds, meta, riskMetadata, agentPol
3130
3931
  let ciContext;
3131
3932
  if (process.env.CI) {
3132
3933
  try {
3133
- const ciContextPath = import_path13.default.join(import_os8.default.homedir(), ".node9", "ci-context.json");
3934
+ const ciContextPath = import_path11.default.join(import_os8.default.homedir(), ".node9", "ci-context.json");
3134
3935
  const stats = import_fs9.default.statSync(ciContextPath);
3135
3936
  if (stats.size > 1e4) throw new Error("ci-context.json exceeds 10 KB");
3136
3937
  const raw = import_fs9.default.readFileSync(ciContextPath, "utf8");
@@ -3246,16 +4047,10 @@ async function resolveNode9SaaS(requestId, creds, approved, decidedBy) {
3246
4047
 
3247
4048
  // src/loop-detector.ts
3248
4049
  var import_fs10 = __toESM(require("fs"));
3249
- var import_path14 = __toESM(require("path"));
4050
+ var import_path12 = __toESM(require("path"));
3250
4051
  var import_os9 = __toESM(require("os"));
3251
- var import_crypto2 = __toESM(require("crypto"));
3252
4052
  function loopStateFile() {
3253
- return import_path14.default.join(import_os9.default.homedir(), ".node9", "loop-state.json");
3254
- }
3255
- var MAX_RECORDS = 500;
3256
- function computeArgsHash(args) {
3257
- const str = JSON.stringify(args ?? "");
3258
- return import_crypto2.default.createHash("sha256").update(str).digest("hex").slice(0, 16);
4053
+ return import_path12.default.join(import_os9.default.homedir(), ".node9", "loop-state.json");
3259
4054
  }
3260
4055
  function readState() {
3261
4056
  try {
@@ -3269,7 +4064,7 @@ function readState() {
3269
4064
  }
3270
4065
  }
3271
4066
  function writeState(records) {
3272
- const dir = import_path14.default.dirname(loopStateFile());
4067
+ const dir = import_path12.default.dirname(loopStateFile());
3273
4068
  if (!import_fs10.default.existsSync(dir)) import_fs10.default.mkdirSync(dir, { recursive: true });
3274
4069
  const tmpPath = `${loopStateFile()}.${import_os9.default.hostname()}.${process.pid}.tmp`;
3275
4070
  import_fs10.default.writeFileSync(tmpPath, JSON.stringify(records));
@@ -3277,14 +4072,9 @@ function writeState(records) {
3277
4072
  }
3278
4073
  function recordAndCheck(tool, args, threshold = 3, windowMs = 12e4) {
3279
4074
  try {
3280
- const hash = computeArgsHash(args);
3281
- const now = Date.now();
3282
- const cutoff = now - windowMs;
3283
- const records = readState().filter((r) => r.ts >= cutoff);
3284
- records.push({ t: tool, h: hash, ts: now });
3285
- const count = records.filter((r) => r.t === tool && r.h === hash).length;
3286
- writeState(records.slice(-MAX_RECORDS));
3287
- return { looping: count >= threshold, count };
4075
+ const result = evaluateLoopWindow(readState(), tool, args, threshold, windowMs, Date.now());
4076
+ writeState(result.nextRecords);
4077
+ return { looping: result.looping, count: result.count };
3288
4078
  } catch {
3289
4079
  return { looping: false, count: 0 };
3290
4080
  }
@@ -3417,7 +4207,7 @@ async function _authorizeHeadlessCore(toolName, args, meta, options) {
3417
4207
  }
3418
4208
  }
3419
4209
  }
3420
- if (config.policy.dlp.enabled && (!isIgnoredTool(toolName) || config.policy.dlp.scanIgnoredTools)) {
4210
+ if (config.policy.dlp.enabled && (!isIgnoredTool2(toolName) || config.policy.dlp.scanIgnoredTools)) {
3421
4211
  const argsObj = args && typeof args === "object" && !Array.isArray(args) ? args : {};
3422
4212
  const filePath = String(argsObj.file_path ?? argsObj.path ?? argsObj.filename ?? "");
3423
4213
  const dlpMatch = (filePath ? scanFilePath(filePath) : null) ?? scanArgs(args);
@@ -3433,6 +4223,16 @@ async function _authorizeHeadlessCore(toolName, args, meta, options) {
3433
4223
  meta,
3434
4224
  true
3435
4225
  );
4226
+ if (approvers.cloud && creds?.apiKey)
4227
+ auditLocalAllow(
4228
+ toolName,
4229
+ args,
4230
+ isObserveMode ? "observe-mode-dlp-would-block" : "dlp-block",
4231
+ creds,
4232
+ meta,
4233
+ { pattern: dlpMatch.patternName, redactedSample: dlpMatch.redactedSample },
4234
+ true
4235
+ );
3436
4236
  if (isWriteTool(toolName) && filePath) {
3437
4237
  await notifyTaint(filePath, `DLP:${dlpMatch.patternName}`);
3438
4238
  }
@@ -3457,8 +4257,8 @@ async function _authorizeHeadlessCore(toolName, args, meta, options) {
3457
4257
  }
3458
4258
  }
3459
4259
  if (isObserveMode) {
3460
- if (!isIgnoredTool(toolName)) {
3461
- const policyResult = await evaluatePolicy(toolName, args, meta?.agent, options?.cwd);
4260
+ if (!isIgnoredTool2(toolName)) {
4261
+ const policyResult = await evaluatePolicy2(toolName, args, meta?.agent, options?.cwd);
3462
4262
  const wouldBlock = policyResult.decision === "block";
3463
4263
  if (!isManual)
3464
4264
  appendLocalAudit(
@@ -3482,8 +4282,8 @@ async function _authorizeHeadlessCore(toolName, args, meta, options) {
3482
4282
  return { approved: true, checkedBy: "audit" };
3483
4283
  }
3484
4284
  if (config.settings.mode === "audit") {
3485
- if (!isIgnoredTool(toolName)) {
3486
- const policyResult = await evaluatePolicy(toolName, args, meta?.agent, options?.cwd);
4285
+ if (!isIgnoredTool2(toolName)) {
4286
+ const policyResult = await evaluatePolicy2(toolName, args, meta?.agent, options?.cwd);
3487
4287
  if (policyResult.decision === "review") {
3488
4288
  appendLocalAudit(toolName, args, "allow", "audit-mode", meta, hashAuditArgs);
3489
4289
  if (approvers.cloud && creds?.apiKey) {
@@ -3493,7 +4293,7 @@ async function _authorizeHeadlessCore(toolName, args, meta, options) {
3493
4293
  }
3494
4294
  return { approved: true, checkedBy: "audit" };
3495
4295
  }
3496
- if (!taintWarning && !isIgnoredTool(toolName)) {
4296
+ if (!taintWarning && !isIgnoredTool2(toolName)) {
3497
4297
  const ld = config.policy.loopDetection;
3498
4298
  if (ld.enabled) {
3499
4299
  const loopResult = recordAndCheck(toolName, args, ld.threshold, ld.windowSeconds * 1e3);
@@ -3501,6 +4301,8 @@ async function _authorizeHeadlessCore(toolName, args, meta, options) {
3501
4301
  const reason = `It looks like you've called "${toolName}" ${loopResult.count} times with identical arguments in the last ${ld.windowSeconds}s. Are you stuck? Step back and reconsider your approach \u2014 what are you actually trying to accomplish, and is there a different way to get there?`;
3502
4302
  if (!isManual)
3503
4303
  appendLocalAudit(toolName, args, "deny", "loop-detected", meta, hashAuditArgs);
4304
+ if (approvers.cloud && creds?.apiKey)
4305
+ auditLocalAllow(toolName, args, "loop-detected", creds, meta, void 0, true);
3504
4306
  return {
3505
4307
  approved: false,
3506
4308
  reason,
@@ -3509,7 +4311,7 @@ async function _authorizeHeadlessCore(toolName, args, meta, options) {
3509
4311
  };
3510
4312
  }
3511
4313
  }
3512
- const policyResult = await evaluatePolicy(toolName, args, meta?.agent);
4314
+ const policyResult = await evaluatePolicy2(toolName, args, meta?.agent);
3513
4315
  if (policyResult.decision === "allow") {
3514
4316
  if (approvers.cloud && creds?.apiKey)
3515
4317
  await auditLocalAllow(toolName, args, "local-policy", creds, meta);