Page MenuHomePhabricator

Discontinue xff.log (remove $wgExtensionFunctions from wmf-config)
Open, LowPublic

Description

Follows from T286515#7235469.

Wikimedia has an XFF log for incident response. Currently this relies on a $wgExtensionFunctions to run after startup, but presumably this is useful for other people as well and should be moved to MediaWiki core. One subtle gotcha might be figuring out how to exclude POST requests on jobrunners in a generic, non-Wikimedia specific way.

Event Timeline

Status quo

What we have today:

wmf-config/CommonSettings.php
$wgExtensionFunctions[] = static function () {
	if (
		isset( $_SERVER['REQUEST_METHOD'] )
		&& $_SERVER['REQUEST_METHOD'] === 'POST'
		// T129982
		&& $_SERVER['HTTP_HOST'] !== 'mw-jobrunner.discovery.wmnet:4448'
	) {
		$uri = ( ( $_SERVER['HTTPS'] ?? null ) ? 'https://' : 'http://' ) .
			$_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
		$logger = LoggerFactory::getInstance( 'xff' );
		$logger->info( "{date}\t{uri}\t{xff}, {remoteaddr}\t{wpSave}",
			[
				'date' => gmdate( 'r' ),
				'uri' => $uri,
				'xff' => $_SERVER['HTTP_X_FORWARDED_FOR'] ?? '',
				'remoteaddr' => $_SERVER['REMOTE_ADDR'],
				'wpSave' => ( $_REQUEST['wpSave'] ?? null ) ? 'save' : '',
			]
		);
	}
};

First problem: This doesn't work.

It tries to exclude mw-jobrunner.discovery.wmnet:4448 but that's not how changeprop-jobqueue makes requests. I don't know how or where this is configured, when it changed, or whether it ever worked, but it doesn't work today. It makes requests using the canonical hostname, and routes it to mw-jobrunner by other means:

krinkle@mwlog1003:/srv/mw-log/archive$ fgrep jobrunner xff.log-2026101723 | head
2026-10-17 23:21:57 […] mw-jobrunner.eqiad.main-… commonswiki 1.47.0-wmf.24 xff INFO: …	 {"date":"Sat, 17 Oct 2026 23:21:57 +0000","uri":"https://commons.wikimedia.org/rpc/RunSingleJob.php","xff":"","remoteaddr":"10.64.181.17","wpSave":""}

30% of the xff.log is useless noise internal mw-jobrunner requests:

krinkle@mwlog1003:/srv/mw-log/archive$ wc -l xff.log-2026101723 
9947996
krinkle@mwlog1003:/srv/mw-log/archive$ fgrep ' mw-jobrunner.' xff.log-2026101723 | wc -l
3327529

Second problem: mw-jobrunner isn't the only source of internal requests. We have mw-api-int as well, which makes up another 50%.

krinkle@mwlog1003:/srv/mw-log/archive$ fgrep ' mw-api-int.' xff.log-2026101723 | head
2026-10-17 23:21:57 […] mw-api-int.eqiad.main-… wikidatawiki 1.47.0-wmf.24 xff INFO: …	 {"date":"Sat, 17 Oct 2026 23:21:57 +0000","uri":"https://www.wikidata.org/w/api.php","xff":"","remoteaddr":"10.64.171.24","wpSave":""} 
…

krinkle@mwlog1003:/srv/mw-log/archive$ fgrep ' mw-api-int.' xff.log-2026101723 | wc -l
4771896

All together, that's 80% of xff.log filled with noise. We keep these logs on mwlog hosts for 45 days and generate about 12GB per day (after gzip), or 580GB in total. It seems like that could be 2GB/day or 100GB overall instead.

Generic solution

I was going to say, a generic solution in MediaWiki core must do at least the above for WMF. However, as we can see, the current WMF filter work.

The main challenge with making it work for other installs, is that JobRunner can be run on CLI (maintenance/runJobs.php) or a SpecialPage (Special:RunJobs) by default, and WMF uses something else entirely (see also T175146: JobQueue: Unify JobRunner entry points). Filtering out the CLI is easy, but filtering out a special page or rest endpoint is hard to do from Setup.php because that's too early. It's before we hand control the relevant request handler (e.g. API module, page action, or special page).

Entry point

