Skip to main content

sanitize_engine/processor/
json_proc.rs

1//! JSON structured processor.
2//!
3//! Parses JSON input, walks the value tree, replaces values at matched
4//! key paths, and serializes back to JSON preserving structure.
5//!
6//! # Key Paths
7//!
8//! Nested keys are expressed as dot-separated paths:
9//! `database.password`, `smtp.credentials.user`.
10//!
11//! Array elements are traversed transparently — a rule for `users.email`
12//! matches the `email` field inside every object in the `users` array.
13
14use crate::error::{Result, SanitizeError};
15use crate::processor::limits::DEFAULT_INPUT_SIZE;
16use crate::processor::{walk_tree, FileTypeProfile, Processor, TreeNode};
17use crate::store::MappingStore;
18use serde_json::Value;
19
20/// Structured processor for JSON files.
21pub struct JsonProcessor;
22
23impl Processor for JsonProcessor {
24    fn name(&self) -> &'static str {
25        "json"
26    }
27
28    fn can_handle(&self, content: &[u8], profile: &FileTypeProfile) -> bool {
29        if profile.processor == "json" {
30            return true;
31        }
32        // Heuristic: starts with `{` or `[` after optional whitespace.
33        let trimmed = content.iter().copied().find(|b| !b.is_ascii_whitespace());
34        matches!(trimmed, Some(b'{' | b'['))
35    }
36
37    fn process(
38        &self,
39        content: &[u8],
40        profile: &FileTypeProfile,
41        store: &MappingStore,
42    ) -> Result<Vec<u8>> {
43        // F-04 fix: enforce input size limit.
44        if content.len() > DEFAULT_INPUT_SIZE {
45            return Err(SanitizeError::InputTooLarge {
46                size: content.len(),
47                limit: DEFAULT_INPUT_SIZE,
48            });
49        }
50
51        let text = std::str::from_utf8(content).map_err(|e| SanitizeError::ParseError {
52            format: "JSON".into(),
53            message: format!("invalid UTF-8: {}", e),
54        })?;
55
56        let mut value: Value =
57            serde_json::from_str(text).map_err(|e| SanitizeError::ParseError {
58                format: "JSON".into(),
59                message: format!("JSON parse error: {}", e),
60            })?;
61
62        walk_json(&mut value, "", profile, store, 0)?;
63
64        let compact = profile.options.get("compact").is_some_and(|v| v == "true");
65
66        let output = if compact {
67            serde_json::to_vec(&value)
68        } else {
69            serde_json::to_vec_pretty(&value)
70        }
71        .map_err(|e| SanitizeError::IoError(format!("JSON serialize error: {}", e)))?;
72
73        Ok(output)
74    }
75}
76
77impl TreeNode for Value {
78    fn for_each_map_entry<F>(&mut self, mut f: F) -> Result<()>
79    where
80        F: FnMut(&str, &mut Self) -> Result<()>,
81    {
82        if let Self::Object(map) = self {
83            let keys: Vec<String> = map.keys().cloned().collect();
84            for key in keys {
85                if let Some(v) = map.get_mut(&key) {
86                    f(&key, v)?;
87                }
88            }
89        }
90        Ok(())
91    }
92
93    fn for_each_seq_item<F>(&mut self, mut f: F) -> Result<()>
94    where
95        F: FnMut(&mut Self) -> Result<()>,
96    {
97        if let Self::Array(arr) = self {
98            for item in arr.iter_mut() {
99                f(item)?;
100            }
101        }
102        Ok(())
103    }
104
105    fn as_str_mut(&mut self) -> Option<&mut String> {
106        if let Self::String(s) = self {
107            Some(s)
108        } else {
109            None
110        }
111    }
112
113    fn is_scalar(&self) -> bool {
114        matches!(self, Self::Number(_) | Self::Bool(_))
115    }
116
117    fn scalar_to_string(&self) -> String {
118        self.to_string()
119    }
120
121    fn set_string(&mut self, s: String) {
122        *self = Self::String(s);
123    }
124}
125
126/// Recursively walk a JSON value tree, replacing matched field values.
127pub(crate) fn walk_json(
128    value: &mut Value,
129    prefix: &str,
130    profile: &FileTypeProfile,
131    store: &MappingStore,
132    depth: usize,
133) -> Result<()> {
134    walk_tree(value, prefix, profile, store, depth, "JSON")
135}
136
137#[cfg(test)]
138mod tests {
139    use super::*;
140    use crate::category::Category;
141    use crate::generator::HmacGenerator;
142    use crate::processor::profile::FieldRule;
143    use std::sync::Arc;
144
145    fn make_store() -> MappingStore {
146        let gen = Arc::new(HmacGenerator::new([42u8; 32]));
147        MappingStore::new(gen, None)
148    }
149
150    #[test]
151    fn basic_json_replacement() {
152        let store = make_store();
153        let proc = JsonProcessor;
154
155        let content =
156            br#"{"database": {"host": "db.corp.com", "password": "s3cret"}, "port": 5432}"#;
157        let profile = FileTypeProfile::new(
158            "json",
159            vec![
160                FieldRule::new("database.password").with_category(Category::Custom("pw".into())),
161                FieldRule::new("database.host").with_category(Category::Hostname),
162            ],
163        )
164        .with_option("compact", "true");
165
166        let result = proc.process(content, &profile, &store).unwrap();
167        let out: Value = serde_json::from_slice(&result).unwrap();
168
169        assert_ne!(out["database"]["password"].as_str().unwrap(), "s3cret");
170        assert_ne!(out["database"]["host"].as_str().unwrap(), "db.corp.com");
171        assert_eq!(out["port"], 5432);
172    }
173
174    #[test]
175    fn json_array_traversal() {
176        let store = make_store();
177        let proc = JsonProcessor;
178
179        let content = br#"{"users": [{"email": "a@b.com"}, {"email": "c@d.com"}]}"#;
180        let profile = FileTypeProfile::new(
181            "json",
182            vec![FieldRule::new("users.email").with_category(Category::Email)],
183        )
184        .with_option("compact", "true");
185
186        let result = proc.process(content, &profile, &store).unwrap();
187        let out: Value = serde_json::from_slice(&result).unwrap();
188
189        let users = out["users"].as_array().unwrap();
190        assert_ne!(users[0]["email"].as_str().unwrap(), "a@b.com");
191        assert_ne!(users[1]["email"].as_str().unwrap(), "c@d.com");
192    }
193
194    #[test]
195    fn json_glob_suffix_pattern() {
196        let store = make_store();
197        let proc = JsonProcessor;
198
199        let content =
200            br#"{"db": {"password": "pw1"}, "cache": {"password": "pw2"}, "name": "app"}"#;
201        let profile = FileTypeProfile::new(
202            "json",
203            vec![FieldRule::new("*.password").with_category(Category::Custom("pw".into()))],
204        )
205        .with_option("compact", "true");
206
207        let result = proc.process(content, &profile, &store).unwrap();
208        let out: Value = serde_json::from_slice(&result).unwrap();
209
210        assert_ne!(out["db"]["password"].as_str().unwrap(), "pw1");
211        assert_ne!(out["cache"]["password"].as_str().unwrap(), "pw2");
212        assert_eq!(out["name"], "app");
213    }
214}