icebox-gov 0.2.6

Runtime governance for autonomous offensive security — the single seam every human operator, REST client, and LLM agent must pass through before anything touches a target.
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
use std::str::FromStr;
use tracing::info;

use crate::core::audit::HashChain;
use crate::core::gee::GeeStage;
use crate::core::module::{Capability, Intent, LoadedModule, ModuleError, ModuleResult};
use crate::core::safety::{
    make_config_policy, now_secs, Charter, ConfigPolicy, DecisionRecord, Evidence, MemoryEntry,
    MemoryKind, PolicyContext, PolicyDecision, PolicyEngine, PolicyRequest, PolicySet, Preflight,
    PreflightError, ReasoningTrace, RiskLevel, ScopeManager,
};
use crate::core::sandbox::Sandbox;
use crate::core::sdk::{ActionOutcome, GovernAction, GovernResult, RecordResult};

const MAX_EVIDENCE: usize = 2000;
const MAX_TRACES: usize = 1000;
const MAX_MEMORIES: usize = 1000;

#[derive(Debug, thiserror::Error)]
pub enum ExecutorError {
    #[error(transparent)]
    Preflight(#[from] PreflightError),
    #[error(transparent)]
    Module(#[from] ModuleError),
    #[error("sandbox error: {0}")]
    Sandbox(String),
}

/// The execution seam of ICEBOX and its fundamental execution primitive:
/// every action runs as a Governed Execution Environment (GEE). This type owns
/// the GEE lifecycle (policy evaluation, sandbox provisioning, approval
/// gating, execution, evidence collection, audit, validation, and teardown)
/// and keeps the state required to enforce it.
#[derive(Debug)]
pub struct ModuleExecutor {
    pub charter: Charter,
    pub scope: ScopeManager,
    pub max_risk: RiskLevel,
    pub policy_set: PolicySet,
    pub sandbox_required: bool,
    pub tier: crate::core::safety::Tier,
    pub audit: HashChain,
    pub evidence: Vec<Evidence>,
    pub traces: Vec<ReasoningTrace>,
    pub memories: Vec<MemoryEntry>,
    pub stage: GeeStage,
}

impl ModuleExecutor {
    pub fn new(charter: Charter, scope: ScopeManager, max_risk: RiskLevel) -> Self {
        ModuleExecutor {
            charter,
            scope,
            max_risk,
            policy_set: PolicySet::default(),
            sandbox_required: false,
            tier: crate::core::safety::Tier::Fridge,
            audit: HashChain::new(),
            evidence: Vec::new(),
            traces: Vec::new(),
            memories: Vec::new(),
            stage: GeeStage::Request,
        }
    }

    pub fn policy(&self, context: PolicyContext) -> ConfigPolicy {
        let mut policy = make_config_policy(self.max_risk, context, &self.policy_set);
        if let Some(thr) = self.tier.cvss_threshold() {
            policy
                .rules
                .add_rule(crate::core::safety::PolicyRule::DenyIfCvssAbove(thr));
        }
        policy
    }

    pub fn recent_traces(&self, n: usize) -> Vec<ReasoningTrace> {
        let end = self.traces.len();
        let start = end.saturating_sub(n);
        self.traces[start..].to_vec()
    }

    pub fn recent_memories(&self, n: usize) -> Vec<MemoryEntry> {
        let end = self.memories.len();
        let start = end.saturating_sub(n);
        self.memories[start..].to_vec()
    }

    pub fn remember(&mut self, kind: MemoryKind, text: impl Into<String>) {
        self.memories.push(MemoryEntry {
            at: now_secs(),
            kind,
            text: text.into(),
        });
        let overflow = self.memories.len().saturating_sub(MAX_MEMORIES);
        if overflow > 0 {
            self.memories.drain(..overflow);
        }
    }

    pub fn record_trace(&mut self, trace: ReasoningTrace) {
        self.traces.push(trace);
        let overflow = self.traces.len().saturating_sub(MAX_TRACES);
        if overflow > 0 {
            self.traces.drain(..overflow);
        }
    }

    pub fn recent_decisions(&self, n: usize) -> Vec<DecisionRecord> {
        self.audit.recent(n)
    }

    pub fn decisions(&self) -> Vec<DecisionRecord> {
        self.audit.records()
    }

    pub fn append_decision(&mut self, record: DecisionRecord) -> u64 {
        self.audit.append(record)
    }

    pub fn verify_audit(&self) -> bool {
        self.audit.verify()
    }

    pub fn audit_chain(&self) -> &HashChain {
        &self.audit
    }

    /// Pure governance check: evaluate policy, scope, and approval gates
    /// without executing the action. Returns a decision the caller acts upon.
    /// This is the single-entry "Stripe-style" govern() call.
    pub fn govern_action(&mut self, action: &GovernAction, context: PolicyContext) -> GovernResult {
        let capability = match Capability::from_str(&action.capability) {
            Ok(c) => c,
            Err(e) => {
                let decision_id = self.audit.append(DecisionRecord {
                    at: now_secs(),
                    target: action.target.clone(),
                    module: action.action.clone(),
                    capabilities: vec![],
                    intents: vec![],
                    impact: action.impact,
                    context,
                    decision: PolicyDecision::Deny(format!("unknown capability: {e}")),
                });
                return GovernResult {
                    approved: false,
                    decision: "deny".into(),
                    reason: Some(format!("unknown capability: {}", action.capability)),
                    decision_id,
                    chain_tip: self.audit.tip_hex(),
                };
            }
        };

        let in_scope = self.scope.is_in_scope(&action.target);

        let req = PolicyRequest {
            target: action.target.clone(),
            capabilities: vec![capability],
            impact: action.impact,
            destructive: action.destructive,
            charter_accepted: self.charter.accepted,
            in_scope,
            approved: false,
            context,
            cvss: None,
        };

        let policy = self.policy(context);
        let decision = policy.evaluate(&req);

        let intents: Vec<Intent> = req.capabilities.iter().map(|c| c.intent()).collect();

        let decision_id = self.audit.append(DecisionRecord {
            at: now_secs(),
            target: action.target.clone(),
            module: action.action.clone(),
            capabilities: req.capabilities.clone(),
            intents,
            impact: action.impact,
            context,
            decision: decision.clone(),
        });

        match decision {
            PolicyDecision::Deny(reason) => GovernResult {
                approved: false,
                decision: "deny".into(),
                reason: Some(reason),
                decision_id,
                chain_tip: self.audit.tip_hex(),
            },
            PolicyDecision::RequireApproval(reason) => GovernResult {
                approved: false,
                decision: "require_approval".into(),
                reason: Some(reason),
                decision_id,
                chain_tip: self.audit.tip_hex(),
            },
            PolicyDecision::Allow => GovernResult {
                approved: true,
                decision: "allow".into(),
                reason: None,
                decision_id,
                chain_tip: self.audit.tip_hex(),
            },
        }
    }

    /// Record completion of a prior governed action.
    /// Appends evidence and an audit-chain entry, then returns the chain tip.
    pub fn record_action(&mut self, action: &GovernAction, outcome: ActionOutcome) -> RecordResult {
        for content in &outcome.evidence {
            let seq = self.evidence.len();
            self.evidence.push(Evidence::new(
                &action.action,
                &action.target,
                content,
                None,
                seq,
            ));
        }

        let capability = Capability::from_str(&action.capability).unwrap_or(Capability::NetworkScan);
        let intents = vec![capability.intent()];

        let decision_id = self.audit.append(DecisionRecord {
            at: now_secs(),
            target: action.target.clone(),
            module: action.action.clone(),
            capabilities: vec![capability],
            intents,
            impact: action.impact,
            context: PolicyContext::Rest,
            decision: PolicyDecision::Allow,
        });

        RecordResult {
            decision_id,
            chain_tip: self.audit.tip_hex(),
        }
    }

    pub fn recent_evidence(&self, n: usize) -> Vec<Evidence> {
        let end = self.evidence.len();
        let start = end.saturating_sub(n);
        self.evidence[start..].to_vec()
    }

    pub async fn preflight(
        &self,
        loaded: &LoadedModule,
        target: &str,
        destructive_override: Option<bool>,
        approved: bool,
        context: PolicyContext,
    ) -> Preflight {
        let destructive = destructive_override
            .unwrap_or_else(|| loaded.info.effective_intents().contains(&Intent::Modify));

        Preflight {
            target: target.to_string(),
            charter_accepted: self.charter.accepted,
            in_scope: self.scope.is_in_scope(target),
            risk: loaded.info.effective_impact(),
            destructive,
            approved,
            capabilities: loaded.info.capabilities.clone(),
            intents: loaded.info.effective_intents(),
            context,
            cvss: None,
        }
    }

    #[allow(clippy::too_many_arguments)]
    pub async fn execute(
        &mut self,
        loaded: &mut LoadedModule,
        target: &str,
        destructive_override: Option<bool>,
        approved: bool,
        context: PolicyContext,
        job_id: Option<u64>,
        sandbox: bool,
        engine: Option<crate::core::sandbox::SandboxEngineType>,
    ) -> Result<ModuleResult, ExecutorError> {
        self.stage = GeeStage::PolicyEvaluation;
        let pf = self
            .preflight(loaded, target, destructive_override, approved, context)
            .await;
        let policy = self.policy(context);
        let decision = policy.evaluate(&pf.to_request());
        self.record_decision(&loaded.info.name, &pf, &decision);
        pf.check(&policy)?;

        self.stage = GeeStage::ScopeEnforcement;
        if !self.scope.is_in_scope(target) {
            let reason = format!("target {target} is out of scope");
            self.record_decision(
                &loaded.info.name,
                &pf,
                &PolicyDecision::Deny(reason.clone()),
            );
            return Err(ExecutorError::Preflight(PreflightError::Denied(reason)));
        }

        self.stage = GeeStage::SandboxProvisioning;
        if (self.sandbox_required || self.tier.requires_sandbox()) && !sandbox {
            let reason = format!("operational tier {} requires sandbox isolation", self.tier);
            self.record_decision(
                &loaded.info.name,
                &pf,
                &PolicyDecision::Deny(reason.clone()),
            );
            return Err(ExecutorError::Preflight(PreflightError::Denied(reason)));
        }

        self.stage = GeeStage::ApprovalCheck;
        if self.tier.requires_explicit_approval() && !approved {
            let reason = format!(
                "operational tier {} requires explicit operator approval",
                self.tier
            );
            self.record_decision(
                &loaded.info.name,
                &pf,
                &PolicyDecision::RequireApproval(reason.clone()),
            );
            return Err(ExecutorError::Preflight(PreflightError::ApprovalRequired));
        }

        self.stage = GeeStage::Execute;
        info!(
            target = %target,
            module = %loaded.info.name,
            risk = %pf.risk.as_str(),
            destructive = pf.destructive,
            sandbox = sandbox,
            stage = %self.stage.as_str(),
            "governed execution: preflight passed"
        );

        if policy.has_deny_payload() {
            if let Ok(preview) = loaded.module.dry_run().await {
                let denied = policy.denied_payload(&preview);
                if !denied.is_empty() {
                    let reason =
                        format!("payload matched denied pattern (pre-execution): {denied}");
                    self.record_decision(
                        &loaded.info.name,
                        &pf,
                        &PolicyDecision::Deny(reason.clone()),
                    );
                    return Ok(ModuleResult {
                        success: false,
                        evidence: vec![format!("[BLOCKED:payload] {denied}")],
                        data: serde_json::Value::Null,
                        ..Default::default()
                    });
                }
            }
        }

        let (result, active_sandbox): (ModuleResult, Option<Sandbox>) = if sandbox {
            let (r, s) = self
                .run_sandboxed(
                    loaded,
                    target,
                    engine.unwrap_or(crate::core::sandbox::SandboxEngineType::Docker),
                    context,
                )
                .await?;
            if r.error.is_some() {
                self.record_failure(
                    &loaded.info.name,
                    target,
                    r.error.as_deref().unwrap_or("sandbox failure"),
                    context,
                );
            }
            (r, Some(s))
        } else {
            match loaded.module.run().await {
                Ok(r) => (r, None),
                Err(e) => {
                    let reason = format!("module execution failed: {e}");
                    self.record_decision(
                        &loaded.info.name,
                        &pf,
                        &PolicyDecision::Deny(reason.clone()),
                    );
                    return Err(ExecutorError::Module(e));
                }
            }
        };

        self.stage = GeeStage::CollectEvidence;
        let mut result = result;
        if let Some(ref s) = active_sandbox {
            let logs = s.capture_logs().await;
            result.evidence.extend(logs);
        }
        let denied = policy.denied_payload(&result);
        if !denied.is_empty() {
            let reason = format!("payload matched denied pattern: {denied}");
            self.record_decision(
                &loaded.info.name,
                &pf,
                &PolicyDecision::Deny(reason.clone()),
            );
            let mut blocked = result;
            blocked.success = false;
            blocked.evidence.push(format!("[BLOCKED:payload] {denied}"));
            blocked.data = serde_json::Value::Null;
            self.record_evidence(&loaded.info.name, target, &blocked, job_id);
            self.stage = GeeStage::Audit;
            self.stage = GeeStage::Validate;
            let _ = self.audit.verify();
            self.stage = GeeStage::Destroy;
            if let Some(s) = active_sandbox {
                let _ = s.melt().await;
            }
            return Ok(blocked);
        }

        self.record_evidence(&loaded.info.name, target, &result, job_id);
        self.stage = GeeStage::Audit;
        self.stage = GeeStage::Validate;
        let _ = self.audit.verify();
        self.stage = GeeStage::Destroy;
        if let Some(s) = active_sandbox {
            let _ = s.melt().await;
        }
        Ok(result)
    }

    fn record_decision(
        &mut self,
        module: &str,
        pf: &Preflight,
        decision: &crate::core::safety::PolicyDecision,
    ) {
        self.audit.append(DecisionRecord {
            at: now_secs(),
            target: pf.target.clone(),
            module: module.to_string(),
            capabilities: pf.capabilities.clone(),
            intents: pf.intents.clone(),
            impact: pf.risk,
            context: pf.context,
            decision: decision.clone(),
        });
    }

    fn record_evidence(
        &mut self,
        module: &str,
        target: &str,
        result: &ModuleResult,
        job_id: Option<u64>,
    ) {
        let seq_start = self.evidence.len();
        for (i, content) in result.evidence.iter().enumerate() {
            self.evidence.push(Evidence::new(
                module,
                target,
                content,
                job_id,
                seq_start + i,
            ));
        }
        let overflow = self.evidence.len().saturating_sub(MAX_EVIDENCE);
        if overflow > 0 {
            self.evidence.drain(..overflow);
        }
    }

    async fn run_sandboxed(
        &mut self,
        loaded: &crate::core::module::LoadedModule,
        target: &str,
        engine: crate::core::sandbox::SandboxEngineType,
        context: PolicyContext,
    ) -> Result<(ModuleResult, Sandbox), ExecutorError> {
        let image = "icebox-sandbox:latest".to_string();
        let module_name = loaded.info.name.clone();
        match Sandbox::freeze(engine, target, &image).await {
            Ok(sandbox) => {
                info!(
                    container = %sandbox.container_id(),
                    stage = %GeeStage::SandboxProvisioning.as_str(),
                    "[SANDBOX] container frozen"
                );
                let options = loaded.module.options_json();
                let result = match sandbox
                    .exec_module(&loaded.info.name, target, &options)
                    .await
                {
                    Ok(r) => r,
                    Err(e) => ModuleResult {
                        error: Some(format!("sandbox module exec failed: {e}")),
                        ..Default::default()
                    },
                };
                Ok((result, sandbox))
            }
            Err(e) => {
                let reason = format!("Sandbox initialization failed: {e}. Isolation is mandatory.");
                self.record_failure(&module_name, target, &reason, context);
                Err(ExecutorError::Sandbox(reason))
            }
        }
    }

    fn record_failure(&mut self, module: &str, target: &str, reason: &str, context: PolicyContext) {
        self.audit.append(DecisionRecord {
            at: now_secs(),
            target: target.to_string(),
            module: module.to_string(),
            capabilities: Vec::new(),
            intents: Vec::new(),
            impact: RiskLevel::None,
            context,
            decision: PolicyDecision::Deny(reason.to_string()),
        });
    }
}