rust-sanitize 0.15.0

Deterministic one-way data sanitization engine
Documentation
//! NDJSON / JSON Lines structured processor.
//!
//! Processes files where each non-empty line is an independent JSON object
//! (Newline-Delimited JSON, also called JSON Lines).
//!
//! The CLI uses [`process_to_edits`](JsonLinesProcessor::process_to_edits): it
//! runs the JSON span walker on each line and offsets the resulting edits by the
//! line's byte position, so each line's exact formatting is preserved and values
//! escaped in the source never leak. Files within `--max-structured-size` are
//! processed this way; larger files fall back to the bounded-memory streaming
//! path ([`process_stream`](JsonLinesProcessor::process_stream)), which parses,
//! walks, and re-serializes each line independently — keeping per-line memory
//! constant for GB-scale NDJSON.
//!
//! # Options
//!
//! | Key | Values | Default | Description |
//! |-----|--------|---------|-------------|
//! | `skip_invalid` | `"true"` / `"false"` | `"false"` | Pass malformed lines through unchanged instead of returning an error. Useful for mixed log files that interleave plain-text lines with JSON. |
//! | `compact` | `"true"` / `"false"` | `"true"` | Serialise each output line as compact JSON. Set to `"false"` only for debugging — pretty-printed NDJSON is non-standard. |
//!
//! # Example profile entry
//!
//! ```yaml
//! - processor: jsonl
//!   extensions: [".jsonl", ".ndjson", ".log"]
//!   options:
//!     skip_invalid: "true"
//!   fields:
//!     - pattern: "*.email"
//!       category: email
//!     - pattern: "*.password"
//!       category: "custom:password"
//! ```

use crate::error::{Result, SanitizeError};
use crate::processor::json_proc::walk_json;
use crate::processor::limits::DEFAULT_INPUT_SIZE;
use crate::processor::{FileTypeProfile, Processor};
use crate::store::MappingStore;
use serde_json::Value;
use std::io::{self, BufRead, BufReader, Write};

/// Structured processor for NDJSON / JSON Lines files.
pub struct JsonLinesProcessor;

impl JsonLinesProcessor {
    /// Core line-by-line processing logic, shared by both `process` and
    /// `process_stream`. Reads from any `BufRead` source and writes to any
    /// `Write` sink.
    fn process_lines(
        reader: impl BufRead,
        writer: &mut dyn Write,
        profile: &FileTypeProfile,
        store: &MappingStore,
    ) -> Result<()> {
        let skip_invalid = profile
            .options
            .get("skip_invalid")
            .is_some_and(|v| v == "true");

        let compact = profile.options.get("compact").is_none_or(|v| v != "false");

        for (line_no, line_result) in reader.lines().enumerate() {
            let raw_line = line_result?;

            if raw_line.trim().is_empty() {
                continue;
            }

            let mut value: Value = match serde_json::from_str(&raw_line) {
                Ok(v) => v,
                Err(e) => {
                    if skip_invalid {
                        writer.write_all(raw_line.as_bytes())?;
                        writer.write_all(b"\n")?;
                        continue;
                    }
                    return Err(SanitizeError::ParseError {
                        format: "JSONL".into(),
                        message: format!("line {}: {}", line_no + 1, e),
                    });
                }
            };

            walk_json(&mut value, "", profile, store, 0)?;

            let serialised = if compact {
                serde_json::to_vec(&value)
            } else {
                serde_json::to_vec_pretty(&value)
            }
            .map_err(|e| {
                SanitizeError::IoError(std::io::Error::other(format!("JSONL serialize error: {e}")))
            })?;

            writer.write_all(&serialised)?;
            writer.write_all(b"\n")?;
        }

        Ok(())
    }
}

