Repository navigation
Bound input length and constraint count in Parse and ParseNative - #64
Merged
Merged
Conversation
Parser gets MaxLength and MaxConstraints, with the Ruby gem's defaults (2048 bytes, 64 constraints); zero means the default. Parse and ParseNative return ErrRangeTooLong for longer input, and every list the parsers intersect or union part by part returns ErrTooManyConstraints above the limit. Those loops are quadratic: a 250-clause composer OR took 4.9 s and 1,000 pypi != specifiers 8.6 s. Fixes git-pkgs#53 Co-authored-by: Claude <[email protected]>
|
Caution Review failedThe pull request is closed.
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
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.
Fixes #53.
ParsergetsMaxLengthandMaxConstraints, with the Ruby gem's defaults (2048 bytes, 64 constraints). Zero means the default, so aParser{}literal is bounded too.ParseandParseNativereturnErrRangeTooLongfor longer input. Every list that a parser intersects or unions part by part returnsErrTooManyConstraintsabove the limit. Both errors are sentinels wrapped with the counts, e.g.range string too long (2049 > 2048).The count check matters on its own, not just the length, because those loops are quadratic. Measured on 1795ed5, n parts joined by the scheme's separator:
ParseNativeinput1.0 || 2.0 || …!=1.0,!=2.0,…!=1.0 !=2.0 …!= 1.0, …… and …A 250-clause composer OR is 2136 bytes, so 2048 alone would still let a single call run for seconds. With both limits, the slowest input I found is a 64-clause composer OR at 163 ms. A nested one (55 OR clauses of five
!=each, 2036 bytes) takes 8 ms, and 64 pypi!=take 22 ms.The checks sit where the gem puts them (
parse_constraintsand npm||), plus every other split whose cost grows with the part count: npm AND tokens, gem, pypi, cargo and hex,/and/or, composer OR and AND, and pub. Maven, NuGet, Go, deb, rpm, conan, nginx and openssl stayed linear in the same measurement, so the length limit covers them. A vers URI is counted inparseConstraints. A separate check beforevalidateVersConstraintswould only save linear work, and a mutation showed nothing depends on it.TestNpmParseManyClausesparses 500 npm clauses to guard the #15 merge fix, so it now uses aParserwith raised limits.Tests (
limits_test.go): for each of the 12 lists, 64 parts pass, 65 fail fromNewParser()and fromParser{}, and 65 pass withMaxConstraints: 65. The length tests cover exactly 2048 and 2049 bytes for both entry points, a raised and a loweredMaxLength, and the message. The package-levelParseandParseNativeuse the defaults. Removing any one of the 13 checks turns its row red, and so does ignoring the configured limits.go test ./...ok (conformance with the submodule), lint 0, gofmt clean.