destructive_command_guard 0.4.3

A Claude Code hook that blocks destructive commands before they execute
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
//! Statistics collection and display for dcg.
//!
//! This module provides functionality to:
//! - Parse log files (both text and JSON formats)
//! - Aggregate statistics by pack
//! - Display statistics for a configurable time period

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::Path;
use std::time::{SystemTime, UNIX_EPOCH};

/// Default time period for statistics (30 days in seconds).
pub const DEFAULT_PERIOD_SECS: u64 = 30 * 24 * 60 * 60;

/// A single log entry parsed from the log file.
#[derive(Debug, Clone)]
pub struct ParsedLogEntry {
    pub timestamp: u64,
    pub decision: Decision,
    pub pack_id: Option<String>,
    pub pattern_name: Option<String>,
    pub command: Option<String>,
    pub allowlist_override: bool,
}

/// Decision type from log entries.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Decision {
    Allow,
    Deny,
    Warn,
    Bypass,
}

/// Statistics for a single pack.
#[derive(Debug, Clone, Default, Serialize)]
pub struct PackStats {
    pub pack_id: String,
    pub blocks: u64,
    pub allows: u64,
    pub bypasses: u64,
    pub warns: u64,
}

impl PackStats {
    #[allow(clippy::missing_const_for_fn)] // String is not const-compatible
    fn new(pack_id: String) -> Self {
        Self {
            pack_id,
            blocks: 0,
            allows: 0,
            bypasses: 0,
            warns: 0,
        }
    }

    #[allow(clippy::missing_const_for_fn)]
    fn record(&mut self, decision: Decision, allowlist_override: bool) {
        match decision {
            Decision::Deny if allowlist_override => self.bypasses += 1,
            Decision::Deny => self.blocks += 1,
            Decision::Allow => self.allows += 1,
            Decision::Warn => self.warns += 1,
            Decision::Bypass => self.bypasses += 1,
        }
    }
}

/// Aggregated statistics across all packs.
#[derive(Debug, Clone, Default, Serialize)]
pub struct AggregatedStats {
    pub period_start: u64,
    pub period_end: u64,
    pub total_entries: u64,
    pub total_blocks: u64,
    pub total_allows: u64,
    pub total_bypasses: u64,
    pub total_warns: u64,
    pub by_pack: Vec<PackStats>,
}

impl AggregatedStats {
    /// Calculate totals from pack stats.
    pub fn calculate_totals(&mut self) {
        self.total_blocks = self.by_pack.iter().map(|p| p.blocks).sum();
        self.total_allows = self.by_pack.iter().map(|p| p.allows).sum();
        self.total_bypasses = self.by_pack.iter().map(|p| p.bypasses).sum();
        self.total_warns = self.by_pack.iter().map(|p| p.warns).sum();
    }
}

/// JSON log entry format (for structured logging).
#[derive(Debug, Deserialize)]
struct JsonLogEntry {
    timestamp: String,
    decision: String,
    #[serde(default)]
    pack_id: Option<String>,
    #[serde(default)]
    pattern_name: Option<String>,
    #[serde(default)]
    command: Option<String>,
    #[serde(default)]
    allowlist_layer: Option<String>,
}

