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
//! Adversarial training data generator for shell safety classifier.
//!
//! Produces parametrically-varied shell scripts for each underrepresented safety class
//! (non-deterministic, non-idempotent, unsafe, needs-quoting), verified against the
//! existing `derive_safety_label` heuristic for self-consistency.

#[cfg(feature = "clap")]
use crate::cli::args::ClassifyFormat;
use crate::corpus::adversarial_templates::{
    self, AdversarialTemplate, COMMENTS, SETUP_LINES, SHEBANGS, TRAILING_LINES,
};
use crate::corpus::dataset::{ClassificationRow, SAFETY_LABELS};
use rand::prelude::IndexedRandom;
use rand::Rng;
use rand::SeedableRng;
use rand_chacha::ChaCha8Rng;

/// Configuration for adversarial data generation.
#[derive(Debug, Clone)]
pub struct AdversarialConfig {
    /// RNG seed for reproducibility
    pub seed: u64,
    /// Number of samples per minority class (2, 3, 4)
    pub count_per_class: usize,
    /// Extra needs-quoting samples (class 1)
    pub extra_needs_quoting: usize,
    /// Verify each generated script against derive_safety_label
    pub verify: bool,
}

/// Statistics from a generation run.
#[derive(Debug, Clone, Default)]
pub struct GenerationStats {
    /// Total scripts generated
    pub total: usize,
    /// Per-class counts [safe, needs-quoting, non-det, non-idem, unsafe]
    pub per_class: [usize; 5],
    /// Number of scripts that failed self-consistency verification
    pub misclassified: usize,
    /// Per-class mismatch counts
    pub misclassified_per_class: [usize; 5],
}

/// Result of an adversarial generation run.
#[derive(Debug)]
pub struct GenerationResult {
    /// Generated classification rows
    pub rows: Vec<ClassificationRow>,
    /// Generation statistics
    pub stats: GenerationStats,
}

/// Generate adversarial training data for the shell safety classifier.
///
/// Produces `count_per_class` samples for classes 2, 3, 4 and
/// `extra_needs_quoting` additional samples for class 1.
pub fn generate_adversarial(config: &AdversarialConfig) -> GenerationResult {
    let mut rng = ChaCha8Rng::seed_from_u64(config.seed);
    let mut rows = Vec::new();
    let mut stats = GenerationStats::default();

    let nondet_templates = adversarial_templates::non_deterministic_templates();
    let nonidem_templates = adversarial_templates::non_idempotent_templates();
    let unsafe_templates = adversarial_templates::unsafe_templates();
    let quoting_templates = adversarial_templates::needs_quoting_templates();

    // Class 2: non-deterministic
    generate_class_samples(
        &nondet_templates,
        2,
        config.count_per_class,
        config.verify,
        &mut rng,
        &mut rows,
        &mut stats,
    );

    // Class 3: non-idempotent
    generate_class_samples(
        &nonidem_templates,
        3,
        config.count_per_class,
        config.verify,
        &mut rng,
        &mut rows,
        &mut stats,
    );

    // Class 4: unsafe
    generate_class_samples(
        &unsafe_templates,
        4,
        config.count_per_class,
        config.verify,
        &mut rng,
        &mut rows,
        &mut stats,
    );

    // Class 1: needs-quoting (extra samples)
    generate_class_samples(
        &quoting_templates,
        1,
        config.extra_needs_quoting,
        config.verify,
        &mut rng,
        &mut rows,
        &mut stats,
    );

    stats.total = rows.len();

    GenerationResult { rows, stats }
}

/// Generate samples for a single class, distributing evenly across templates.
fn generate_class_samples(
    templates: &[AdversarialTemplate],
    target_class: u8,
    count: usize,
    verify: bool,
    rng: &mut ChaCha8Rng,
    rows: &mut Vec<ClassificationRow>,
    stats: &mut GenerationStats,
) {
    if templates.is_empty() || count == 0 {
        return;
    }

    let per_template = count / templates.len();
    let remainder = count % templates.len();

    for (idx, template) in templates.iter().enumerate() {
        let n = per_template + usize::from(idx < remainder);
        for _ in 0..n {
            let script = expand_template(rng, template);
            let label = if verify {
                let verified = verify_classification(&script, target_class);
                if !verified {
                    stats.misclassified += 1;
                    stats.misclassified_per_class[target_class as usize] += 1;
                }
                target_class
            } else {
                target_class
            };
            stats.per_class[label as usize] += 1;
            rows.push(ClassificationRow {
                input: script,
                label,
            });
        }
    }
}

