Skip to content

cbor: don't allow infinite nesting by default#373

Merged
kaczmarczyck merged 1 commit intogoogle:developfrom
daviddrysdale:infinity-is-127
Sep 9, 2021
Merged

cbor: don't allow infinite nesting by default#373
kaczmarczyck merged 1 commit intogoogle:developfrom
daviddrysdale:infinity-is-127

Conversation

@daviddrysdale
Copy link
Copy Markdown
Contributor

Change the read()/write() methods to use a nesting limit of 127
internally, to avoid the possibility of heavily nested inputs exhausting
the stack.

Library users that still want to skip nesting checks can still get at
this functionality by using {read,write}_nested(..., None).

@google-cla google-cla bot added the cla: yes label Aug 16, 2021
@coveralls
Copy link
Copy Markdown

coveralls commented Aug 16, 2021

Coverage Status

Coverage increased (+0.1%) to 49.066% when pulling db52294 on daviddrysdale:infinity-is-127 into c6af7c0 on google:develop.

@daviddrysdale daviddrysdale marked this pull request as ready for review August 17, 2021 05:39
/// Supports arbitrarily nested CBOR (so the [`DecoderError::TooMuchNesting`] error is never emitted).
pub fn read(encoded_cbor: &[u8]) -> Result<Value, DecoderError> {
read_nested(encoded_cbor, None)
read_nested(encoded_cbor, Some(i8::MAX))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Actually, now I'm wondering why we even need to support infinite nesting. Shouldn't we just have read_nested(i8)? When is infinite nesting useful? We could keep read() for convenience but I'm not even sure it's a good decision. It's better to see explicitly at call site the nesting. Users could wrap that value in a convenience read() function used everywhere in their project.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, IIRC the read() / write() entrypoints were for convenience, so that library users mostly don't have to think about it (after all, 127 levels of nesting ought to be enough for anyone?).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Intention was to keep the simple interface from before. Means we have to decide between the current state of this PR (default 127, None possible) and have users always pass in the maximum nesting?

I start to see the appeal of the simpler interface that @ia0 is proposing. It does require most users to write a wrapper function for their preferred nesting level, but that is already true for everyone who doesn't like the 127 default. Also, if we don't think anyone should ever call with None, removing that Option makes sense too. And we don't have to provide the default 127 anymore.

So this PR good to merge, but feel free to change to the simple API too if you'd like.

ia0
ia0 previously approved these changes Aug 24, 2021
Copy link
Copy Markdown
Member

@ia0 ia0 left a comment

Choose a reason for hiding this comment

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

I'm still wondering if it wouldn't be better to just remove the foot gun of running with infinite nesting (i.e. read_nested/write_nested take an i8 instead of an Option<i8>). But the PR as it is, is already an improvement by making the default not a foot gun. It should also fix some of the fuzzing errors we have.

kaczmarczyck
kaczmarczyck previously approved these changes Aug 30, 2021
Change the read()/write() methods to use a nesting limit of 127
internally, to avoid the possibility of heavily nested inputs exhausting
the stack.

Library users that still want to skip nesting checks can still get at
this functionality by using `{read,write}_nested(..., None)`.
Copy link
Copy Markdown
Member

@ia0 ia0 left a comment

Choose a reason for hiding this comment

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

I see how removing the Option touches more lines. I can do it afterwards. This looks good to me as is.

Copy link
Copy Markdown
Collaborator

@kaczmarczyck kaczmarczyck left a comment

Choose a reason for hiding this comment

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

What @ia0 said :)

@kaczmarczyck kaczmarczyck merged commit c2b3aec into google:develop Sep 9, 2021
@daviddrysdale daviddrysdale deleted the infinity-is-127 branch October 1, 2021 06:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants