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
456
457
458
459
460
461
462
463
use super::*;
use crate::linter::Severity;

// RED Phase: Write failing tests first

#[test]
fn test_SEC010_detects_cp_with_user_file() {
    let script = r#"cp "$USER_FILE" /destination/"#;
    let result = check(script);

    assert_eq!(result.diagnostics.len(), 1);
    let diag = &result.diagnostics[0];
    assert_eq!(diag.code, "SEC010");
    assert_eq!(diag.severity, Severity::Error);
    assert!(diag.message.contains("Path traversal"));
}

#[test]
fn test_SEC010_detects_cat_with_input_path() {
    let script = r#"cat "$INPUT_PATH""#;
    let result = check(script);

    assert_eq!(result.diagnostics.len(), 1);
    assert_eq!(result.diagnostics[0].code, "SEC010");
}

#[test]
fn test_SEC010_detects_tar_with_archive() {
    let script = r#"tar -xf "$ARCHIVE""#;
    let result = check(script);

    assert_eq!(result.diagnostics.len(), 1);
    assert_eq!(result.diagnostics[0].code, "SEC010");
}

#[test]
fn test_SEC010_detects_mkdir_with_user_dir() {
    let script = r#"mkdir -p "$USER_DIR""#;
    let result = check(script);

    assert_eq!(result.diagnostics.len(), 1);
    assert_eq!(result.diagnostics[0].code, "SEC010");
}

#[test]
fn test_SEC010_detects_cd_with_user_path() {
    let script = r#"cd "$USER_PATH""#;
    let result = check(script);

    assert_eq!(result.diagnostics.len(), 1);
    assert_eq!(result.diagnostics[0].code, "SEC010");
}

#[test]
fn test_SEC010_safe_with_hardcoded_path() {
    let script = r#"cp /etc/config /backup/"#;
    let result = check(script);

    // Hardcoded paths are safe (no variables)
    assert_eq!(result.diagnostics.len(), 0);
}

#[test]
fn test_SEC010_detects_explicit_traversal() {
    let script = r#"cp file.txt ../../sensitive/"#;
    let result = check(script);

    // Should warn about explicit ../ usage
    assert!(!result.diagnostics.is_empty());
}

#[test]
fn test_SEC010_no_false_positive_validation() {
    let script = r#"if [[ "$FILE" == *".."* ]]; then exit 1; fi"#;
    let result = check(script);

    // This is validation, not a vulnerability
    // Should not flag (or flag with lower severity)
    // Conservative: might still flag but acceptable for security
}

#[test]
fn test_SEC010_no_auto_fix() {
    let script = r#"cp "$USER_FILE" /dest/"#;
    let result = check(script);

    assert_eq!(result.diagnostics.len(), 1);
    let diag = &result.diagnostics[0];
    assert!(diag.fix.is_none(), "SEC010 should not provide auto-fix");
}

#[test]
fn test_SEC010_multiple_vulnerabilities() {
    let script = r#"
cp "$USER_FILE" /dest/
cat "$INPUT_PATH"
    "#;
    let result = check(script);

    assert_eq!(result.diagnostics.len(), 2);
}

#[test]
fn test_SEC010_no_false_positive_comment() {
    let script = r#"# cp "$USER_FILE" is dangerous"#;
    let result = check(script);

    // Comments should not trigger the rule
    assert_eq!(result.diagnostics.len(), 0);
}

#[test]
fn test_SEC010_106_heredoc_not_file_read() {
    // Issue #106: cat <<EOF is not a file read, it's a heredoc
    let script = r#"content=$(cat <<EOF
some content here
EOF
)"#;
    let result = check(script);

    // Heredocs should not trigger the rule
    assert_eq!(result.diagnostics.len(), 0);
}

#[test]
fn test_SEC010_106_heredoc_multiline() {
    // Issue #106: Heredoc with quoted delimiter
    let script = r#"cargo_content=$(cat <<'EOF'
[build]
jobs = 4
EOF
)"#;
    let result = check(script);

    // Heredocs should not trigger the rule
    assert_eq!(result.diagnostics.len(), 0);
}

#[test]
fn test_SEC010_106_heredoc_with_tee() {
    // tee with heredoc
    let script = r#"tee /etc/config <<EOF
config here
EOF"#;
    let result = check(script);

    // The tee has a path but it's a heredoc input
    assert_eq!(result.diagnostics.len(), 0);
}

#[test]
fn test_SEC010_real_cat_still_flagged() {
    // Real cat with user file should still be flagged
    let script = r#"cat "$USER_FILE""#;
    let result = check(script);

    assert_eq!(result.diagnostics.len(), 1);
    assert_eq!(result.diagnostics[0].code, "SEC010");
}

