shellfirm 0.3.7

`shellfirm` will intercept any risky patterns (default or defined by you) and prompt you a small challenge for double verification, kinda like a captcha for your terminal.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
//! Runtime context detection.
//!
//! Detects environment signals (SSH, root, git branch, k8s context, etc.)
//! and computes a [`RiskLevel`] used to escalate challenge severity.

use serde_derive::{Deserialize, Serialize};
use tracing::debug;

use crate::{checks, config::Challenge, env::Environment};

/// Risk level computed from environment context signals.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum RiskLevel {
    /// No risk signals detected.
    #[default]
    Normal,
    /// Moderate signals: SSH session, staging-like env, etc.
    Elevated,
    /// High-risk signals: root, production k8s, protected branch, production env.
    Critical,
}

/// Snapshot of environment context at the time a command is evaluated.
#[derive(Debug, Clone, Default)]
pub struct RuntimeContext {
    pub is_ssh: bool,
    pub is_root: bool,
    pub git_branch: Option<String>,
    pub k8s_context: Option<String>,
    pub env_signals: Vec<String>,
    pub risk_level: RiskLevel,
    /// Human-readable labels shown in the banner (e.g. "branch=main").
    pub labels: Vec<String>,
}

/// User-configurable context settings (stored in `settings.yaml`).
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct ContextConfig {
    #[serde(default = "default_protected_branches")]
    pub protected_branches: Vec<String>,
    #[serde(default = "default_production_k8s_patterns")]
    pub production_k8s_patterns: Vec<String>,
    #[serde(default = "default_production_env_vars")]
    pub production_env_vars: std::collections::BTreeMap<String, String>,
    #[serde(default)]
    pub sensitive_paths: Vec<String>,
    #[serde(default)]
    pub escalation: EscalationConfig,
}

fn default_protected_branches() -> Vec<String> {
    vec![
        "main".into(),
        "master".into(),
        "production".into(),
        "release/*".into(),
    ]
}

fn default_production_k8s_patterns() -> Vec<String> {
    vec![
        "prod".into(),
        "production".into(),
        "prd".into(),
        "live".into(),
    ]
}

fn default_production_env_vars() -> std::collections::BTreeMap<String, String> {
    let mut m = std::collections::BTreeMap::new();
    m.insert("NODE_ENV".into(), "production".into());
    m.insert("RAILS_ENV".into(), "production".into());
    m.insert("ENVIRONMENT".into(), "production".into());
    m
}

impl Default for ContextConfig {
    fn default() -> Self {
        Self {
            protected_branches: default_protected_branches(),
            production_k8s_patterns: default_production_k8s_patterns(),
            production_env_vars: default_production_env_vars(),
            sensitive_paths: vec![],
            escalation: EscalationConfig::default(),
        }
    }
}

/// Maps risk levels to the minimum challenge type.
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct EscalationConfig {
    #[serde(default = "default_elevated_challenge")]
    pub elevated: Challenge,
    #[serde(default = "default_critical_challenge")]
    pub critical: Challenge,
}

const fn default_elevated_challenge() -> Challenge {
    Challenge::Enter
}
const fn default_critical_challenge() -> Challenge {
    Challenge::Yes
}

impl Default for EscalationConfig {
    fn default() -> Self {
        Self {
            elevated: default_elevated_challenge(),
            critical: default_critical_challenge(),
        }
    }
}

impl RuntimeContext {
    /// Return a filtered copy that only contains signals relevant to the
    /// matched check groups.
    ///
    /// **Global signals** (`is_ssh`, `is_root`, `env_signals`) are always
    /// kept — they apply regardless of what command matched.
    ///
    /// **Domain signals** are kept only when the corresponding group is
    /// present in `matched_groups`:
    /// - `git_branch` → `"git"`
    /// - `k8s_context` → `"kubernetes"`
    ///
    /// Labels and `risk_level` are recomputed from the kept signals.
    #[must_use]
    pub fn filter_for_groups(
        &self,
        matched_groups: &std::collections::HashSet<&str>,
        config: &ContextConfig,
    ) -> Self {
        let keep_git = matched_groups.contains("git");
        let keep_k8s = matched_groups.contains("kubernetes");

        let git_branch = if keep_git {
            self.git_branch.clone()
        } else {
            None
        };
        let k8s_context = if keep_k8s {
            self.k8s_context.clone()
        } else {
            None
        };

        // Rebuild labels from kept signals
        let mut labels = Vec::new();
        if self.is_ssh {
            labels.push("ssh=true".into());
        }
        if self.is_root {
            labels.push("root=true".into());
        }
        if let Some(ref branch) = git_branch {
            labels.push(format!("branch={branch}"));
        }
        if let Some(ref k8s) = k8s_context {
            labels.push(format!("k8s={k8s}"));
        }

        let filtered = Self {
            is_ssh: self.is_ssh,
            is_root: self.is_root,
            git_branch,
            k8s_context,
            env_signals: self.env_signals.clone(),
            risk_level: RiskLevel::Normal, // placeholder
            labels,
        };

        Self {
            risk_level: compute_risk_level(&filtered, config),
            ..filtered
        }
    }
}

