syncable_cli/analyzer/hadolint/rules/
dl3055.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 "DL3055",
13 Severity::Warning,
14 "Label `org.opencontainers.image.documentation` 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.documentation" && !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 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.documentation=\"https://example.com/docs\"",
52 );
53 assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3055"));
54 }
55
56 #[test]
57 fn test_invalid_url() {
58 let result = lint_dockerfile(
59 "FROM ubuntu:20.04\nLABEL org.opencontainers.image.documentation=\"not-a-url\"",
60 );
61 assert!(result.failures.iter().any(|f| f.code.as_str() == "DL3055"));
62 }
63}