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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//! Tests extracted from audit.rs for file health compliance.
#![allow(clippy::unwrap_used)]

use crate::installer::audit::*;

/// Simplified test spec for audit testing
        #[test]
        fn prop_json_is_valid_structure(name in "[a-z]{5,10}", version in "[0-9]\\.[0-9]\\.[0-9]") {
            let report = AuditReport::new(&name, &version, PathBuf::from("/test"));
            let json = report.to_json();

            // Basic JSON structure validation
            let starts_correct = json.starts_with("{");
            let ends_correct = json.ends_with("}");
            let expected_name = format!("\"installer_name\": \"{}\"", name);
            let contains_name = json.contains(&expected_name);
            prop_assert!(starts_correct, "JSON should start with opening brace");
            prop_assert!(ends_correct, "JSON should end with closing brace");
            prop_assert!(contains_name, "JSON should contain installer name");
        }

/// Tests for Issue #112: audit postconditions not recognized with commands format
mod issue_112_tests {
    use crate::installer::audit::*;
    use crate::installer::spec::InstallerSpec;

    #[test]
    fn test_112_postconditions_verification_commands_recognized() {
        // Issue #112: Step with verification.commands should NOT trigger QUAL001
        let toml = r#"
[installer]
name = "test-installer"
version = "1.0.0"
description = "Test installer"

[[step]]
id = "install-app"
name = "Install Application"
action = "script"

[step.script]
content = "apt-get install app"

[step.verification]
commands = [
{ cmd = "which app", expect = "/usr/bin/app" }
]
"#;
        let spec = InstallerSpec::parse(toml).expect("Valid TOML");
        let ctx = AuditContext::new();
        let report = ctx.audit_parsed_spec(&spec, &PathBuf::from("/test.toml"));

        // Should NOT have QUAL001 finding - verification.commands counts as postconditions
        let qual001 = report.findings.iter().find(|f| f.rule_id == "QUAL001");
        assert!(
            qual001.is_none(),
            "QUAL001 should not be raised when verification.commands is present"
        );
    }

    #[test]
    fn test_112_postconditions_file_mode_recognized() {
        // Issue #112: Step with file_mode postcondition should NOT trigger QUAL001
        let toml = r#"
[installer]
name = "test-installer"
version = "1.0.0"

[[step]]
id = "set-perms"
name = "Set Permissions"
action = "script"

[step.script]
content = "chmod 755 /app"

[step.postconditions]
file_mode = "/app:755"
"#;
        let spec = InstallerSpec::parse(toml).expect("Valid TOML");
        let ctx = AuditContext::new();
        let report = ctx.audit_parsed_spec(&spec, &PathBuf::from("/test.toml"));

        let qual001 = report.findings.iter().find(|f| f.rule_id == "QUAL001");
        assert!(
            qual001.is_none(),
            "QUAL001 should not be raised when file_mode postcondition is present"
        );
    }

    #[test]
    fn test_112_postconditions_service_active_recognized() {
        // Issue #112: Step with service_active postcondition should NOT trigger QUAL001
        let toml = r#"
[installer]
name = "test-installer"
version = "1.0.0"

[[step]]
id = "start-service"
name = "Start Service"
action = "script"

[step.script]
content = "systemctl start myapp"

[step.postconditions]
service_active = "myapp"
"#;
        let spec = InstallerSpec::parse(toml).expect("Valid TOML");
        let ctx = AuditContext::new();
        let report = ctx.audit_parsed_spec(&spec, &PathBuf::from("/test.toml"));

        let qual001 = report.findings.iter().find(|f| f.rule_id == "QUAL001");
        assert!(
            qual001.is_none(),
            "QUAL001 should not be raised when service_active postcondition is present"
        );
    }

