syncable_cli/analyzer/hadolint/rules/
dl3056.rs1use 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" {
19 if !is_valid_url(value) {
20 return false;
21 }
22 }
23 }
24 true
25 }
26 _ => true,
27 },
28 )
29}
30
31fn is_valid_url(url: &str) -> bool {
32 if url.is_empty() {
33 return false;
34 }
35
36 url.starts_with("http://") || url.starts_with("https://")
38}
39
40#[cfg(test)]
41mod tests {
42 use super::*;
43 use crate::analyzer::hadolint::config::HadolintConfig;
44 use crate::analyzer::hadolint::lint::{LintResult, lint};
45
46 fn lint_dockerfile(content: &str) -> LintResult {
47 lint(content, &HadolintConfig::default())
48 }
49
50 #[test]
51 fn test_valid_url() {
52 let result = lint_dockerfile(
53 "FROM ubuntu:20.04\nLABEL org.opencontainers.image.source=\"https://github.com/example/repo\"",
54 );
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(
61 "FROM ubuntu:20.04\nLABEL org.opencontainers.image.source=\"not-a-url\"",
62 );
63 assert!(result.failures.iter().any(|f| f.code.as_str() == "DL3056"));
64 }
65}