// Issue #104 tests: Path validation guards

#[test]
fn test_SEC010_104_validated_path_not_flagged() {
    // Issue #104: If a path is validated with if [[ "$VAR" == *".."* ]], skip subsequent use
    let script = r#"
if [[ "$USER_FILE" == *".."* ]]; then
echo "Invalid path" >&2
exit 1
fi
cp "$USER_FILE" /destination/
"#;
    let result = check(script);

    // Should NOT flag because USER_FILE was validated
    assert_eq!(
        result.diagnostics.len(),
        0,
        "Expected no diagnostics for validated path, got: {:?}",
        result.diagnostics
    );
}

#[test]
fn test_SEC010_104_realpath_validated() {
    // Issue #104: Variables assigned from realpath are considered validated
    let script = r#"
SAFE_PATH=$(realpath -m "$USER_INPUT")
cp "$SAFE_PATH" /destination/
"#;
    let result = check(script);

    // SAFE_PATH is derived from realpath, so it's validated
    assert_eq!(
        result.diagnostics.len(),
        0,
        "Expected no diagnostics for realpath-validated path"
    );
}

#[test]
fn test_SEC010_104_readlink_validated() {
    // Issue #104: Variables assigned from readlink -f are validated
    let script = r#"
RESOLVED=$(readlink -f "$USER_PATH")
cat "$RESOLVED"
"#;
    let result = check(script);

    assert_eq!(
        result.diagnostics.len(),
        0,
        "Expected no diagnostics for readlink-f-validated path"
    );
}

#[test]
fn test_SEC010_104_unvalidated_still_flagged() {
    // Issue #104: Variables that are NOT validated should still be flagged
    let script = r#"
echo "Processing file..."
cp "$USER_FILE" /destination/
"#;
    let result = check(script);

    assert_eq!(result.diagnostics.len(), 1);
    assert_eq!(result.diagnostics[0].code, "SEC010");
}

#[test]
fn test_SEC010_104_different_var_still_flagged() {
    // Issue #104: Validating one variable doesn't validate others
    let script = r#"
if [[ "$SAFE_VAR" == *".."* ]]; then
exit 1
fi
cp "$USER_FILE" /destination/
"#;
    let result = check(script);

    // USER_FILE was not validated, only SAFE_VAR was
    assert_eq!(result.diagnostics.len(), 1);
    assert_eq!(result.diagnostics[0].code, "SEC010");
}