    #[test]
    fn test_112_postconditions_env_matches_recognized() {
        // Issue #112: Step with env_matches postcondition should NOT trigger QUAL001
        let toml = r#"
[installer]
name = "test-installer"
version = "1.0.0"

[[step]]
id = "setup-env"
name = "Setup Environment"
action = "script"

[step.script]
content = "export PATH=/app/bin:$PATH"

[step.postconditions.env_matches]
PATH = "/app/bin"
"#;
        let spec = InstallerSpec::parse(toml).expect("Valid TOML");
        let ctx = AuditContext::new();
        let report = ctx.audit_parsed_spec(&spec, &PathBuf::from("/test.toml"));

        let qual001 = report.findings.iter().find(|f| f.rule_id == "QUAL001");
        assert!(
            qual001.is_none(),
            "QUAL001 should not be raised when env_matches postcondition is present"
        );
    }

    #[test]
    fn test_112_postconditions_user_in_group_recognized() {
        // Issue #112: Step with user_in_group postcondition should NOT trigger QUAL001
        let toml = r#"
[installer]
name = "test-installer"
version = "1.0.0"

[[step]]
id = "add-group"
name = "Add User to Group"
action = "script"

[step.script]
content = "usermod -aG docker $USER"

[step.postconditions.user_in_group]
user = "deploy"
group = "docker"
"#;
        let spec = InstallerSpec::parse(toml).expect("Valid TOML");
        let ctx = AuditContext::new();
        let report = ctx.audit_parsed_spec(&spec, &PathBuf::from("/test.toml"));

        let qual001 = report.findings.iter().find(|f| f.rule_id == "QUAL001");
        assert!(
            qual001.is_none(),
            "QUAL001 should not be raised when user_in_group postcondition is present"
        );
    }

    #[test]
    fn test_112_no_postconditions_triggers_qual001() {
        // Sanity check: Step with NO postconditions SHOULD trigger QUAL001
        let toml = r#"
[installer]
name = "test-installer"
version = "1.0.0"

[[step]]
id = "no-post"
name = "Step Without Postconditions"
action = "script"

[step.script]
content = "echo hello"
"#;
        let spec = InstallerSpec::parse(toml).expect("Valid TOML");
        let ctx = AuditContext::new();
        let report = ctx.audit_parsed_spec(&spec, &PathBuf::from("/test.toml"));

        let qual001 = report.findings.iter().find(|f| f.rule_id == "QUAL001");
        assert!(
            qual001.is_some(),
            "QUAL001 should be raised when no postconditions are present"
        );
    }
}

// ============================================================================
// Coverage Tests - audit_security_parsed (SEC_COV_001-012)
// ============================================================================
mod security_parsed_tests {
    use crate::installer::audit::*;
    use crate::installer::spec::InstallerSpec;

    fn sec_audit(toml: &str) -> AuditReport {
        let spec = InstallerSpec::parse(toml).expect("Valid TOML");
        // Use new() not security_only() — security_only has min_severity=Warning
        // which filters out Info-level findings like SEC002 and SEC006
        let ctx = AuditContext::new();
        ctx.audit_parsed_spec(&spec, &PathBuf::from("/test.toml"))
    }

    #[test]
    fn test_SEC_COV_001_signatures_not_required() {
        // SEC001: require_signatures = false triggers warning
        let report = sec_audit(
            r#"
[installer]
name = "test"
version = "1.0.0"

[installer.security]
trust_model = "keyring"
require_signatures = false
"#,
        );
        assert!(report.findings.iter().any(|f| f.rule_id == "SEC001"));
    }

    #[test]
    fn test_SEC_COV_002_signatures_required_no_sec001() {
        // SEC001: require_signatures = true should NOT trigger
        let report = sec_audit(
            r#"
[installer]
name = "test"
version = "1.0.0"

[installer.security]
trust_model = "keyring"
require_signatures = true
"#,
        );
        assert!(!report.findings.iter().any(|f| f.rule_id == "SEC001"));
    }

    #[test]
    fn test_SEC_COV_003_tofu_trust_model() {
        // SEC002: trust_model = "tofu" triggers info
        let report = sec_audit(
            r#"
[installer]
name = "test"
version = "1.0.0"

[installer.security]
trust_model = "tofu"
require_signatures = true
"#,
        );
        assert!(report.findings.iter().any(|f| f.rule_id == "SEC002"));
    }

