frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

Open in hackernews

What is X-Forwarded-For and when can you trust it? (2024)

https://httptoolkit.com/blog/what-is-x-forwarded-for/
58•ayoisaiah•3d ago

Comments

westurner•3d ago
From the article: https://httptoolkit.com/blog/what-is-x-forwarded-for/ :

> Dropping all external values like this is the safest approach when you're not sure how secure and reliable the rest of your call chain is going to be. If other proxies and backend apps are likely to blindly trust the incoming information, or generally make insecure choices (which we'll get into more later) then it's probably safest to completely replace the X-Forwarded-For header at that outside-world facing reverse proxy, and ditch any untrustworthy data in the process.

X-Forwarded-For: https://en.wikipedia.org/wiki/X-Forwarded-For :

> Just logging the X-Forwarded-For field is not always enough as the last proxy IP address in a chain is not contained within the X-Forwarded-For field, it is in the actual IP header. A web server should log both the request's source IP address and the X-Forwarded-For field information for completeness

HTTP header injection: https://en.wikipedia.org/wiki/HTTP_header_injection

This OWASP page has a list of X-Forwarded-For and X-FORWARDED-foR and similar headers; "Headers for IP Spoofing" https://owasp.org/www-community/pages/attacks/ip_spoofing_vi...

A sufficient WAF should detect all such attempts.

The X-Forwarded-For Wikipedia article mentions that RFC 7239 actually standardizes the header and parsing:

  Forwarded: for=192.0.2.60;proto=http;by=203.0.113.43
  Forwarded: for="[2001:db8::1234]"
RFC 7239: "Forwarded HTTP Extension" (2014): https://www.rfc-editor.org/rfc/rfc7239
vlod•2d ago
If I remember correctly, it's sometimes used by nginx (used as a reverse proxy) to inject values into the request of the real ip address. e.g. nginx in front of several node processes.

I've had problems with this though. I want to get the ip4 address from cloudflare, instead of ip6 and it's next to impossible AFAICT. (for the free plan anyway)

remram•10h ago
A request doesn't come in with both an IPv4 and an IPv6. If the user connected over IPv6, the connection only has an IPv6 address. You can't get the IPv4 address, there is none.
miyuru•9h ago
what the usecase and what is the problem using the IPv6 address?

In fact if the user has IPv6, IP blocks/Rate limits wont affect the other users on the CGNAT legacy address.

supriyo-biswas•8h ago
> I've had problems with this though. I want to get the ip4 address from cloudflare, instead of ip6 and it's next to impossible AFAICT. (for the free plan anyway)

I have multiple websites on Cloudflare and can receive IPv4 addresses just fine, though for Cloudflare fronted websites it's usually better to use the CF-Connecting-IP as people can send any value in the X-Forwarded-For header.

Maybe some intermediate layer turns the IPv4 into a IPv6-mapped IPv4 address?

nodesocket•2d ago
I know that the Python module proxy_fix[1] requires you to configure how many X-Forwarded-For ip entries it should trust with the default being 1.

[1] https://werkzeug.palletsprojects.com/en/stable/middleware/pr...

SSchick•10h ago
See https://expressjs.com/en/guide/behind-proxies.html for a fun read.
francislavoie•9h ago
A very in-depth article on the topic: https://adam-p.ca/blog/2022/03/x-forwarded-for/

I implemented those recommendations in Caddy to enable a "trusted proxies" system which informs the proxy, logging, request matching etc to safely use the client IP taken from proxy headers.

danhite•2h ago
The article you cited was very informative.

For example it reminds that standards & intermediaries you trust might still allow multiple unconcatenated XFF headers though and the XFF unpeel by right to left heuristic fails if you only look at the e.g. the first such header.

This reminded me that there always seems to be a past or future pitfall case lurking, such as using old code/library honoring a X-HTTP-Method-Override header (e.g. historically used to ~convert POST to PUT bypassing client CORS restrictions).

Many holes I've noticed over the years seem to spring from legacy preservation of well intended endruns of restrictions, for example I can presently in Safari exploit a bug to get a crypto digest calculated for me within an insecure context ... I find this quite usefully locally (e.g. lan http: , data: , ~bookmarklets that I control) but nonetheless I reported the bug and am trying not to rely on it.

OutOfHere•37m ago
This was the only article that opened my eyes; it's a lot better than the Wikipedia article on the topic. It covers the XFF header, also related headers, clearly from both defense and offense perspectives.

Two things it failed to advise for defense are:

