fix quadratic backtracking in Forwarded and Link header parsing - #13285
Draft
arshsmith1 wants to merge 1 commit into
Draft
fix quadratic backtracking in Forwarded and Link header parsing#13285arshsmith1 wants to merge 1 commit into
arshsmith1 wants to merge 1 commit into
Conversation
Merging this PR will not alter performance
Comparing Footnotes
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #13285 +/- ##
=======================================
Coverage 98.98% 98.98%
=======================================
Files 132 132
Lines 49023 49050 +27
Branches 2551 2552 +1
=======================================
+ Hits 48526 48553 +27
Misses 373 373
Partials 124 124
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
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.
What do these changes do?
HeadersDictProxy.getallsplits a comma-separated header into elements with_LIST_ELEMENT_RE, whose unquoted-element group used a lazy quantifier ahead of a trailing[ \t]*(?:,|\Z). When an element carries a long run of whitespace before a non-space character (e.g.for=afollowed by many spaces thenb), the engine backtracks quadratically. That regex sits on two attacker-reachable paths:request.forwarded(a request header, server side) andresponse.links(a response header, client side), so a single ~8 KB header value burns hundreds of ms of CPU per access.response.linksalso parsed eachLinkparameter with(.*?)(\2)\s*$, which backtracks the same way on an unquoted value that ends in trailing whitespace. Making the split-regex group greedy fixes the first case with no change in output (the captured group is stripped afterwards anyway), and theLinkparameter is now captured greedily with the surrounding quotes and whitespace stripped in Python. Both go from O(n^2) to O(n).Are there changes in behavior for the user?
No. Parsed results are identical for every input; only the worst-case timing changes. I ran a differential fuzz of the old and new
getallregex over a few hundred thousand generated inputs with zero divergence, and the same for theLinkparameter parsing.Is it a substantial burden for the maintainers to support this?
No. It is a one-character change to the split regex plus a small rewrite of the
Linkparameter parse, each covered by a regression test that fails on the unpatched tree.Related issue number
N/A
Checklist
CONTRIBUTORS.txtCHANGES/folderBefore/after timing (pure-Python parser)