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
//! Parse JSON while blocking prototype-poisoning keys.
//!
//! This crate parses untrusted JSON and detects two key patterns that pollute a
//! JavaScript object's prototype once the parsed value is copied, merged, or
//! iterated:
//!
//! 1. a key literally named `__proto__`
//! 2. a key named `constructor` whose value is an object that contains a
//! `prototype` key (a `constructor.prototype` nesting)
//!
//! For each pattern you choose an [`Action`]: error out, remove the key, or
//! ignore it and behave like a plain JSON parse. A `safe` flag turns any
//! detected violation into `Ok(None)` instead of an error.
//!
//! The parsed value is a [`serde_json::Value`]. In a `Value` tree `__proto__`
//! and `constructor` are ordinary map keys, so the danger is latent rather than
//! immediate. The job here is to detect, remove, or reject those keys before the
//! value reaches code that would treat them as a prototype.
//!
//! # Quick start
//!
//! ```
//! use secure_json_parse::{parse, Action, Options, Error};
//!
//! // Default options error on a forbidden key.
//! let err = parse(r#"{"__proto__": {"x": 7}}"#, &Options::default());
//! assert!(matches!(err, Err(Error::ForbiddenProperty)));
//!
//! // Remove the key instead.
//! let opts = Options::default().proto_action(Action::Remove);
//! let value = parse(r#"{"a": 5, "__proto__": {"x": 7}}"#, &opts).unwrap().unwrap();
//! assert_eq!(value, serde_json::json!({"a": 5}));
//! ```
//!
//! # Safe parsing
//!
//! [`safe_parse`] folds every outcome into one three-valued result:
//!
//! ```
//! use secure_json_parse::{safe_parse, SafeOutcome};
//!
//! // Clean input parses to a value.
//! assert!(matches!(safe_parse(r#"{"a": 1}"#), SafeOutcome::Value(_)));
//! // A forbidden key yields Violation.
//! assert!(matches!(safe_parse(r#"{"__proto__": {}}"#), SafeOutcome::Violation));
//! // Malformed JSON yields Malformed.
//! assert!(matches!(safe_parse(r#"{"a": "#), SafeOutcome::Malformed));
//! ```
pub use scan;
use Value;
/// What to do when a forbidden key is found.
///
/// `proto_action` and `constructor_action` each take one of these. The default
/// is [`Action::Error`].
/// Configuration for [`parse`], [`parse_bytes`], and [`scan`].
///
/// Build one with [`Options::default`] and the chained setters. The defaults
/// match a strict parser: both actions are [`Action::Error`] and `safe` is off.
/// Why a parse failed.
///
/// [`Error::Syntax`] wraps a malformed-JSON error from the underlying parser.
/// [`Error::ForbiddenProperty`] reports a `__proto__` or `constructor.prototype`
/// violation when the relevant [`Action`] is [`Action::Error`] and `safe` is
/// off. Both violation kinds share one variant and one message.
/// The three outcomes of [`safe_parse`].
///
/// The variant names state the outcome. The JavaScript `safeParse` maps the
/// same three cases to a value, `null` for a violation, and `undefined` for a
/// parse error.
/// Drop a leading `U+FEFF` byte order mark from a string.
///
/// The UTF-8 BOM is `EF BB BF`, which decodes to `U+FEFF`. Removing it before
/// parsing matches a JavaScript parser that strips a leading BOM.
/// Parse JSON text and apply prototype-poisoning checks.
///
/// On success returns `Ok(Some(value))`. When `safe` is on and a violation is
/// found, returns `Ok(None)`. A malformed input gives [`Error::Syntax`]; a
/// violation under [`Action::Error`] gives [`Error::ForbiddenProperty`].
///
/// Scalars and `null` skip scanning and are returned as is. Objects and arrays
/// are walked.
///
/// # Errors
///
/// Returns [`Error::Syntax`] if `text` is not valid JSON, or
/// [`Error::ForbiddenProperty`] if a forbidden key is found and the matching
/// [`Action`] is [`Action::Error`] with `safe` off.
///
/// # Examples
///
/// ```
/// use secure_json_parse::{parse, Action, Options};
///
/// let opts = Options::default().constructor_action(Action::Remove);
/// let v = parse(r#"{"constructor": {"prototype": {}}, "a": 1}"#, &opts)
/// .unwrap()
/// .unwrap();
/// assert_eq!(v, serde_json::json!({"a": 1}));
/// ```
/// Parse JSON bytes and apply prototype-poisoning checks.
///
/// Same as [`parse`] but takes UTF-8 bytes. A leading UTF-8 byte order mark
/// (`EF BB BF`) is stripped before parsing.
///
/// # Errors
///
/// Returns [`Error::Syntax`] if the bytes are not valid UTF-8 JSON, or
/// [`Error::ForbiddenProperty`] under the same rules as [`parse`].
/// Parse JSON text and fold every outcome into a [`SafeOutcome`].
///
/// Runs with both actions at [`Action::Error`] and `safe` on. A clean parse
/// returns [`SafeOutcome::Value`]. A forbidden key returns
/// [`SafeOutcome::Violation`]. Malformed JSON returns [`SafeOutcome::Malformed`].
/// This never returns an error type.
///
/// # Examples
///
/// ```
/// use secure_json_parse::{safe_parse, SafeOutcome};
///
/// match safe_parse(r#"{"a": 1}"#) {
/// SafeOutcome::Value(v) => assert_eq!(v, serde_json::json!({"a": 1})),
/// _ => panic!("expected a value"),
/// }
/// ```
/// Parse JSON bytes and fold every outcome into a [`SafeOutcome`].
///
/// The byte counterpart to [`safe_parse`]. A leading UTF-8 byte order mark is
/// stripped before parsing.
/// Collapse a parse result into a [`SafeOutcome`].