syncable_cli/analyzer/hadolint/rules/
dl4001.rs

1//! DL4001: Either use wget or curl, but not both
2//!
3//! When downloading files, use either wget or curl consistently, not both.
4
5use crate::analyzer::hadolint::parser::instruction::Instruction;
6use crate::analyzer::hadolint::rules::{very_custom_rule, VeryCustomRule, RuleState, CheckFailure};
7use crate::analyzer::hadolint::shell::ParsedShell;
8use crate::analyzer::hadolint::types::Severity;
9
10pub fn rule() -> VeryCustomRule<
11    impl Fn(&mut RuleState, u32, &Instruction, Option<&ParsedShell>) + Send + Sync,
12    impl Fn(RuleState) -> Vec<CheckFailure> + Send + Sync
13> {
14    very_custom_rule(
15        "DL4001",
16        Severity::Warning,
17        "Either use `wget` or `curl`, but not both.",
18        |state, line, instr, shell| {
19            if let Instruction::Run(_) = instr {
20                if let Some(shell) = shell {
21                    if shell.any_command(|cmd| cmd.name == "wget") {
22                        // Store wget lines as comma-separated string
23                        let existing = state.data.get_string("wget_lines").unwrap_or("").to_string();
24                        let new = if existing.is_empty() {
25                            line.to_string()
26                        } else {
27                            format!("{},{}", existing, line)
28                        };
29                        state.data.set_string("wget_lines", new);
30                    }
31                    if shell.any_command(|cmd| cmd.name == "curl") {
32                        let existing = state.data.get_string("curl_lines").unwrap_or("").to_string();
33                        let new = if existing.is_empty() {
34                            line.to_string()
35                        } else {
36                            format!("{},{}", existing, line)
37                        };
38                        state.data.set_string("curl_lines", new);
39                    }
40                }
41            }
42        },
43        |state| {
44            let wget_lines = state.data.get_string("wget_lines").unwrap_or("");
45            let curl_lines = state.data.get_string("curl_lines").unwrap_or("");
46
47            // If both wget and curl are used, report failures
48            if !wget_lines.is_empty() && !curl_lines.is_empty() {
49                let mut failures = state.failures;
50                for line in wget_lines.split(',').filter_map(|s| s.parse::<u32>().ok()) {
51                    failures.push(CheckFailure::new("DL4001", Severity::Warning, "Either use `wget` or `curl`, but not both.", line));
52                }
53                for line in curl_lines.split(',').filter_map(|s| s.parse::<u32>().ok()) {
54                    failures.push(CheckFailure::new("DL4001", Severity::Warning, "Either use `wget` or `curl`, but not both.", line));
55                }
56                failures
57            } else {
58                state.failures
59            }
60        },
61    )
62}
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67    use crate::analyzer::hadolint::lint::{lint, LintResult};
68    use crate::analyzer::hadolint::config::HadolintConfig;
69
70    fn lint_dockerfile(content: &str) -> LintResult {
71        lint(content, &HadolintConfig::default())
72    }
73
74    #[test]
75    fn test_only_wget() {
76        let result = lint_dockerfile("FROM ubuntu:20.04\nRUN wget http://example.com/file");
77        assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL4001"));
78    }
79
80    #[test]
81    fn test_only_curl() {
82        let result = lint_dockerfile("FROM ubuntu:20.04\nRUN curl http://example.com/file");
83        assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL4001"));
84    }
85
86    #[test]
87    fn test_both_wget_and_curl() {
88        let result = lint_dockerfile("FROM ubuntu:20.04\nRUN wget http://example.com/file\nRUN curl http://example.com/other");
89        assert!(result.failures.iter().any(|f| f.code.as_str() == "DL4001"));
90    }
91}