bashrs 6.66.0

Rust-to-Shell transpiler for deterministic bootstrap scripts
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
//! Installer Audit Command (#120)
//!
//! Security and quality review for installer specifications.
//!
//! This module provides comprehensive auditing capabilities:
//!
//! - **Security Audit**: Check signature requirements, trust model, privilege escalation
//! - **Quality Audit**: Validate idempotency, preconditions, postconditions
//! - **Hermetic Audit**: Verify lockfile integrity, reproducibility settings
//! - **Best Practices**: Check for common anti-patterns and recommendations
//!
//! # Example
//!
//! ```ignore
//! use bashrs::installer::{AuditContext, AuditReport, AuditSeverity};
//!
//! let ctx = AuditContext::new();
//! let report = ctx.audit_installer(&spec)?;
//!
//! if report.has_critical_issues() {
//!     eprintln!("Critical issues found!");
//! }
//! ```

use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};

/// Severity level for audit findings
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum AuditSeverity {
    /// Informational finding, no action required
    Info,
    /// Suggestion for improvement
    Suggestion,
    /// Warning that should be addressed
    Warning,
    /// Error that must be fixed
    Error,
    /// Critical security or reliability issue
    Critical,
}

impl AuditSeverity {
    /// Get display symbol for severity
    pub fn symbol(&self) -> &'static str {
        match self {
            AuditSeverity::Info => "",
            AuditSeverity::Suggestion => "💡",
            AuditSeverity::Warning => "",
            AuditSeverity::Error => "",
            AuditSeverity::Critical => "🚨",
        }
    }

    /// Get display name
    pub fn name(&self) -> &'static str {
        match self {
            AuditSeverity::Info => "INFO",
            AuditSeverity::Suggestion => "SUGGESTION",
            AuditSeverity::Warning => "WARNING",
            AuditSeverity::Error => "ERROR",
            AuditSeverity::Critical => "CRITICAL",
        }
    }
}

/// Category of audit finding
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum AuditCategory {
    /// Security-related finding
    Security,
    /// Quality/reliability finding
    Quality,
    /// Hermetic/reproducibility finding
    Hermetic,
    /// Best practices finding
    BestPractices,
    /// Configuration finding
    Configuration,
}

impl AuditCategory {
    /// Get display name
    pub fn name(&self) -> &'static str {
        match self {
            AuditCategory::Security => "Security",
            AuditCategory::Quality => "Quality",
            AuditCategory::Hermetic => "Hermetic",
            AuditCategory::BestPractices => "Best Practices",
            AuditCategory::Configuration => "Configuration",
        }
    }
}

/// An individual audit finding
#[derive(Debug, Clone)]
pub struct AuditFinding {
    /// Unique rule ID (e.g., "SEC001", "QUAL003")
    pub rule_id: String,
    /// Severity level
    pub severity: AuditSeverity,
    /// Category of finding
    pub category: AuditCategory,
    /// Short title
    pub title: String,
    /// Detailed description
    pub description: String,
    /// Location in the installer (step ID, artifact ID, etc.)
    pub location: Option<String>,
    /// Suggested fix
    pub suggestion: Option<String>,
    /// Related documentation URL
    pub doc_url: Option<String>,
}

impl AuditFinding {
    /// Create a new audit finding
    pub fn new(
        rule_id: impl Into<String>,
        severity: AuditSeverity,
        category: AuditCategory,
        title: impl Into<String>,
        description: impl Into<String>,
    ) -> Self {
        Self {
            rule_id: rule_id.into(),
            severity,
            category,
            title: title.into(),
            description: description.into(),
            location: None,
            suggestion: None,
            doc_url: None,
        }
    }

    /// Set location
    pub fn with_location(mut self, location: impl Into<String>) -> Self {
        self.location = Some(location.into());
        self
    }

    /// Set suggestion
    pub fn with_suggestion(mut self, suggestion: impl Into<String>) -> Self {
        self.suggestion = Some(suggestion.into());
        self
    }

    /// Set documentation URL
    pub fn with_doc_url(mut self, url: impl Into<String>) -> Self {
        self.doc_url = Some(url.into());
        self
    }

    /// Format finding for display
    pub fn format(&self) -> String {
        let mut output = format!(
            "{} [{}] {}: {}\n",
            self.severity.symbol(),
            self.rule_id,
            self.severity.name(),
            self.title
        );

        output.push_str(&format!("   {}\n", self.description));

        if let Some(ref loc) = self.location {
            output.push_str(&format!("   Location: {}\n", loc));
        }

        if let Some(ref suggestion) = self.suggestion {
            output.push_str(&format!("   Suggestion: {}\n", suggestion));
        }

        output
    }
}

/// Complete audit report
#[derive(Debug, Clone)]
pub struct AuditReport {
    /// Installer name
    pub installer_name: String,
    /// Installer version
    pub installer_version: String,
    /// Path to installer
    pub installer_path: PathBuf,
    /// All findings
    pub findings: Vec<AuditFinding>,
    /// Audit metadata
    pub metadata: AuditMetadata,
}

/// Audit metadata
#[derive(Debug, Clone)]
pub struct AuditMetadata {
    /// When the audit was performed
    pub audited_at: String,
    /// Number of steps audited
    pub steps_audited: usize,
    /// Number of artifacts audited
    pub artifacts_audited: usize,
    /// Audit duration in milliseconds
    pub duration_ms: u64,
}

impl AuditReport {
    /// Create a new empty report
    pub fn new(name: impl Into<String>, version: impl Into<String>, path: PathBuf) -> Self {
        Self {
            installer_name: name.into(),
            installer_version: version.into(),
            installer_path: path,
            findings: Vec::new(),
            metadata: AuditMetadata {
                audited_at: String::new(),
                steps_audited: 0,
                artifacts_audited: 0,
                duration_ms: 0,
            },
        }
    }

