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
// ! Standard Library Support
//!
//! This module provides support for stdlib functions that are transpiled
//! to POSIX shell runtime functions.

/// Check if a function name is a stdlib function
pub fn is_stdlib_function(name: &str) -> bool {
    matches!(
        name,
        // String module
        "string_trim"
            | "string_contains"
            | "string_len"
            | "string_split"
            | "string_replace"
            | "string_to_upper"
            | "string_to_lower"
            | "string_starts_with"
            | "string_ends_with"
            // File system module
            | "fs_exists"
            | "fs_read_file"
            | "fs_write_file"
            | "fs_copy"
            | "fs_remove"
            | "fs_is_file"
            | "fs_is_dir"
            // Array module
            | "array_len"
            | "array_join"
            // Environment module (Sprint 27a)
            | "env"
            | "env_var_or"
            // Arguments module (Sprint 27b)
            | "arg"
            | "args"
            | "arg_count"
            // Exit code module (Sprint 27c)
            | "exit_code"
            // Command execution module (GH-148)
            | "capture"
            | "exec"
            | "exit"
            | "sleep"
            // File iteration module (GH-148)
            | "glob"
            // Directory/file management module (GH-148)
            | "mkdir"
            | "mv"
            | "chmod"
    )
}

/// Get the shell function name for a stdlib function
pub fn get_shell_function_name(name: &str) -> String {
    format!("rash_{}", name)
}

// Re-export metadata from extracted module (file size discipline)
pub use crate::stdlib_metadata::{StdlibFunction, STDLIB_FUNCTIONS};

