syncable_cli/analyzer/hadolint/rules/
dl3033.rs

1//! DL3033: Pin versions in yum install
2//!
3//! Yum packages should be pinned to specific versions.
4
5use crate::analyzer::hadolint::parser::instruction::Instruction;
6use crate::analyzer::hadolint::rules::{SimpleRule, simple_rule};
7use crate::analyzer::hadolint::shell::ParsedShell;
8use crate::analyzer::hadolint::types::Severity;
9
10pub fn rule() -> SimpleRule<impl Fn(&Instruction, Option<&ParsedShell>) -> bool + Send + Sync> {
11    simple_rule(
12        "DL3033",
13        Severity::Warning,
14        "Specify version with `yum install -y <package>-<version>`.",
15        |instr, shell| {
16            match instr {
17                Instruction::Run(_) => {
18                    if let Some(shell) = shell {
19                        !shell.any_command(|cmd| {
20                            if cmd.name == "yum" && cmd.has_any_arg(&["install"]) {
21                                // Get packages (args after install, excluding flags)
22                                let packages = get_yum_packages(cmd);
23                                // Check if any package is unpinned
24                                packages.iter().any(|pkg| !is_pinned_yum_package(pkg))
25                            } else {
26                                false
27                            }
28                        })
29                    } else {
30                        true
31                    }
32                }
33                _ => true,
34            }
35        },
36    )
37}
38
39/// Extract package names from yum install command
40fn get_yum_packages(cmd: &crate::analyzer::hadolint::shell::Command) -> Vec<&str> {
41    let mut packages = Vec::new();
42    let mut found_install = false;
43
44    for arg in &cmd.arguments {
45        if arg == "install" {
46            found_install = true;
47            continue;
48        }
49        if found_install && !arg.starts_with('-') {
50            packages.push(arg.as_str());
51        }
52    }
53
54    packages
55}
56
57/// Check if yum package is pinned
58fn is_pinned_yum_package(pkg: &str) -> bool {
59    // Skip flags
60    if pkg.starts_with('-') {
61        return true;
62    }
63
64    // Skip local RPM files
65    if pkg.ends_with(".rpm") {
66        return true;
67    }
68
69    // Yum version formats: package-version or package-version-release
70    // Simple heuristic: contains a hyphen followed by a digit
71    let parts: Vec<&str> = pkg.rsplitn(2, '-').collect();
72    if parts.len() >= 2 {
73        let potential_version = parts[0];
74        // Version typically starts with a digit
75        potential_version
76            .chars()
77            .next()
78            .map(|c| c.is_ascii_digit())
79            .unwrap_or(false)
80    } else {
81        false
82    }
83}
84
85#[cfg(test)]
86mod tests {
87    use super::*;
88    use crate::analyzer::hadolint::config::HadolintConfig;
89    use crate::analyzer::hadolint::lint::{LintResult, lint};
90
91    fn lint_dockerfile(content: &str) -> LintResult {
92        lint(content, &HadolintConfig::default())
93    }
94
95    #[test]
96    fn test_yum_install_unpinned() {
97        let result = lint_dockerfile("FROM centos:7\nRUN yum install -y nginx");
98        assert!(result.failures.iter().any(|f| f.code.as_str() == "DL3033"));
99    }
100
101    #[test]
102    fn test_yum_install_pinned() {
103        let result = lint_dockerfile("FROM centos:7\nRUN yum install -y nginx-1.20.1");
104        assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3033"));
105    }
106
107    #[test]
108    fn test_yum_install_local_rpm() {
109        let result = lint_dockerfile("FROM centos:7\nRUN yum install -y /tmp/package.rpm");
110        assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3033"));
111    }
112
113    #[test]
114    fn test_yum_update() {
115        let result = lint_dockerfile("FROM centos:7\nRUN yum update -y");
116        assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3033"));
117    }
118}