/// Detect runtime context from the given environment.
///
/// This is called once per shellfirm invocation.
pub fn detect(env: &dyn Environment, config: &ContextConfig) -> RuntimeContext {
    let mut ctx = RuntimeContext {
        is_ssh: env.var("SSH_CONNECTION").is_some() || env.var("SSH_TTY").is_some(),
        ..RuntimeContext::default()
    };
    if ctx.is_ssh {
        ctx.labels.push("ssh=true".into());
    }

    // Root user
    ctx.is_root = env.var("EUID").is_some_and(|v| v == "0");
    if ctx.is_root {
        ctx.labels.push("root=true".into());
    }

    // Git branch
    ctx.git_branch = env.run_command("git", &["rev-parse", "--abbrev-ref", "HEAD"], 100);
    if let Some(ref branch) = ctx.git_branch {
        ctx.labels.push(format!("branch={branch}"));
    }

    // Kubernetes context
    ctx.k8s_context = env.run_command("kubectl", &["config", "current-context"], 100);
    if let Some(ref k8s) = ctx.k8s_context {
        ctx.labels.push(format!("k8s={k8s}"));
    }

    // Production environment variables
    for (key, expected_val) in &config.production_env_vars {
        if let Some(val) = env.var(key) {
            if val.eq_ignore_ascii_case(expected_val) {
                ctx.env_signals.push(format!("{key}={val}"));
            }
        }
    }

    // Compute risk level
    ctx.risk_level = compute_risk_level(&ctx, config);

    debug!("detected context: {ctx:?}");
    ctx
}

/// Compute the aggregate risk level from context signals.
pub(crate) fn compute_risk_level(ctx: &RuntimeContext, config: &ContextConfig) -> RiskLevel {
    // Critical signals
    if ctx.is_root {
        return RiskLevel::Critical;
    }
    if let Some(ref branch) = ctx.git_branch {
        if branch_matches_any(branch, &config.protected_branches) {
            return RiskLevel::Critical;
        }
    }
    if let Some(ref k8s) = ctx.k8s_context {
        if matches_any_pattern(k8s, &config.production_k8s_patterns) {
            return RiskLevel::Critical;
        }
    }
    if !ctx.env_signals.is_empty() {
        return RiskLevel::Critical;
    }

    // Elevated signals
    if ctx.is_ssh {
        return RiskLevel::Elevated;
    }

    RiskLevel::Normal
}

/// Check if a branch name matches any of the given branch patterns.
/// Supports exact matches and wildcard prefixes like `"release/*"`.
#[must_use]
pub fn branch_matches_any(branch: &str, patterns: &[String]) -> bool {
    for pattern in patterns {
        if pattern.ends_with("/*") {
            let prefix = &pattern[..pattern.len() - 1]; // "release/"
            if branch.starts_with(prefix) {
                return true;
            }
        } else if branch == pattern {
            return true;
        }
    }
    false
}

/// Check if a string contains any of the given substrings (case-insensitive).
fn matches_any_pattern(value: &str, patterns: &[String]) -> bool {
    let lower = value.to_ascii_lowercase();
    patterns
        .iter()
        .any(|p| lower.contains(p.to_ascii_lowercase().as_str()))
}

