fix(json): bound JSON reader nesting depth to prevent stack overflow (FB-2936) - #42
Merged
Merged
Conversation
…(FB-2936)
The JSON block parser parses iteratively (kParseIterativeFlag) and cannot
overflow, but the array/struct builders are finalized recursively
(RawArrayBuilder<kArray|kObject>::Finish / RawBuilderSet::Finish), so deeply
nested input (e.g. {"a":{"a":{...}}} or [[[...]]]) overflows the native stack and
crashes the process before any limit fires.
Reject input nested deeper than kMaxNestingDepth (1000) in StartNested() — the
single point where built (non-skipped) object/array nesting deepens for every
handler — so the pathological case is unreachable by construction. Mirrors the
existing thrift skip() recursion-depth guard.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
moshap-firebolt
force-pushed
the
moshap/fb-2936-json-nesting-depth-guard
branch
from
August 14, 2026 04:41
83b978e to
c438631
Compare
lorenzhs
approved these changes
Aug 17, 2026
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.
Summary
Fixes FB-2936 — an unauthenticated remote crash (stack-overflow SIGSEGV) in the Arrow C++ JSON reader on deeply nested JSON. Firebolt's
read_json()(and JSON external tables /COPY FROM) reach this via schema inference; any read-only query over an attacker-supplied JSON file can take down the engine process.Root cause
arrow::json's block parser runs RapidJSON iteratively (kParseIterativeFlag), so parsing deep JSON is stack-safe. But the array/struct builders are finalized recursively —RawArrayBuilder<Kind::kArray|kObject>::Finish⇄RawBuilderSet::Finishrecurse one native frame per nesting level with no depth guard. Deeply nested input ({"a":{"a":{…}}}or[[[…]]], ~20k levels) overflows the thread stack during finalization, before any limit can fire.ParseOptionsexposes no depth cap (checked against upstreammaintoo).Fix
Reject input nested deeper than
kMaxNestingDepth(1000) inHandlerBase::StartNested()— the single point where built (non-skipped) object/array nesting deepens, shared by every handler (InferType/Ignore/Error) viaStartObjectImpl/StartArrayImpl.builder_stack_.size()is the live depth; on exceed we returnStatus::Invalid, which aborts the (already-iterative) parse cleanly. Skipped subtrees don't build and can't drive theFinishrecursion, so guarding built nesting is sufficient. This mirrors the existingfix(thrift): restore skip() recursion-depth guardpatch.Three lines of behavior change at one choke point; ~1 comparison per nested container (negligible).
Validation
Built Firebolt with this patch and its packdb-side guard removed, then ran the JSON inference SQL suite:
JSON nesting depth exceeds the maximum of 1000) instead of SIGSEGV.read_json,read_files,read_relative_path, and external-table JSON tests all pass unchanged.Notes
ParseOptions::max_nesting_depthfield — that's also the form to propose upstream to apache/arrow, which has the same gap.Co-Authored-By: Claude Opus 4.8 noreply@anthropic.com
🤖 Generated with Claude Code
Note
Cursor Bugbot is generating a summary for commit 83b978e. Configure here.