(1) I can simply just reject requests that provide multiple keys of this header. In Go, I will use `http.Header.Values(headerName)` to check the count. There is no good reason for having multiple keys of it. Any misconfiguration in setting them is the client's problem.

(2) I can and I must reject large requests that have too many header bytes. In Go, when initializing `http.Server`, I can give it the `MaxHeaderBytes` argument. Sending megabytes of headers stops here.

If I understood correctly, when wanting the rightmost-ish XFF value, I can use the rightmost value that is not in a list of trusted subnets, assuming there is at least one remaining value left of it.

pityJuke•8h ago
Brings back memories of changing the header to watch South Park episodes for free (the official site was very vulnerable to just changing the header to a NA IP).
0points•8h ago
X-Forwarded-For lets us bypass geoblocking ;-)
wutwutwat•3h ago
A properly configured load balancer is going to drop this header if the client sends it, and then set it itself, with the request connection's ip being first, then the proxy ip being second. Every proxy after that should append its own ip to that header, then finally when the request reaches your app server, you should filter out your known proxy ips to be left hopefully with just the ip address of the connection the request was forwarded for, which was not set via any client header, and not able to be spoofed.

I'm sure plenty of lbs/reverse proxies and app servers don't set things, establish trust, or filter the header properly though, because, people, but it is easy to lock down.

tetha•3h ago
Yeah we got dinged by our pentesters a few years ago because the LB didn't clear X-Forwarded-For headers. So you could just set some trusted IP into the X-Forwarded-For header and various ip whitelists went "Well, it came from there, so we gonna let it though".

Oops :)

It is one of these trust-based headers that need to be cleared at the edge of your network / trust zone.