// Tests use STDLIB_FUNCTIONS below — the const is defined in stdlib_metadata.rs
// Legacy anchor (do not add new entries here — edit stdlib_metadata.rs):
const _STDLIB_METADATA_ANCHOR: () = {
    // Compile-time check: STDLIB_FUNCTIONS exists and is non-empty
    assert!(!STDLIB_FUNCTIONS.is_empty());
};
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_is_stdlib_function() {
        // String functions
        assert!(is_stdlib_function("string_trim"));
        assert!(is_stdlib_function("string_replace"));
        assert!(is_stdlib_function("string_to_upper"));
        assert!(is_stdlib_function("string_to_lower"));

        // File system functions
        assert!(is_stdlib_function("fs_exists"));
        assert!(is_stdlib_function("fs_copy"));
        assert!(is_stdlib_function("fs_remove"));
        assert!(is_stdlib_function("fs_is_file"));
        assert!(is_stdlib_function("fs_is_dir"));

        // Command execution functions (GH-148)
        assert!(is_stdlib_function("capture"));
        assert!(is_stdlib_function("exec"));
        assert!(is_stdlib_function("exit"));
        assert!(is_stdlib_function("sleep"));

        // String prefix/suffix functions (GH-148)
        assert!(is_stdlib_function("string_starts_with"));
        assert!(is_stdlib_function("string_ends_with"));

        // File iteration functions (GH-148)
        assert!(is_stdlib_function("glob"));

        // Directory/file management functions (GH-148)
        assert!(is_stdlib_function("mkdir"));
        assert!(is_stdlib_function("mv"));
        assert!(is_stdlib_function("chmod"));

        // Not stdlib functions
        assert!(!is_stdlib_function("custom_function"));
        assert!(!is_stdlib_function("println"));
    }

    #[test]
    fn test_get_shell_function_name() {
        assert_eq!(get_shell_function_name("string_trim"), "rash_string_trim");
        assert_eq!(get_shell_function_name("fs_exists"), "rash_fs_exists");
    }

    // Sprint 27a: Environment Variables Support - RED PHASE
    #[test]
    fn test_stdlib_env_function_recognized() {
        // RED: This test will fail until we add "env" to is_stdlib_function()
        assert!(
            is_stdlib_function("env"),
            "env() should be recognized as stdlib function"
        );
    }

    #[test]
    fn test_stdlib_env_var_or_function_recognized() {
        // RED: This test will fail until we add "env_var_or" to is_stdlib_function()
        assert!(
            is_stdlib_function("env_var_or"),
            "env_var_or() should be recognized as stdlib function"
        );
    }

    // Sprint 27b: Command-Line Arguments Support - RED PHASE
    #[test]
    fn test_stdlib_arg_function_recognized() {
        // RED: This test will fail until we add "arg" to is_stdlib_function()
        assert!(
            is_stdlib_function("arg"),
            "arg() should be recognized as stdlib function"
        );
    }

    #[test]
    fn test_stdlib_args_function_recognized() {
        // RED: This test will fail until we add "args" to is_stdlib_function()
        assert!(
            is_stdlib_function("args"),
            "args() should be recognized as stdlib function"
        );
    }

    #[test]
    fn test_stdlib_arg_count_function_recognized() {
        // RED: This test will fail until we add "arg_count" to is_stdlib_function()
        assert!(
            is_stdlib_function("arg_count"),
            "arg_count() should be recognized as stdlib function"
        );
    }

    // Sprint 27c: Exit Code Handling - RED PHASE
    #[test]
    fn test_stdlib_exit_code_function_recognized() {
        // RED: This test will fail until we add "exit_code" to is_stdlib_function()
        assert!(
            is_stdlib_function("exit_code"),
            "exit_code() should be recognized as stdlib function"
        );
    }

    #[test]
    fn test_stdlib_exit_code_metadata() {
        // RED: This test will fail until we add metadata for exit_code
        let metadata: Vec<&StdlibFunction> = STDLIB_FUNCTIONS
            .iter()
            .filter(|f| f.name == "exit_code")
            .collect();

        assert_eq!(metadata.len(), 1, "exit_code should have metadata entry");
        assert_eq!(
            metadata[0].module, "status",
            "exit_code should be in 'status' module"
        );
        assert_eq!(
            metadata[0].shell_name, "inline_exit_code",
            "exit_code should use inline shell syntax"
        );
    }

    // Sprint 28: Complete Missing Stdlib Functions - RED PHASE

    #[test]
    fn test_stdlib_string_split_recognized() {
        // RED: string_split is in is_stdlib_function() but needs metadata
        assert!(
            is_stdlib_function("string_split"),
            "string_split() should be recognized as stdlib function"
        );
    }

    #[test]
    fn test_stdlib_string_split_metadata() {
        // RED: This test will fail until we add metadata for string_split
        let metadata: Vec<&StdlibFunction> = STDLIB_FUNCTIONS
            .iter()
            .filter(|f| f.name == "string_split")
            .collect();

        assert_eq!(metadata.len(), 1, "string_split should have metadata entry");
        assert_eq!(
            metadata[0].module, "string",
            "string_split should be in 'string' module"
        );
        assert_eq!(
            metadata[0].shell_name, "rash_string_split",
            "string_split should use rash_ prefix"
        );
    }

    #[test]
    fn test_stdlib_array_len_recognized() {
        // RED: array_len is in is_stdlib_function() but needs metadata
        assert!(
            is_stdlib_function("array_len"),
            "array_len() should be recognized as stdlib function"
        );
    }

    #[test]
    fn test_stdlib_array_len_metadata() {
        // RED: This test will fail until we add metadata for array_len
        let metadata: Vec<&StdlibFunction> = STDLIB_FUNCTIONS
            .iter()
            .filter(|f| f.name == "array_len")
            .collect();

        assert_eq!(metadata.len(), 1, "array_len should have metadata entry");
        assert_eq!(
            metadata[0].module, "array",
            "array_len should be in 'array' module"
        );
        assert_eq!(
            metadata[0].shell_name, "rash_array_len",
            "array_len should use rash_ prefix"
        );
    }

    #[test]
    fn test_stdlib_array_join_recognized() {
        // RED: array_join is in is_stdlib_function() but needs metadata
        assert!(
            is_stdlib_function("array_join"),
            "array_join() should be recognized as stdlib function"
        );
    }

    #[test]
    fn test_GH148_starts_with_recognized() {
        assert!(
            is_stdlib_function("string_starts_with"),
            "string_starts_with() should be recognized as stdlib function"
        );
    }

    #[test]
    fn test_GH148_ends_with_recognized() {
        assert!(
            is_stdlib_function("string_ends_with"),
            "string_ends_with() should be recognized as stdlib function"
        );
    }

    #[test]
    fn test_GH148_starts_with_metadata() {
        let metadata: Vec<&StdlibFunction> = STDLIB_FUNCTIONS
            .iter()
            .filter(|f| f.name == "string_starts_with")
            .collect();

        assert_eq!(
            metadata.len(),
            1,
            "string_starts_with should have metadata entry"
        );
        assert_eq!(metadata[0].module, "string");
        assert_eq!(metadata[0].shell_name, "rash_string_starts_with");
    }

    #[test]
    fn test_GH148_ends_with_metadata() {
        let metadata: Vec<&StdlibFunction> = STDLIB_FUNCTIONS
            .iter()
            .filter(|f| f.name == "string_ends_with")
            .collect();

        assert_eq!(
            metadata.len(),
            1,
            "string_ends_with should have metadata entry"
        );
        assert_eq!(metadata[0].module, "string");
        assert_eq!(metadata[0].shell_name, "rash_string_ends_with");
    }

    #[test]
    fn test_GH148_glob_recognized() {
        assert!(
            is_stdlib_function("glob"),
            "glob() should be recognized as stdlib function"
        );
    }

    #[test]
    fn test_GH148_glob_metadata() {
        let metadata: Vec<&StdlibFunction> = STDLIB_FUNCTIONS
            .iter()
            .filter(|f| f.name == "glob")
            .collect();

        assert_eq!(metadata.len(), 1, "glob should have metadata entry");
        assert_eq!(metadata[0].module, "fs", "glob should be in 'fs' module");
        assert_eq!(
            metadata[0].shell_name, "inline_glob",
            "glob should use inline shell syntax"
        );
    }

    #[test]
    fn test_stdlib_array_join_metadata() {
        // RED: This test will fail until we add metadata for array_join
        let metadata: Vec<&StdlibFunction> = STDLIB_FUNCTIONS
            .iter()
            .filter(|f| f.name == "array_join")
            .collect();

        assert_eq!(metadata.len(), 1, "array_join should have metadata entry");
        assert_eq!(
            metadata[0].module, "array",
            "array_join should be in 'array' module"
        );
        assert_eq!(
            metadata[0].shell_name, "rash_array_join",
            "array_join should use rash_ prefix"
        );
    }
}