The closest analog to the existing filter, would be a MW_ENTRYPOINT condition, and limit the xff log to index, api and rest. That's essentially, but based on entrypoint we want to keep instead of what we want to exclude. It will continue to exclude CLI, continue to exclude load.php (since it's GET-only). It will also fix the above bug and filter out WMF's rpc/RunSingleJob.php because it isn't one of those three entrypoints. We'd only write to the XFF log on POST requests to index.php, api.php, and rest.php.

A similar approach would be to use the ActionEntryPoint, ApiEntryPoint, and Rest\EntryPoint. I slightly prefer this over putting more conditions in Setup.php, so that it is naturally limited to those entrypoints (no conditional needed), reduces code in the critical path shared across requests, and makes it testable.

Upside:

  • Fixes mw-jobrunner exclusion for WMF, because we use a custom entrypoint (rpc/RunSingleJob.php) in the wmf-config repo.

Downside:

  • Continues to spam logs from mw-api-int at WMF.
  • Excluding mw-jobrunner would stop working after T175146: JobQueue: Unify JobRunner entry points, assuming that we switch to rest.php or a SpecialPage.
  • Does not exclude Special:RunJobs for third parties.

Internal IP

We don't have to limit ourselves to the approach we take in wmf-config today. The purpose of all this filtering isn't really about mw-jobrunner, or mw-api-int, or Special:RunJobs. It's about internal requests. We want to exclude these from the XFF log, because they don't have XFF details, and it would take up space and make the logs harder and slower to consume and search through.

So... can we just do that? We could use something like IPUtils::isPublic( WebRequest->getIP() ).

At the Mini Hackathon today, I looked into this further and asked around whether it is actually used in practice. Not just the log files, but the TrustedXFF extension in general, because:

As of 2019, many wikis force HTTPS for all things. Any manipulation with headers on a proxy compromises HTTPs server authentication unless the proxy possesses an SSL certificate authoritative for the wiki in question. It makes the extension far less useful nowadays than it was originally envisaged.

This makes logical sense. For an external XFF header to influence MediaWiki's perceived client IP today in 2026, we need:

  1. an MITM proxy that decrypts our traffic,
  2. with users on devices with a custom root certificate and/or from a browser that trusted this intermediary,
  3. the proxy must support XFF by adding the downstream IP to an X-Forwarded-For header,
  4. the proxy must be listed in our trusted-hosts.txt list.

It wouldn't surprise me if these have zero overlap, especially criteria 1 and 4.

krinkle@mwlog1003:/srv/mw-log$ wc -l xff.log 
4046921 xff.log
# Given "xff" formatted as client,proxy1,proxy2
# Exclude:
# - client is external (not internal 10.x.x.x)
# - proxy1 is external (not internal 10.x.x.x)
# - The suggested client IP from xff differs from the immediate external IP via remote_addr (set by Wikimedia CDN via X-Client-IP)
krinkle@mwlog1003:/srv/mw-log$ cat xff.log | fgrep -v '"xff":""' | grep -vE '"xff":"10\.' | grep -vE '"xff":"[^,]+,\s*10\.' | ack -P '(?=.*"xff":"([^,"]+),)(?!.*"remoteaddr":"\1")' | head

This does have results. However, just there are external request setting this header, doesn't mean MediaWiki trusts them. Unfortunately, xff.log does not include the result of WebRequest->getIP today.

Change #1358370 had a related patch set uploaded (by Krinkle; author: Krinkle):

[operations/mediawiki-config@master] xff.log: Fix jobrunner filter and log whether TrustedXFF was used

https://gerrit.wikimedia.org/r/1358370

Change #1358370 merged by jenkins-bot:

[operations/mediawiki-config@master] xff.log: Fix jobrunner filter and log whether TrustedXFF was used

https://gerrit.wikimedia.org/r/1358370

