100% Free · Open Source GPLv2 · Zero setup requiredGet the Plugin

Opens in a new tab
ITX UserOps

Back to Blog

Why your WordPress logs show Cloudflare’s IP instead of your visitor’s

REMOTE_ADDR is not broken — it is faithfully reporting your CDN. The obvious fix turns a logging annoyance into an authentication bypass. Here is the rule that actually works.

Engineering

Diagram: a chain of proxy hops with a trust boundary, the untrusted client node highlighted and traversal running right to left.

This is one of those problems where the naive fix is worse than the bug. It is easy to make the addresses look right and, in the process, hand every visitor the ability to write whatever address they like into your audit trail. So it is worth understanding properly rather than pasting a snippet.

Why it happens

PHP fills $_SERVER['REMOTE_ADDR'] from the TCP connection. Not from a header, not from anything the client can set — from the actual peer on the other end of the socket. That is what makes it trustworthy.

It is also what makes it useless behind a CDN. When Cloudflare sits in front of your site, your server is not talking to the visitor. It is talking to Cloudflare. The visitor’s connection terminates at the edge, and a second, separate connection is made to your origin.

REMOTE_ADDR is correct. It is faithfully reporting who your server is talking to. That just happens to be a datacentre in the same country as your host, on behalf of someone who could be anywhere.

The same applies to an AWS load balancer, an nginx reverse proxy, Varnish, or any other hop in front of PHP.

What quietly breaks

This is not only a cosmetic problem in a log viewer. Anything that makes a decision based on an address is now making it about the wrong one:

  • IP blocklists match the edge. Block the address you see and you block Cloudflare — which is to say, everyone.
  • Brute-force lockouts target the CDN. Per-IP throttling counts every visitor as the same handful of IPs. Either the lockout never trips, or it trips and locks out the entire internet.
  • Geo lookups report the datacentre. Country rules and “unusual location” alerts key off where the edge is.
  • The audit log identifies nobody. When you eventually need to answer “where did that login come from”, the answer is a proxy.

That last one is the expensive one, because you do not discover it until you are already investigating something.

The fix everyone reaches for, and why it is a vulnerability

Proxies pass the original address along in a header. So the obvious fix is to read it:

// Do not do this.
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'];

Headers are supplied by whoever made the request. Any visitor can send any header they like. Nothing about X-Forwarded-For makes it special, and nothing stops someone sending it directly to your origin:

curl -H 'X-Forwarded-For: 8.8.8.8' https://example.com/wp-login.php

With that snippet in place, an attacker can now:

  • Write any address they choose into your audit log, making it worthless as evidence.
  • Walk straight past an IP blocklist by claiming to be a different address.
  • Defeat per-IP brute-force lockouts entirely, by presenting a fresh address on every attempt.

You have converted a reporting annoyance into an authentication bypass. The logs look much better, which is the worst part — the fix appears to have worked.

The actual rule

A forwarding header is only meaningful if it was written by a proxy you control. So the question is never “what does the header say?” It is:

Did this request actually arrive from a proxy I trust?

And you already have a trustworthy answer to that, because REMOTE_ADDR cannot be forged. It is the one thing in the request the client does not control.

So the rule is: read forwarding headers only when REMOTE_ADDR is itself a known proxy address. If the request came from anywhere else, ignore the headers completely and use REMOTE_ADDR. A direct visitor sending X-Forwarded-For is simply not believed.

The corollary matters too: with no trusted proxies configured, headers must never be consulted at all. Trusting them by default is how this becomes a vulnerability on the majority of sites that are not behind a proxy.

Reading the chain the right way round

Passing the trust check is not the end of it, because X-Forwarded-For is not one address. It is a list, and each hop appends the address it received the request from:

X-Forwarded-For: 203.0.113.9, 172.68.1.1, 10.0.0.5

Read left to right: the claimed client, then the first proxy, then the second. The standard advice is to take the leftmost entry, since that is the original client.

That advice is wrong, and it is wrong in the dangerous direction.

