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
//! # TICKET-1002 RED Phase: Unicode String Escaping Property Tests
//!
//! This module contains EXTREME TDD property-based tests for unicode string escaping.
//! These tests are designed to FAIL and expose bugs in the shell escaping logic.
//!
//! ## Testing Strategy
//!
//! 1. **Unicode Coverage**: Test all unicode categories (emoji, CJK, RTL, control chars)
//! 2. **Security**: Ensure no injection vectors via unicode
//! 3. **Roundtrip**: Escaped strings should preserve original content
//! 4. **Shell Safety**: All escaped strings must be valid POSIX shell syntax

use crate::emitter::escape::{escape_shell_string, escape_variable_name};
use std::process::Command;
use tempfile::TempDir;

// ============================================================================
// PROPERTY 1: Unicode strings escape safely without injection
// ============================================================================

#[test]
fn test_unicode_emoji_no_injection() {
    let test_cases = vec![
        "Hello 👋 World",
        "🔥💯✨",
        "Test 😀😁😂🤣",
        "Mixed text with 🚀 emoji",
    ];

    for input in test_cases {
        let escaped = escape_shell_string(input);

        // Should be quoted
        assert!(
            escaped.starts_with('\'') || escaped.starts_with('"'),
            "Emoji string not quoted: {} -> {}",
            input,
            escaped
        );

        // Test in actual shell - no injection
        let result = execute_shell_echo(&escaped);
        assert_eq!(
            result.trim(),
            input,
            "Emoji roundtrip failed: {} -> {} -> {}",
            input,
            escaped,
            result
        );
    }
}

#[test]
fn test_unicode_cjk_characters_safe() {
    let test_cases = vec![
        "你好世界",   // Chinese
        "こんにちは", // Japanese Hiragana
        "コンニチハ", // Japanese Katakana
        "안녕하세요", // Korean
        "Mixed English 中文",
    ];

    for input in test_cases {
        let escaped = escape_shell_string(input);

        let result = execute_shell_echo(&escaped);
        assert_eq!(
            result.trim(),
            input,
            "CJK roundtrip failed: {} -> {} -> {}",
            input,
            escaped,
            result
        );
    }
}

#[test]
fn test_unicode_rtl_languages_safe() {
    let test_cases = vec![
        "مرحبا", // Arabic
        "שלום",  // Hebrew
        "Mixed مرحبا English",
    ];

    for input in test_cases {
        let escaped = escape_shell_string(input);

        let result = execute_shell_echo(&escaped);
        assert_eq!(
            result.trim(),
            input,
            "RTL roundtrip failed: {} -> {} -> {}",
            input,
            escaped,
            result
        );
    }
}

#[test]
fn test_unicode_combining_characters_safe() {
    let test_cases = vec![
        "café",      // é = e + combining acute
        "naïve",     // ï = i + combining diaeresis
        "Zürich",    // ü = u + combining diaeresis
        "e\u{0301}", // e + combining acute accent
    ];

    for input in test_cases {
        let escaped = escape_shell_string(input);

        let result = execute_shell_echo(&escaped);
        assert_eq!(
            result.trim(),
            input,
            "Combining char roundtrip failed: {} -> {} -> {}",
            input,
            escaped,
            result
        );
    }
}

// ============================================================================
// PROPERTY 2: Control characters are properly escaped
// ============================================================================

#[test]
fn test_unicode_control_characters_safe() {
    let test_cases = vec![
        "line1\nline2",  // Newline
        "tab\there",     // Tab
        "null\0byte",    // Null byte
        "bell\x07",      // Bell character
        "escape\x1b[0m", // ANSI escape sequence
    ];

    for input in test_cases {
        let escaped = escape_shell_string(input);

        // Control characters should be quoted
        assert!(
            escaped.contains('\'') || escaped.contains('"'),
            "Control characters not quoted: {:?} -> {}",
            input,
            escaped
        );

        // Execute in shell - should preserve or safely handle
        let result = execute_shell_echo(&escaped);

        // Note: Some control chars may be processed by shell/echo
        // We just verify no injection or crash
        assert!(
            !result.contains("rm -rf"),
            "Possible injection with control chars: {:?} -> {}",
            input,
            result
        );
    }
}