    /// Add a finding to the report
    pub fn add_finding(&mut self, finding: AuditFinding) {
        self.findings.push(finding);
    }

    /// Check if there are critical issues
    pub fn has_critical_issues(&self) -> bool {
        self.findings
            .iter()
            .any(|f| f.severity == AuditSeverity::Critical)
    }

    /// Check if there are errors or critical issues
    pub fn has_errors(&self) -> bool {
        self.findings
            .iter()
            .any(|f| f.severity >= AuditSeverity::Error)
    }

    /// Get findings by severity
    pub fn findings_by_severity(&self, severity: AuditSeverity) -> Vec<&AuditFinding> {
        self.findings
            .iter()
            .filter(|f| f.severity == severity)
            .collect()
    }

    /// Get findings by category
    pub fn findings_by_category(&self, category: AuditCategory) -> Vec<&AuditFinding> {
        self.findings
            .iter()
            .filter(|f| f.category == category)
            .collect()
    }

    /// Count findings by severity
    pub fn count_by_severity(&self) -> HashMap<AuditSeverity, usize> {
        let mut counts = HashMap::new();
        for finding in &self.findings {
            *counts.entry(finding.severity).or_insert(0) += 1;
        }
        counts
    }

    /// Get overall score (0-100)
    pub fn score(&self) -> u8 {
        let base_score = 100i32;
        let mut deductions = 0i32;

        for finding in &self.findings {
            deductions += match finding.severity {
                AuditSeverity::Info => 0,
                AuditSeverity::Suggestion => 1,
                AuditSeverity::Warning => 3,
                AuditSeverity::Error => 10,
                AuditSeverity::Critical => 25,
            };
        }

        (base_score - deductions).max(0) as u8
    }

    /// Get grade based on score
    pub fn grade(&self) -> &'static str {
        match self.score() {
            90..=100 => "A",
            80..=89 => "B",
            70..=79 => "C",
            60..=69 => "D",
            _ => "F",
        }
    }

    /// Format report for display
    pub fn format(&self) -> String {
        let mut output = String::new();

        output.push_str(&format!("Installer Audit Report\n{}\n\n", "".repeat(60)));

        output.push_str(&format!(
            "Installer: {} v{}\n",
            self.installer_name, self.installer_version
        ));
        output.push_str(&format!("Path: {}\n", self.installer_path.display()));
        output.push_str(&format!(
            "Audited: {} ({} steps, {} artifacts)\n\n",
            self.metadata.audited_at, self.metadata.steps_audited, self.metadata.artifacts_audited
        ));

        // Score summary
        let score = self.score();
        let grade = self.grade();
        output.push_str(&format!("Score: {}/100 (Grade: {})\n\n", score, grade));

        // Severity summary
        let counts = self.count_by_severity();
        output.push_str("Summary:\n");
        for severity in [
            AuditSeverity::Critical,
            AuditSeverity::Error,
            AuditSeverity::Warning,
            AuditSeverity::Suggestion,
            AuditSeverity::Info,
        ] {
            let count = counts.get(&severity).unwrap_or(&0);
            if *count > 0 {
                output.push_str(&format!(
                    "  {} {}: {}\n",
                    severity.symbol(),
                    severity.name(),
                    count
                ));
            }
        }
        output.push('\n');

        // Findings grouped by category
        let mut categories_seen: HashSet<AuditCategory> = HashSet::new();
        for finding in &self.findings {
            categories_seen.insert(finding.category);
        }

        for category in [
            AuditCategory::Security,
            AuditCategory::Quality,
            AuditCategory::Hermetic,
            AuditCategory::BestPractices,
            AuditCategory::Configuration,
        ] {
            if categories_seen.contains(&category) {
                output.push_str(&format!("{}\n{}\n", category.name(), "-".repeat(40)));
                for finding in self.findings_by_category(category) {
                    output.push_str(&finding.format());
                    output.push('\n');
                }
            }
        }

        output
    }

    /// Export to JSON
    pub fn to_json(&self) -> String {
        let findings_json: Vec<String> = self
            .findings
            .iter()
            .map(|f| {
                let location = f
                    .location
                    .as_ref()
                    .map_or_else(|| "null".to_string(), |l| format!("\"{}\"", l));
                let suggestion = f.suggestion.as_ref().map_or_else(
                    || "null".to_string(),
                    |s| format!("\"{}\"", s.replace('\"', "\\\"")),
                );

                format!(
                    r#"    {{
      "rule_id": "{}",
      "severity": "{}",
      "category": "{}",
      "title": "{}",
      "description": "{}",
      "location": {},
      "suggestion": {}
    }}"#,
                    f.rule_id,
                    f.severity.name(),
                    f.category.name(),
                    f.title.replace('\"', "\\\""),
                    f.description.replace('\"', "\\\""),
                    location,
                    suggestion
                )
            })
            .collect();

        format!(
            r#"{{
  "installer_name": "{}",
  "installer_version": "{}",
  "installer_path": "{}",
  "score": {},
  "grade": "{}",
  "metadata": {{
    "audited_at": "{}",
    "steps_audited": {},
    "artifacts_audited": {},
    "duration_ms": {}
  }},
  "findings": [
{}
  ]
}}"#,
            self.installer_name,
            self.installer_version,
            self.installer_path.display(),
            self.score(),
            self.grade(),
            self.metadata.audited_at,
            self.metadata.steps_audited,
            self.metadata.artifacts_audited,
            self.metadata.duration_ms,
            findings_json.join(",\n")
        )
    }
}

include!("audit_default.rs");