syncable_cli/analyzer/hadolint/rules/
dl3011.rs

1//! DL3011: Valid UNIX ports range from 0 to 65535
2//!
3//! EXPOSE instruction must use valid port numbers.
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        "DL3011",
13        Severity::Error,
14        "Valid UNIX ports range from 0 to 65535.",
15        |instr, _shell| {
16            match instr {
17                Instruction::Expose(ports) => {
18                    // All ports must be valid (0-65535)
19                    // The parser already validates this as u16, so this should always pass
20                    // But we check anyway for safety
21                    ports.iter().all(|p| p.number <= 65535)
22                }
23                _ => true,
24            }
25        },
26    )
27}
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32    use crate::analyzer::hadolint::config::HadolintConfig;
33    use crate::analyzer::hadolint::lint::{LintResult, lint};
34
35    fn lint_dockerfile(content: &str) -> LintResult {
36        lint(content, &HadolintConfig::default())
37    }
38
39    #[test]
40    fn test_valid_port() {
41        let result = lint_dockerfile("FROM ubuntu:20.04\nEXPOSE 8080");
42        assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3011"));
43    }
44
45    #[test]
46    fn test_valid_multiple_ports() {
47        let result = lint_dockerfile("FROM ubuntu:20.04\nEXPOSE 80 443 8080");
48        assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3011"));
49    }
50
51    #[test]
52    fn test_max_valid_port() {
53        let result = lint_dockerfile("FROM ubuntu:20.04\nEXPOSE 65535");
54        assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3011"));
55    }
56
57    #[test]
58    fn test_min_valid_port() {
59        let result = lint_dockerfile("FROM ubuntu:20.04\nEXPOSE 0");
60        assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3011"));
61    }
62}