// ============================================================================
// PROPERTY 3: Zero-width and invisible unicode are safe
// ============================================================================

#[test]
fn test_unicode_zero_width_characters_safe() {
    let test_cases = vec![
        "test\u{200B}invisible", // Zero-width space
        "test\u{200C}joiner",    // Zero-width non-joiner
        "test\u{200D}joiner",    // Zero-width joiner
        "test\u{FEFF}bom",       // Zero-width no-break space (BOM)
    ];

    for input in test_cases {
        let escaped = escape_shell_string(input);

        let result = execute_shell_echo(&escaped);

        // Verify no injection
        assert!(
            !result.contains(';'),
            "Possible injection with zero-width chars: {:?} -> {}",
            input,
            result
        );
    }
}

// ============================================================================
// PROPERTY 4: Unicode normalization doesn't affect safety
// ============================================================================

#[test]
fn test_unicode_normalization_forms_safe() {
    // Same visual character in different unicode representations
    let nfc = "café"; // NFC: é as single codepoint U+00E9
    let nfd = "café"; // NFD: e + ́ (combining acute U+0301)

    let escaped_nfc = escape_shell_string(nfc);
    let escaped_nfd = escape_shell_string(nfd);

    // Both should escape safely
    let result_nfc = execute_shell_echo(&escaped_nfc);
    let result_nfd = execute_shell_echo(&escaped_nfd);

    // Results should match their inputs (modulo normalization)
    assert_eq!(result_nfc.trim(), nfc, "NFC roundtrip failed");
    assert_eq!(result_nfd.trim(), nfd, "NFD roundtrip failed");
}

// ============================================================================
// PROPERTY 5: Mixed unicode and shell metacharacters are safe
// ============================================================================

#[test]
fn test_unicode_with_shell_metacharacters() {
    let test_cases = vec![
        "Hello $USER 你好",
        "Path: $(pwd) 🚀",
        "Injection; rm -rf / 😈",
        "`backtick` attack 中文",
        "Pipe | redirect > 한글",
    ];

    for input in test_cases {
        let escaped = escape_shell_string(input);

        // Must be quoted to prevent injection
        assert!(
            escaped.starts_with('\''),
            "Shell metacharacters with unicode not quoted: {} -> {}",
            input,
            escaped
        );

        let result = execute_shell_echo(&escaped);

        // Metacharacters should be literal, not executed
        assert!(
            !result.contains("root") && !result.contains("bin"),
            "Possible command execution: {} -> {}",
            input,
            result
        );
    }
}

// ============================================================================
// PROPERTY 6: Very long unicode strings don't cause issues
// ============================================================================

#[test]
fn test_unicode_long_strings_safe() {
    // 1000 emoji characters
    let long_emoji = "🚀".repeat(1000);
    let escaped = escape_shell_string(&long_emoji);

    // Should still be escapable
    assert!(escaped.len() > 0, "Long emoji string produced empty escape");

    // Should roundtrip (we'll test a shorter version due to shell limits)
    let short_emoji = "🚀".repeat(10);
    let escaped_short = escape_shell_string(&short_emoji);
    let result = execute_shell_echo(&escaped_short);
    assert_eq!(result.trim(), short_emoji, "Long emoji roundtrip failed");
}

// ============================================================================
// PROPERTY 7: Unicode in variable names is handled safely
// ============================================================================

