1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
//! A crate to protect against malicious JSON payloads.
//!
//! This crate provides functionality to validate JSON payloads against a set of constraints.
//! * Maximum depth of the JSON structure.
//! * Maximum length of strings.
//! * Maximum number of entries in arrays.
//! * Maximum number of entries in objects.
//! * Maximum length of object entry names.
//! * Whether to allow duplicate object entry names.
//!
//! This crate is designed to process untrusted JSON payloads,
//! such as it does not use recursion to validate the JSON structure.
//!
//! # Examples
//!
//! ```rust
//! use json_threat_protection as jtp;
//!
//! fn reject_highly_nested_json(data: &[u8], depth: usize) -> Result<(), jtp::Error> {
//!     jtp::from_slice(data).with_max_depth(depth).validate()
//! }
//!
//! fn reject_too_long_strings(data: &[u8], max_string_length: usize) -> Result<(), jtp::Error> {
//!     jtp::from_slice(data).with_max_string_length(max_string_length).validate()
//! }
//!
//! fn reject_too_many_array_entries(data: &[u8], max_array_entries: usize) -> Result<(), jtp::Error> {
//!     jtp::from_slice(data).with_max_array_entries(max_array_entries).validate()
//! }
//!
//! fn reject_too_many_object_entries(data: &[u8], max_object_entries: usize) -> Result<(), jtp::Error> {
//!    jtp::from_slice(data).with_max_object_entries(max_object_entries).validate()
//! }
//!
//! fn reject_too_long_object_entry_names(data: &[u8], max_object_entry_name_length: usize) -> Result<(), jtp::Error> {
//!    jtp::from_slice(data).with_max_object_entry_name_length(max_object_entry_name_length).validate()
//! }
//!
//! fn reject_duplicate_object_entry_names(data: &[u8]) -> Result<(), jtp::Error> {
//!   jtp::from_slice(data).disallow_duplicate_object_entry_name().validate()
//! }
//! ```
//!
//! # Default constraints
//!
//! By default, the validator just checks the JSON syntax without any constraints,
//! and also allows duplicate object entry names.
//!
//! You could set the limit to [`NO_LIMIT`] to disable a specific constraint.
//!
//! # Incremental validation
//!
//! The `Validator` struct is designed to be used incrementally,
//! so you can validate huge JSON payloads in multiple function calls
//! without blocking the current thread for a long time.
//!
//! ```rust
//! use json_threat_protection as jtp;
//!
//! fn validate_incrementally(data: &[u8]) -> Result<(), jtp::Error> {
//!     let mut validator = jtp::from_slice(data);
//!     
//!     // validate the JSON payload in 2000 steps,
//!     // and return `Some(true)` if the validation is finished and no errors.
//!     // return `Some(false)` to continue the validation.
//!     // Otherwise, return `Err` if an error occurred.
//!     while validator.validate_with_steps(2000)? {
//!         // do something else such as processing other tasks
//!     }
//!
//!     Ok(())
//! }
//! ```
//!
//! This feature is useful when you want to validate a JSON payload in a non-blocking way,
//! the typical use case is used to build FFI bindings to other software
//! that needs to validate JSON payloads in a non-blocking way to avoid blocking the thread.
//!
//! # Error handling
//!
//! This crate has limited place where might panic, most of errors are returned as `Err`.
//! And some unintended bugs might also return as `Err` with explicit error kind
//! to indicate we are running into buggy code.
//!
//! Whatever the error kind is,
//! it always contains the position where the error occurred, such as line, column, and offset,
//! the `offset` is the byte offset from the beginning of the JSON payload.
//!
//! # Special behavior compared to [serde_json](https://crates.io/crates/serde_json)
//!
//! This crate do it best to keep consistent with `serde_json`'s behavior
//! using the [cargo-fuzz](https://crates.io/crates/cargo-fuzz)
//! to process the same JSON payloading with both this crate and `serde_json`
//! and compare the validation results.
//!
//! However, there are some differences between this crate and `serde_json` so far:
//! * This crate allow any precision of numbers,
//!   even if it cannot be represented in Rust's native number types ([`i64`], [`u64`], [`i128`], [`u128`], [`f64`], [`f128`]).
//!   The [serde_json](https://crates.io/crates/serde_json)
//!   without [arbitrary_precision](https://github.com/serde-rs/json/blob/3f1c6de4af28b1f6c5100da323f2bffaf7c2083f/Cargo.toml#L69-L75)
//!   feature enabled will return an error for such numbers.
//!
//! # Performance
//!
//! This crate is designed to be fast and efficient,
//! and has its own benchmark suite under the `benches` directory.
//! You can run the benchmarks with the following command:
//! ```bash
//! JSON_FILE=/path/to/file.json cargo bench --bench memory -- --verbose
//! ```
//!
//! This suite validates the JSON syntax using both this crate and `serde_json`,
//! you could get your own performance number by specifying the `JSON_FILE` to your dataset.
//!
//! # Fuzzing
//!
//! This crate is fuzzed using the [cargo-fuzz](https://crates.io/crates/cargo-fuzz) tool,
//! program is under the `fuzz` directory.
//!
//! The initial seed corpus is from [nlohmann/json_test_data](https://github.com/nlohmann/json_test_data/),
//! and extra corpus follows the [nlohmann/json/blob/develop/tests/fuzzing](https://github.com/nlohmann/json/blob/develop/tests/fuzzing.md).
//!
mod lexer;
pub mod read;
mod validator;

