Skip to main content

alint_rules/
file_content_forbidden.rs

1//! `file_content_forbidden` — files in scope must NOT match a regex.
2
3use alint_core::{Context, Error, Level, Result, Rule, RuleSpec, Scope, Violation};
4use regex::Regex;
5use serde::Deserialize;
6
7#[derive(Debug, Deserialize)]
8struct Options {
9    pattern: String,
10}
11
12#[derive(Debug)]
13pub struct FileContentForbiddenRule {
14    id: String,
15    level: Level,
16    policy_url: Option<String>,
17    message: Option<String>,
18    scope: Scope,
19    pattern_src: String,
20    pattern: Regex,
21}
22
23impl Rule for FileContentForbiddenRule {
24    fn id(&self) -> &str {
25        &self.id
26    }
27    fn level(&self) -> Level {
28        self.level
29    }
30    fn policy_url(&self) -> Option<&str> {
31        self.policy_url.as_deref()
32    }
33
34    fn evaluate(&self, ctx: &Context<'_>) -> Result<Vec<Violation>> {
35        let mut violations = Vec::new();
36        for entry in ctx.index.files() {
37            if !self.scope.matches(&entry.path) {
38                continue;
39            }
40            let full = ctx.root.join(&entry.path);
41            let bytes = match std::fs::read(&full) {
42                Ok(b) => b,
43                Err(e) => {
44                    violations.push(
45                        Violation::new(format!("could not read file: {e}")).with_path(&entry.path),
46                    );
47                    continue;
48                }
49            };
50            let Ok(text) = std::str::from_utf8(&bytes) else {
51                // Non-UTF-8 files are silently skipped; they can't contain a
52                // text regex match. Use `file_is_text` to flag binaries.
53                continue;
54            };
55            if let Some(m) = self.pattern.find(text) {
56                let line = text[..m.start()].matches('\n').count() + 1;
57                let msg = self
58                    .message
59                    .clone()
60                    .unwrap_or_else(|| format!("forbidden pattern /{}/ found", self.pattern_src));
61                violations.push(
62                    Violation::new(msg)
63                        .with_path(&entry.path)
64                        .with_location(line, 1),
65                );
66            }
67        }
68        Ok(violations)
69    }
70}
71
72pub fn build(spec: &RuleSpec) -> Result<Box<dyn Rule>> {
73    let Some(paths) = &spec.paths else {
74        return Err(Error::rule_config(
75            &spec.id,
76            "file_content_forbidden requires a `paths` field",
77        ));
78    };
79    let opts: Options = spec
80        .deserialize_options()
81        .map_err(|e| Error::rule_config(&spec.id, format!("invalid options: {e}")))?;
82    let pattern = Regex::new(&opts.pattern)
83        .map_err(|e| Error::rule_config(&spec.id, format!("invalid pattern: {e}")))?;
84    Ok(Box::new(FileContentForbiddenRule {
85        id: spec.id.clone(),
86        level: spec.level,
87        policy_url: spec.policy_url.clone(),
88        message: spec.message.clone(),
89        scope: Scope::from_paths_spec(paths)?,
90        pattern_src: opts.pattern,
91        pattern,
92    }))
93}
94
95#[cfg(test)]
96mod tests {
97    use super::*;
98    use crate::test_support::{ctx, spec_yaml, tempdir_with_files};
99
100    #[test]
101    fn build_rejects_missing_paths_field() {
102        let spec = spec_yaml(
103            "id: t\n\
104             kind: file_content_forbidden\n\
105             pattern: \"X\"\n\
106             level: error\n",
107        );
108        assert!(build(&spec).is_err());
109    }
110
111    #[test]
112    fn build_rejects_invalid_regex() {
113        let spec = spec_yaml(
114            "id: t\n\
115             kind: file_content_forbidden\n\
116             paths: \"**/*\"\n\
117             pattern: \"[bad\"\n\
118             level: error\n",
119        );
120        assert!(build(&spec).is_err());
121    }
122
123    #[test]
124    fn evaluate_fires_on_forbidden_match_with_line_number() {
125        let spec = spec_yaml(
126            "id: t\n\
127             kind: file_content_forbidden\n\
128             paths: \"src/**/*.rs\"\n\
129             pattern: \"\\\\bTODO\\\\b\"\n\
130             level: error\n",
131        );
132        let rule = build(&spec).unwrap();
133        let (tmp, idx) = tempdir_with_files(&[(
134            "src/main.rs",
135            b"fn main() {\n    let x = 1;\n    // TODO\n}\n",
136        )]);
137        let v = rule.evaluate(&ctx(tmp.path(), &idx)).unwrap();
138        assert_eq!(v.len(), 1);
139        assert_eq!(v[0].line, Some(3), "violation should point at line 3");
140    }
141
142    #[test]
143    fn evaluate_passes_when_pattern_absent() {
144        let spec = spec_yaml(
145            "id: t\n\
146             kind: file_content_forbidden\n\
147             paths: \"src/**/*.rs\"\n\
148             pattern: \"\\\\bTODO\\\\b\"\n\
149             level: error\n",
150        );
151        let rule = build(&spec).unwrap();
152        let (tmp, idx) =
153            tempdir_with_files(&[("src/main.rs", b"fn main() {\n    let x = 1;\n}\n")]);
154        let v = rule.evaluate(&ctx(tmp.path(), &idx)).unwrap();
155        assert!(v.is_empty(), "clean file should pass: {v:?}");
156    }
157
158    #[test]
159    fn evaluate_silent_on_non_utf8() {
160        let spec = spec_yaml(
161            "id: t\n\
162             kind: file_content_forbidden\n\
163             paths: \"**/*\"\n\
164             pattern: \"X\"\n\
165             level: error\n",
166        );
167        let rule = build(&spec).unwrap();
168        let (tmp, idx) = tempdir_with_files(&[("img.bin", &[0xff, 0xfe])]);
169        let v = rule.evaluate(&ctx(tmp.path(), &idx)).unwrap();
170        assert!(v.is_empty());
171    }
172}