shuck-linter 0.0.26

Lint rule engine and checker for shell scripts
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
use crate::{Checker, Edit, Fix, FixAvailability, Rule, Violation};

pub struct SingleQuotedLiteral;

impl Violation for SingleQuotedLiteral {
    const FIX_AVAILABILITY: FixAvailability = FixAvailability::Sometimes;

    fn rule() -> Rule {
        Rule::SingleQuotedLiteral
    }

    fn message(&self) -> String {
        "shell expansion inside single quotes stays literal".to_owned()
    }

    fn fix_title(&self) -> Option<String> {
        Some("rewrite the fragment with double quotes".to_owned())
    }
}

#[derive(Debug, Clone, Copy, Default)]
struct ScanContext<'a> {
    command_name: Option<&'a str>,
    assignment_target: Option<&'a str>,
    variable_set_operand: bool,
    literal_expansion_exempt: bool,
}

pub fn single_quoted_literal(checker: &mut Checker) {
    let source = checker.source();
    let diagnostics = checker
        .facts()
        .single_quoted_fragments()
        .iter()
        .filter_map(|fragment| {
            if fragment.dollar_quoted() {
                return None;
            }

            let context = ScanContext {
                command_name: fragment.command_name(),
                assignment_target: fragment.assignment_target(),
                variable_set_operand: fragment.variable_set_operand(),
                literal_expansion_exempt: fragment.literal_expansion_exempt(),
            };

            should_report_single_quoted_literal(fragment.span().slice(source), context).then(|| {
                let diagnostic =
                    crate::Diagnostic::new(SingleQuotedLiteral, fragment.diagnostic_span());
                match single_quoted_literal_fix(fragment.span(), fragment.span().slice(source)) {
                    Some(fix) => diagnostic.with_fix(fix),
                    None => diagnostic,
                }
            })
        })
        .collect::<Vec<_>>();

    for diagnostic in diagnostics {
        checker.report_diagnostic_dedup(diagnostic);
    }
}

fn single_quoted_literal_fix(span: shuck_ast::Span, text: &str) -> Option<Fix> {
    quoted_fragment_to_double_quotes(text)
        .map(|replacement| Fix::unsafe_edit(Edit::replacement(replacement, span)))
}

fn quoted_fragment_to_double_quotes(text: &str) -> Option<String> {
    if !(text.starts_with('\'') && text.ends_with('\'')) {
        return None;
    }

    let body = &text[1..text.len() - 1];
    let mut replacement = String::with_capacity(body.len() + 2);
    replacement.push('"');
    for ch in body.chars() {
        match ch {
            '"' | '\\' => {
                replacement.push('\\');
                replacement.push(ch);
            }
            _ => replacement.push(ch),
        }
    }
    replacement.push('"');
    Some(replacement)
}

fn should_report_single_quoted_literal(text: &str, context: ScanContext<'_>) -> bool {
    if !contains_sc2016_trigger(text)
        || context.variable_set_operand
        || context.literal_expansion_exempt
    {
        return false;
    }

    if context.command_name == Some("sed") {
        return !sed_text_is_exempt(text);
    }

    if context
        .assignment_target
        .is_some_and(assignment_target_is_exempt)
    {
        return false;
    }

    true
}

fn contains_sc2016_trigger(text: &str) -> bool {
    let bytes = text.as_bytes();
    let mut index = 0usize;

    while index + 1 < bytes.len() {
        if bytes[index] == b'$'
            && matches!(
                bytes[index + 1],
                b'{' | b'(' | b'_' | b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9'
            )
        {
            return true;
        }

        if bytes[index] == b'`'
            && bytes.get(index + 1).is_some_and(|next| *next != b'`')
            && bytes[index + 2..].contains(&b'`')
        {
            return true;
        }

        index += 1;
    }

    false
}

fn sed_text_is_exempt(text: &str) -> bool {
    let bytes = text.as_bytes();

    for index in 0..bytes.len().saturating_sub(1) {
        if bytes[index] != b'$' {
            continue;
        }

        let next = bytes[index + 1];
        if !matches!(next, b'{' | b'd' | b'p' | b's' | b'a' | b'i' | b'c') {
            continue;
        }

        let following = bytes.get(index + 2).copied();
        if following.is_none_or(|byte| !byte.is_ascii_alphabetic()) {
            return true;
        }
    }

    false
}

fn assignment_target_is_exempt(target: &str) -> bool {
    matches!(target, "PS1" | "PS2" | "PS3" | "PS4" | "PROMPT_COMMAND")
}

