syncable_cli/analyzer/hadolint/rules/
dl3056.rs

1//! DL3056: Label `org.opencontainers.image.source` is not a valid URL
2//!
3//! The source label should contain a valid URL.
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        "DL3056",
13        Severity::Warning,
14        "Label `org.opencontainers.image.source` is not a valid URL.",
15        |instr, _shell| match instr {
16            Instruction::Label(pairs) => {
17                for (key, value) in pairs {
18                    if key == "org.opencontainers.image.source" && !is_valid_url(value) {
19                        return false;
20                    }
21                }
22                true
23            }
24            _ => true,
25        },
26    )
27}
28
29fn is_valid_url(url: &str) -> bool {
30    if url.is_empty() {
31        return false;
32    }
33
34    // Basic URL validation - must start with http:// or https://
35    url.starts_with("http://") || url.starts_with("https://")
36}
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41    use crate::analyzer::hadolint::config::HadolintConfig;
42    use crate::analyzer::hadolint::lint::{LintResult, lint};
43
44    fn lint_dockerfile(content: &str) -> LintResult {
45        lint(content, &HadolintConfig::default())
46    }
47
48    #[test]
49    fn test_valid_url() {
50        let result = lint_dockerfile(
51            "FROM ubuntu:20.04\nLABEL org.opencontainers.image.source=\"https://github.com/example/repo\"",
52        );
53        assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3056"));
54    }
55
56    #[test]
57    fn test_invalid_url() {
58        let result = lint_dockerfile(
59            "FROM ubuntu:20.04\nLABEL org.opencontainers.image.source=\"not-a-url\"",
60        );
61        assert!(result.failures.iter().any(|f| f.code.as_str() == "DL3056"));
62    }
63}