#[test]
fn test_unicode_variable_names_sanitized() {
    let test_cases = vec![
        ("hello_世界", "hello___"),     // CJK replaced (世界 is 2 chars)
        ("test_🚀", "test__"),          // Emoji replaced
        ("café_var", "caf__var"), // Accented chars replaced (é is 2 chars in NFD form, but here it's 1)
        ("_valid", "_valid"),     // Valid name unchanged
        ("123_invalid", "_23_invalid"), // Leading digit fixed
    ];

    for (input, expected) in test_cases {
        let result = escape_variable_name(input);
        assert_eq!(
            result, expected,
            "Variable name escaping failed: {} -> {} (expected {})",
            input, result, expected
        );

        // Result should be valid shell identifier
        assert!(
            is_valid_shell_identifier(&result),
            "Escaped variable name not valid: {}",
            result
        );
    }
}

// ============================================================================
// PROPERTY 8: Bidirectional unicode doesn't cause injection
// ============================================================================

#[test]
fn test_unicode_bidi_override_safe() {
    // Unicode bidirectional override can be used for obfuscation attacks
    let test_cases = vec![
        "test\u{202E}esrever",         // Right-to-left override
        "normal\u{202D}forced_ltr",    // Left-to-right override
        "embed\u{202A}rtl\u{202C}end", // Left-to-right embedding
    ];

    for input in test_cases {
        let escaped = escape_shell_string(input);

        // Should be quoted
        assert!(
            escaped.contains('\''),
            "Bidi characters not quoted: {:?} -> {}",
            input,
            escaped
        );

        let result = execute_shell_echo(&escaped);

        // Bidi characters should be preserved (not executed as commands)
        // The visual appearance may be affected, but no actual code execution
        // Just verify no crash and successful roundtrip
        assert!(
            result.trim().len() > 0,
            "Bidi test failed: empty result for {:?}",
            input
        );
    }
}

// ============================================================================
// Helper Functions
// ============================================================================

/// Execute a shell echo command and capture output
/// This tests if our escaping works in a real shell
fn execute_shell_echo(escaped: &str) -> String {
    let temp_dir = TempDir::new().unwrap();
    let script_path = temp_dir.path().join("test.sh");

    let script = format!("#!/bin/sh\necho {}\n", escaped);

    std::fs::write(&script_path, script).unwrap();

    let output = Command::new("sh")
        .arg(&script_path)
        .output()
        .expect("Failed to execute shell");

    if !output.status.success() {
        // Shell syntax error - this is a test failure
        panic!(
            "Shell execution failed for escaped string: {}\nStderr: {}",
            escaped,
            String::from_utf8_lossy(&output.stderr)
        );
    }

    String::from_utf8_lossy(&output.stdout).to_string()
}

/// Check if a string is a valid POSIX shell identifier
fn is_valid_shell_identifier(name: &str) -> bool {
    if name.is_empty() {
        return false;
    }

    let first_char = name.chars().next().unwrap();
    if !first_char.is_ascii_alphabetic() && first_char != '_' {
        return false;
    }

    name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
}

// ============================================================================
// PROPERTY 9: Fuzzing-style random unicode property test
// ============================================================================

#[cfg(test)] // Only run this expensive test when explicitly testing
#[test]
fn test_unicode_fuzzing_random_strings() {
    use rand::Rng;

    let mut rng = rand::rng();

    for _ in 0..100 {
        // Generate random unicode string
        let len = rng.random_range(1..50);
        let random_string: String = (0..len)
            .map(|_| {
                let codepoint = rng.random_range(0x0000..0x10FFFF);
                char::from_u32(codepoint).unwrap_or('?')
            })
            .collect();

        let escaped = escape_shell_string(&random_string);

        // Should not panic
        assert!(escaped.len() > 0, "Empty escape for: {:?}", random_string);

        // Should not contain unescaped dangerous characters
        if !escaped.starts_with('\'') {
            // If unquoted, must be safe
            assert!(
                !random_string.contains('$')
                    && !random_string.contains('`')
                    && !random_string.contains(';'),
                "Dangerous chars unquoted: {:?} -> {}",
                random_string,
                escaped
            );
        }
    }
}