Skip to content

Add input limits to the parsers and fix round-trip bugs - #71

Open
CDRayn wants to merge 35 commits into
mainfrom
fix/marker-nesting-depth-limit
Open

CDRayn wants to merge 35 commits into
mainfrom
fix/marker-nesting-depth-limit

Conversation

@CDRayn

@CDRayn CDRayn commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Add input limits to the parsers and fix round-trip bugs

This PR adds a limit to each unbounded parser and adds fuzz tests for each package.

Limits

Each limit returns an error. Nothing panics or recurses without a limit.

Package Limit
marker, requirement, reqtxt 100 nested parentheses in a marker
reqtxt Parse: 100,000 entries in a file, 100,000 options in a line
reqtxt Flatten: 100 nested includes, 10,000 opened files, 64 MiB of included files, 100,000 entries in the result
reqtxt WithEnv, when a value makes a new ${NAME} reference: 100 references in a line, 1 MiB of growth, 16 MiB read in all
version 4,300 digits in a version number (the same as Python's int())
version Specifiers.And: 10,000 OR groups (new ErrTooManyOrGroups), 100,000 specifiers in the result (new ErrTooManySpecifiers)
tags, wheelname a tag that expands to more than 10,000 tags
distribution 64 MiB for a METADATA or PKG-INFO entry
distribution 1 MiB for a GPG signature file

Shorter error text

  • A syntax error for an input over 1024 bytes now shows only 80 bytes on each side of the error.
  • Other errors that show input text now show at most 200 bytes of it.
  • Before, a 2 MB input made an error of 2 to 4 MB.

Fixes found by fuzzing

  • marker, requirement and reqtxt: String() now writes every value so that it parses back to the same value. This includes backslashes, mixed quotes, # or - after whitespace, an extra with a trailing _, and a URL that starts with #.
  • reqtxt now matches pip in these cases:
    • Every line break that Python's str.splitlines() uses ends a line.
    • On an option line, a token that does not start with - is ignored.
    • foo.zip @ https://... is a named URL requirement.
    • Whitespace in a line is kept as written.
  • wheelname: an empty project name is now an error. CompareBuildTags now compares build numbers of any size.
  • distribution: Parse no longer panics on an empty or non-gzip .tar.gz file.

Errors that callers can match

  • distribution.Parse now wraps the cause of a read error.
  • New distribution.ErrTooLarge and distribution.ErrUnknownArchiveFormat. Before, these errors were in an internal package, so other modules could not match them.
  • The text of these errors does not change.

Breaking changes

All limits above are breaking, but only for very large or bad input. The reqtxt changes to line breaks, option lines and an empty -r/-c path can change the entries of a file. The CHANGELOG.md Breaking section has each one.

Tests

  • Fuzz tests for each parser package, with seeds from internal/fuzzseed and a seed for each failure that the fuzz tests found.
  • Table tests at each limit, at the limit plus 1, and with very large input (for example, 2 million ().
  • Round-trip tests for String(), and pip-parity tests checked against pip 21.2.4.
  • Error tests check errors.Is and the message text, not only the happy path.

Performance tests

  • Benchmarks for the hot paths of version, requirement, marker, reqtxt, wheelname, tags and distribution, in each package's bench_test.go.
  • Allocation-budget tests (TestAllocBudget_*) over the same inputs. They run in CI as part of go test ./.... A test fails when a call allocates more or fewer times than its budget, so a performance regression is caught without timing anything. The tests skip under -race, which adds allocations of its own.
  • The benchmarks do not run in CI, because timing on shared runners is too noisy. The "Performance" section of CLAUDE.md describes how to compare a branch with main using benchstat.

How to verify

go test ./...
go vet ./...
golangci-lint run ./...
go test ./marker -run '^$' -fuzz FuzzParse -fuzztime 60s
go test ./reqtxt -run '^$' -bench . -benchmem

@CDRayn
CDRayn marked this pull request as ready for review December 4, 2026 19:02

@nodivbyzero nodivbyzero left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed with benchmarks against main and the PR's fuzz targets. Two bugs (1, 2), four limits that can be bypassed or don't cap memory (3–6), one slowdown (7), and one limit Parse never reaches (8). Each comment has the input that shows it.

Comment thread reqtxt/parse.go Resolved
Comment thread internal/pep508/tokenizer.go Resolved
Comment thread version/specifier_query.go Resolved
Comment thread reqtxt/flatten.go Resolved
Comment thread reqtxt/preprocess.go Resolved
Comment thread reqtxt/string.go Resolved

@jonyoder jonyoder left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, Clay. The limits themselves look solid; I mutated each constant by one and the tests caught almost all of them. But two of the limits can still be bypassed, two of the new fuzz targets fail within seconds on a fresh run, and one reqtxt change rejects lines that pip accepts.

My bigger question is scope: only about 900 of the 13K lines are the security limits. The pip-parity changes, the String() fixes, the fast paths and the alloc budgets carry most of the breaking risk. Could we split this into three PRs (limits, reqtxt parity and round trip, perf), so the security part can land on its own?

reqtxt/parse.go:431: This now sends the whole line to the PEP 508 parser, which only allows space and tab. So foo ; os_name=='a' (and the same with NBSP or U+3000 after foo[x] or foo @ url) is now an error. Base accepted these lines, and pip 26.3 accepts them too, because pip splits at the ; and .strip()s each side first. Can we split and trim with isPythonSpace the way pip does? If this is meant to narrow, the Breaking entry needs to cover it. Right now that entry only mentions whitespace after a version, and it cites packaging rather than pip.

Comment thread internal/pep508/tokenizer.go Resolved
Comment thread version/specifier_query.go Resolved
Comment thread reqtxt/fuzz_test.go Resolved
Comment thread reqtxt/fuzz_test.go Resolved
Comment thread reqtxt/string.go Resolved
Comment thread CHANGELOG.md Resolved
@CDRayn
CDRayn force-pushed the fix/marker-nesting-depth-limit branch from d1f303a to 21a3bea Compare December 5, 2026 00:51
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.

3 participants