-
-
Notifications
You must be signed in to change notification settings - Fork 94
fix(http): accept IPv6 loopback Host headers #172
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
0xbrayo
wants to merge
2
commits into
ActivityWatch:master
Choose a base branch
from
0xbrayo:fix/92-ipv6-host
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.
+127
−2
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,95 @@ | ||
| import http.client | ||
| import json | ||
| import socket | ||
| from threading import Thread | ||
|
|
||
| import pytest | ||
| from flask import request | ||
| from werkzeug.serving import make_server | ||
|
|
||
| from aw_server.server import AWFlask | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "host", ["localhost", "localhost:5600", "127.0.0.1:5600", "[::1]", "[::1]:5600"] | ||
| ) | ||
| def test_loopback_host_headers(flask_client, host): | ||
| response = flask_client.get("/api/0/buckets/", headers={"Host": host}) | ||
| assert response.status_code == 200 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "host", | ||
| [ | ||
| "evil.example", | ||
| "evil.example:5600", | ||
| "[2001:db8::1]:5600", | ||
| "::1", | ||
| "[::1", | ||
| "[::1]:bad", | ||
| "[::1]:65536", | ||
| "evil.example@localhost", | ||
| "localhost/evil", | ||
| "localhost?evil", | ||
| "localhost#evil", | ||
| "[::1]:", | ||
| "[::1]?", | ||
| "[::1]#", | ||
| "localhost:", | ||
| "localhost?", | ||
| "localhost#", | ||
| "[::1]junk", | ||
| "[::1]junk:5600", | ||
| "local\thost", | ||
| ], | ||
| ) | ||
| def test_untrusted_or_malformed_host_headers(app, host): | ||
| # Inject after context creation: Werkzeug's test client parses Host for | ||
| # its cookie jar and rejects malformed authorities before dispatch. | ||
| with app.test_request_context("/api/0/buckets/"): | ||
| request.environ["HTTP_HOST"] = host | ||
| response = app.full_dispatch_request() | ||
| assert response.status_code == 400 | ||
|
|
||
|
|
||
| def test_configured_ipv6_host(): | ||
| app = AWFlask("2001:db8::1", testing=True, cors_origins=[]) | ||
| assert ( | ||
| app.test_client() | ||
| .get("/api/0/buckets/", headers={"Host": "[2001:db8::1]:5600"}) | ||
| .status_code | ||
| == 200 | ||
| ) | ||
|
|
||
|
|
||
| def test_missing_host_header(flask_client): | ||
| assert ( | ||
| flask_client.get( | ||
| "/api/0/buckets/", environ_overrides={"HTTP_HOST": ""} | ||
| ).status_code | ||
| == 400 | ||
| ) | ||
|
|
||
|
|
||
| def test_ipv6_loopback_listener(): | ||
| # Skip only when this machine cannot bind IPv6 loopback at all. | ||
| try: | ||
| with socket.socket(socket.AF_INET6, socket.SOCK_STREAM) as probe: | ||
| probe.bind(("::1", 0)) | ||
| except OSError as error: | ||
| pytest.skip(f"IPv6 loopback unavailable: {error}") | ||
| app = AWFlask("::1", testing=True, cors_origins=[]) | ||
| server = make_server("::1", 0, app, threaded=True) | ||
| thread = Thread(target=server.serve_forever) | ||
| thread.start() | ||
| connection = http.client.HTTPConnection("::1", server.server_port, timeout=5) | ||
| try: | ||
| connection.request("GET", "/api/0/buckets/") | ||
| response = connection.getresponse() | ||
| assert response.status == 200 | ||
| assert json.loads(response.read()) == {} | ||
| finally: | ||
| connection.close() | ||
| server.shutdown() | ||
| thread.join(timeout=5) | ||
| server.server_close() |
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.