-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Optimize _WS_EXT_RE backtracking on Python 3.11+ #12346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
HarshithReddy01
wants to merge
8
commits into
aio-libs:master
Choose a base branch
from
HarshithReddy01:fix-connection-leak
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+63
−0
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
86edaba
Optimize websocket extension regex backtracking behavior
ed7bc48
Add contributor entry and changelog fragment
99a5848
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 770f7af
Address review: parametrize tests, rename CHANGES fragment to PR number
4e81c23
Rename test_ws_ext_helpers.py to test_websocket_helpers.py
Dreamsorcerer 604a9e9
Update 12346.misc.rst
Dreamsorcerer 9347ca3
Update helpers.py
Dreamsorcerer d5f8764
Merge branch 'master' into fix-connection-leak
Dreamsorcerer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Improved performance of ``_WS_EXT_RE`` regular expression on Python 3.11+ | ||
| by using atomic grouping when parsing ``Sec-WebSocket-Extensions`` headers | ||
| -- by :user:`HarshithReddy01`. |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import time | ||
|
|
||
| import pytest | ||
|
|
||
| from aiohttp._websocket.helpers import ws_ext_parse | ||
| from aiohttp.http_websocket import WSHandshakeError | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("msg", "server", "expected"), | ||
| ( | ||
| ("permessage-deflate", False, (15, False)), | ||
| ("permessage-deflate; server_no_context_takeover", True, (15, True)), | ||
| ("permessage-deflate; client_no_context_takeover", False, (15, True)), | ||
| ("permessage-deflate; server_max_window_bits=12", True, (12, False)), | ||
| ("permessage-deflate; client_max_window_bits=10", False, (10, False)), | ||
| # out-of-range wbits on server side → skip rather than fail | ||
| ("permessage-deflate; server_max_window_bits=8", True, (0, False)), | ||
| # unknown param on server side → no match, return zero | ||
| ("permessage-deflate; unknown_param", True, (0, False)), | ||
| ), | ||
| ) | ||
| def test_ws_ext_parse(msg: str, server: bool, expected: tuple[int, bool]) -> None: | ||
| assert ws_ext_parse(msg, isserver=server) == expected | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("msg", "server"), | ||
| ( | ||
| ("permessage-deflate; client_max_window_bits=8", False), | ||
| ("permessage-deflate; unknown_param", False), | ||
| ), | ||
| ) | ||
| def test_ws_ext_parse_raises(msg: str, server: bool) -> None: | ||
| with pytest.raises(WSHandshakeError): | ||
| ws_ext_parse(msg, isserver=server) | ||
|
|
||
|
|
||
| def test_ws_ext_parse_empty() -> None: | ||
| assert ws_ext_parse(None) == (0, False) | ||
| assert ws_ext_parse("") == (0, False) | ||
|
|
||
|
|
||
| def test_ws_ext_parse_backtracking_performance() -> None: | ||
| # Many valid tokens followed by an invalid suffix — the classic input that | ||
| # triggers exponential backtracking in the outer repeating group. | ||
| evil = "permessage-deflate" + ("; server_no_context_takeover" * 30) + ";INVALID" | ||
| start = time.perf_counter() | ||
| with pytest.raises(WSHandshakeError): | ||
| ws_ext_parse(evil, isserver=False) | ||
| elapsed = time.perf_counter() - start | ||
| assert elapsed < 1.0, f"backtracking regression: took {elapsed:.3f}s" |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.