#[test]
fn test_SEC010_104_absolute_path_check() {
    // Issue #104: Check for absolute path validation
    let script = r#"
if [[ "$INPUT_PATH" == /* ]]; then
echo "Absolute paths not allowed" >&2
exit 1
fi
cp "$INPUT_PATH" /destination/
"#;
    let result = check(script);

    assert_eq!(
        result.diagnostics.len(),
        0,
        "Expected no diagnostics after absolute path validation"
    );
}

// Issue #127 tests: Custom validation function tracking

#[test]
fn test_SEC010_127_validate_function_tracks_var() {
    // Issue #127: Variables passed to validate_* functions should be tracked
    let script = r#"
validate_path() {
local path="$1"
if [[ "$path" == *".."* ]]; then
    echo "Invalid path" >&2
    exit 1
fi
}

validate_path "$RAID_PATH"
mkdir -p "$RAID_PATH/targets"
"#;
    let result = check(script);

    assert_eq!(
        result.diagnostics.len(),
        0,
        "Expected no diagnostics for variable passed to validate_path()"
    );
}

#[test]
fn test_SEC010_127_check_function_tracks_var() {
    // Issue #127: check_* functions also count as validation
    let script = r#"
check_path "$SRC_PATH"
cp "$SRC_PATH/file" /destination/
"#;
    let result = check(script);

    assert_eq!(
        result.diagnostics.len(),
        0,
        "Expected no diagnostics for variable passed to check_path()"
    );
}

#[test]
fn test_SEC010_127_sanitize_function_tracks_var() {
    // Issue #127: sanitize_* functions also count as validation
    let script = r#"
sanitize_input "$USER_FILE"
cat "$USER_FILE"
"#;
    let result = check(script);

    assert_eq!(
        result.diagnostics.len(),
        0,
        "Expected no diagnostics for variable passed to sanitize_input()"
    );
}

#[test]
fn test_SEC010_127_unvalidated_still_flagged() {
    // Issue #127: Variables NOT passed to validation functions should still be flagged
    let script = r#"
validate_path "$OTHER_PATH"
mkdir -p "$USER_DIR"
"#;
    let result = check(script);

    // USER_DIR was not validated, should be flagged
    assert_eq!(result.diagnostics.len(), 1);
    assert_eq!(result.diagnostics[0].code, "SEC010");
}

#[test]
fn test_SEC010_127_function_definition_not_call() {
    // Issue #127: Function definitions should not count as validation calls
    let script = r#"
validate_path() {
echo "validating"
}
mkdir -p "$USER_DIR"
"#;
    let result = check(script);

    // USER_DIR was not validated (just function was defined), should be flagged
    assert_eq!(result.diagnostics.len(), 1);
}

// Unit tests for helper functions to increase coverage

#[test]
fn test_is_validation_function_call_various_prefixes() {
    // Test all validation function prefixes
    assert!(is_validation_function_call(r#"validate_path "$PATH""#));
    assert!(is_validation_function_call(r#"check_input "$INPUT""#));
    assert!(is_validation_function_call(r#"verify_file "$FILE""#));
    assert!(is_validation_function_call(r#"sanitize_input "$INPUT""#));
    assert!(is_validation_function_call(r#"clean_path "$PATH""#));
    assert!(is_validation_function_call(r#"safe_copy "$FILE""#));
    assert!(is_validation_function_call(r#"is_valid_path "$PATH""#));
    assert!(is_validation_function_call(r#"is_safe_input "$INPUT""#));
    assert!(is_validation_function_call(r#"assert_path "$PATH""#));

    // Should not match without variable
    assert!(!is_validation_function_call("validate_path /fixed/path"));
    // Should not match function definitions
    assert!(!is_validation_function_call("validate_path() {"));
    assert!(!is_validation_function_call("validate_path()"));
}

#[test]
fn test_extract_function_argument_variable_formats() {
    // Test ${VAR} format
    assert_eq!(
        extract_function_argument_variable(r#"validate_path "${PATH}""#),
        Some("PATH".to_string())
    );
    // Test ${VAR[0]} format (array index stripped)
    assert_eq!(
        extract_function_argument_variable(r#"validate_path "${ARGS[0]}""#),
        Some("ARGS".to_string())
    );
    // Test $VAR format
    assert_eq!(
        extract_function_argument_variable(r#"validate_path "$PATH""#),
        Some("PATH".to_string())
    );
    // Test no variable
    assert_eq!(
        extract_function_argument_variable("validate_path /fixed/path"),
        None
    );
}

#[test]
fn test_is_heredoc_pattern_variants() {
    // Test various heredoc patterns
    assert!(is_heredoc_pattern("cat <<EOF"));
    assert!(is_heredoc_pattern("cat <<'EOF'"));
    assert!(is_heredoc_pattern("cat <<-EOF"));
    assert!(is_heredoc_pattern("cat<<<'EOF'"));
    assert!(is_heredoc_pattern("echo <<EOF"));
    assert!(is_heredoc_pattern("read <<EOF"));
    assert!(is_heredoc_pattern("tee <<EOF"));
    assert!(is_heredoc_pattern("content=$(cat <<EOF"));
    assert!(is_heredoc_pattern("x=$(cat<<EOF"));

    // Should not match regular cat
    assert!(!is_heredoc_pattern("cat /etc/passwd"));
    assert!(!is_heredoc_pattern(r#"cat "$FILE""#));
}

#[cfg(test)]
mod property_tests {
use super::*;
use crate::linter::Severity;
use proptest::prelude::*;

proptest! {
    #![proptest_config(proptest::test_runner::Config::with_cases(10))]
    #[test]
    fn prop_sec010_never_panics(s in ".*") {
        let _ = check(&s);
    }

    #[test]
    fn prop_sec010_safe_hardcoded_paths(
        src in "/[a-z/]{1,20}",
        dst in "/[a-z/]{1,20}",
    ) {
        let cmd = format!("cp {} {}", src, dst);
        let result = check(&cmd);
        // Hardcoded paths (no variables) should be safe
        prop_assert_eq!(result.diagnostics.len(), 0);
    }

    #[test]
    fn prop_sec010_detects_user_variables(
        file_op_idx in 0..9usize,
        var_name in "(USER|INPUT|FILE|PATH|DIR|ARCHIVE|NAME|ARG)_[A-Z]{1,5}",
    ) {
        let file_op = match file_op_idx {
            0 => "cp",
            1 => "mv",
            2 => "cat",
            3 => "tar",
            4 => "unzip",
            5 => "rm",
            6 => "mkdir",
            7 => "cd",
            _ => "ln",
        };
        let cmd = format!(r#"{} "${{{}}}""#, file_op, var_name);
        let result = check(&cmd);
        // Should detect path traversal risk with user variables
        prop_assert!(!result.diagnostics.is_empty());
        prop_assert_eq!(result.diagnostics[0].code.as_str(), "SEC010");
    }
}
}