3 ms·
Parsing untrusted input on a machine where failure could affect someone other than the person who provided the input is definitely a known case to be mindful of
by munificent 3y ago
Parsing untrusted input on a machine where failure could affect someone other than the person who provided the input is definitely a known case to be mindful of.
You'll want to cap the maximum allowed nesting depth. Even if not using recursion, you probably don't want untrusted input to be able to make your stack data structure allocate arbitrary amounts of memory.
If you do put a nesting limit in, you can do that equally well using an actual stack or recursion. (For example, I believe v8 still uses recursive descent for parsing JSON and JavaScript. It detects and handles stack overflows to ensure that untrusted input can't blow the stack and crash. I'm not sure if it's using a hardcoded nesting limit or actually trapping the stack/heap collision somehow.)
- Joel_Mckay 3y agoYou mean like the reply depth limit in forums like YC. Shouldn't you stick to a depth-first branch reply limit until moving to the next fork. I want to respect your beliefs. =)
- jcgrillo 3y agoYes, unfortunately in the python case the standard library's json parser has no way to configure a reasonable limit. It'll just consume stack frames until it hits the interpeter's recursion limit. And, as has been mentioned elsewhere, python stack frames are expensive for a variety of reasons. I've run into similar issues with recursive code in java. The story gets better in C or rust, but there wre still sharp edges. I guess there are sharp edges everywhere though...