Skip to main content

citum_schema_style/style/
validation.rs

1/*
2SPDX-License-Identifier: MIT OR Apache-2.0
3SPDX-FileCopyrightText: © 2023-2026 Bruce D'Arcus
4*/
5
6//! Style validation and resource-limit checks.
7
8use crate::template::{
9    LocalizedTemplateSpec, TemplateComponent, TemplateVariant, TemplateVariants,
10};
11use crate::version::{MAX_TEMPLATE_COMPONENTS, MAX_TEMPLATE_NESTING_DEPTH};
12use crate::{BibliographySpec, CitationSpec, ResolutionError};
13
14use super::Style;
15
16#[cfg(test)]
17use crate::template::TemplateGroup;
18
19/// A non-fatal validation warning emitted by [`Style::validate`].
20#[derive(Debug, Clone, PartialEq)]
21pub enum SchemaWarning {
22    /// A `TypeSelector` references an unrecognized reference type name.
23    ///
24    /// This usually indicates a typo (e.g., `article_journal` instead of
25    /// `article-journal`). The selector will silently match nothing at
26    /// render time.
27    UnknownTypeName {
28        /// The unrecognized type name string.
29        name: String,
30        /// Human-readable location hint (e.g., `"bibliography.type-variants"`).
31        location: String,
32    },
33}
34
35impl std::fmt::Display for SchemaWarning {
36    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37        match self {
38            SchemaWarning::UnknownTypeName { name, location } => {
39                write!(
40                    f,
41                    "unknown reference type \"{name}\" in {location} \
42                     (will silently match nothing; check for typos)"
43                )
44            }
45        }
46    }
47}
48
49impl Style {
50    /// Validate hard resource limits for style templates.
51    ///
52    /// # Errors
53    ///
54    /// Returns an error when authored template structure exceeds the maximum
55    /// depth or component count accepted by the engine.
56    pub fn validate_resource_limits(&self) -> Result<(), String> {
57        let mut budget = TemplateResourceBudget::default();
58
59        if let Some(templates) = &self.templates {
60            for (name, template) in templates {
61                budget.check_template(template, &format!("templates.{name}"), 0)?;
62            }
63        }
64        if let Some(citation) = &self.citation {
65            budget.check_citation_spec(citation, "citation", 0)?;
66        }
67        if let Some(bibliography) = &self.bibliography {
68            budget.check_bibliography_spec(bibliography, "bibliography", 0)?;
69        }
70
71        Ok(())
72    }
73
74    /// Validate the style and return any non-fatal warnings.
75    ///
76    /// This method checks for issues that are syntactically valid but
77    /// semantically suspect, such as unrecognized reference type names in
78    /// `TypeSelector` values.
79    ///
80    /// Warnings do not prevent rendering; they are informational only.
81    pub fn validate(&self) -> Vec<SchemaWarning> {
82        let mut warnings = Vec::new();
83        self.collect_type_selector_warnings(&mut warnings);
84        warnings
85    }
86
87    /// Collect warnings for all `TypeSelector` values in the style.
88    fn collect_type_selector_warnings(&self, warnings: &mut Vec<SchemaWarning>) {
89        if let Some(bib) = &self.bibliography
90            && let Some(type_variants) = &bib.type_variants
91        {
92            for selector in type_variants.keys() {
93                for name in selector.unknown_type_names() {
94                    warnings.push(SchemaWarning::UnknownTypeName {
95                        name: name.to_string(),
96                        location: "bibliography.type-variants".to_string(),
97                    });
98                }
99            }
100        }
101        if let Some(cit) = &self.citation {
102            collect_citation_spec_warnings(cit, "citation", warnings);
103        }
104    }
105
106    pub(crate) fn validate_profile_shape(&self) -> Result<(), ResolutionError> {
107        if self.templates.is_some() || yaml_path_present(self.raw_yaml.as_ref(), &["templates"]) {
108            return Err(ResolutionError::InvalidProfileOverride {
109                location: "templates".to_string(),
110            });
111        }
112
113        if let Some(location) = forbidden_profile_template_path(self.raw_yaml.as_ref()) {
114            return Err(ResolutionError::InvalidProfileOverride { location });
115        }
116
117        Ok(())
118    }
119}
120
121fn forbidden_profile_template_path(raw_yaml: Option<&serde_yaml::Value>) -> Option<String> {
122    let raw_yaml = raw_yaml?;
123    for (section, recursive) in [("citation", true), ("bibliography", false)] {
124        if let Some(section_value) = mapping_child(raw_yaml, section) {
125            if recursive {
126                if let Some(location) = forbidden_citation_template_path(section_value, section) {
127                    return Some(location);
128                }
129            } else if let Some(location) = forbidden_section_template_path(section_value, section) {
130                return Some(location);
131            }
132        }
133    }
134    None
135}
136
137fn forbidden_section_template_path(section: &serde_yaml::Value, location: &str) -> Option<String> {
138    for key in ["template", "template-ref", "type-variants", "locales"] {
139        if mapping_child(section, key).is_some() {
140            return Some(format!("{location}.{key}"));
141        }
142    }
143    None
144}
145
146fn forbidden_citation_template_path(section: &serde_yaml::Value, location: &str) -> Option<String> {
147    if let Some(location) = forbidden_section_template_path(section, location) {
148        return Some(location);
149    }
150
151    for sub_section in ["integral", "non-integral", "subsequent", "ibid"] {
152        if let Some(child) = mapping_child(section, sub_section)
153            && let Some(location) =
154                forbidden_citation_template_path(child, &format!("{location}.{sub_section}"))
155        {
156            return Some(location);
157        }
158    }
159    None
160}
161
162fn mapping_child<'a>(value: &'a serde_yaml::Value, segment: &str) -> Option<&'a serde_yaml::Value> {
163    let serde_yaml::Value::Mapping(map) = value else {
164        return None;
165    };
166    let key = serde_yaml::Value::String(segment.to_string());
167    map.get(&key)
168}
169
170fn yaml_path_present(value: Option<&serde_yaml::Value>, path: &[&str]) -> bool {
171    let Some(mut current) = value else {
172        return false;
173    };
174    for segment in path {
175        let Some(next) = mapping_child(current, segment) else {
176            return false;
177        };
178        current = next;
179    }
180    true
181}
182
183/// Collect warnings from a `CitationSpec` and its sub-specs.
184fn collect_citation_spec_warnings(
185    spec: &CitationSpec,
186    location: &str,
187    warnings: &mut Vec<SchemaWarning>,
188) {
189    if let Some(type_variants) = &spec.type_variants {
190        for selector in type_variants.keys() {
191            for name in selector.unknown_type_names() {
192                warnings.push(SchemaWarning::UnknownTypeName {
193                    name: name.to_string(),
194                    location: format!("{location}.type-variants"),
195                });
196            }
197        }
198    }
199    // Recurse into sub-specs
200    for (sub_name, sub_spec) in [
201        ("integral", spec.integral.as_deref()),
202        ("non-integral", spec.non_integral.as_deref()),
203        ("subsequent", spec.subsequent.as_deref()),
204        ("ibid", spec.ibid.as_deref()),
205    ]
206    .into_iter()
207    .filter_map(|(n, s)| s.map(|s| (n, s)))
208    {
209        collect_citation_spec_warnings(sub_spec, &format!("{location}.{sub_name}"), warnings);
210    }
211}
212
213#[derive(Default)]
214struct TemplateResourceBudget {
215    component_count: usize,
216}
217
218impl TemplateResourceBudget {
219    fn check_template(
220        &mut self,
221        template: &[TemplateComponent],
222        location: &str,
223        depth: usize,
224    ) -> Result<(), String> {
225        if depth > MAX_TEMPLATE_NESTING_DEPTH {
226            return Err(format!(
227                "{location} exceeds maximum template nesting depth of {MAX_TEMPLATE_NESTING_DEPTH}"
228            ));
229        }
230        for component in template {
231            self.check_component(component, location, depth)?;
232        }
233        Ok(())
234    }
235
236    fn check_component(
237        &mut self,
238        component: &TemplateComponent,
239        location: &str,
240        depth: usize,
241    ) -> Result<(), String> {
242        self.component_count = self.component_count.saturating_add(1);
243        if self.component_count > MAX_TEMPLATE_COMPONENTS {
244            return Err(format!(
245                "style exceeds maximum template component count of {MAX_TEMPLATE_COMPONENTS}"
246            ));
247        }
248
249        match component {
250            TemplateComponent::Date(date) => {
251                if let Some(fallback) = &date.fallback {
252                    self.check_template(fallback, &format!("{location}.date.fallback"), depth + 1)?;
253                }
254            }
255            TemplateComponent::Group(group) => {
256                self.check_template(&group.group, &format!("{location}.group"), depth + 1)?;
257            }
258            TemplateComponent::Contributor(_)
259            | TemplateComponent::Title(_)
260            | TemplateComponent::Number(_)
261            | TemplateComponent::Variable(_)
262            | TemplateComponent::Term(_) => {}
263        }
264
265        Ok(())
266    }
267
268    fn check_variant(
269        &mut self,
270        variant: &TemplateVariant,
271        location: &str,
272        depth: usize,
273    ) -> Result<(), String> {
274        match variant {
275            TemplateVariant::Full(template) => self.check_template(template, location, depth),
276            TemplateVariant::Diff(diff) => {
277                for (index, add) in diff.add.iter().enumerate() {
278                    self.check_component(
279                        &add.component,
280                        &format!("{location}.add[{index}].component"),
281                        depth,
282                    )?;
283                }
284                Ok(())
285            }
286        }
287    }
288
289    fn check_variants(
290        &mut self,
291        variants: &TemplateVariants,
292        location: &str,
293        depth: usize,
294    ) -> Result<(), String> {
295        for (selector, variant) in variants {
296            self.check_variant(variant, &format!("{location}.{selector:?}"), depth)?;
297        }
298        Ok(())
299    }
300
301    fn check_locales(
302        &mut self,
303        locales: &[LocalizedTemplateSpec],
304        location: &str,
305        depth: usize,
306    ) -> Result<(), String> {
307        for (index, locale) in locales.iter().enumerate() {
308            self.check_template(
309                &locale.template,
310                &format!("{location}[{index}].template"),
311                depth,
312            )?;
313        }
314        Ok(())
315    }
316
317    fn check_citation_spec(
318        &mut self,
319        spec: &CitationSpec,
320        location: &str,
321        depth: usize,
322    ) -> Result<(), String> {
323        if let Some(template) = &spec.template {
324            self.check_template(template, &format!("{location}.template"), depth)?;
325        }
326        if let Some(locales) = &spec.locales {
327            self.check_locales(locales, &format!("{location}.locales"), depth)?;
328        }
329        if let Some(variants) = &spec.type_variants {
330            self.check_variants(variants, &format!("{location}.type-variants"), depth)?;
331        }
332        for (sub_name, sub_spec) in [
333            ("integral", spec.integral.as_deref()),
334            ("non-integral", spec.non_integral.as_deref()),
335            ("subsequent", spec.subsequent.as_deref()),
336            ("ibid", spec.ibid.as_deref()),
337        ]
338        .into_iter()
339        .filter_map(|(n, s)| s.map(|s| (n, s)))
340        {
341            self.check_citation_spec(sub_spec, &format!("{location}.{sub_name}"), depth + 1)?;
342        }
343        Ok(())
344    }
345
346    fn check_bibliography_spec(
347        &mut self,
348        spec: &BibliographySpec,
349        location: &str,
350        depth: usize,
351    ) -> Result<(), String> {
352        if let Some(template) = &spec.template {
353            self.check_template(template, &format!("{location}.template"), depth)?;
354        }
355        if let Some(locales) = &spec.locales {
356            self.check_locales(locales, &format!("{location}.locales"), depth)?;
357        }
358        if let Some(variants) = &spec.type_variants {
359            self.check_variants(variants, &format!("{location}.type-variants"), depth)?;
360        }
361        Ok(())
362    }
363}
364
365#[cfg(test)]
366#[allow(
367    clippy::unwrap_used,
368    clippy::expect_used,
369    clippy::panic,
370    clippy::indexing_slicing,
371    clippy::todo,
372    clippy::unimplemented,
373    clippy::unreachable,
374    clippy::get_unwrap,
375    reason = "Panicking is acceptable and often desired in tests."
376)]
377mod security_resource_tests {
378    use super::*;
379
380    fn nested_group(depth: usize) -> TemplateComponent {
381        if depth == 0 {
382            TemplateComponent::default()
383        } else {
384            TemplateComponent::Group(TemplateGroup {
385                group: vec![nested_group(depth - 1)],
386                ..TemplateGroup::default()
387            })
388        }
389    }
390
391    #[test]
392    fn validate_resource_limits_rejects_deeply_nested_templates() {
393        let style = Style {
394            bibliography: Some(BibliographySpec {
395                template: Some(vec![nested_group(MAX_TEMPLATE_NESTING_DEPTH + 1)]),
396                ..BibliographySpec::default()
397            }),
398            ..Style::default()
399        };
400
401        let err = style
402            .validate_resource_limits()
403            .expect_err("deep template must be rejected");
404
405        assert!(err.contains("maximum template nesting depth"));
406    }
407
408    #[test]
409    fn validate_resource_limits_rejects_too_many_components() {
410        let style = Style {
411            bibliography: Some(BibliographySpec {
412                template: Some(vec![
413                    TemplateComponent::default();
414                    MAX_TEMPLATE_COMPONENTS + 1
415                ]),
416                ..BibliographySpec::default()
417            }),
418            ..Style::default()
419        };
420
421        let err = style
422            .validate_resource_limits()
423            .expect_err("oversized template must be rejected");
424
425        assert!(err.contains("maximum template component count"));
426    }
427}