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