codeint-lang-shell 0.10.2

Built-in Shell Language plugin for codeint
Documentation
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
use std::collections::BTreeSet;

use codeint_model::path_file_name;

use crate::commands::{SimpleCommand, parse_commands};
use crate::dialect::ShellDialect;
use crate::lexer::{ShellToken, is_assignment_word};

use super::{ErrexitScope, emit_line};

const ASSIGNMENT_BUILTINS: &[&str] = &["declare", "env", "export", "local", "readonly", "typeset"];

const SHELL_INTERPRETERS: &[&str] = &["ash", "bash", "dash", "ksh", "sh", "zsh"];

const CURL_VALUE_SHORT_FLAGS: &[u8] = b"AbcCdDeEFHKmoTuUwxXyYz";

const READ_VALUE_SHORT_FLAGS: &[u8] = b"adinNptu";

pub(super) fn check_stream(
    tokens: &[ShellToken],
    code_lines: &BTreeSet<u32>,
    scope: ErrexitScope<'_>,
    dialect: ShellDialect,
    hits: &mut BTreeSet<(u32, &'static str)>,
) {
    let parsed = parse_commands(tokens);
    for cmd in &parsed.commands {
        check_unquoted_vars(tokens, cmd, dialect, code_lines, hits);
        check_eval(tokens, cmd, code_lines, hits);
        check_unchecked_cd(tokens, cmd, scope, code_lines, hits);
        check_unguarded_rm_rf(tokens, cmd, code_lines, hits);
        check_read_without_r(tokens, cmd, code_lines, hits);
        check_insecure_tls(tokens, cmd, code_lines, hits);
        check_shell_process_sub_download(tokens, cmd, code_lines, hits);
    }
    check_download_pipelines(
        tokens,
        &parsed.commands,
        &parsed.pipelines,
        code_lines,
        hits,
    );
}

fn check_unquoted_vars(
    tokens: &[ShellToken],
    cmd: &SimpleCommand,
    dialect: ShellDialect,
    code_lines: &BTreeSet<u32>,
    hits: &mut BTreeSet<(u32, &'static str)>,
) {
    let is_assign_builtin = cmd.cmd_idx.is_some_and(|i| {
        !tokens[i].has_quotes && ASSIGNMENT_BUILTINS.contains(&tokens[i].text.as_str())
    });
    if let Some(cmd_idx) = cmd.cmd_idx {
        emit_unquoted_token(&tokens[cmd_idx], dialect, code_lines, hits);
    }
    for &idx in &cmd.arg_indices {
        let tok = &tokens[idx];
        if is_assign_builtin && is_assignment_word(&tok.text) {
            continue;
        }
        emit_unquoted_token(tok, dialect, code_lines, hits);
    }
    for &idx in &cmd.redir_targets {
        emit_unquoted_token(&tokens[idx], dialect, code_lines, hits);
    }
}

fn emit_unquoted_token(
    tok: &ShellToken,
    dialect: ShellDialect,
    code_lines: &BTreeSet<u32>,
    hits: &mut BTreeSet<(u32, &'static str)>,
) {
    if tok.is_heredoc_delimiter {
        return;
    }
    let lines = if dialect == ShellDialect::Zsh {
        &tok.zsh_split_lines
    } else if tok.has_unquoted_var {
        &tok.unquoted_var_lines
    } else {
        return;
    };
    for &line in lines {
        emit_line(line, "unquoted-variable", code_lines, hits);
    }
}

fn check_eval(
    tokens: &[ShellToken],
    cmd: &SimpleCommand,
    code_lines: &BTreeSet<u32>,
    hits: &mut BTreeSet<(u32, &'static str)>,
) {
    let Some(cmd_idx) = cmd.cmd_idx else {
        return;
    };
    if !tokens[cmd_idx].is_word("eval") {
        return;
    }
    if cmd
        .arg_indices
        .iter()
        .any(|&i| references_variable(&tokens[i]))
    {
        emit_line(tokens[cmd_idx].line, "eval-with-variable", code_lines, hits);
    }
}

fn references_variable(tok: &ShellToken) -> bool {
    tok.has_param_expansion
        || tok.has_backtick
        || tok
            .sub_tokens
            .iter()
            .any(|stream| stream.iter().any(references_variable))
}

fn check_unchecked_cd(
    tokens: &[ShellToken],
    cmd: &SimpleCommand,
    scope: ErrexitScope<'_>,
    code_lines: &BTreeSet<u32>,
    hits: &mut BTreeSet<(u32, &'static str)>,
) {
    let Some(cmd_idx) = cmd.cmd_idx else {
        return;
    };
    if scope.at(cmd_idx) || cmd.in_condition {
        return;
    }
    if !tokens[cmd_idx].is_word("cd") {
        return;
    }
    if matches!(cmd.followed_by_op.as_deref(), Some("&&" | "||")) {
        return;
    }
    emit_line(tokens[cmd_idx].line, "unchecked-cd", code_lines, hits);
}

fn check_download_pipelines(
    tokens: &[ShellToken],
    commands: &[SimpleCommand],
    pipelines: &[Vec<usize>],
    code_lines: &BTreeSet<u32>,
    hits: &mut BTreeSet<(u32, &'static str)>,
) {
    for pipe in pipelines {
        if pipe.len() < 2 {
            continue;
        }
        let mut download_line: Option<u32> = None;
        for &cmd_id in pipe {
            let Some((name, line, _)) = effective_command(tokens, &commands[cmd_id]) else {
                continue;
            };
            if matches!(name, "curl" | "wget") && download_line.is_none() {
                download_line = Some(line);
            } else if let Some(dl_line) = download_line
                && SHELL_INTERPRETERS.contains(&name)
            {
                emit_line(dl_line, "pipe-download-to-shell", code_lines, hits);
                break;
            }
        }
    }
}

fn check_shell_process_sub_download(
    tokens: &[ShellToken],
    cmd: &SimpleCommand,
    code_lines: &BTreeSet<u32>,
    hits: &mut BTreeSet<(u32, &'static str)>,
) {
    let Some((name, line, arg_start)) = effective_command(tokens, cmd) else {
        return;
    };
    if !SHELL_INTERPRETERS.contains(&name) {
        return;
    }
    for &idx in &cmd.arg_indices[arg_start..] {
        let tok = &tokens[idx];
        if tok.is_process_sub
            && tok
                .sub_tokens
                .iter()
                .map(Vec::as_slice)
                .any(stream_starts_downloader)
        {
            emit_line(line, "pipe-download-to-shell", code_lines, hits);
        }
    }
}

fn stream_starts_downloader(sub_tokens: &[ShellToken]) -> bool {
    let parsed = parse_commands(sub_tokens);
    parsed.commands.iter().any(|cmd| {
        effective_command(sub_tokens, cmd)
            .is_some_and(|(name, _, _)| matches!(name, "curl" | "wget"))
    })
}

fn check_unguarded_rm_rf(
    tokens: &[ShellToken],
    cmd: &SimpleCommand,
    code_lines: &BTreeSet<u32>,
    hits: &mut BTreeSet<(u32, &'static str)>,
) {
    let Some((name, line, arg_start)) = effective_command(tokens, cmd) else {
        return;
    };
    if name != "rm" {
        return;
    }
    let mut has_r = false;
    let mut has_f = false;
    let mut paths = Vec::new();
    let mut after_double_dash = false;
    for &idx in &cmd.arg_indices[arg_start..] {
        let tok = &tokens[idx];
        if after_double_dash || !tok.text.starts_with('-') || tok.text == "-" {
            paths.push(tok);
            continue;
        }
        if tok.text == "--" {
            after_double_dash = true;
            continue;
        }
        record_rm_flag(&tok.text, &mut has_r, &mut has_f);
    }
    if has_r && has_f && paths.iter().copied().any(is_unguarded_rm_path) {
        emit_line(line, "unguarded-rm-rf", code_lines, hits);
    }
}

fn record_rm_flag(flag: &str, has_r: &mut bool, has_f: &mut bool) {
    if flag == "--recursive" {
        *has_r = true;
    } else if flag == "--force" {
        *has_f = true;
    } else if let Some(short) = flag.strip_prefix('-')
        && !short.starts_with('-')
    {
        if short.contains('r') || short.contains('R') {
            *has_r = true;
        }
        if short.contains('f') {
            *has_f = true;
        }
    }
}

fn is_unguarded_rm_path(tok: &ShellToken) -> bool {
    if !tok.has_var_expansion {
        return false;
    }
    let trimmed = tok.text.trim_start_matches('"');
    let after_slash = trimmed.strip_prefix('/').unwrap_or(trimmed);
    let Some(rest) = after_slash.strip_prefix('$') else {
        return false;
    };
    if let Some(braced) = rest.strip_prefix('{') {
        return is_unguarded_braced_var_before_slash(braced);
    }
    is_simple_var_before_slash(rest)
}

fn is_unguarded_braced_var_before_slash(braced: &str) -> bool {
    let Some((inner, after_brace)) = braced.split_once('}') else {
        return false;
    };
    if inner.contains(":?") || inner.contains('?') {
        return false;
    }
    let tail = after_brace.trim_start_matches('"');
    tail.starts_with('/')
}

fn is_simple_var_before_slash(rest: &str) -> bool {
    let var_len = rest
        .bytes()
        .take_while(|b| b.is_ascii_alphanumeric() || *b == b'_')
        .count();
    if var_len == 0 {
        return false;
    }
    let tail = rest[var_len..].trim_start_matches('"');
    tail.starts_with('/')
}

fn check_read_without_r(
    tokens: &[ShellToken],
    cmd: &SimpleCommand,
    code_lines: &BTreeSet<u32>,
    hits: &mut BTreeSet<(u32, &'static str)>,
) {
    let Some(cmd_idx) = cmd.cmd_idx else {
        return;
    };
    if !tokens[cmd_idx].is_word("read") {
        return;
    }
    if !read_args_include_r(tokens, &cmd.arg_indices) {
        emit_line(tokens[cmd_idx].line, "read-without-r", code_lines, hits);
    }
}

fn read_args_include_r(tokens: &[ShellToken], arg_indices: &[usize]) -> bool {
    let mut skip_next = false;
    for &idx in arg_indices {
        if skip_next {
            skip_next = false;
            continue;
        }
        let text = tokens[idx].text.as_str();
        if text == "--" || !text.starts_with('-') || text == "-" {
            break;
        }
        if let Some(short) = text.strip_prefix('-')
            && !short.starts_with('-')
        {
            if short.contains('r') {
                return true;
            }
            if short
                .as_bytes()
                .last()
                .is_some_and(|b| READ_VALUE_SHORT_FLAGS.contains(b))
            {
                skip_next = true;
            }
        }
    }
    false
}

fn check_insecure_tls(
    tokens: &[ShellToken],
    cmd: &SimpleCommand,
    code_lines: &BTreeSet<u32>,
    hits: &mut BTreeSet<(u32, &'static str)>,
) {
    let Some((name, line, arg_start)) = effective_command(tokens, cmd) else {
        return;
    };
    let args = &cmd.arg_indices[arg_start..];
    let insecure = match name {
        "curl" => curl_has_insecure_flag(tokens, args),
        "wget" => args
            .iter()
            .any(|&i| tokens[i].text == "--no-check-certificate"),
        _ => false,
    };
    if insecure {
        emit_line(line, "insecure-tls-download", code_lines, hits);
    }
}

enum CurlFlagStep {
    Insecure,
    Stop,
    SkipNext,
    Continue,
}

fn curl_has_insecure_flag(tokens: &[ShellToken], args: &[usize]) -> bool {
    let mut skip_next = false;
    for &idx in args {
        if skip_next {
            skip_next = false;
            continue;
        }
        match classify_curl_flag(&tokens[idx].text) {
            CurlFlagStep::Insecure => return true,
            CurlFlagStep::Stop => break,
            CurlFlagStep::SkipNext => skip_next = true,
            CurlFlagStep::Continue => {}
        }
    }
    false
}

fn classify_curl_flag(text: &str) -> CurlFlagStep {
    if text == "--" {
        return CurlFlagStep::Stop;
    }
    if text == "--insecure" {
        return CurlFlagStep::Insecure;
    }
    if text.starts_with("--") {
        return if is_curl_long_value_flag(text) {
            CurlFlagStep::SkipNext
        } else {
            CurlFlagStep::Continue
        };
    }
    let Some(short) = text.strip_prefix('-') else {
        return CurlFlagStep::Continue;
    };
    if short.contains('k') {
        CurlFlagStep::Insecure
    } else if short
        .as_bytes()
        .last()
        .is_some_and(|b| CURL_VALUE_SHORT_FLAGS.contains(b))
    {
        CurlFlagStep::SkipNext
    } else {
        CurlFlagStep::Continue
    }
}

fn is_curl_long_value_flag(flag: &str) -> bool {
    !flag.contains('=')
        && matches!(
            flag,
            "--header"
                | "--data"
                | "--data-raw"
                | "--data-binary"
                | "--data-urlencode"
                | "--output"
                | "--request"
                | "--user"
                | "--user-agent"
                | "--cookie"
                | "--cookie-jar"
                | "--form"
                | "--max-time"
                | "--connect-timeout"
                | "--write-out"
                | "--config"
                | "--proxy"
                | "--referer"
                | "--cacert"
                | "--cert"
                | "--key"
        )
}

fn effective_command<'a>(
    tokens: &'a [ShellToken],
    cmd: &SimpleCommand,
) -> Option<(&'a str, u32, usize)> {
    let cmd_idx = cmd.cmd_idx?;
    let cmd_tok = &tokens[cmd_idx];
    let base = path_file_name(&cmd_tok.text);
    if !matches!(base, "sudo" | "env" | "command") {
        return Some((base, cmd_tok.line, 0));
    }
    for (pos, &arg_idx) in cmd.arg_indices.iter().enumerate() {
        let arg = &tokens[arg_idx];
        if arg.text.starts_with('-') || is_assignment_word(&arg.text) {
            continue;
        }
        return Some((path_file_name(&arg.text), arg.line, pos + 1));
    }
    None
}