impl Processor for JsonLinesProcessor {
    fn name(&self) -> &'static str {
        "jsonl"
    }

    fn can_handle(&self, content: &[u8], profile: &FileTypeProfile) -> bool {
        if profile.processor == "jsonl" {
            return true;
        }
        // Heuristic: first non-empty line starts with `{` and there are
        // multiple lines — distinguishes NDJSON from a single-object JSON file.
        let Ok(text) = std::str::from_utf8(content) else {
            return false;
        };
        let mut lines = text.lines().filter(|l| !l.trim().is_empty());
        let first = match lines.next() {
            Some(l) => l.trim_start(),
            None => return false,
        };
        first.starts_with('{') && lines.next().is_some()
    }

    fn supports_streaming(&self) -> bool {
        true
    }

    fn process(
        &self,
        content: &[u8],
        profile: &FileTypeProfile,
        store: &MappingStore,
    ) -> Result<Vec<u8>> {
        // Validate size and UTF-8 upfront so the error points at the file.
        crate::processor::check_size_and_decode(content, "JSONL", DEFAULT_INPUT_SIZE)?;
        let mut output = Vec::with_capacity(content.len());
        Self::process_lines(BufReader::new(content), &mut output, profile, store)?;
        Ok(output)
    }

    /// Span-based redaction: run the JSON span walker on each line, offsetting
    /// the resulting edits by the line's byte position. Preserves each line's
    /// exact formatting; invalid lines are passed through (no edits) when
    /// `skip_invalid` is set, matching `process`.
    fn process_to_edits(
        &self,
        content: &[u8],
        profile: &FileTypeProfile,
        store: &MappingStore,
    ) -> Result<Option<Vec<crate::processor::Replacement>>> {
        crate::processor::check_size_and_decode(content, "JSONL", DEFAULT_INPUT_SIZE)?;
        let skip_invalid = profile
            .options
            .get("skip_invalid")
            .is_some_and(|v| v == "true");

        let mut edits = Vec::new();
        let mut pos = 0usize;
        let mut line_no = 0usize;
        while pos < content.len() {
            let rel_nl = content[pos..].iter().position(|&b| b == b'\n');
            let end = rel_nl.map_or(content.len(), |i| pos + i);
            let line = &content[pos..end];
            line_no += 1;

            if !line.iter().all(u8::is_ascii_whitespace) {
                match crate::processor::json_proc::json_value_edits(line, profile, store) {
                    Ok(line_edits) => {
                        for e in line_edits {
                            edits.push(crate::processor::Replacement {
                                start: e.start + pos,
                                end: e.end + pos,
                                value: e.value,
                            });
                        }
                    }
                    Err(e) => {
                        if !skip_invalid {
                            return Err(SanitizeError::ParseError {
                                format: "JSONL".into(),
                                message: format!("line {line_no}: {e}"),
                            });
                        }
                        // skip_invalid: leave the line unchanged (no edits).
                    }
                }
            }

            match rel_nl {
                Some(_) => pos = end + 1,
                None => break,
            }
        }
        Ok(Some(edits))
    }

    fn process_stream(
        &self,
        reader: &mut dyn io::Read,
        writer: &mut dyn io::Write,
        profile: &FileTypeProfile,
        store: &MappingStore,
    ) -> Result<()> {
        Self::process_lines(BufReader::new(reader), writer, profile, store)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::category::Category;
    use crate::generator::HmacGenerator;
    use crate::processor::profile::FieldRule;
    use std::sync::Arc;

    fn make_store() -> MappingStore {
        let gen = Arc::new(HmacGenerator::new([42u8; 32]));
        MappingStore::new(gen, None)
    }

    fn make_profile(fields: Vec<FieldRule>) -> FileTypeProfile {
        FileTypeProfile::new("jsonl", fields).with_option("compact", "true")
    }

    #[test]
    fn replaces_matched_fields_across_lines() {
        let store = make_store();
        let proc = JsonLinesProcessor;
        let input = b"{\"email\":\"a@b.com\",\"level\":\"info\"}\n{\"email\":\"c@d.com\",\"level\":\"warn\"}\n";
        let profile = make_profile(vec![FieldRule::new("email").with_category(Category::Email)]);

        let result = proc.process(input, &profile, &store).unwrap();
        let lines: Vec<&str> = std::str::from_utf8(&result).unwrap().lines().collect();

        assert_eq!(lines.len(), 2);
        let v0: Value = serde_json::from_str(lines[0]).unwrap();
        let v1: Value = serde_json::from_str(lines[1]).unwrap();
        assert_ne!(v0["email"].as_str().unwrap(), "a@b.com");
        assert_ne!(v1["email"].as_str().unwrap(), "c@d.com");
        assert_eq!(v0["level"].as_str().unwrap(), "info");
        assert_eq!(v1["level"].as_str().unwrap(), "warn");
    }

    #[test]
    fn process_stream_matches_process() {
        let input = b"{\"email\":\"a@b.com\"}\n{\"email\":\"c@d.com\"}\n";
        let profile = make_profile(vec![FieldRule::new("email").with_category(Category::Email)]);

        // process
        let store1 = make_store();
        let proc = JsonLinesProcessor;
        let from_process = proc.process(input, &profile, &store1).unwrap();

        // process_stream with identical store seed
        let store2 = make_store();
        let mut reader = io::Cursor::new(input);
        let mut from_stream = Vec::new();
        proc.process_stream(&mut reader, &mut from_stream, &profile, &store2)
            .unwrap();

        assert_eq!(from_process, from_stream);
    }

    #[test]
    fn glob_suffix_pattern() {
        let store = make_store();
        let proc = JsonLinesProcessor;
        let input = b"{\"db\":{\"password\":\"pw1\"},\"name\":\"app\"}\n";
        let profile = make_profile(vec![
            FieldRule::new("*.password").with_category(Category::Custom("pw".into()))
        ]);

        let result = proc.process(input, &profile, &store).unwrap();
        let trimmed = result
            .iter()
            .rposition(|b| !b.is_ascii_whitespace())
            .map_or(&[][..], |i| &result[..=i]);
        let v: Value = serde_json::from_slice(trimmed).unwrap();
        assert_ne!(v["db"]["password"].as_str().unwrap(), "pw1");
        assert_eq!(v["name"].as_str().unwrap(), "app");
    }

    #[test]
    fn skip_invalid_passes_through_bad_lines() {
        let store = make_store();
        let proc = JsonLinesProcessor;
        let input = b"{\"email\":\"a@b.com\"}\nnot json at all\n{\"email\":\"c@d.com\"}\n";
        let profile = FileTypeProfile::new(
            "jsonl",
            vec![FieldRule::new("email").with_category(Category::Email)],
        )
        .with_option("skip_invalid", "true")
        .with_option("compact", "true");

        let result = proc.process(input, &profile, &store).unwrap();
        let text = std::str::from_utf8(&result).unwrap();
        let lines: Vec<&str> = text.lines().collect();

        assert_eq!(lines.len(), 3);
        assert_eq!(lines[1], "not json at all");
        let v0: Value = serde_json::from_str(lines[0]).unwrap();
        assert_ne!(v0["email"].as_str().unwrap(), "a@b.com");
    }

    #[test]
    fn error_on_invalid_line_by_default() {
        let store = make_store();
        let proc = JsonLinesProcessor;
        let input = b"{\"email\":\"a@b.com\"}\nnot json\n";
        let profile = make_profile(vec![FieldRule::new("email").with_category(Category::Email)]);

        assert!(proc.process(input, &profile, &store).is_err());
    }

    #[test]
    fn deterministic_same_value_same_replacement() {
        let store = make_store();
        let proc = JsonLinesProcessor;
        let input = b"{\"email\":\"a@b.com\"}\n{\"email\":\"a@b.com\"}\n";
        let profile = make_profile(vec![FieldRule::new("email").with_category(Category::Email)]);

        let result = proc.process(input, &profile, &store).unwrap();
        let lines: Vec<&str> = std::str::from_utf8(&result).unwrap().lines().collect();
        let v0: Value = serde_json::from_str(lines[0]).unwrap();
        let v1: Value = serde_json::from_str(lines[1]).unwrap();
        assert_eq!(v0["email"].as_str().unwrap(), v1["email"].as_str().unwrap());
    }

    #[test]
    fn can_handle_heuristic_multi_line_json_objects() {
        let proc = JsonLinesProcessor;
        let profile = FileTypeProfile::new("yaml", vec![]);
        let input = b"{\"a\":1}\n{\"b\":2}\n";
        assert!(proc.can_handle(input, &profile));
    }

    #[test]
    fn can_handle_rejects_single_object() {
        let proc = JsonLinesProcessor;
        let profile = FileTypeProfile::new("yaml", vec![]);
        let input = b"{\"a\":1}";
        assert!(!proc.can_handle(input, &profile));
    }

    #[test]
    fn supports_streaming_is_true() {
        assert!(JsonLinesProcessor.supports_streaming());
    }

    /// Edit-mode redacts matched values per line (including non-canonical `\/`
    /// escapes), offsets spans correctly, and leaves invalid lines unchanged
    /// when `skip_invalid` is set.
    #[test]
    fn edits_redact_per_line_and_skip_invalid() {
        let store = make_store();
        let proc = JsonLinesProcessor;
        let content =
            b"{\"email\":\"a-SEC1@e.test\"}\n{\"u\":\"http:\\/\\/SEC2.test\"}\nnot json\n";
        let mut profile = FileTypeProfile::new(
            "jsonl",
            vec![
                FieldRule::new("email").with_category(Category::Email),
                FieldRule::new("u").with_category(Category::Custom("url".into())),
            ],
        );
        profile.options.insert("skip_invalid".into(), "true".into());
        let edits = proc
            .process_to_edits(content, &profile, &store)
            .unwrap()
            .unwrap();
        let out = crate::processor::apply_edits(content, edits);
        let text = String::from_utf8(out).unwrap();
        assert!(!text.contains("SEC1"), "leaked SEC1: {text}");
        assert!(!text.contains("SEC2"), "leaked SEC2 (\\/ escape): {text}");
        assert!(text.contains("not json"), "invalid line dropped: {text}");
    }
}