#[cfg(test)]
mod tests {
    use std::path::Path;

    use super::{
        assignment_target_is_exempt, contains_sc2016_trigger, quoted_fragment_to_double_quotes,
        sed_text_is_exempt,
    };
    use crate::test::{test_path_with_fix, test_snippet, test_snippet_with_fix};
    use crate::{Applicability, Diagnostic, LinterSettings, Rule, assert_diagnostics_diff};

    fn c005(source: &str) -> usize {
        c005_diagnostics(source).len()
    }

    fn c005_diagnostics(source: &str) -> Vec<Diagnostic> {
        test_snippet(source, &LinterSettings::for_rule(Rule::SingleQuotedLiteral))
    }

    #[test]
    fn rewrites_plain_single_quoted_fragments_as_double_quoted_fragments() {
        assert_eq!(
            quoted_fragment_to_double_quotes("'$HOME'"),
            Some("\"$HOME\"".to_owned())
        );
        assert_eq!(
            quoted_fragment_to_double_quotes("'\"$HOME\" and \\\\path'"),
            Some("\"\\\"$HOME\\\" and \\\\\\\\path\"".to_owned())
        );
        assert_eq!(quoted_fragment_to_double_quotes("$'$HOME'"), None);
    }

    #[test]
    fn detects_sc2016_variable_like_sequences_and_backticks() {
        assert!(contains_sc2016_trigger("$HOME"));
        assert!(contains_sc2016_trigger("${name:-default}"));
        assert!(contains_sc2016_trigger("$(pwd)"));
        assert!(contains_sc2016_trigger("$1"));
        assert!(contains_sc2016_trigger("`pwd`"));
    }

    #[test]
    fn ignores_shellcheck_exempt_special_parameter_sequences() {
        for text in ["$$", "$?", "$#", "$@", "$*", "$!", "$-", "$", "hello world"] {
            assert!(!contains_sc2016_trigger(text), "{text}");
        }
    }

    #[test]
    fn recognizes_sed_exemptions() {
        assert!(sed_text_is_exempt("$p"));
        assert!(sed_text_is_exempt("${/lol/d}"));
        assert!(!sed_text_is_exempt("$pattern"));
    }

    #[test]
    fn recognizes_prompt_assignment_exemptions() {
        for target in ["PS1", "PS2", "PS3", "PS4", "PROMPT_COMMAND"] {
            assert!(assignment_target_is_exempt(target), "{target}");
        }

        assert!(!assignment_target_is_exempt("HOME"));
    }

    #[test]
    fn rule_detects_backticks_and_respects_exemptions() {
        assert_eq!(c005("echo '`pwd`'\n"), 1);
        assert_eq!(c005("echo '$@'\n"), 0);
        assert_eq!(c005("awk '{print $1}'\n"), 0);
        assert_eq!(c005("PS1='$PWD \\\\$ '\n"), 0);
        assert_eq!(c005("command jq '$__loc__'\n"), 0);
        assert_eq!(c005("jq --arg loc '$__loc__' '$loc'\n"), 0);
        assert_eq!(c005("sed -n '$p'\n"), 0);
        assert_eq!(c005("sed -n '$pattern'\n"), 1);
    }

    #[test]
    fn command_domain_exemptions_are_computed_by_facts() {
        assert_eq!(c005("awk '{print $1}' file\n"), 0);
        assert_eq!(c005("awk -v value='$HOME' '{print value}' file\n"), 0);
        assert_eq!(c005("jq '$item.name' file\n"), 0);
        assert_eq!(c005("jq --arg name '$HOME' '$name' file\n"), 0);
        assert_eq!(c005("bash -c 'echo $HOME'\n"), 0);
        assert_eq!(c005("bash '$HOME'\n"), 1);
        assert_eq!(c005("ssh host 'echo $HOME'\n"), 0);
        assert_eq!(c005("ssh '$HOME' uptime\n"), 1);
        assert_eq!(c005("sudo sh -c 'echo $HOME'\n"), 0);
        assert_eq!(c005("sudo echo '$HOME'\n"), 1);
        assert_eq!(c005("trap 'echo $SECONDS' EXIT\n"), 0);
        assert_eq!(c005("eval 'echo $HOME'\n"), 0);
        assert_eq!(c005("rename 's/(.)a/$1/g' *\n"), 0);
        assert_eq!(c005("rg '$HOME' file\n"), 0);
        assert_eq!(c005("rg pattern '$HOME'\n"), 1);
        assert_eq!(c005("jq '.' '$HOME'\n"), 1);
        assert_eq!(
            c005("dpkg-query -W -f '${db:Status-Status}\\n' package\n"),
            0
        );
        assert_eq!(c005("docker inspect -f '{{.Name}}' container\n"), 0);
        assert_eq!(
            c005("docker run image sh -c 'echo $JETTY_HOME/start.jar'\n"),
            0
        );
        assert_eq!(
            c005("docker run -d \"$server_image\" sh -c 'echo $JETTY_HOME/start.jar'\n"),
            0
        );
        assert_eq!(
            c005("docker run --entrypoint sh image -c 'command -v gcc'\n"),
            0
        );
        assert_eq!(
            c005("docker run --entrypoint sh \"$image\" -c 'command -v gcc'\n"),
            0
        );
        assert_eq!(c005("xprop -set WM_NAME '$HOME'\n"), 0);
        assert_eq!(
            c005(
                "PERLIO=:utf8 perl -pe '$_=lc'\nperl -MConfig -le 'print $Config{installvendorlib}'\n"
            ),
            0
        );
    }