/// Expand a template by substituting parameters and adding context.
fn expand_template(rng: &mut ChaCha8Rng, template: &AdversarialTemplate) -> String {
    let mut body = template.template.to_string();

    // Substitute each parameter
    for param in template.params {
        if let Some(value) = param.pool.choose(rng) {
            let placeholder = format!("{{{}}}", param.name);
            body = body.replace(&placeholder, value);
        }
    }

    // Build script with context wrapping
    let mut parts = Vec::new();

    // Shebang
    if let Some(shebang) = SHEBANGS.choose(rng) {
        parts.push(shebang.to_string());
    }

    // Optional comment (70% chance)
    if rng.random_range(0..10) < 7 {
        if let Some(comment) = COMMENTS.choose(rng) {
            parts.push(comment.to_string());
        }
    }

    // Optional setup line (50% chance)
    if rng.random_range(0..10) < 5 {
        if let Some(setup) = SETUP_LINES.choose(rng) {
            if !setup.is_empty() {
                parts.push(setup.to_string());
            }
        }
    }

    // Template body
    parts.push(body);

    // Optional trailing line (40% chance)
    if rng.random_range(0..10) < 4 {
        if let Some(trailing) = TRAILING_LINES.choose(rng) {
            if !trailing.is_empty() {
                parts.push(trailing.to_string());
            }
        }
    }

    parts.join("\n")
}

/// Verify that a generated script classifies to the expected class.
///
/// Uses the same `analyze_lint` + `derive_safety_label` pipeline as the
/// classify command to ensure self-consistency.
#[cfg(feature = "clap")]
fn verify_classification(script: &str, expected_class: u8) -> bool {
    use crate::cli::commands::classify_cmds::analyze_lint;
    use crate::corpus::dataset::derive_safety_label;

    let signals = analyze_lint(script, &ClassifyFormat::Bash);

    let lint_clean = !signals.has_security_issues;
    let deterministic = !signals.has_determinism_issues;

    // For adversarial data, scripts are raw shell (not transpiler output)
    // so we pass transpiled=true to avoid everything being forced to class 4
    let actual_class = derive_safety_label(script, true, lint_clean, deterministic);

    actual_class == expected_class
}

/// Stub when classify CLI is not available (minimal build).
#[cfg(not(feature = "clap"))]
fn verify_classification(_script: &str, _expected_class: u8) -> bool {
    true
}

