Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit recursion to 128 levels #163

Merged
merged 1 commit into from Dec 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 33 additions & 6 deletions json/src/de.rs
Expand Up @@ -94,6 +94,7 @@ impl<Iter> de::Deserializer for Deserializer<Iter>
struct DeserializerImpl<R: Read> {
read: R,
str_buf: Vec<u8>,
remaining_depth: u8,
}

macro_rules! overflow {
Expand All @@ -107,6 +108,7 @@ impl<R: Read> DeserializerImpl<R> {
DeserializerImpl {
read: read,
str_buf: Vec::with_capacity(128),
remaining_depth: 128,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about adding a Deserializer::with_recursion_limit method so this can be customized? Or maybe a Deserializer builder with a with_recursion_limit method?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am keeping #162 open to decide whether this needs to be configurable.

}
}

Expand Down Expand Up @@ -205,12 +207,30 @@ impl<R: Read> DeserializerImpl<R> {
visitor.visit_str(s)
}
b'[' => {
self.remaining_depth -= 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose all this could be moved into the SeqVisitor but I'm not sure if that'd make things any more clean.

if self.remaining_depth == 0 {
return Err(self.peek_error(stack_overflow()));
}

self.eat_char();
visitor.visit_seq(SeqVisitor::new(self))
let ret = visitor.visit_seq(SeqVisitor::new(self));

self.remaining_depth += 1;

ret
}
b'{' => {
self.remaining_depth -= 1;
if self.remaining_depth == 0 {
return Err(self.peek_error(stack_overflow()));
}

self.eat_char();
visitor.visit_map(MapVisitor::new(self))
let ret = visitor.visit_map(MapVisitor::new(self));

self.remaining_depth += 1;

ret
}
_ => Err(self.peek_error(ErrorCode::ExpectedSomeValue)),
};
Expand Down Expand Up @@ -523,6 +543,10 @@ impl<R: Read> DeserializerImpl<R> {
}
}

fn stack_overflow() -> ErrorCode {
ErrorCode::Custom("recursion limit exceeded".into())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a shame we can't add error codes in a backwards compatible fashion. Next major release though :)

}

static POW10: [f64; 309] =
[1e000, 1e001, 1e002, 1e003, 1e004, 1e005, 1e006, 1e007, 1e008, 1e009,
1e010, 1e011, 1e012, 1e013, 1e014, 1e015, 1e016, 1e017, 1e018, 1e019,
Expand Down Expand Up @@ -610,12 +634,15 @@ impl<R: Read> de::Deserializer for DeserializerImpl<R> {

match try!(self.peek_or_null()) {
b'{' => {
self.remaining_depth -= 1;
if self.remaining_depth == 0 {
return Err(self.peek_error(stack_overflow()));
}

self.eat_char();
try!(self.parse_whitespace());
let value = try!(visitor.visit(VariantVisitor::new(self)));

let value = {
try!(visitor.visit(VariantVisitor::new(self)))
};
self.remaining_depth += 1;

try!(self.parse_whitespace());

Expand Down
14 changes: 13 additions & 1 deletion json_tests/tests/test_json.rs
@@ -1,6 +1,7 @@
use std::f64;
use std::fmt::Debug;
use std::i64;
use std::iter;
use std::marker::PhantomData;
use std::u64;

Expand Down Expand Up @@ -737,7 +738,7 @@ macro_rules! test_parse_err {
}

// FIXME (#5527): these could be merged once UFCS is finished.
fn test_parse_err<T>(errors: Vec<(&'static str, Error)>)
fn test_parse_err<T>(errors: Vec<(&str, Error)>)
where T: Debug + PartialEq + de::Deserialize,
{
for &(s, ref err) in &errors {
Expand Down Expand Up @@ -1632,3 +1633,14 @@ fn test_json_pointer() {
assert!(data.pointer("/foo/00").is_none());
assert!(data.pointer("/foo/01").is_none());
}

#[test]
fn test_stack_overflow() {
let brackets: String = iter::repeat('[').take(127).chain(iter::repeat(']').take(127)).collect();
let _: Value = serde_json::from_str(&brackets).unwrap();

let brackets: String = iter::repeat('[').take(128).collect();
test_parse_err::<Value>(vec![
(&brackets, Error::Syntax(ErrorCode::Custom("recursion limit exceeded".into()), 1, 128)),
]);
}