-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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 3 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 @@ | ||
| Used an atomic group in ``_WS_EXT_RE`` on Python 3.11+ to prevent | ||
| unnecessary backtracking 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,80 @@ | ||
| import time | ||
|
|
||
| import pytest | ||
|
|
||
| from aiohttp._websocket.helpers import ws_ext_parse | ||
| from aiohttp.http_websocket import WSHandshakeError | ||
|
|
||
|
|
||
| class TestWsExtParse: | ||
| def test_empty(self) -> None: | ||
| assert ws_ext_parse(None) == (0, False) | ||
| assert ws_ext_parse("") == (0, False) | ||
|
|
||
| def test_permessage_deflate_only(self) -> None: | ||
| compress, notakeover = ws_ext_parse("permessage-deflate") | ||
| assert compress == 15 | ||
| assert notakeover is False | ||
|
|
||
| def test_server_no_context_takeover(self) -> None: | ||
| compress, notakeover = ws_ext_parse( | ||
| "permessage-deflate; server_no_context_takeover", isserver=True | ||
| ) | ||
| assert compress == 15 | ||
| assert notakeover is True | ||
|
|
||
| def test_client_no_context_takeover(self) -> None: | ||
| compress, notakeover = ws_ext_parse( | ||
| "permessage-deflate; client_no_context_takeover", isserver=False | ||
| ) | ||
| assert compress == 15 | ||
| assert notakeover is True | ||
|
|
||
| def test_server_max_window_bits(self) -> None: | ||
| compress, notakeover = ws_ext_parse( | ||
| "permessage-deflate; server_max_window_bits=12", isserver=True | ||
| ) | ||
| assert compress == 12 | ||
| assert notakeover is False | ||
|
|
||
| def test_client_max_window_bits(self) -> None: | ||
| compress, notakeover = ws_ext_parse( | ||
| "permessage-deflate; client_max_window_bits=10", isserver=False | ||
| ) | ||
| assert compress == 10 | ||
| assert notakeover is False | ||
|
|
||
| def test_window_bits_out_of_range_server(self) -> None: | ||
| # out-of-range wbits on server side → skip, return 0 | ||
| compress, _ = ws_ext_parse( | ||
| "permessage-deflate; server_max_window_bits=8", isserver=True | ||
| ) | ||
| assert compress == 0 | ||
|
|
||
| def test_window_bits_out_of_range_client(self) -> None: | ||
| with pytest.raises(WSHandshakeError): | ||
| ws_ext_parse("permessage-deflate; client_max_window_bits=8", isserver=False) | ||
|
|
||
| def test_invalid_extension_client_raises(self) -> None: | ||
| with pytest.raises(WSHandshakeError): | ||
| ws_ext_parse("permessage-deflate; unknown_param", isserver=False) | ||
|
|
||
| def test_no_match_server_returns_zero(self) -> None: | ||
| compress, notakeover = ws_ext_parse( | ||
| "permessage-deflate; unknown_param", isserver=True | ||
| ) | ||
| assert compress == 0 | ||
| assert notakeover is False | ||
|
|
||
| def test_backtracking_performance(self) -> None: | ||
| # Crafted input: many valid tokens followed by an invalid suffix. | ||
| # Without the atomic group fix this causes exponential backtracking. | ||
| evil = "permessage-deflate" + ("; server_no_context_takeover" * 30) + ";INVALID" | ||
| start = time.perf_counter() | ||
| try: | ||
|
Dreamsorcerer marked this conversation as resolved.
Outdated
|
||
| ws_ext_parse(evil, isserver=True) | ||
| except WSHandshakeError: | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| pass | ||
| elapsed = time.perf_counter() - start | ||
Check noticeCode scanning / CodeQL Empty except Note test
'except' clause does nothing but pass and there is no explanatory comment.
|
||
| # Should complete in well under a second on any reasonable hardware. | ||
| assert elapsed < 1.0, f"possible 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.