    #[test]
    fn test_SEC_COV_004_keyring_trust_model_no_sec002() {
        // SEC002: trust_model = "keyring" should NOT trigger
        let report = sec_audit(
            r#"
[installer]
name = "test"
version = "1.0.0"

[installer.security]
trust_model = "keyring"
require_signatures = true
"#,
        );
        assert!(!report.findings.iter().any(|f| f.rule_id == "SEC002"));
    }

    #[test]
    fn test_SEC_COV_005_unsigned_artifact() {
        // SEC004: artifact without signature or signed_by
        let report = sec_audit(
            r#"
[installer]
name = "test"
version = "1.0.0"

[installer.security]
require_signatures = true
trust_model = "keyring"

[[artifact]]
id = "myapp"
url = "https://example.com/myapp.tar.gz"
sha256 = "abc123"
"#,
        );
        assert!(report.findings.iter().any(|f| f.rule_id == "SEC004"));
    }

    #[test]
    fn test_SEC_COV_006_signed_artifact_no_sec004() {
        // SEC004: signed artifact should NOT trigger
        let report = sec_audit(
            r#"
[installer]
name = "test"
version = "1.0.0"

[installer.security]
require_signatures = true
trust_model = "keyring"

[[artifact]]
id = "myapp"
url = "https://example.com/myapp.tar.gz"
sha256 = "abc123"
signature = "myapp.sig"
signed_by = "key-001"
"#,
        );
        assert!(!report.findings.iter().any(|f| f.rule_id == "SEC004"));
    }

    #[test]
    fn test_SEC_COV_007_missing_sha256() {
        // SEC005: artifact without sha256
        let report = sec_audit(
            r#"
[installer]
name = "test"
version = "1.0.0"

[installer.security]
require_signatures = true
trust_model = "keyring"

[[artifact]]
id = "myapp"
url = "https://example.com/myapp.tar.gz"
signature = "myapp.sig"
signed_by = "key-001"
"#,
        );
        assert!(report.findings.iter().any(|f| f.rule_id == "SEC005"));
    }

    #[test]
    fn test_SEC_COV_008_root_privileges() {
        // SEC006: privileges = "root" triggers info
        let report = sec_audit(
            r#"
[installer]
name = "test"
version = "1.0.0"

[installer.security]
require_signatures = true
trust_model = "keyring"

[installer.requirements]
privileges = "root"
"#,
        );
        assert!(report.findings.iter().any(|f| f.rule_id == "SEC006"));
    }

    #[test]
    fn test_SEC_COV_009_user_privileges_no_sec006() {
        // SEC006: privileges = "user" should NOT trigger
        let report = sec_audit(
            r#"
[installer]
name = "test"
version = "1.0.0"

[installer.security]
require_signatures = true
trust_model = "keyring"

[installer.requirements]
privileges = "user"
"#,
        );
        assert!(!report.findings.iter().any(|f| f.rule_id == "SEC006"));
    }

    #[test]
    fn test_SEC_COV_010_curl_pipe_bash() {
        // SEC007: curl ... | bash pattern
        let report = sec_audit(
            r#"
[installer]
name = "test"
version = "1.0.0"

[installer.security]
require_signatures = true
trust_model = "keyring"

[[step]]
id = "install"
name = "Install"
action = "script"

[step.script]
content = "curl https://example.com/setup.sh | bash"
"#,
        );
        let sec007 = report.findings.iter().find(|f| f.rule_id == "SEC007");
        assert!(sec007.is_some());
        assert_eq!(
            sec007.expect("has sec007").severity,
            AuditSeverity::Critical
        );
    }

    #[test]
    fn test_SEC_COV_011_eval_in_script() {
        // SEC008: eval in script
        let report = sec_audit(
            r#"
[installer]
name = "test"
version = "1.0.0"

[installer.security]
require_signatures = true
trust_model = "keyring"

[[step]]
id = "install"
name = "Install"
action = "script"

[step.script]
content = "eval $DYNAMIC_CMD"
"#,
        );
        assert!(report.findings.iter().any(|f| f.rule_id == "SEC008"));
    }