Skip to content

Harden the SNBT parser, inventory string parser and cache keys - #118

Open
Aerhhh wants to merge 1 commit into
masterfrom
security/parser-hardening
Open

Aerhhh wants to merge 1 commit into
masterfrom
security/parser-hardening

Conversation

@Aerhhh

@Aerhhh Aerhhh commented Nov 21, 2026

Copy link
Copy Markdown
Owner

Why

Follow-up to the security review (see #116). Three smaller issues in code that handles untrusted text.

What changed

SnbtParser

  • Nesting is capped at 512 levels (vanilla's NBT limit). Parsing is recursive, so {a:[[[[… with a few thousand brackets overflowed the stack, and tryParse only caught Exception, so the StackOverflowError escaped. It is now an NbtParseException.
  • A number that matches a typed pattern but is out of range for the type (999b, 99999s) stays an unquoted string instead of throwing a raw NumberFormatException.

InventoryStringParser

  • The slot-overlap check between neighbouring items compared every slot of one against every slot of the other with nested streams. It now uses a BitSet; behaviour is the same (the later item wins shared slots).
  • Range expansion drops duplicate slots, so [1-54,1-54,…] no longer expands to many times the input length.
  • Malformed numbers in ranges and {slot:amount} maps are reported as GeneratorException instead of NumberFormatException.

GeneratorCacheKey

  • Short values are written with a length prefix and hashed values carry a separate tag. Previously lore="x;name=y", name="z" and lore="x", name="y;name=z" flattened to the same string, and a 64-character value equal to another value's SHA-256 collided with it, so one configuration could be served another's cached image. The cache is in memory only, so the changed key format needs no migration.

Testing

  • New SnbtParserLimitsTest (4) and GeneratorCacheKeyTest (3); three tests added to InventoryStringParserTest.
  • Targeted run of 75 tests across NBT parsing, inventory, recipe and tool tests: all pass. The full suite was not run locally.

- SnbtParser: cap nesting at 512 levels so deep input is a parse error
  instead of a StackOverflowError that tryParse did not catch; keep
  out-of-range typed numbers (999b) as text instead of throwing
  NumberFormatException.
- InventoryStringParser: replace the quadratic slot-overlap check with a
  BitSet, drop duplicate slots when expanding ranges, and report
  malformed numbers as GeneratorException.
- GeneratorCacheKey: length-prefix short values and tag hashed ones so a
  value containing ';name=' or spelling a hash cannot collide with
  another configuration.
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.

1 participant