Skip to content

Commit

Permalink
Merge pull request #100 from lsst-sqre/tickets/DM-34821
Browse files Browse the repository at this point in the history
[DM-34821] Allow request.client to be None
  • Loading branch information
rra authored May 16, 2022
2 parents 9cfa0f9 + 4976ac7 commit 29339c0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ Change log
.. Headline template:
X.Y.Z (YYYY-MM-DD)
3.0.3 (2022-05-16)
==================

- Correctly handle the possibility that ``request.client`` is ``None``.

3.0.2 (2022-03-25)
==================

Expand Down
3 changes: 2 additions & 1 deletion src/safir/dependencies/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ async def __call__(self, request: Request) -> BoundLogger:
request_data = {
"requestMethod": request.method,
"requestUrl": str(request.url),
"remoteIp": request.client.host,
}
if request.client:
request_data["remoteIp"] = request.client.host
user_agent = request.headers.get("User-Agent")
if user_agent:
request_data["userAgent"] = user_agent
Expand Down
5 changes: 4 additions & 1 deletion src/safir/middleware/x_forwarded.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ async def dispatch(

# Update the request's understanding of the client IP. This uses an
# undocumented interface; hopefully it will keep working.
request.scope["client"] = (client, request.client.port)
if request.client:
request.scope["client"] = (client, request.client.port)
else:
request.scope["client"] = (client, None)

# Ideally this should take the scheme corresponding to the entry in
# X-Forwarded-For that was chosen, but some proxies (the Kubernetes
Expand Down
6 changes: 6 additions & 0 deletions tests/middleware/x_forwarded_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ async def test_ok() -> None:

@app.get("/")
async def handler(request: Request) -> Dict[str, str]:
assert request.client
assert request.client.host == "10.10.10.10"
assert request.state.forwarded_host == "foo.example.com"
assert request.url == "https://foo.example.com/"
Expand All @@ -49,6 +50,7 @@ async def test_defaults() -> None:

@app.get("/")
async def handler(request: Request) -> Dict[str, str]:
assert request.client
assert request.client.host == "192.168.0.1"
assert request.state.forwarded_host == "foo.example.com"
assert request.url == "http://example.com/"
Expand All @@ -73,6 +75,7 @@ async def test_no_forwards() -> None:
@app.get("/")
async def handler(request: Request) -> Dict[str, str]:
assert not request.state.forwarded_host
assert request.client
assert request.client.host == "127.0.0.1"
assert request.url == "http://example.com/"
return {}
Expand All @@ -88,6 +91,7 @@ async def test_all_filtered() -> None:

@app.get("/")
async def handler(request: Request) -> Dict[str, str]:
assert request.client
assert request.client.host == "10.10.10.10"
assert request.state.forwarded_host == "foo.example.com"
assert request.url == "https://example.com/"
Expand All @@ -111,6 +115,7 @@ async def test_one_proto() -> None:

@app.get("/")
async def handler(request: Request) -> Dict[str, str]:
assert request.client
assert request.client.host == "10.10.10.10"
assert request.state.forwarded_host == "foo.example.com"
assert request.url == "https://example.com/"
Expand All @@ -135,6 +140,7 @@ async def test_no_proto_or_host() -> None:
@app.get("/")
async def handler(request: Request) -> Dict[str, str]:
assert not request.state.forwarded_host
assert request.client
assert request.client.host == "10.10.10.10"
assert request.url == "http://example.com/"
return {}
Expand Down

0 comments on commit 29339c0

Please sign in to comment.