    #[test]
    fn corpus_regression_teamcity_awk_is_exempt() {
        assert_eq!(c005("awk '{print $5}' || :\n"), 0);
    }

    #[test]
    fn corpus_regression_alias_wrapper_is_exempt() {
        assert_eq!(c005("alias hosts='sudo $EDITOR /etc/hosts'\n"), 0);
    }

    #[test]
    fn corpus_regression_special_parameters_are_exempt() {
        assert_eq!(c005("SHOBJ_LDFLAGS='-shared -Wl,-h,$@'\n"), 0);
        assert_eq!(c005("SHOBJ_LDFLAGS='-G -dy -z text -i -h $@'\n"), 0);
    }

    #[test]
    fn corpus_regression_backticks_are_reported() {
        let diagnostics = c005_diagnostics("SHOBJ_ARCHFLAGS='-arch_only `/usr/bin/arch`'\n");
        assert_eq!(diagnostics.len(), 1);
        assert_eq!(diagnostics[0].span.start.line, 1);
        assert_eq!(diagnostics[0].span.start.column, 17);
    }

    #[test]
    fn corpus_regression_openvpn_sample_anchors_on_opening_quote() {
        let diagnostics = c005_diagnostics(
            "if ! grep -q sbin <<< \"$PATH\"; then\n\techo '$PATH does not include sbin. Try using \"su -\" instead of \"su\".'\nfi\n",
        );

        assert_eq!(diagnostics.len(), 1);
        assert_eq!(diagnostics[0].span.start.line, 2);
        assert_eq!(diagnostics[0].span.start.column, 7);
    }

    #[test]
    fn diagnostic_span_covers_the_full_single_quoted_region_and_attaches_fix_metadata() {
        let diagnostics = c005_diagnostics("echo '$HOME'\n");

        assert_eq!(diagnostics.len(), 1);
        assert_eq!(diagnostics[0].span.start.line, 1);
        assert_eq!(diagnostics[0].span.start.column, 6);
        assert_eq!(diagnostics[0].span.end.line, 1);
        assert_eq!(diagnostics[0].span.end.column, 13);
        assert_eq!(
            diagnostics[0].fix.as_ref().map(|fix| fix.applicability()),
            Some(Applicability::Unsafe)
        );
        assert_eq!(
            diagnostics[0].fix_title.as_deref(),
            Some("rewrite the fragment with double quotes")
        );
    }

    #[test]
    fn corpus_regression_omarchy_sample_anchors_on_opening_quote() {
        let diagnostics = c005_diagnostics(
            "  sed -i '/bindd = SUPER, RETURN, Terminal, exec, \\$terminal/ s|$| --working-directory=$(omarchy-cmd-terminal-cwd)|' ~/.config/hypr/bindings.conf\n",
        );

        assert_eq!(diagnostics.len(), 1);
        assert_eq!(diagnostics[0].span.start.line, 1);
        assert_eq!(diagnostics[0].span.start.column, 10);
    }

    #[test]
    fn variable_set_operand_helper_does_not_panic_on_incomplete_operands() {
        assert_eq!(c005("test -v\n"), 0);
        assert_eq!(c005("test -v name\n"), 0);
    }

    #[test]
    fn reports_single_quoted_literals_inside_case_patterns() {
        assert_eq!(c005("case $x in '$HOME') : ;; esac\n"), 1);
    }

