Cap parser nesting depth to prevent stack overflow#4
Merged
Conversation
parseArray/parseDict recursed without limit, so deeply nested [[[...]]] or <<...>> in a hostile PDF could overflow the stack -- a fatal, unrecoverable crash. Cap nesting at maxParseDepth (1000) and return an error past it; real PDFs nest only a few levels. Tests: deeply nested array and dict are rejected; moderate nesting still resolves.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
parseArray/parseDictrecurse with no depth limit. A hostile PDF with deeply nested[[[…]]]or<<…>>drives the recursive-descent parser into a stack overflow — a fatal, unrecoverable crash (it isn't catchable byrecover). Reachable fromOpenand from any object resolution.Fix
Cap nesting at
maxParseDepth(1000) in bothparseArrayandparseDict, returning an error past it. Real PDFs nest only a handful of levels; 1000 is far above legitimate use and far below a stack overflow.Tests
TestDeeplyNestedRejected(array + dict subtests) — input nested well past the cap returns an error instead of crashing.TestModeratelyNestedArrayResolves— moderate nesting (100) still resolves to anArray, so the cap doesn't break legitimate input.Test depths sit above the cap but below an actual stack overflow, so a regression surfaces as a clean failure. Full suite +
go vetpass; gofmt-clean.