Skip to content

Bound input length and constraint count in Parse and ParseNative - #64

Merged
andrew merged 2 commits into
git-pkgs:mainfrom
charlie-morrison:parser-input-limits
Nov 24, 2026
Merged

andrew merged 2 commits into
git-pkgs:mainfrom
charlie-morrison:parser-input-limits

Conversation

@charlie-morrison

Copy link
Copy Markdown
Contributor

Fixes #53.

Parser gets MaxLength and MaxConstraints, with the Ruby gem's defaults (2048 bytes, 64 constraints). Zero means the default, so a Parser{} literal is bounded too. Parse and ParseNative return ErrRangeTooLong for longer input. Every list that a parser intersects or unions part by part returns ErrTooManyConstraints above 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:

ParseNative input n=250 n=1000
composer 1.0 || 2.0 || … 4.9 s 65.7 s
pypi !=1.0,!=2.0,… 596 ms 8.6 s
composer !=1.0 !=2.0 … 170 ms 2.7 s
gem != 1.0, … 26 ms 343 ms
hex … and … 15 ms 165 ms
cargo / npm AND / pub 5-8 ms 56-105 ms

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_constraints and 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 in parseConstraints. A separate check before validateVersConstraints would only save linear work, and a mutation showed nothing depends on it.

TestNpmParseManyClauses parses 500 npm clauses to guard the #15 merge fix, so it now uses a Parser with raised limits.

Tests (limits_test.go): for each of the 12 lists, 64 parts pass, 65 fail from NewParser() and from Parser{}, and 65 pass with MaxConstraints: 65. The length tests cover exactly 2048 and 2049 bytes for both entry points, a raised and a lowered MaxLength, and the message. The package-level Parse and ParseNative use 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.

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]>
@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Explain Change →

Caution

Review failed

The pull request is closed.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Share

@andrew
andrew merged commit 70710c0 into git-pkgs:main Nov 24, 2026
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.

Bound input length and constraint count in Parse/ParseNative

2 participants