Only the segments written by your own proxies can be believed. Everything to the left of those was either written by an upstream you do not control, or supplied by the client in the original request. If a visitor sends X-Forwarded-For: 8.8.8.8 to Cloudflare, Cloudflare appends their real address and passes both along. The leftmost entry is the attacker’s invention, sitting exactly where the naive parser looks for it.

So you walk the list from the right, skipping addresses that belong to proxies you trust. The first address that is not one of your proxies is the real client — it was written by the innermost proxy you control, and is therefore the last entry anyone could still vouch for.

In the example above, with Cloudflare and an internal load balancer trusted, 10.0.0.5 and 172.68.1.1 are skipped and 203.0.113.9 is the answer. If a visitor had prepended a fake entry, the chain would read 8.8.8.8, 203.0.113.9, 172.68.1.1, 10.0.0.5 and the walk would still stop at 203.0.113.9 — the forged value is to the left of it and never reached.

One edge case: if every hop in the chain is a trusted proxy, there is no untrusted entry to stop at. Falling back to the leftmost is the closest thing to an origin available.

Which header to read

Not every proxy uses X-Forwarded-For, and some send something better. Cloudflare sets CF-Connecting-IP, which contains a single address rather than a chain — no parsing, no ambiguity. Others use X-Real-IP or True-Client-IP the same way.

Single-value headers are simpler and preferable where available, but the trust rule does not change. A single-value header from an untrusted peer is exactly as forgeable as a chain; it just has less to parse.

Where the trusted list comes from

Cloudflare publishes its edge ranges at cloudflare.com/ips. It is tempting to fetch that list at runtime so it is always current.

Resist it. That puts an outbound HTTP request on a security-critical path, and creates a much worse failure mode: if the fetch fails or is intercepted, the set of addresses you trust changes silently. A hardcoded list that is occasionally out of date is far safer than a dynamic one that can be influenced from outside.

Ship the ranges as configuration, make them editable, and check them when you change providers. Cloudflare does update the list, but rarely, and an announced change you apply deliberately beats an unannounced one you inherit.

Check what your site is recording

Two tests. First, from a machine that is not behind your proxy, send a forged header and see what gets logged:

curl -H 'X-Forwarded-For: 8.8.8.8' https://example.com/wp-login.php

If 8.8.8.8 appears anywhere in your logs, something is trusting headers it should not.

Second, check that a normal failed login records an address that plausibly belongs to a visitor rather than to your CDN. If every entry shares a prefix, you are still logging the edge.

How ITX UserOps handles it

Proxy resolution ships disabled. With no trusted ranges configured, forwarding headers are never read and the log records REMOTE_ADDR — accurate, if not always useful. Honouring headers by default would be a vulnerability on every site that is not behind a proxy, which is most of them.

Turning it on is one question rather than a CIDR list. Settings → Client IP detection asks where traffic comes from, with four answers: Direct (the default, trust no headers), Cloudflare, Reverse proxy (X-Forwarded-For), or a custom header. Choosing Cloudflare loads its published ranges for you; the reverse-proxy option is where you name your own.

The ITX UserOps settings screen showing the Client IP detection section, with the where-does-traffic-come-from dropdown open and listing Direct (trust no headers, the default), Cloudflare, Reverse proxy (X-Forwarded-For) and Custom header.
The whole control is one question. It ships on Direct — trust no headers, because honouring forwarding headers by default would be a vulnerability on every site that is not behind a proxy. The line above the dropdown reports the address currently being detected, which is how you confirm a change actually took.

Whichever you pick, the rules above still apply underneath: the peer has to be a trusted proxy before any header is read, and chains are walked right to left. The screen also prints the address it currently detects for you, which is the quickest way to confirm the change took effect — if it still shows your proxy after saving, the ranges are wrong.

The parsing is deliberately side-effect free and unit-tested against forged chains, because this is the kind of code that is easy to get subtly wrong and hard to notice. The source is on GitHub, and the documentation covers configuration.

A clearer WordPress admin
starts here.

Start with the free plugin. Add more control when you need it.

Log in

Vote on ideas, submit your own and join the discussion.