/// Parse a log file and return aggregated statistics.
///
/// # Arguments
/// * `path` - Path to the log file
/// * `period_secs` - Time period in seconds (from now backwards)
///
/// # Errors
/// Returns an error if the file cannot be read.
pub fn parse_log_file(path: &Path, period_secs: u64) -> std::io::Result<AggregatedStats> {
    let file = File::open(path)?;
    let reader = BufReader::new(file);

    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs();
    let cutoff = now.saturating_sub(period_secs);

    let mut pack_stats: HashMap<String, PackStats> = HashMap::new();
    let mut total_entries = 0u64;

    for line in reader.lines() {
        let line = line?;
        let trimmed = line.trim();

        if trimmed.is_empty() {
            continue;
        }

        // Try to parse as JSON first
        if trimmed.starts_with('{') {
            if let Some(entry) = parse_json_entry(trimmed, cutoff) {
                total_entries += 1;
                let pack_id = entry
                    .pack_id
                    .clone()
                    .unwrap_or_else(|| "unknown".to_string());
                let stats = pack_stats
                    .entry(pack_id.clone())
                    .or_insert_with(|| PackStats::new(pack_id));
                stats.record(entry.decision, entry.allowlist_override);
            }
            continue;
        }

        // Try to parse as text format from structured logging
        // Format: [timestamp] DECISION rule_id "command" -- reason
        if let Some(entry) = parse_text_entry(trimmed, cutoff) {
            total_entries += 1;
            let pack_id = entry
                .pack_id
                .clone()
                .unwrap_or_else(|| "unknown".to_string());
            let stats = pack_stats
                .entry(pack_id.clone())
                .or_insert_with(|| PackStats::new(pack_id));
            stats.record(entry.decision, entry.allowlist_override);
            continue;
        }

        // Try to parse as simple log format from hook.rs
        // Format: [timestamp] [pack] reason
        if let Some((ts, pack)) = parse_simple_header(trimmed) {
            if ts >= cutoff {
                total_entries += 1;
                let stats = pack_stats
                    .entry(pack.clone())
                    .or_insert_with(|| PackStats::new(pack));
                stats.record(Decision::Deny, false);
            }
        }
    }

    // Sort packs by block count descending
    let mut by_pack: Vec<PackStats> = pack_stats.into_values().collect();
    by_pack.sort_by_key(|p| std::cmp::Reverse(p.blocks));

    let mut stats = AggregatedStats {
        period_start: cutoff,
        period_end: now,
        total_entries,
        by_pack,
        ..Default::default()
    };
    stats.calculate_totals();

    Ok(stats)
}

/// Parse a JSON log entry.
fn parse_json_entry(line: &str, cutoff: u64) -> Option<ParsedLogEntry> {
    let entry: JsonLogEntry = serde_json::from_str(line).ok()?;

    // Parse timestamp (ISO 8601 or Unix epoch)
    let timestamp = parse_timestamp(&entry.timestamp)?;
    if timestamp < cutoff {
        return None;
    }

    let decision = match entry.decision.to_lowercase().as_str() {
        "deny" => Decision::Deny,
        "warn" => Decision::Warn,
        "allow" | "log" => Decision::Allow,
        "bypass" => Decision::Bypass,
        _ => return None,
    };

    Some(ParsedLogEntry {
        timestamp,
        decision,
        pack_id: entry.pack_id,
        pattern_name: entry.pattern_name,
        command: entry.command,
        allowlist_override: entry.allowlist_layer.is_some(),
    })
}

/// Parse a text log entry from structured logging.
fn parse_text_entry(line: &str, cutoff: u64) -> Option<ParsedLogEntry> {
    // Format: [timestamp] DECISION rule_id "command" -- reason
    if !line.starts_with('[') {
        return None;
    }

    let close_bracket = line.find(']')?;
    let timestamp_str = &line[1..close_bracket];
    let timestamp = parse_timestamp(timestamp_str)?;
    if timestamp < cutoff {
        return None;
    }

    let rest = line[close_bracket + 1..].trim();
    let mut parts = rest.split_whitespace();

    let decision_str = parts.next()?;
    let decision = match decision_str.to_uppercase().as_str() {
        "DENY" => Decision::Deny,
        "WARN" => Decision::Warn,
        "ALLOW" | "LOG" => Decision::Allow,
        "BYPASS" => Decision::Bypass,
        _ => return None,
    };

    // Extract rule_id (pack_id:pattern_name)
    let rule_id = parts.next();
    let (pack_id, pattern_name) = rule_id
        .filter(|r| r.contains(':'))
        .map_or((None, None), |r| {
            let mut split = r.splitn(2, ':');
            (
                split.next().map(String::from),
                split.next().map(String::from),
            )
        });

    // Check for allowlist marker
    let allowlist_override = line.contains("[allowlist:");

    Some(ParsedLogEntry {
        timestamp,
        decision,
        pack_id,
        pattern_name,
        command: None,
        allowlist_override,
    })
}

