Registry Mirror
Skip to content

Conversation

@christophwille

Copy link
Copy Markdown
Member

Summary

Caps the nesting depth of the bundled LightJson parser so that pathologically nested JSON fails with a catchable exception instead of crashing the process with an uncatchable StackOverflowException.

The vulnerability (CWE-674, uncontrolled recursion)

ICSharpCode.Decompiler/Metadata/LightJson/Serialization/JsonReader.cs parses values through three mutually recursive methods with no depth limit:

ReadJsonValue -> ReadObject / ReadArray -> ReadJsonValue -> ...

Each level of [ / { nesting adds stack frames. Input nested tens of thousands of levels deep ([[[[ ... ]]]]) exhausts the stack and raises a StackOverflowException, which on .NET cannot be caught and terminates the whole process.

Reachability

DotNetCorePathFinder parses the *.deps.json that ships next to an opened assembly:

// ICSharpCode.Decompiler/Metadata/DotNetCorePathFinder.cs:190
var dependencies = JsonReader.Parse(File.ReadAllText(depsJsonFileName));

So a crafted .deps.json placed beside a target assembly turns ordinary dependency resolution into a clean process kill. It is a secondary surface (the attacker must drop a file next to the assembly rather than embed bytes in the assembly itself), but the outcome - an uncatchable crash during a routine load - is severe enough to fix.

The fix

Thread an integer depth counter through ReadJsonValue / ReadObject / ReadArray, incrementing on each descent into a nested container, and throw a catchable JsonParseException (new ErrorType.MaximumNestingDepthExceeded) once nesting passes a fixed cap.

The cap is 64, matching the System.Text.Json default MaxDepth and sitting far beyond any real dependency manifest (real .deps.json files nest only a handful of levels). This is the smallest change that closes the hole without adding a dependency or altering the parser's accepted grammar for legitimate input.

Why patch in place rather than swap to a maintained fork

The actively maintained fork CypherPotato/LightJson was evaluated as an alternative and rejected for this purpose:

  • It does not fix this bug. Its ReadJsonValue/ReadObject/ReadArray are still mutually recursive with no depth guard on the DOM parse path. Its DynamicObjectMaxDepth = 64 limit lives only in the reflection object-mapper layer (JsonDeserializer/JsonSerializer), not in the raw Parse path we use. The same crafted input still overflows the stack.
  • It is not a drop-in. The API diverged substantially: no static JsonReader.Parse(string); parsing goes through JsonValue/an instance Parse, and value access moved from As* properties to GetString()/GetNumber().
  • It changes default semantics. Our copy always strips // and /* */ comments and always rejects duplicate object keys; the fork gates both behind opt-in options that are off by default, so a straight swap would silently start accepting duplicate keys and rejecting commented manifests.

Adopting the fork would therefore cost an API migration and a behavior audit while still leaving this exact bug unpatched. A ~5-line depth guard on the vendored copy is the lower-risk fix.

Tests

ICSharpCode.Decompiler.Tests/LightJsonParserTests.cs (new):

  • Deeply nested arrays -> JsonParseException (not a crash).
  • Deeply nested objects -> JsonParseException (not a crash).
  • Moderately nested JSON (within the cap) still round-trips, so the guard does not reject legitimately structured documents.

Verified red before the change (the deep-nesting cases parsed without throwing) and green after.


Generated with Claude Code

The JSON parser's value/object/array readers are mutually recursive with
no depth limit, so input nested tens of thousands of levels deep overflows
the stack with an uncatchable StackOverflowException (CWE-674) that kills
the process. This is reachable through DotNetCorePathFinder, which parses
the .deps.json shipped next to an opened assembly, so a crafted manifest
beside a target turns dependency resolution into a clean process kill.

Thread a depth counter through the readers and throw a catchable
JsonParseException once nesting passes a fixed cap. The cap (64) matches
the System.Text.Json default and is far beyond any real dependency graph.

Assisted-by: Claude:claude-fable-5:Claude Code
@christophwille
christophwille merged commit 436a82a into master Aug 25, 2026
17 checks passed
@christophwille
christophwille deleted the fix/lightjson-parser-depth-limit branch August 25, 2026 16:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants