Skip to content

Commit

Permalink
Add multi-host ip-range testcase, run scripts/lint
Browse files Browse the repository at this point in the history
  • Loading branch information
pypae committed Aug 22, 2022
1 parent be3fc3f commit 6947d19
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 20 deletions.
20 changes: 7 additions & 13 deletions tests/middleware/test_proxy_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ async def test_proxy_headers_trusted_hosts(
trusted_hosts: Union[List[str], str], response_text: str
) -> None:
app_with_middleware = ProxyHeadersMiddleware(app, trusted_hosts=trusted_hosts)
async with httpx.AsyncClient(
app=app_with_middleware, base_url="http://testserver"
) as client:
async with httpx.AsyncClient(app=app_with_middleware, base_url="http://testserver") as client:
headers = {"X-Forwarded-Proto": "https", "X-Forwarded-For": "1.2.3.4"}
response = await client.get("/", headers=headers)

Expand All @@ -76,15 +74,15 @@ async def test_proxy_headers_trusted_hosts(
),
# should set first untrusted as remote address
(["192.168.0.2", "127.0.0.1"], "Remote: https://10.0.2.1:0"),
# Mixed literals and networks
(["127.0.0.1", "10.0.0.0/8", "192.168.0.2"], "Remote: https://1.2.3.4:0"),
],
)
async def test_proxy_headers_multiple_proxies(
trusted_hosts: Union[List[str], str], response_text: str
) -> None:
app_with_middleware = ProxyHeadersMiddleware(app, trusted_hosts=trusted_hosts)
async with httpx.AsyncClient(
app=app_with_middleware, base_url="http://testserver"
) as client:
async with httpx.AsyncClient(app=app_with_middleware, base_url="http://testserver") as client:
headers = {
"X-Forwarded-Proto": "https",
"X-Forwarded-For": "1.2.3.4, 10.0.2.1, 192.168.0.2",
Expand All @@ -98,9 +96,7 @@ async def test_proxy_headers_multiple_proxies(
@pytest.mark.anyio
async def test_proxy_headers_invalid_x_forwarded_for() -> None:
app_with_middleware = ProxyHeadersMiddleware(app, trusted_hosts="*")
async with httpx.AsyncClient(
app=app_with_middleware, base_url="http://testserver"
) as client:
async with httpx.AsyncClient(app=app_with_middleware, base_url="http://testserver") as client:
headers = httpx.Headers(
{
"X-Forwarded-Proto": "https",
Expand All @@ -119,9 +115,7 @@ async def test_proxy_headers_empty_x_forwarded_for() -> None:
# https://github.com/encode/uvicorn/issues/1068#issuecomment-855371576
app_with_middleware = ProxyHeadersMiddleware(app, trusted_hosts="*")
transport = httpx.ASGITransport(app=app_with_middleware, client=("1.2.3.4", 8080))
async with httpx.AsyncClient(
transport=transport, base_url="http://testserver"
) as client:
async with httpx.AsyncClient(transport=transport, base_url="http://testserver") as client:
headers = httpx.Headers(
{
"X-Forwarded-Proto": "https",
Expand All @@ -131,4 +125,4 @@ async def test_proxy_headers_empty_x_forwarded_for() -> None:
)
response = await client.get("/", headers=headers)
assert response.status_code == 200
assert response.text == "Remote: https://1.2.3.4:8080"
assert response.text == "Remote: https://1.2.3.4:8080"
9 changes: 2 additions & 7 deletions uvicorn/middleware/proxy_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,10 @@
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers#Proxies
"""
import ipaddress
from typing import TYPE_CHECKING, List, Optional, Tuple, Union, cast, Set
from typing import TYPE_CHECKING, List, Optional, Set, Tuple, Union, cast

if TYPE_CHECKING:
from asgiref.typing import (
ASGI3Application,
ASGIReceiveCallable,
ASGISendCallable,
Scope,
)
from asgiref.typing import ASGI3Application, ASGIReceiveCallable, ASGISendCallable, Scope


def _parse_raw_hosts(value: str) -> List[str]:
Expand Down

0 comments on commit 6947d19

Please sign in to comment.