/// Parse simple log header from hook.rs format.
/// Format: [timestamp] [pack] reason
fn parse_simple_header(line: &str) -> Option<(u64, String)> {
    if !line.starts_with('[') {
        return None;
    }

    // Find first timestamp bracket
    let first_close = line.find(']')?;
    let timestamp_str = &line[1..first_close];
    let timestamp = parse_timestamp(timestamp_str)?;

    // Find second bracket (pack)
    let rest = &line[first_close + 1..].trim_start();
    if !rest.starts_with('[') {
        return None;
    }
    let pack_close = rest.find(']')?;
    let pack = rest[1..pack_close].to_string();

    // Skip "budget" entries
    if pack == "budget" {
        return None;
    }

    Some((timestamp, pack))
}

/// Parse a timestamp string into Unix epoch seconds.
fn parse_timestamp(s: &str) -> Option<u64> {
    // Try parsing as Unix epoch (just digits)
    if let Ok(ts) = s.parse::<u64>() {
        return Some(ts);
    }

    // Try parsing as ISO 8601 / RFC 3339
    // Format: 2024-01-15T10:30:00Z or 2024-01-15T10:30:00+00:00
    if let Ok(dt) = chrono::DateTime::parse_from_rfc3339(s) {
        return u64::try_from(dt.timestamp()).ok();
    }

    // Try without timezone (assume UTC)
    if let Ok(dt) = chrono::NaiveDateTime::parse_from_str(s, "%Y-%m-%dT%H:%M:%S") {
        return u64::try_from(dt.and_utc().timestamp()).ok();
    }

    None
}

/// Format statistics for display.
#[must_use]
pub fn format_stats_pretty(stats: &AggregatedStats, period_days: u64) -> String {
    use std::fmt::Write;

    let mut output = String::new();

    let _ = writeln!(output, "Pack Statistics (last {period_days} days):");
    let _ = writeln!(output);

    if stats.by_pack.is_empty() {
        let _ = writeln!(output, "  No events recorded in this period.");
        return output;
    }

    // Calculate column widths
    let max_pack_len = stats
        .by_pack
        .iter()
        .map(|p| p.pack_id.len())
        .max()
        .unwrap_or(10)
        .max(10);

    // Header
    let _ = writeln!(
        output,
        "  {:<width$}  {:>7}  {:>7}  {:>8}  {:>6}",
        "Pack",
        "Blocks",
        "Allows",
        "Bypasses",
        "Warns",
        width = max_pack_len
    );
    let _ = writeln!(
        output,
        "  {:-<width$}  {:->7}  {:->7}  {:->8}  {:->6}",
        "",
        "",
        "",
        "",
        "",
        width = max_pack_len
    );

    // Pack rows
    for pack in &stats.by_pack {
        let _ = writeln!(
            output,
            "  {:<width$}  {:>7}  {:>7}  {:>8}  {:>6}",
            pack.pack_id,
            pack.blocks,
            pack.allows,
            pack.bypasses,
            pack.warns,
            width = max_pack_len
        );
    }

    // Total row
    let _ = writeln!(
        output,
        "  {:-<width$}  {:->7}  {:->7}  {:->8}  {:->6}",
        "",
        "",
        "",
        "",
        "",
        width = max_pack_len
    );
    let _ = writeln!(
        output,
        "  {:<width$}  {:>7}  {:>7}  {:>8}  {:>6}",
        "Total",
        stats.total_blocks,
        stats.total_allows,
        stats.total_bypasses,
        stats.total_warns,
        width = max_pack_len
    );

    output
}

