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
#[test]
fn test_idempotency_makefile_not_checked() {
    // Idempotency rules only apply to ShellScript and ShellConfig, not Makefile
    let content = "all:\n\tmkdir /tmp/build\n";
    let artifact = Artifact::new(
        PathBuf::from("Makefile"),
        Scope::Project,
        ArtifactKind::Makefile,
    );
    let result = check_rule(RuleId::Idempotency, content, &artifact);
    assert!(result.passed, "Makefile should skip idempotency check");
}

#[test]
fn test_idempotency_rm_rf_is_fine() {
    let content = "#!/bin/sh\nrm -rf /tmp/build\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Idempotency, content, &artifact);
    assert!(result.passed, "rm -rf should be considered idempotent");
}

#[test]
fn test_idempotency_ln_sf_is_fine() {
    let content = "#!/bin/sh\nln -sf /src /dst\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Idempotency, content, &artifact);
    assert!(result.passed, "ln -sf should be considered idempotent");
}

#[test]
fn test_idempotency_ln_s_without_f_fails() {
    let content = "#!/bin/sh\nln -s /src /dst\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Idempotency, content, &artifact);
    assert!(!result.passed, "ln -s without -f should fail idempotency");
}

#[test]
fn test_determinism_date_patterns() {
    let content = "#!/bin/sh\nTIMESTAMP=$(date +%s)\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Determinism, content, &artifact);
    assert!(
        !result.passed,
        "date +%s should be flagged as non-deterministic"
    );
}

#[test]
fn test_determinism_date_nano() {
    let content = "#!/bin/sh\nNANO=$(date +%N)\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Determinism, content, &artifact);
    assert!(
        !result.passed,
        "date +%N should be flagged as non-deterministic"
    );
}

#[test]
fn test_security_wget_pipe_sh() {
    let content = "#!/bin/sh\nwget -q https://example.com/setup.sh | sh\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Security, content, &artifact);
    assert!(!result.passed, "wget | sh should be flagged as SEC002");
}

#[test]
fn test_shellcheck_dangerous_rm_rf() {
    let content = "#!/bin/sh\nrm -rf /$DIR\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::ShellCheck, content, &artifact);
    assert!(
        !result.passed,
        "rm -rf with variable path should be flagged as SC2115"
    );
}

#[test]
fn test_dockerfile_add_http_is_ok() {
    let content = "FROM ubuntu:22.04\nADD https://example.com/file.tar.gz /app/\nUSER nobody\n";
    let artifact = Artifact::new(
        PathBuf::from("Dockerfile"),
        Scope::Project,
        ArtifactKind::Dockerfile,
    );
    let result = check_rule(RuleId::DockerfileBest, content, &artifact);
    assert!(result.passed, "ADD with HTTP URL should be allowed");
}

#[test]
fn test_pzsh_budget_always_passes() {
    let content = "anything here";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::PzshBudget, content, &artifact);
    assert!(
        result.passed,
        "PzshBudget should always pass (handled externally)"
    );
}

#[test]
fn test_rule_id_codes_complete() {
    // Verify all 10 rules have unique codes
    let rules = vec![
        RuleId::Posix,
        RuleId::Determinism,
        RuleId::Idempotency,
        RuleId::Security,
        RuleId::Quoting,
        RuleId::ShellCheck,
        RuleId::MakefileSafety,
        RuleId::DockerfileBest,
        RuleId::ConfigHygiene,
        RuleId::PzshBudget,
    ];
    let codes: Vec<&str> = rules.iter().map(|r| r.code()).collect();
    assert_eq!(codes.len(), 10);
    // Verify sequential COMPLY-001 through COMPLY-010
    for (i, code) in codes.iter().enumerate() {
        assert_eq!(*code, format!("COMPLY-{:03}", i + 1));
    }
}

#[test]
fn test_rule_weights_sum_to_110() {
    // Total weight pool: 20+15+15+20+10+10+5+5+5+5 = 110
    let rules = vec![
        RuleId::Posix,
        RuleId::Determinism,
        RuleId::Idempotency,
        RuleId::Security,
        RuleId::Quoting,
        RuleId::ShellCheck,
        RuleId::MakefileSafety,
        RuleId::DockerfileBest,
        RuleId::ConfigHygiene,
        RuleId::PzshBudget,
    ];
    let total: u32 = rules.iter().map(|r| r.weight()).sum();
    assert_eq!(total, 110, "Total weight pool should be 110");
}

#[test]
fn test_devcontainer_has_no_applicable_rules() {
    let rules = RuleId::applicable_rules(ArtifactKind::DevContainer);
    assert!(
        rules.is_empty(),
        "DevContainer should have no applicable rules"
    );
}

#[test]
fn test_workflow_only_has_security() {
    let rules = RuleId::applicable_rules(ArtifactKind::Workflow);
    assert_eq!(rules.len(), 1);
    assert_eq!(rules[0], RuleId::Security);
}

// ═══════════════════════════════════════════════════════════════
// COMPLY-005 quote tracker: escaped quotes and subshell handling
// ═══════════════════════════════════════════════════════════════

// ═══════════════════════════════════════════════════════════════
// SEC004: TLS verification disabled
// ═══════════════════════════════════════════════════════════════