/// Format generation statistics as a human-readable report.
pub fn format_stats(stats: &GenerationStats) -> String {
    let mut lines = Vec::new();
    lines.push(format!("Total generated: {}", stats.total));
    lines.push(String::new());
    lines.push("Per-class distribution:".to_string());
    for (i, &count) in stats.per_class.iter().enumerate() {
        if count > 0 {
            let pct = if stats.total > 0 {
                count as f64 / stats.total as f64 * 100.0
            } else {
                0.0
            };
            lines.push(format!(
                "  {} ({}): {} ({:.1}%)",
                SAFETY_LABELS[i], i, count, pct
            ));
        }
    }
    if stats.misclassified > 0 {
        lines.push(String::new());
        lines.push(format!(
            "Misclassified: {} ({:.1}%)",
            stats.misclassified,
            stats.misclassified as f64 / stats.total as f64 * 100.0
        ));
        for (i, &count) in stats.misclassified_per_class.iter().enumerate() {
            if count > 0 {
                lines.push(format!("  {} ({}): {}", SAFETY_LABELS[i], i, count));
            }
        }
    } else {
        lines.push(String::new());
        lines.push("Misclassified: 0 (100% self-consistent)".to_string());
    }
    lines.join("\n")
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;

    #[test]
    fn test_generate_basic() {
        let config = AdversarialConfig {
            seed: 42,
            count_per_class: 10,
            extra_needs_quoting: 5,
            verify: false,
        };
        let result = generate_adversarial(&config);
        assert_eq!(result.stats.total, 35); // 10*3 + 5
        assert_eq!(result.stats.per_class[2], 10);
        assert_eq!(result.stats.per_class[3], 10);
        assert_eq!(result.stats.per_class[4], 10);
        assert_eq!(result.stats.per_class[1], 5);
        assert_eq!(result.rows.len(), 35);
    }

    #[test]
    fn test_deterministic_output() {
        let config = AdversarialConfig {
            seed: 42,
            count_per_class: 5,
            extra_needs_quoting: 3,
            verify: false,
        };
        let result1 = generate_adversarial(&config);
        let result2 = generate_adversarial(&config);
        assert_eq!(result1.rows.len(), result2.rows.len());
        for (a, b) in result1.rows.iter().zip(result2.rows.iter()) {
            assert_eq!(a.input, b.input);
            assert_eq!(a.label, b.label);
        }
    }

    #[test]
    fn test_all_scripts_have_shebang() {
        let config = AdversarialConfig {
            seed: 123,
            count_per_class: 10,
            extra_needs_quoting: 5,
            verify: false,
        };
        let result = generate_adversarial(&config);
        for row in &result.rows {
            assert!(
                row.input.starts_with("#!"),
                "Script should start with shebang: {}",
                &row.input[..row.input.len().min(80)]
            );
        }
    }

    #[test]
    fn test_all_scripts_nonempty() {
        let config = AdversarialConfig {
            seed: 99,
            count_per_class: 25,
            extra_needs_quoting: 10,
            verify: false,
        };
        let result = generate_adversarial(&config);
        for row in &result.rows {
            assert!(!row.input.is_empty());
            // Scripts should have at least shebang + one content line
            assert!(
                row.input.lines().count() >= 2,
                "Script too short: {}",
                row.input
            );
        }
    }

    #[test]
    fn test_template_expansion_all_families() {
        let all = adversarial_templates::all_templates();
        let mut rng = ChaCha8Rng::seed_from_u64(42);
        for template in &all {
            let script = expand_template(&mut rng, template);
            // Check that no {PARAM}-style placeholders remain (but allow
            // shell syntax like ${VAR}, ${{VAR}}, etc.)
            for param in template.params {
                let placeholder = format!("{{{}}}", param.name);
                assert!(
                    !script.contains(&placeholder),
                    "Template {} has unexpanded placeholder {}: {}",
                    template.family,
                    placeholder,
                    script
                );
            }
        }
    }

    #[test]
    fn test_verify_nondet_scripts() {
        let config = AdversarialConfig {
            seed: 42,
            count_per_class: 5,
            extra_needs_quoting: 0,
            verify: true,
        };
        let result = generate_adversarial(&config);
        // Allow some misclassifications from heuristic gaps, but most should match
        let nondet_count = result.stats.per_class[2];
        let nondet_misclass = result.stats.misclassified_per_class[2];
        assert!(
            nondet_misclass as f64 / nondet_count as f64 <= 0.5,
            "Too many non-det misclassifications: {nondet_misclass}/{nondet_count}"
        );
    }

    #[test]
    fn test_format_stats() {
        let stats = GenerationStats {
            total: 100,
            per_class: [0, 10, 30, 30, 30],
            misclassified: 2,
            misclassified_per_class: [0, 0, 1, 1, 0],
        };
        let report = format_stats(&stats);
        assert!(report.contains("Total generated: 100"));
        assert!(report.contains("non-deterministic"));
        assert!(report.contains("Misclassified: 2"));
    }

    #[test]
    fn test_distribution_accuracy() {
        let config = AdversarialConfig {
            seed: 42,
            count_per_class: 100,
            extra_needs_quoting: 50,
            verify: false,
        };
        let result = generate_adversarial(&config);
        assert_eq!(result.stats.per_class[2], 100);
        assert_eq!(result.stats.per_class[3], 100);
        assert_eq!(result.stats.per_class[4], 100);
        assert_eq!(result.stats.per_class[1], 50);
        assert_eq!(result.stats.total, 350);
    }
}