syncable_cli/analyzer/hadolint/rules/
dl3046.rs1use crate::analyzer::hadolint::parser::instruction::Instruction;
7use crate::analyzer::hadolint::rules::{simple_rule, SimpleRule};
8use crate::analyzer::hadolint::shell::ParsedShell;
9use crate::analyzer::hadolint::types::Severity;
10
11pub fn rule() -> SimpleRule<impl Fn(&Instruction, Option<&ParsedShell>) -> bool + Send + Sync> {
12 simple_rule(
13 "DL3046",
14 Severity::Warning,
15 "`useradd` without flag `-l` and target UID not within `/etc/login.defs` may result in excessively large image.",
16 |instr, shell| {
17 match instr {
18 Instruction::Run(_) => {
19 if let Some(shell) = shell {
20 !shell.any_command(|cmd| {
21 if cmd.name == "useradd" {
22 let has_l_flag = cmd.arguments.iter().any(|a| {
25 a == "-l" || a == "--no-log-init" ||
26 (a.starts_with('-') && !a.starts_with("--") && a.contains('l'))
27 });
28 !has_l_flag
29 } else {
30 false
31 }
32 })
33 } else {
34 true
35 }
36 }
37 _ => true,
38 }
39 },
40 )
41}
42
43#[cfg(test)]
44mod tests {
45 use super::*;
46 use crate::analyzer::hadolint::lint::{lint, LintResult};
47 use crate::analyzer::hadolint::config::HadolintConfig;
48
49 fn lint_dockerfile(content: &str) -> LintResult {
50 lint(content, &HadolintConfig::default())
51 }
52
53 #[test]
54 fn test_useradd_without_l() {
55 let result = lint_dockerfile("FROM ubuntu:20.04\nRUN useradd -m myuser");
56 assert!(result.failures.iter().any(|f| f.code.as_str() == "DL3046"));
57 }
58
59 #[test]
60 fn test_useradd_with_l() {
61 let result = lint_dockerfile("FROM ubuntu:20.04\nRUN useradd -l -m myuser");
62 assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3046"));
63 }
64
65 #[test]
66 fn test_useradd_with_no_log_init() {
67 let result = lint_dockerfile("FROM ubuntu:20.04\nRUN useradd --no-log-init -m myuser");
68 assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3046"));
69 }
70}