Repository navigation
Conversation
nodivbyzero
left a comment
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
d1f303a to
21a3bea
Compare
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.
marker,requirement,reqtxtreqtxtParse: 100,000 entries in a file, 100,000 options in a linereqtxtFlatten: 100 nested includes, 10,000 opened files, 64 MiB of included files, 100,000 entries in the resultreqtxtWithEnv, when a value makes a new${NAME}reference: 100 references in a line, 1 MiB of growth, 16 MiB read in allversionint())versionSpecifiers.And: 10,000 OR groups (newErrTooManyOrGroups), 100,000 specifiers in the result (newErrTooManySpecifiers)tags,wheelnamedistributiondistributionShorter error text
Fixes found by fuzzing
marker,requirementandreqtxt: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#.reqtxtnow matches pip in these cases:str.splitlines()uses ends a line.-is ignored.foo.zip @ https://...is a named URL requirement.wheelname: an empty project name is now an error.CompareBuildTagsnow compares build numbers of any size.distribution:Parseno longer panics on an empty or non-gzip.tar.gzfile.Errors that callers can match
distribution.Parsenow wraps the cause of a read error.distribution.ErrTooLargeanddistribution.ErrUnknownArchiveFormat. Before, these errors were in an internal package, so other modules could not match them.Breaking changes
All limits above are breaking, but only for very large or bad input. The
reqtxtchanges to line breaks, option lines and an empty-r/-cpath can change the entries of a file. TheCHANGELOG.mdBreakingsection has each one.Tests
internal/fuzzseedand a seed for each failure that the fuzz tests found.().String(), and pip-parity tests checked against pip 21.2.4.errors.Isand the message text, not only the happy path.Performance tests
version,requirement,marker,reqtxt,wheelname,tagsanddistribution, in each package'sbench_test.go.TestAllocBudget_*) over the same inputs. They run in CI as part ofgo 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.CLAUDE.mddescribes how to compare a branch withmainusingbenchstat.How to verify