/// Format statistics as JSON.
#[must_use]
pub fn format_stats_json(stats: &AggregatedStats) -> String {
    serde_json::to_string_pretty(stats).unwrap_or_else(|_| "{}".to_string())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::NamedTempFile;

    #[test]
    fn test_parse_timestamp_unix() {
        assert_eq!(parse_timestamp("1704672000"), Some(1_704_672_000));
    }

    #[test]
    fn test_parse_timestamp_iso8601() {
        assert!(parse_timestamp("2024-01-15T10:30:00Z").is_some());
    }

    #[test]
    fn test_parse_simple_header() {
        let line = "[1704672000] [core.git] git reset --hard is dangerous";
        let result = parse_simple_header(line);
        assert!(result.is_some());
        let (ts, pack) = result.unwrap();
        assert_eq!(ts, 1_704_672_000);
        assert_eq!(pack, "core.git");
    }

    #[test]
    fn test_parse_json_entry() {
        let json = r#"{"timestamp":"1704672000","decision":"deny","pack_id":"core.git","pattern_name":"reset-hard"}"#;
        let entry = parse_json_entry(json, 0);
        assert!(entry.is_some());
        let entry = entry.unwrap();
        assert_eq!(entry.decision, Decision::Deny);
        assert_eq!(entry.pack_id, Some("core.git".to_string()));
    }

    #[test]
    fn test_parse_json_entry_log_maps_to_allow() {
        let json = r#"{"timestamp":"1704672000","decision":"log","pack_id":"core.git"}"#;
        let entry = parse_json_entry(json, 0);
        assert!(entry.is_some());
        let entry = entry.unwrap();
        assert_eq!(entry.decision, Decision::Allow);
        assert_eq!(entry.pack_id, Some("core.git".to_string()));
    }

    #[test]
    fn test_parse_text_entry() {
        let line =
            "[2024-01-15T10:30:00Z] DENY core.git:reset-hard \"git reset --hard\" -- dangerous";
        let entry = parse_text_entry(line, 0);
        assert!(entry.is_some());
        let entry = entry.unwrap();
        assert_eq!(entry.decision, Decision::Deny);
        assert_eq!(entry.pack_id, Some("core.git".to_string()));
    }

    #[test]
    fn test_parse_text_entry_log_maps_to_allow() {
        let line = "[2024-01-15T10:30:00Z] LOG core.git:reset-hard \"git reset --hard\" -- logged";
        let entry = parse_text_entry(line, 0);
        assert!(entry.is_some());
        let entry = entry.unwrap();
        assert_eq!(entry.decision, Decision::Allow);
        assert_eq!(entry.pack_id, Some("core.git".to_string()));
    }

    #[test]
    fn test_aggregate_stats() {
        let mut file = NamedTempFile::new().unwrap();
        writeln!(file, "[1704672000] [core.git] blocked git reset --hard").unwrap();
        writeln!(file, "  Command: git reset --hard HEAD").unwrap();
        writeln!(file).unwrap();
        writeln!(file, "[1704672100] [core.rm] blocked rm -rf").unwrap();
        writeln!(file, "  Command: rm -rf /").unwrap();
        writeln!(file).unwrap();
        writeln!(file, "[1704672000] [core.git] blocked git push --force").unwrap();
        writeln!(file, "  Command: git push --force").unwrap();

        // Use very long period to include all entries
        let stats = parse_log_file(file.path(), u64::MAX).unwrap();
        assert_eq!(stats.total_entries, 3);
        assert_eq!(stats.total_blocks, 3);
    }

    #[test]
    fn test_format_stats_pretty() {
        let stats = AggregatedStats {
            period_start: 0,
            period_end: 1_704_672_000,
            total_entries: 5,
            total_blocks: 3,
            total_allows: 1,
            total_bypasses: 1,
            total_warns: 0,
            by_pack: vec![
                PackStats {
                    pack_id: "core.git".to_string(),
                    blocks: 2,
                    allows: 1,
                    bypasses: 0,
                    warns: 0,
                },
                PackStats {
                    pack_id: "core.rm".to_string(),
                    blocks: 1,
                    allows: 0,
                    bypasses: 1,
                    warns: 0,
                },
            ],
        };

        let output = format_stats_pretty(&stats, 30);
        assert!(output.contains("core.git"));
        assert!(output.contains("core.rm"));
        assert!(output.contains("Total"));
    }
}