Skip to content

fix: support standard X-Forwarded-For header in get_ipaddr - #307

Open
agustin18 wants to merge 1 commit into
laurentS:masterfrom
agustin18:fix/get-ipaddr-x-forwarded-for
Open

agustin18 wants to merge 1 commit into
laurentS:masterfrom
agustin18:fix/get-ipaddr-x-forwarded-for

Conversation

@agustin18

Copy link
Copy Markdown

In Starlette / ASGI, request.headers lowercases header keys and retains hyphens. The previous implementation checked for "X_FORWARDED_FOR", causing standard reverse proxy headers (X-Forwarded-For) to be ignored and falling back to the proxy's IP.

This PR:

  • Adds lookup for the standard "x-forwarded-for" header.
  • Preserves backward compatibility with legacy underscore usage ("x_forwarded_for").
  • Correctly parses the client IP when X-Forwarded-For contains a comma-separated list of proxies.
  • Falls back to request.client.host (or "127.0.0.1") if the header is missing or empty.
  • Adds unit tests in tests/test_util.py.

Fixes #306

In Starlette/ASGI, request headers are case-insensitive and retain hyphens.
Update get_ipaddr to inspect 'x-forwarded-for', support multiple proxy IPs,
and maintain backward compatibility with legacy underscore usage.

Fixes laurentS#306
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

get_ipaddr uses incorrect label for X-Forwarded-For

2 participants