OutOfHere•27m ago
I do not agree that the XFF header must be dropped and re-set. Doing so can in fact be harmful. There is a reason for preserving the chain of IPs, which is that it allows the app to use the rightmost-ish IP after skipping the known proxy IPs.
knorker•5h ago
1. Have (and maintain!) a list of addresses you trust to not lie (e.g. your own proxy layers, cloudflare's proxy IP list, akamai, GCP LB, AWS LB, etc…)

2. If the connecting party (real TCP connection remote end) is in the trusted list, then take the rightmost address in XFF and set as remote end.

3. Repeat 2 until you get an address not in the trusted list.

4. That is now the real client IP. Discard anything to the left of it in XFF. (though maybe log it, if you want)

The article seems to forget the step of checking the real TCP connection remote address (from my skimming), which means that if the web server can be accessed directly, and not just through a load balancer that always sets the header, then the article is a security hole.

danhite•2h ago
thank you for your comment :

> The article seems to forget the step of checking the real TCP connection remote address (from my skimming)

as this alerted me when reading the article to see their very important, but not highlighted, caveat emptor that covers this dangerous case :

  Note that this logic assumes that your server is not directly accessible. 
  If it is, you need to check the actual request source IP address is one of yours first - 
  effectively treating that as an extra right-most address.
nfriedly•3h ago
I have a rate limiting library, and for a long time, some of the most frequent issues related to misconfiguration around X-Forwarded-For headers: either ignoring them when it shouldn't and limiting the load balancer's IP instead of the end user, or blindly trusting any XFF header and allowing limits to be trivially bypassed.

Eventually I added some runtime checks that log a warning and linked to documentation for both of those issues and a few other common ones.

My support burden has decreased dramatically since then.

MajesticHobo2•2h ago
XFF handling is the bug that keeps on giving. I'd estimate I've seen incorrect parsing of it in at least half of the web applications I've audited professionally.

The funniest is when the app renders user IP addresses somewhere and you can get XSS through it.

Avamander•1h ago
Side note, don't use XFF if you have any option not to. The "Forwarded" header is much nicer.
OutOfHere•32m ago
The XFF header is set a lot more commonly, and this gives the app the freedom to be implicitly compatible with a lot more reverse proxy servers than the Forwarded header without needing special configuration.

Moreover, the Forwarded header has all the security pitfalls of the XFF header.

How We Rooted Copilot

https://research.eye.security/how-we-rooted-copilot/
99•uponasmile•2h ago•41 comments

Rust running on every GPU

https://rust-gpu.github.io/blog/2025/07/25/rust-on-every-gpu/
366•littlestymaar•8h ago•123 comments

Purple Earth Hypothesis

https://en.wikipedia.org/wiki/Purple_Earth_hypothesis
26•colinprince•2d ago•0 comments

Font-size-adjust Is Useful

https://matklad.github.io/2025/07/16/font-size-adjust.html
100•Bogdanp•3d ago•32 comments

Bringing a decade old bicycle navigator back to life with open source software

https://raymii.org/s/blog/Bringing_a_Decade_Old_Bicycle_Navigator_Back_to_Life_with_Open_Source_Software_and_DOOM.html
127•mtlynch•7h ago•16 comments

Inverted Indexes: A Step-by-Step Implementation Guide

https://www.chashnikov.dev/post/inverted-indexes-a-step-by-step-implementation-guide
15•klaussilveira•3d ago•4 comments

Open Sauce is a confoundingly brilliant Bay Area event

https://www.jeffgeerling.com/blog/2025/open-sauce-confoundingly-brilliant-bay-area-event
257•rbanffy•3d ago•142 comments

Breaking the WASM/JS communication performance barrier

https://github.com/ealmloff/sledgehammer_bindgen
87•weinzierl•3d ago•13 comments

CCTV footage captures the first-ever video of an earthquake fault in motion

https://www.smithsonianmag.com/smart-news/cctv-footage-captures-the-first-ever-video-of-an-earthquake-fault-in-motion-shining-a-rare-light-on-seismic-dynamics-180987034/
328•chrononaut•15h ago•56 comments

Ageing accelerates around age 50 ― some organs faster than others

https://www.nature.com/articles/d41586-025-02333-z
58•rntn•1h ago•9 comments

Earth Has Tilted 31.5 Inches. That Shouldn't Happen

https://www.popularmechanics.com/science/environment/a65515974/why-earth-has-tilted-science/
75•dataflow•1h ago•39 comments

Upsides and Downsides

https://calv.info/upsides-and-downsides
23•nohide•1d ago•1 comments

It's time for modern CSS to kill the SPA

https://www.jonoalderson.com/conjecture/its-time-for-modern-css-to-kill-the-spa/
636•tambourine_man•21h ago•414 comments

The rise and fall of the Hanseatic League

https://worksinprogress.co/issue/the-rise-and-fall-of-the-hanseatic-league/
125•loeber•3d ago•37 comments

Yes, the Book of PF, Fourth Edition Is Coming Soon

https://bsdly.blogspot.com/2025/07/yes-book-of-pf-4th-edition-is-coming.html
82•turtleyacht•3d ago•21 comments

Simon Tatham's Portable Puzzle Collection

https://www.chiark.greenend.org.uk/~sgtatham/puzzles/
138•sogen•11h ago•25 comments

The Rise of Shippable Microfactories

https://www.thesisdriven.com/p/the-rise-of-shippable-microfactories
21•mhb•5h ago•4 comments

The append-and-review note

https://karpathy.bearblog.dev/the-append-and-review-note/
53•vinhnx•3d ago•21 comments

Instapaper Rakuten Kobo Integration

https://blog.instapaper.com/post/789685899750424576/instapaper-rakuten-kobo-integration
33•robin_reala•3d ago•15 comments

Users claim Discord's age verification can be tricked with video game characters

https://www.thepinknews.com/2025/07/25/discord-video-game-characters-age-verification-checks-uk-online-safety-act/
112•mediumdeviation•13h ago•113 comments

Do not download the app, use the website

https://idiallo.com/blog/dont-download-apps
1156•foxfired•20h ago•629 comments

Keep Pydantic out of your Domain Layer

https://coderik.nl/posts/keep-pydantic-out-of-your-domain-layer/
58•erikvdven•3d ago•81 comments

It's a DE9, not a DB9 (but we know what you mean)

https://news.sparkfun.com/14298
412•jgrahamc•1d ago•263 comments

Never write your own date parsing library

https://www.zachleat.com/web/adventures-in-date-parsing/
234•ulrischa•1d ago•273 comments

Why MIT switched from Scheme to Python (2009)

https://www.wisdomandwonder.com/link/2110/why-mit-switched-from-scheme-to-python
262•borski•1d ago•194 comments

Vanilla JavaScript support for Tailwind Plus

https://tailwindcss.com/blog/vanilla-js-support-for-tailwind-plus
285•ulrischa•1d ago•160 comments

The future is not self-hosted

https://www.drewlyton.com/story/the-future-is-not-self-hosted/
411•drew_lytle•1d ago•366 comments

Efficient Computer's Electron E1 CPU – 100x more efficient than Arm?

https://morethanmoore.substack.com/p/efficient-computers-electron-e1-cpu
230•rpiguy•1d ago•90 comments

Generic Containers in C: Vec

https://uecker.codeberg.page/2025-07-20.html
49•uecker•3d ago•59 comments

Animated Cursors

https://tattoy.sh/news/animated-cursors/
225•speckx•1d ago•51 comments