@rafter-security/cli 0.9.0 → 0.10.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.
@@ -1,6 +1,6 @@
1
1
  import { ConfigManager } from "./config-manager.js";
2
2
  import { AuditLogger } from "./audit-logger.js";
3
- import { assessCommandRisk, matchedCriticalPattern } from "./risk-rules.js";
3
+ import { assessCommandRisk, matchedCriticalPattern, sanitizeCommandForMatching, } from "./risk-rules.js";
4
4
  export class CommandInterceptor {
5
5
  constructor() {
6
6
  this.config = new ConfigManager();
@@ -47,12 +47,19 @@ export class CommandInterceptor {
47
47
  requiresApproval: false
48
48
  };
49
49
  }
50
- // Check blocked patterns (always block)
50
+ // Check blocked patterns (always block).
51
+ //
52
+ // A deny-list match denies — that is what a deny-list is for — but it must
53
+ // NOT rewrite the command's risk. Reporting every deny-list hit as
54
+ // "critical" made the hook tell users a `gh pr create` was an irreversible
55
+ // system-damage command. The assessed risk is reported as assessed; the
56
+ // genuinely unconditional hard-blocks are the CRITICAL_PATTERNS handled
57
+ // above, and the default deny-list is exactly that set.
51
58
  for (const pattern of policy.blockedPatterns) {
52
59
  if (this.matchesPattern(command, pattern)) {
53
60
  return {
54
61
  command,
55
- riskLevel: "critical",
62
+ riskLevel,
56
63
  allowed: false,
57
64
  requiresApproval: false,
58
65
  reason: `Matches blocked pattern: ${pattern}`,
@@ -63,7 +70,6 @@ export class CommandInterceptor {
63
70
  // Check approval patterns
64
71
  for (const pattern of policy.requireApproval) {
65
72
  if (this.matchesPattern(command, pattern)) {
66
- const riskLevel = this.assessRisk(command);
67
73
  return {
68
74
  command,
69
75
  riskLevel,
@@ -74,47 +80,21 @@ export class CommandInterceptor {
74
80
  };
75
81
  }
76
82
  }
77
- // Check policy mode
78
- if (policy.mode === "deny-list") {
79
- // If not in blocked or approval lists, allow
80
- return {
81
- command,
82
- riskLevel: this.assessRisk(command),
83
- allowed: true,
84
- requiresApproval: false
85
- };
86
- }
87
- else if (policy.mode === "approve-dangerous") {
88
- // Assess risk and require approval for high/critical
89
- const riskLevel = this.assessRisk(command);
90
- if (riskLevel === "high" || riskLevel === "critical") {
91
- return {
92
- command,
93
- riskLevel,
94
- allowed: false,
95
- requiresApproval: true,
96
- reason: `High risk command requires approval`
97
- };
98
- }
83
+ // Check policy mode. `riskLevel` is the assessment made at the top of
84
+ // evaluate() — critical already returned, so it is high/medium/low here.
85
+ if (policy.mode === "approve-dangerous" && riskLevel === "high") {
99
86
  return {
100
87
  command,
101
88
  riskLevel,
102
- allowed: true,
103
- requiresApproval: false
104
- };
105
- }
106
- else if (policy.mode === "allow-all") {
107
- return {
108
- command,
109
- riskLevel: this.assessRisk(command),
110
- allowed: true,
111
- requiresApproval: false
89
+ allowed: false,
90
+ requiresApproval: true,
91
+ reason: `High risk command requires approval`
112
92
  };
113
93
  }
114
- // Default: allow
94
+ // deny-list / allow-all / unknown mode: not blocked, not approval-gated.
115
95
  return {
116
96
  command,
117
- riskLevel: this.assessRisk(command),
97
+ riskLevel,
118
98
  allowed: true,
119
99
  requiresApproval: false
120
100
  };
@@ -126,16 +106,23 @@ export class CommandInterceptor {
126
106
  this.audit.logCommandIntercepted(evaluation.command, evaluation.allowed, actionTaken, evaluation.reason);
127
107
  }
128
108
  /**
129
- * Match command against pattern
109
+ * Match a command against a policy pattern.
110
+ *
111
+ * Matching runs against the SANITIZED command line, not the raw string: the
112
+ * policy patterns describe commands, so quoted text a command merely consumes
113
+ * as data (a commit message, a PR body) must not match them, while text a
114
+ * shell or eval wrapper executes (`bash -c "…"`) must. See
115
+ * `sanitizeCommandForMatching`.
130
116
  */
131
117
  matchesPattern(command, pattern) {
118
+ const target = sanitizeCommandForMatching(command);
132
119
  try {
133
120
  const regex = new RegExp(pattern, "i");
134
- return regex.test(command);
121
+ return regex.test(target);
135
122
  }
136
123
  catch {
137
124
  // If pattern is not valid regex, try case-insensitive substring match
138
- return command.toLowerCase().includes(pattern.toLowerCase());
125
+ return target.toLowerCase().includes(pattern.toLowerCase());
139
126
  }
140
127
  }
141
128
  /**
@@ -129,6 +129,10 @@ function validateConfig(raw) {
129
129
  console.error('Warning: config "agent.scan.autoUpdateBetterleaks" must be a boolean — using default.');
130
130
  delete scan.autoUpdateBetterleaks;
131
131
  }
132
+ if (scan.plusRequiresApproval !== undefined && typeof scan.plusRequiresApproval !== "boolean") {
133
+ console.error('Warning: config "agent.scan.plusRequiresApproval" must be a boolean — using default.');
134
+ delete scan.plusRequiresApproval;
135
+ }
132
136
  }
133
137
  }
134
138
  return raw;
@@ -299,6 +303,11 @@ export class ConfigManager {
299
303
  if (policy.scan.autoUpdateBetterleaks !== undefined) {
300
304
  config.agent.scan.autoUpdateBetterleaks = policy.scan.autoUpdateBetterleaks;
301
305
  }
306
+ // sable-9ddf — OR merge: a project policy may turn the Plus-approval gate
307
+ // ON, but must never turn OFF a gate the machine owner set globally.
308
+ if (policy.scan.plusRequiresApproval === true) {
309
+ config.agent.scan.plusRequiresApproval = true;
310
+ }
302
311
  }
303
312
  // Ignore rules — top-level policy key, applied per finding at scan time
304
313
  if (policy.ignore && config.agent) {
@@ -2,6 +2,22 @@ const GENERIC_PATTERN_NAMES = new Set(["Generic API Key", "Generic Secret"]);
2
2
  const VARIABLE_NAME_RE = /^[A-Z][A-Z0-9]*(?:_[A-Z0-9]+)+$/;
3
3
  const LOWERCASE_IDENT_RE = /^[a-z][a-z0-9]*(?:_[a-z0-9]+)+$/;
4
4
  const QUOTED_VALUE_RE = /['"]([^'"]+)['"]/;
5
+ /**
6
+ * Matches an environment-assignment prefix in a command line: an identifier
7
+ * followed immediately by `=` and a run of non-whitespace (the value). Used to
8
+ * find `NAME=VALUE` tokens like `RAFTER_API_KEY=<secret> rafter ...` so their
9
+ * value can be redacted before the command is written to the audit log.
10
+ */
11
+ const ENV_ASSIGN_RE = /(^|\s)([A-Za-z_][A-Za-z0-9_]*)=(\S+)/g;
12
+ /**
13
+ * A `NAME` in a `NAME=VALUE` assignment is treated as secret-bearing when it
14
+ * ends in a credential-suggesting word (e.g. RAFTER_API_KEY, GITHUB_TOKEN,
15
+ * DB_PASSWORD, AUTH). Values behind such names are redacted even when they
16
+ * don't match any known secret pattern (that's the whole point — the value of
17
+ * a bespoke API key won't match a built-in pattern, but it's still a secret).
18
+ * Plain names like FOO or NODE_ENV do not match, so `FOO=bar` is left intact.
19
+ */
20
+ const SECRET_ENV_NAME_RE = /(?:^|_)(KEY|TOKEN|SECRET|SECRETS|PASSWORD|PASSWD|PWD|API[_-]?KEY|ACCESS[_-]?KEY|CREDENTIALS?|AUTH)$/i;
5
21
  export class PatternEngine {
6
22
  constructor(patterns) {
7
23
  this.patterns = patterns;
@@ -53,16 +69,31 @@ export class PatternEngine {
53
69
  return matches;
54
70
  }
55
71
  /**
56
- * Redact text by replacing sensitive patterns
72
+ * Redact text by replacing sensitive patterns.
73
+ *
74
+ * Two layers, both additive:
75
+ * 1. Env-assignment redaction: any `NAME=VALUE` whose NAME looks
76
+ * secret-bearing has its VALUE masked, even if the VALUE matches no
77
+ * known pattern. This catches leaks like `RAFTER_API_KEY=<key> rafter …`
78
+ * in a logged command line, where the key's shape is unknown.
79
+ * 2. Pattern-based redaction: values that match a built-in secret pattern
80
+ * are masked wherever they appear.
57
81
  */
58
82
  redactText(text) {
59
- let redacted = text;
83
+ let redacted = this.redactEnvAssignments(text);
60
84
  for (const pattern of this.patterns) {
61
85
  const regex = this.createRegex(pattern.regex);
62
86
  redacted = redacted.replace(regex, (match) => this.isFalsePositive(pattern, match) ? match : this.redact(match));
63
87
  }
64
88
  return redacted;
65
89
  }
90
+ /**
91
+ * Mask the VALUE of every `NAME=VALUE` token whose NAME looks
92
+ * secret-bearing. Non-secret names (FOO, NODE_ENV, …) are left untouched.
93
+ */
94
+ redactEnvAssignments(text) {
95
+ return text.replace(ENV_ASSIGN_RE, (full, prefix, name, value) => SECRET_ENV_NAME_RE.test(name) ? `${prefix}${name}=${this.redact(value)}` : full);
96
+ }
66
97
  /**
67
98
  * Check if text contains any sensitive patterns
68
99
  */
@@ -99,6 +99,9 @@ function mapPolicy(raw) {
99
99
  if (typeof raw.scan.auto_update_betterleaks === "boolean") {
100
100
  policy.scan.autoUpdateBetterleaks = raw.scan.auto_update_betterleaks;
101
101
  }
102
+ if (typeof raw.scan.plus_requires_approval === "boolean") {
103
+ policy.scan.plusRequiresApproval = raw.scan.plus_requires_approval;
104
+ }
102
105
  }
103
106
  // sable-c1c — backend flat-shape compat. rafter-backend reads
104
107
  // exclude_paths / custom_patterns at the top level (no `scan:` nesting),
@@ -307,6 +310,12 @@ function validatePolicy(policy, raw) {
307
310
  typeof raw.scan.auto_update_betterleaks !== "boolean") {
308
311
  console.error(`Warning: "scan.auto_update_betterleaks" must be a boolean — ignoring.`);
309
312
  }
313
+ // sable-9ddf — only a boolean reaches policy.scan (mapping guards the type).
314
+ if (raw.scan && typeof raw.scan === "object" &&
315
+ raw.scan.plus_requires_approval !== undefined &&
316
+ typeof raw.scan.plus_requires_approval !== "boolean") {
317
+ console.error(`Warning: "scan.plus_requires_approval" must be a boolean — ignoring.`);
318
+ }
310
319
  }
311
320
  if (policy.ignore !== undefined) {
312
321
  if (!Array.isArray(policy.ignore)) {