    #[test]
    fn reports_single_quoted_literals_inside_parameter_patterns() {
        assert_eq!(c005("echo ${value#'$HOME'}\n"), 1);
    }

    #[test]
    fn ignores_single_quoted_literals_split_by_double_quoted_expansions() {
        assert_eq!(c005("rx=${rx:-'prefix'\"$pkgname\"'suffix'}\n"), 0);
    }

    #[test]
    fn reports_single_quoted_literals_inside_keyed_array_subscripts() {
        assert_eq!(c005("declare -A map=(['$HOME']=1)\n"), 1);
    }

    #[test]
    fn applies_unsafe_fix_to_reported_single_quoted_fragments() {
        let source = "\
#!/bin/sh
echo '$HOME'
printf '%s\\n' '${value:-fallback}'
msg='$(pwd)'
echo '`pwd`'
echo '\"$HOME\" and \\\\path'
";
        let result = test_snippet_with_fix(
            source,
            &LinterSettings::for_rule(Rule::SingleQuotedLiteral),
            Applicability::Unsafe,
        );

        assert_eq!(result.fixes_applied, 5);
        assert_eq!(
            result.fixed_source,
            "\
#!/bin/sh
echo \"$HOME\"
printf '%s\\n' \"${value:-fallback}\"
msg=\"$(pwd)\"
echo \"`pwd`\"
echo \"\\\"$HOME\\\" and \\\\\\\\path\"
"
        );
        assert!(result.fixed_diagnostics.is_empty());
    }

    #[test]
    fn ignores_ansi_c_single_quoted_fragments() {
        assert_eq!(c005("echo $'$HOME'\n"), 0);
        assert_eq!(
            c005("cmd --payload $'proxy_set_header X-Forwarded-Proto $scheme;'\n"),
            0
        );
    }

    #[test]
    fn leaves_ansi_c_single_quoted_fragments_unchanged_when_fixing() {
        let source = "echo $'$HOME'\n";
        let result = test_snippet_with_fix(
            source,
            &LinterSettings::for_rule(Rule::SingleQuotedLiteral),
            Applicability::Unsafe,
        );

        assert!(result.diagnostics.is_empty());
        assert_eq!(result.fixes_applied, 0);
        assert_eq!(result.fixed_source, source);
        assert!(result.fixed_diagnostics.is_empty());
    }

    #[test]
    fn corpus_regression_tmux_compgen_wordlist_is_reported() {
        let source = "if [[ $option_type ]]; then\n\
             _comp_cmd_tmux__value \"$subcommand\" \"$option_type\"\n\
             return\n\
         elif ((positional_start < 0)) && [[ $cur == -* ]]; then\n\
             _comp_compgen -- -W '\"${!options[@]}\"'\n\
             return\n\
         fi\n";
        let diagnostics = c005_diagnostics(source);

        assert_eq!(diagnostics.len(), 1);
        assert_eq!(diagnostics[0].span.start.line, 5);
        assert_eq!(diagnostics[0].span.slice(source), "'\"${!options[@]}\"'");
    }