// Sprint 27a: Security Tests - RED PHASE

/// RED TEST: Invalid variable names should be rejected
/// Tests that env() rejects variable names with invalid characters
#[test]
fn test_env_rejects_invalid_var_names() {
    // Valid var names: alphanumeric + underscore only
    assert!(is_valid_var_name("HOME"));
    assert!(is_valid_var_name("MY_VAR"));
    assert!(is_valid_var_name("VAR123"));
    assert!(is_valid_var_name("_PRIVATE"));

    // Invalid var names (injection attempts)
    assert!(!is_valid_var_name("'; rm -rf /; #"));
    assert!(!is_valid_var_name("VAR; echo hack"));
    assert!(!is_valid_var_name("$(whoami)"));
    assert!(!is_valid_var_name("VAR`id`"));
    assert!(!is_valid_var_name("VAR$OTHER"));
    assert!(!is_valid_var_name("VAR-NAME")); // Dash not allowed
    assert!(!is_valid_var_name("VAR.NAME")); // Dot not allowed
}

/// RED TEST: Default values must be properly escaped
/// Tests that env_var_or() safely handles special characters in defaults
#[test]
fn test_env_var_or_escapes_default() {
    // These should be safely handled (no injection)
    let safe_defaults = vec![
        "/usr/local",
        "hello world",
        "/path/to/file",
        "value-with-dash",
    ];

    for default in safe_defaults {
        // RED: This will fail until we implement escaping
        assert!(
            is_safe_default_value(default),
            "Default '{}' should be considered safe",
            default
        );
    }

    // These are dangerous and should either be escaped or rejected
    let dangerous_defaults = vec![
        "\"; rm -rf /; echo \"",
        "value`whoami`",
        "value$(id)",
        "value;ls",
    ];

    for default in dangerous_defaults {
        // RED: This will fail until we implement injection detection
        assert!(
            contains_injection_attempt(default),
            "Default '{}' contains injection attempt and should be detected",
            default
        );
    }
}

// Helper functions for validation (used by test code, Phase 2 production integration pending)
#[cfg(test)]
fn is_valid_var_name(name: &str) -> bool {
    name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
}

#[cfg(test)]
fn is_safe_default_value(_value: &str) -> bool {
    true // Placeholder - will be refined in Phase 2
}

#[cfg(test)]
fn contains_injection_attempt(value: &str) -> bool {
    value.contains(';') || value.contains('`') || value.contains("$(") || value.contains("${")
}