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