use read::{IoRead, Read, SliceRead, StrRead};

/// Represents no limit for a specific constraint.
pub const NO_LIMIT: usize = std::usize::MAX;
pub use lexer::LexerError;
pub use read::ReadError;
pub use validator::ValidatorError as Error;

/// The JSON validator.
pub struct Validator<R: Read> {
    inner: validator::Validator<R>,
}

impl<R: Read> Validator<R> {
    /// Creates a new `Validator` instance with the given reader without any constraints.
    /// You could prefer to use the [`from_slice`], [`from_str`], or [`from_reader`] functions
    pub fn new(read: R) -> Self {
        Validator {
            inner: validator::Validator::new(
                read, NO_LIMIT, NO_LIMIT, NO_LIMIT, NO_LIMIT, NO_LIMIT, true,
            ),
        }
    }

    /// Sets the maximum depth of the JSON structure.
    pub fn with_max_depth(mut self, max_depth: usize) -> Self {
        let inner = self.inner.with_max_depth(max_depth);
        self.inner = inner;
        self
    }

    /// Sets the maximum length of strings.
    pub fn with_max_string_length(mut self, max_string_length: usize) -> Self {
        let inner = self.inner.with_max_string_length(max_string_length);
        self.inner = inner;
        self
    }

    /// Sets the maximum number of entries in arrays.
    pub fn with_max_array_entries(mut self, max_array_length: usize) -> Self {
        let inner = self.inner.with_max_array_entries(max_array_length);
        self.inner = inner;
        self
    }

    /// Sets the maximum number of entries in objects.
    pub fn with_max_object_entries(mut self, max_object_length: usize) -> Self {
        let inner = self.inner.with_max_object_entries(max_object_length);
        self.inner = inner;
        self
    }

    /// Sets the maximum length of object entry names.
    pub fn with_max_object_entry_name_length(
        mut self,
        max_object_entry_name_length: usize,
    ) -> Self {
        let inner = self
            .inner
            .with_max_object_entry_name_length(max_object_entry_name_length);
        self.inner = inner;
        self
    }

    /// Allows duplicate object entry names.
    pub fn allow_duplicate_object_entry_name(mut self) -> Self {
        let inner = self.inner.allow_duplicate_object_entry_name();
        self.inner = inner;
        self
    }

    /// Disallows duplicate object entry names.
    pub fn disallow_duplicate_object_entry_name(mut self) -> Self {
        let inner = self.inner.disallow_duplicate_object_entry_name();
        self.inner = inner;
        self
    }

    /// Validates the JSON payload in a single call, and consumes current [`Validator`] instance.
    ///
    /// # Returns
    ///
    /// * `Ok(())` - If the JSON payload is valid and did not violate any constraints.
    /// * `Err` - If the JSON payload is invalid or violates any constraints.
    ///
    /// # Errors
    ///
    /// * [`Error`] - If the JSON payload is invalid or violates any constraints.
    ///
    /// In the extreme case, the error might return an `Err` and indicate
    /// this crate is running into buggy code.
    /// Please report it to the crate maintainer if you see this error.
    pub fn validate(self) -> Result<(), validator::ValidatorError> {
        self.inner.validate()
    }

    /// Validates the JSON payload in a single call.
    ///
    /// # Arguments
    ///
    /// * `steps` - The number of steps to validate the JSON payload,
    ///             roughly corresponds to the number of tokens processed.
    ///
    /// # Returns
    ///
    /// * `Ok(true)` - If the validation is finished and no errors.
    /// * `Ok(false)` - If the validation is not finished yet, and you should call this function again.
    /// * `Err` - If the JSON payload is invalid or violates any constraints.
    ///
    /// # Errors
    ///
    /// * [`Error`] - If the JSON payload is invalid or violates any constraints.
    ///
    /// In the extreme case, the error might return an `Err` and indicate
    /// this crate is running into buggy code.
    /// Please report it to the crate maintainer if you see this error.
    pub fn validate_with_steps(&mut self, steps: usize) -> Result<bool, validator::ValidatorError> {
        self.inner.validate_with_steps(steps)
    }
}

/// Creates a new `Validator` instance with the given slice of bytes without any constraints.
pub fn from_slice(slice: &[u8]) -> Validator<SliceRead> {
    Validator::new(SliceRead::new(slice))
}

/// Creates a new `Validator` instance with the given `&str` without any constraints.
pub fn from_str(string: &str) -> Validator<StrRead> {
    Validator::new(StrRead::new(string))
}

/// Creates a new `Validator` instance with the given reader without any constraints.
///
/// # Arguments
///
/// * `reader` - The value that implements the [`std::io::Read`] trait.
///
/// # Performance
///
/// Constructing a `Validator` instance with a reader is slower than
/// using [`from_slice`] or [`from_str`] functions.
///
/// And also it is recommended to use the [`std::io::BufReader`] to wrap the reader
/// to improve the performance instead of using the reader directly.
///
/// # Examples
///
/// ```rust
/// use std::io::BufReader;
///
/// fn validate_from_reader<R: std::io::Read>(reader: R) -> Result<(), json_threat_protection::Error> {
///     let buf_reader = BufReader::new(std::fs::File::open("huge.json").unwrap());
///     json_threat_protection::from_reader(buf_reader).validate()
/// }
/// ```
pub fn from_reader<R: std::io::Read>(reader: R) -> Validator<IoRead<R>> {
    Validator::new(IoRead::new(reader))
}