/// Given a base challenge level and a risk level, return the escalated
/// challenge. Escalation can only make things **stricter**, never weaker.
#[must_use]
pub fn escalate_challenge(
    base: &Challenge,
    risk_level: RiskLevel,
    escalation: &EscalationConfig,
) -> Challenge {
    let context_min = match risk_level {
        RiskLevel::Normal => return *base,
        RiskLevel::Elevated => &escalation.elevated,
        RiskLevel::Critical => &escalation.critical,
    };

    // Return the stricter of the two
    checks::max_challenge(*base, *context_min)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::env::MockEnvironment;
    use std::collections::HashMap;

    fn default_config() -> ContextConfig {
        ContextConfig::default()
    }

    #[test]
    fn test_detect_normal_context() {
        let env = MockEnvironment {
            cwd: "/Users/dev/project".into(),
            ..Default::default()
        };
        let ctx = detect(&env, &default_config());
        assert_eq!(ctx.risk_level, RiskLevel::Normal);
        assert!(!ctx.is_ssh);
        assert!(!ctx.is_root);
    }

    #[test]
    fn test_detect_ssh_session() {
        let mut env_vars = HashMap::new();
        env_vars.insert("SSH_CONNECTION".into(), "10.0.0.1 22 10.0.0.2 54321".into());
        let env = MockEnvironment {
            env_vars,
            cwd: "/home/user".into(),
            ..Default::default()
        };
        let ctx = detect(&env, &default_config());
        assert!(ctx.is_ssh);
        assert_eq!(ctx.risk_level, RiskLevel::Elevated);
    }

    #[test]
    fn test_detect_root_user() {
        let mut env_vars = HashMap::new();
        env_vars.insert("EUID".into(), "0".into());
        let env = MockEnvironment {
            env_vars,
            cwd: "/root".into(),
            ..Default::default()
        };
        let ctx = detect(&env, &default_config());
        assert!(ctx.is_root);
        assert_eq!(ctx.risk_level, RiskLevel::Critical);
    }

    #[test]
    fn test_detect_protected_branch() {
        let mut cmd_outputs = HashMap::new();
        cmd_outputs.insert("git rev-parse --abbrev-ref HEAD".into(), "main".into());
        let env = MockEnvironment {
            command_outputs: cmd_outputs,
            cwd: "/repo".into(),
            ..Default::default()
        };
        let ctx = detect(&env, &default_config());
        assert_eq!(ctx.git_branch, Some("main".into()));
        assert_eq!(ctx.risk_level, RiskLevel::Critical);
    }

    #[test]
    fn test_detect_production_k8s() {
        let mut cmd_outputs = HashMap::new();
        cmd_outputs.insert(
            "kubectl config current-context".into(),
            "prod-us-east-1".into(),
        );
        let env = MockEnvironment {
            command_outputs: cmd_outputs,
            cwd: "/app".into(),
            ..Default::default()
        };
        let ctx = detect(&env, &default_config());
        assert_eq!(ctx.k8s_context, Some("prod-us-east-1".into()));
        assert_eq!(ctx.risk_level, RiskLevel::Critical);
    }

    #[test]
    fn test_detect_production_env() {
        let mut env_vars = HashMap::new();
        env_vars.insert("NODE_ENV".into(), "production".into());
        let env = MockEnvironment {
            env_vars,
            cwd: "/app".into(),
            ..Default::default()
        };
        let ctx = detect(&env, &default_config());
        assert_eq!(ctx.risk_level, RiskLevel::Critical);
        assert_eq!(ctx.env_signals, vec!["NODE_ENV=production"]);
    }

    #[test]
    fn test_feature_branch_is_normal() {
        let mut cmd_outputs = HashMap::new();
        cmd_outputs.insert(
            "git rev-parse --abbrev-ref HEAD".into(),
            "feature/my-thing".into(),
        );
        let env = MockEnvironment {
            command_outputs: cmd_outputs,
            cwd: "/repo".into(),
            ..Default::default()
        };
        let ctx = detect(&env, &default_config());
        assert_eq!(ctx.risk_level, RiskLevel::Normal);
    }

    #[test]
    fn test_release_wildcard_branch() {
        let mut cmd_outputs = HashMap::new();
        cmd_outputs.insert(
            "git rev-parse --abbrev-ref HEAD".into(),
            "release/v2.0".into(),
        );
        let env = MockEnvironment {
            command_outputs: cmd_outputs,
            cwd: "/repo".into(),
            ..Default::default()
        };
        let ctx = detect(&env, &default_config());
        assert_eq!(ctx.risk_level, RiskLevel::Critical);
    }

    #[test]
    fn test_escalate_challenge_normal() {
        let esc = EscalationConfig::default();
        assert_eq!(
            escalate_challenge(&Challenge::Math, RiskLevel::Normal, &esc),
            Challenge::Math
        );
    }

    #[test]
    fn test_escalate_challenge_elevated() {
        let esc = EscalationConfig::default();
        assert_eq!(
            escalate_challenge(&Challenge::Math, RiskLevel::Elevated, &esc),
            Challenge::Enter
        );
    }

    #[test]
    fn test_escalate_challenge_critical() {
        let esc = EscalationConfig::default();
        assert_eq!(
            escalate_challenge(&Challenge::Math, RiskLevel::Critical, &esc),
            Challenge::Yes
        );
    }

    #[test]
    fn test_escalate_cannot_lower() {
        let esc = EscalationConfig::default();
        // Yes is already stricter than Enter (elevated escalation)
        assert_eq!(
            escalate_challenge(&Challenge::Yes, RiskLevel::Elevated, &esc),
            Challenge::Yes
        );
    }

    // -----------------------------------------------------------------------
    // filter_for_groups tests
    // -----------------------------------------------------------------------

    fn full_context() -> RuntimeContext {
        RuntimeContext {
            is_ssh: true,
            is_root: false,
            git_branch: Some("main".into()),
            k8s_context: Some("prod-us-east-1".into()),
            env_signals: vec!["NODE_ENV=production".into()],
            risk_level: RiskLevel::Critical,
            labels: vec![
                "ssh=true".into(),
                "branch=main".into(),
                "k8s=prod-us-east-1".into(),
            ],
        }
    }

    #[test]
    fn test_filter_git_command_hides_k8s() {
        let ctx = full_context();
        let groups: std::collections::HashSet<&str> = ["git"].into_iter().collect();
        let filtered = ctx.filter_for_groups(&groups, &default_config());

        assert_eq!(filtered.git_branch, Some("main".into()));
        assert!(filtered.k8s_context.is_none());
        assert!(filtered.labels.contains(&"branch=main".to_string()));
        assert!(!filtered.labels.iter().any(|l| l.starts_with("k8s=")));
    }

    #[test]
    fn test_filter_k8s_command_hides_branch() {
        let ctx = full_context();
        let groups: std::collections::HashSet<&str> = ["kubernetes"].into_iter().collect();
        let filtered = ctx.filter_for_groups(&groups, &default_config());

        assert!(filtered.git_branch.is_none());
        assert_eq!(filtered.k8s_context, Some("prod-us-east-1".into()));
        assert!(!filtered.labels.iter().any(|l| l.starts_with("branch=")));
        assert!(filtered.labels.contains(&"k8s=prod-us-east-1".to_string()));
    }

    #[test]
    fn test_filter_fs_command_global_only() {
        let ctx = full_context();
        let groups: std::collections::HashSet<&str> = ["fs"].into_iter().collect();
        let filtered = ctx.filter_for_groups(&groups, &default_config());

        assert!(filtered.git_branch.is_none());
        assert!(filtered.k8s_context.is_none());
        assert!(filtered.is_ssh);
        assert!(filtered.labels.contains(&"ssh=true".to_string()));
        assert!(!filtered.labels.iter().any(|l| l.starts_with("branch=")));
        assert!(!filtered.labels.iter().any(|l| l.starts_with("k8s=")));
    }

    #[test]
    fn test_filter_compound_git_and_k8s() {
        let ctx = full_context();
        let groups: std::collections::HashSet<&str> = ["git", "kubernetes"].into_iter().collect();
        let filtered = ctx.filter_for_groups(&groups, &default_config());

        assert_eq!(filtered.git_branch, Some("main".into()));
        assert_eq!(filtered.k8s_context, Some("prod-us-east-1".into()));
        assert!(filtered.labels.contains(&"branch=main".to_string()));
        assert!(filtered.labels.contains(&"k8s=prod-us-east-1".to_string()));
    }

    #[test]
    fn test_filter_global_signals_never_hidden() {
        let ctx = RuntimeContext {
            is_ssh: true,
            is_root: true,
            git_branch: Some("main".into()),
            k8s_context: Some("prod".into()),
            env_signals: vec!["NODE_ENV=production".into()],
            risk_level: RiskLevel::Critical,
            labels: vec![
                "ssh=true".into(),
                "root=true".into(),
                "branch=main".into(),
                "k8s=prod".into(),
            ],
        };
        // Even with an unrelated group, SSH, root, and env_signals remain
        let groups: std::collections::HashSet<&str> = ["fs"].into_iter().collect();
        let filtered = ctx.filter_for_groups(&groups, &default_config());

        assert!(filtered.is_ssh);
        assert!(filtered.is_root);
        assert_eq!(filtered.env_signals, vec!["NODE_ENV=production"]);
        assert!(filtered.labels.contains(&"ssh=true".to_string()));
        assert!(filtered.labels.contains(&"root=true".to_string()));
    }

    #[test]
    fn test_filter_risk_level_recomputed() {
        // Context with only branch=main making it Critical
        let ctx = RuntimeContext {
            is_ssh: false,
            is_root: false,
            git_branch: Some("main".into()),
            k8s_context: None,
            env_signals: vec![],
            risk_level: RiskLevel::Critical,
            labels: vec!["branch=main".into()],
        };
        // Matched groups: {"fs"} — branch is irrelevant, so risk drops
        let groups: std::collections::HashSet<&str> = ["fs"].into_iter().collect();
        let filtered = ctx.filter_for_groups(&groups, &default_config());

        assert!(filtered.git_branch.is_none());
        assert_eq!(filtered.risk_level, RiskLevel::Normal);
    }
}