#[test]
fn test_sec004_wget_no_check_certificate() {
    let content = "#!/bin/sh\nwget --no-check-certificate https://example.com/file\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Security, content, &artifact);
    assert!(
        !result.passed,
        "SEC004: --no-check-certificate should be flagged"
    );
    assert!(result
        .violations
        .iter()
        .any(|v| v.message.contains("SEC004")));
}

#[test]
fn test_sec004_curl_insecure() {
    let content = "#!/bin/sh\ncurl --insecure https://api.example.com/data\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Security, content, &artifact);
    assert!(!result.passed, "SEC004: --insecure should be flagged");
}

#[test]
fn test_sec004_curl_k_flag() {
    let content = "#!/bin/sh\ncurl -k https://api.example.com/data\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Security, content, &artifact);
    assert!(!result.passed, "SEC004: curl -k should be flagged");
}

#[test]
fn test_sec004_curl_without_k_is_ok() {
    let content = "#!/bin/sh\ncurl https://api.example.com/data\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Security, content, &artifact);
    assert!(result.passed, "curl without TLS flags should pass");
}

// ═══════════════════════════════════════════════════════════════
// SEC005: Hardcoded secrets
// ═══════════════════════════════════════════════════════════════

#[test]
fn test_sec005_hardcoded_api_key() {
    let content = "#!/bin/sh\nAPI_KEY=\"sk-1234567890abcdef\"\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Security, content, &artifact);
    assert!(
        !result.passed,
        "SEC005: hardcoded API_KEY should be flagged"
    );
    assert!(result
        .violations
        .iter()
        .any(|v| v.message.contains("SEC005")));
}

#[test]
fn test_sec005_hardcoded_password() {
    let content = "#!/bin/sh\nPASSWORD=\"MyS3cret!\"\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Security, content, &artifact);
    assert!(
        !result.passed,
        "SEC005: hardcoded PASSWORD should be flagged"
    );
}

#[test]
fn test_sec005_github_token_prefix() {
    let content = "#!/bin/sh\nTOKEN=\"ghp_xxxxxxxxxxxxxxxxxxxx\"\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Security, content, &artifact);
    assert!(
        !result.passed,
        "SEC005: ghp_ token prefix should be flagged"
    );
}

#[test]
fn test_sec005_variable_expansion_not_flagged() {
    let content = "#!/bin/sh\nAPI_KEY=\"$MY_API_KEY\"\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Security, content, &artifact);
    // Should not flag variable expansion as hardcoded secret
    let sec005_violations: Vec<_> = result
        .violations
        .iter()
        .filter(|v| v.message.contains("SEC005"))
        .collect();
    assert!(
        sec005_violations.is_empty(),
        "Variable expansion should not trigger SEC005: {:?}",
        sec005_violations
    );
}

#[test]
fn test_sec005_empty_value_not_flagged() {
    let content = "#!/bin/sh\nAPI_KEY=\"\"\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Security, content, &artifact);
    let sec005_violations: Vec<_> = result
        .violations
        .iter()
        .filter(|v| v.message.contains("SEC005"))
        .collect();
    assert!(
        sec005_violations.is_empty(),
        "Empty value should not trigger SEC005"
    );
}

// ═══════════════════════════════════════════════════════════════
// SEC006: Unsafe temporary files
// ═══════════════════════════════════════════════════════════════

#[test]
fn test_sec006_unsafe_tmp_path() {
    let content = "#!/bin/sh\nTMPFILE=\"/tmp/myapp.tmp\"\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Security, content, &artifact);
    assert!(
        !result.passed,
        "SEC006: /tmp/ literal path should be flagged"
    );
    assert!(result
        .violations
        .iter()
        .any(|v| v.message.contains("SEC006")));
}

#[test]
fn test_sec006_mktemp_is_ok() {
    let content = "#!/bin/sh\nTMPFILE=\"$(mktemp)\"\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Security, content, &artifact);
    assert!(result.passed, "mktemp usage should not be flagged");
}

// ═══════════════════════════════════════════════════════════════
// SEC007: sudo with dangerous command
// ═══════════════════════════════════════════════════════════════

#[test]
fn test_sec007_sudo_rm_rf_unquoted() {
    let content = "#!/bin/sh\nsudo rm -rf $DIR\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Security, content, &artifact);
    assert!(
        !result.passed,
        "SEC007: sudo rm -rf with unquoted var should be flagged"
    );
    assert!(result
        .violations
        .iter()
        .any(|v| v.message.contains("SEC007")));
}

#[test]
fn test_sec007_sudo_chmod_777() {
    let content = "#!/bin/sh\nsudo chmod 777 $FILE\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Security, content, &artifact);
    assert!(
        !result.passed,
        "SEC007: sudo chmod 777 with unquoted var should be flagged"
    );
}

#[test]
fn test_sec007_sudo_rm_rf_quoted_is_ok() {
    let content = "#!/bin/sh\nsudo rm -rf \"$DIR\"\n";
    let artifact = Artifact::new(
        PathBuf::from("test.sh"),
        Scope::Project,
        ArtifactKind::ShellScript,
    );
    let result = check_rule(RuleId::Security, content, &artifact);
    let sec007_violations: Vec<_> = result
        .violations
        .iter()
        .filter(|v| v.message.contains("SEC007"))
        .collect();
    assert!(
        sec007_violations.is_empty(),
        "Quoted variable with sudo should not trigger SEC007"
    );
}