getsentry / getsentry/sentry

Using the value last X-Forwarded-For is wrong

Open
#96,507 17 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
44.8k
Forks
4.9k
Avg merge
21h 23m
Merged PRs (30d)
607

Description

(Also see attached PDF for email from Geoff)

Steps to Reproduce

See this PR which can no longer be commented on

IMHO this is exactly wrong. The PR even states that proxies append to the X-Forwarded-For header — which is correct -- but then draws the wrong conclusion by using the last IP as the client.

If proxies append, the last IP is the last proxy, not the original client. So taking the last entry gives you the identity of some part of your own infrastructure, not the user.

It’s true that if there’s exactly one proxy, then the first and last IP are the same -- so the code might still "work." But in that case, this PR doesn't actually change anything either. The behavior only differs once you add more proxies, and that’s exactly when this logic becomes incorrect.

The correct interpretation requires knowing how many proxies are in front, and then extracting the right entry based on that count. The Bugsink docs (disclosure: competing tool, which I wrote) explain this well. To do it right, you need to explicitly configure the number of proxies and extract the client IP accordingly. Anything else will be either spoofable or wrong.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing PR 68884, which the issue identifies as the source of the behavior and discussion. Trace how Sentry interprets X-Forwarded-For and how proxy configuration is represented. Done means the client IP is selected using an explicitly configured proxy count rather than blindly taking the last header value, with behavior covered by the relevant existing tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
networking, security
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.