Validate deserialized fields in MergingDigest.fromBytes() - #228
Open
August829 wants to merge 2 commits into
Open
Validate deserialized fields in MergingDigest.fromBytes()#228August829 wants to merge 2 commits into
August829 wants to merge 2 commits into
Conversation
Fixes tdunning#227: MergingDigest.fromBytes() reads compression, n, bufferSize, and lastUsedCell from untrusted input without validation, allowing crafted byte sequences to trigger NegativeArraySizeException or ArrayIndexOutOfBoundsException (denial of service). This commit adds comprehensive validation: - compression must be in (0, 1e9] to prevent integer overflow - n/bufferSize/lastUsedCell must be non-negative and cross-validated - centroid count must not exceed buffer remaining bytes - centroid count must not exceed digest array capacity - all weights must be positive - centroid means must be in non-decreasing order - total weight must be >= centroid count All invalid inputs now throw IllegalArgumentException with descriptive messages. Includes 11 new test cases covering all three original crash triggers plus semantic validation and round-trip correctness. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Two bugs in the initial validation: 1. NaN/Infinity bypass: IEEE 754 comparisons like `weight <= 0` and `mean < prevMean` silently pass NaN (all comparisons with NaN return false). Fix: use negated form `!(w > 0)` which correctly catches NaN, plus explicit `Double.isInfinite()` / `Double.isNaN()` checks. Extract into `checkWeight()` and `checkMean()` helper methods with Javadoc explaining the IEEE 754 rationale. 2. OOM from MAX_COMPRESSION = 1e9: compression of 1e9 causes ~2 billion element arrays (~16GB), trivially triggering OutOfMemoryError from a 32-byte input. Lower MAX_COMPRESSION to 1e6 (~2M array entries, ~16MB), which is generous for any real use case. Add 7 new test cases: NaN mean, NaN weight, Infinity weight, Infinity mean, NaN compression (verbose encoding), NaN weight and NaN mean (small encoding). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Author
|
Hi @tdunning , Thanks for the reply. I’ve submitted a pull request. One more suggestion: could you set up a SECURITY.md to allow more private vulnerability submissions? |
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 #227:
MergingDigest.fromBytes(ByteBuffer)readscompression,n,bufferSize, andlastUsedCellfrom untrusted input without any validation, allowing crafted byte sequences to crash the JVM thread withNegativeArraySizeExceptionorArrayIndexOutOfBoundsException.This PR adds comprehensive input validation to both
VERBOSE_ENCODINGandSMALL_ENCODINGpaths:(0, 1e9]to prevent integer overflow in array sizingAll invalid inputs now throw
IllegalArgumentExceptionwith descriptive messages instead of crashing with rawRuntimeExceptionsubclasses.Test plan
@Test(expected = IllegalArgumentException.class)tests covering:nnexceeding array capacitylastUsedCell > n🤖 Generated with Claude Code