Mentioned in SAL (#wikimedia-operations) [2026-10-27T14:38:05Z] <krinkle@deploy1003> Started scap sync-world: Backport for [[gerrit:1358370|xff.log: Fix jobrunner filter and log whether TrustedXFF was used (T288223)]]

Mentioned in SAL (#wikimedia-operations) [2026-10-27T14:42:17Z] <krinkle@deploy1003> krinkle: Backport for [[gerrit:1358370|xff.log: Fix jobrunner filter and log whether TrustedXFF was used (T288223)]] synced to the testservers (see https://wikitech.wikimedia.org/wiki/Mwdebug). Changes can now be verified there.

Mentioned in SAL (#wikimedia-operations) [2026-10-27T14:51:29Z] <krinkle@deploy1003> Finished scap sync-world: Backport for [[gerrit:1358370|xff.log: Fix jobrunner filter and log whether TrustedXFF was used (T288223)]] (duration: 13m 23s)

We're now storing ~85% fewer XFF entries, now that mw-jobrunner and mw-api-int are correctly excluded.

krinkle@mwlog1003:/srv/mw-log/archive$ zcat xff.log-2026102713.gz | cut -d':' -f1-2 | sort | uniq -c
…
 207386 2026-10-27 14:30
 196801 2026-10-27 14:31
 195113 2026-10-27 14:32
 186476 2026-10-27 14:33
 192294 2026-10-27 14:34
 191644 2026-10-27 14:35
 193892 2026-10-27 14:36
 194593 2026-10-27 14:37
 192127 2026-10-27 14:38
 187541 2026-10-27 14:39
 199888 2026-10-27 14:40
 191528 2026-10-27 14:41
 190053 2026-10-27 14:42
 187543 2026-10-27 14:43
 188855 2026-10-27 14:44
 196423 2026-10-27 14:45
 189379 2026-10-27 14:46
 193737 2026-10-27 14:47
 157893 2026-10-27 14:48
  82765 2026-10-27 14:49
  27161 2026-10-27 14:50
  22400 2026-10-27 14:51
  22444 2026-10-27 14:52
  22042 2026-10-27 14:53
  22216 2026-10-27 14:54
  22789 2026-10-27 14:55

All together, that's 80% of xff.log filled with noise. We keep these logs on mwlog hosts for 45 days and generate about 12GB per day (after gzip), or 580GB in total. It seems like that could be 2GB/day or 100GB overall instead. […]

For the past two hours, there were zero requests with a trusted XFF:

krinkle@mwlog1003:/srv/mw-log$ fgrep '"TrustedXFF":true' xff.log 
krinkle@mwlog1003:/srv/mw-log$ 

krinkle@mwlog1003:/srv/mw-log/archive$ fgrep '"TrustedXFF":true' xff.log-2026102714 
krinkle@mwlog1003:/srv/mw-log/archive$
Krinkle renamed this task from Move Wikimedia's XFF log to MediaWiki core to Discontinue xff.log (remove $wgExtensionFunctions from wmf-config).Tue, Oct 27, 11:21 PM
Krinkle updated the task description. (Show Details)
Krinkle added a subscriber: tstarling.

I asked @tstarling about it. He said he originally created xff.log to investigate abuse of TrustedXFF proxies to create edits by arbitrary IPs. This use is redundant now because CheckUser stores X-Forwarded-For data.

https://gerrit.wikimedia.org/g/mediawiki/extensions/CheckUser/+/fc942eadababbcaa306e1bdd6daed16f4bda76f8/src/Services/CheckUserInsert.php#494

XFF is also likely redundant more generally for the reasons described at T288223#12469015 and the absence of any non-internal data at T288223#12478995.

I note that the mediawiki.httpd.accesslog log has recently been favored by SREs during forensic analysis, and it already includes XFF, E.g.

mediawiki.httpd.accesslog (redacted sample)
{"host": {"name": "wikikube-worker1328.eqiad.wmnet"},
 "orchestrator": {…, "resource": {"name":"mw-web.eqiad.main-7899f49578-tgnj6"}},
 "labels": {"deployment":"mw-web", "release":"main"},
 "client.ip": …,
 "source.ip": …,
 "server.ip": …,
 …,
 "timestamp": "2026-10-21T09:39:16",
 "http.request.headers.x_client_ip": …,
 "http.request.headers.x_forwarded_for": …,
 "http.request.headers.x_request_id": …,
 "http.request.method": "POST",
 "http.response.status_code": 200,
 "url.domain": "fr.wikipedia.org",
 "url.full": …,
 "url.path": …,
 "url.query": …,
 "user_agent.original": …
}

Unlike xff.log or api.php, this stream contains all MediaWiki backend requests. The caveat is that this Kafka topic is limited to the last ~10h, and the 90 day version in Logstash is sampled. See T441953 about making this last a bit longer, at least for non-internal requests (i.e. not mw-jobrunner or mw-api-int).

Change #1361194 had a related patch set uploaded (by Krinkle; author: Krinkle):

[operations/mediawiki-config@master] Discontinue xff.log

https://gerrit.wikimedia.org/r/1361194