    #[test]
    fn multiline_arithmetic_for_headers_do_not_drop_earlier_function_body_fragments() {
        let source = "\
subcommand()
{
    if [[ $option_type ]]; then
        _value \"$subcommand\" \"$option_type\"
        return
    elif ((positional_start < 0)) && [[ $cur == -* ]]; then
        _comp_compgen -- -W '\"${!options[@]}\"'
        return
    fi

    local args_index=$positional_start
    local usage_args_index
    for ((\\
    usage_args_index = 0;  \\
    usage_args_index < ${#args[@]};  \\
    args_index++, usage_args_index++)); do
        :
    done
}
";
        let diagnostics = c005_diagnostics(source);

        assert_eq!(diagnostics.len(), 1);
        assert_eq!(diagnostics[0].span.start.line, 7);
        assert_eq!(diagnostics[0].span.slice(source), "'\"${!options[@]}\"'");
    }

    #[test]
    fn multiline_sed_program_assignments_anchor_on_the_assignment_line() {
        let source = "\
lt_compile=`echo \"$ac_compile\" | $SED \\\n\
-e 's:.*FLAGS}\\{0,1\\} :&$lt_compiler_flag :; t' \\\n\
-e 's: [^ ]*conftest\\.: $lt_compiler_flag&:; t' \\\n\
-e 's:$: $lt_compiler_flag:'`\n";
        let diagnostics = c005_diagnostics(source);

        assert_eq!(diagnostics.len(), 3);
        assert_eq!(
            diagnostics
                .iter()
                .map(|diagnostic| {
                    (
                        diagnostic.span.start.line,
                        diagnostic.span.start.column,
                        diagnostic.span.end.line,
                        diagnostic.span.end.column,
                    )
                })
                .collect::<Vec<_>>(),
            vec![(1, 42, 1, 86), (1, 90, 1, 134), (1, 138, 1, 163)]
        );
    }

    #[test]
    fn continued_command_arguments_stay_on_physical_lines() {
        let source = "\
sed -i -e 's/foo/$bar/' \\\n\
  -e 's/baz/$qux/' file\n";
        let diagnostics = c005_diagnostics(source);

        assert_eq!(diagnostics.len(), 2);
        assert_eq!(
            diagnostics
                .iter()
                .map(|diagnostic| diagnostic.span.start.line)
                .collect::<Vec<_>>(),
            vec![1, 2]
        );
        assert_eq!(
            diagnostics
                .iter()
                .map(|diagnostic| diagnostic.span.slice(source))
                .collect::<Vec<_>>(),
            vec!["'s/foo/$bar/'", "'s/baz/$qux/'"]
        );
    }

    #[test]
    fn dollar_paren_command_substitutions_stay_on_physical_lines() {
        let source = "\
x=$(printf %s \\\n\
'$HOME')\n";
        let diagnostics = c005_diagnostics(source);

        assert_eq!(diagnostics.len(), 1);
        assert_eq!(
            (
                diagnostics[0].span.start.line,
                diagnostics[0].span.start.column,
                diagnostics[0].span.end.line,
                diagnostics[0].span.end.column,
            ),
            (2, 1, 2, 8)
        );
    }

    #[test]
    fn backtick_sed_replacements_match_shellcheck_single_line_span() {
        let source = "\
relink_command=`$ECHO \"$compile_var$compile_command$compile_rpath\" | $SED 's%@OUTPUT@%\\$progdir/\\$file%g'`\n";
        let diagnostics = c005_diagnostics(source);

        assert_eq!(diagnostics.len(), 1);
        assert_eq!(
            (
                diagnostics[0].span.start.line,
                diagnostics[0].span.start.column,
                diagnostics[0].span.end.line,
                diagnostics[0].span.end.column,
            ),
            (1, 75, 1, 104)
        );
    }

    #[test]
    fn ignores_single_quoted_sequences_inside_expanding_heredoc_bodies() {
        assert_eq!(
            c005("cat <<EOF\n'$HOME should expand but does not'\nEOF\n",),
            0
        );
    }

    #[test]
    fn ignores_multiple_single_quoted_sequences_inside_expanding_heredoc_bodies() {
        assert_eq!(c005("cat <<EOF\n'$HOME' and '$(pwd)'\nEOF\n"), 0);
    }

    #[test]
    fn ignores_single_quoted_sequences_inside_tab_stripped_heredoc_bodies() {
        assert_eq!(c005("cat <<-EOF\n\t'$HOME'\nEOF\n"), 0);
    }

    #[test]
    fn ignores_realistic_config_template_payloads_in_heredocs() {
        assert_eq!(
            c005("cat <<EOF > .cargo/config\ndirectory = '$(pwd)/vendor'\nEOF\n"),
            0
        );
    }

    #[test]
    fn ignores_single_quoted_here_strings_passed_to_shell_commands() {
        assert_eq!(
            c005(
                "bash --init-file \"${BASH_IT?}/bash_it.sh\" -i <<< '_bash-it-flash-term \"${#BASH_IT_THEME}\" \"${BASH_IT_THEME}\"'\n",
            ),
            0
        );
    }

    #[test]
    fn reports_single_quoted_plain_redirect_targets_for_exempt_commands() {
        assert_eq!(c005("bash > '$HOME'\n"), 1);
    }

    #[test]
    fn ignores_single_quoted_sequences_inside_quoted_heredoc_bodies() {
        assert_eq!(c005("cat <<'EOF'\n'$HOME'\nEOF\n"), 0);
    }

    #[test]
    fn snapshots_unsafe_fix_output_for_fixture() -> anyhow::Result<()> {
        let result = test_path_with_fix(
            Path::new("correctness").join("C005.sh").as_path(),
            &LinterSettings::for_rule(Rule::SingleQuotedLiteral),
            Applicability::Unsafe,
        )?;

        assert_diagnostics_diff!("C005_fix_C005.sh", result);
        Ok(())
    }
}