From 9bd9289c6c9b43f7fff70d82df912e5d99f57d93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerg=C5=91=20Tisza?= Date: Mon, 11 Aug 2025 00:42:50 +0200 Subject: [PATCH] filerepo: Improve identification of ForeignAPIRepo requests These requests are usually sent to a wiki operated by a different organization so UA etiquette is important. * Add the site's URL (the URL of the main page, more specifically) as a contact address. * Add the site's URL as a referer as well. Considered but not done: * Use $wgEmergencyContact as the contact part of the UA. It's not guaranteed to be set correctly, while the main page URL always exists and will usually be enough to pinpoint the wiki (except maybe in some intranet scenarios). * Include information about the user making the request. Would be a privacy risk + probably useless due to caching. * Include information about the page the request is for. Would require lots of refactoring (making the patch harder to backport) or relying on the context title (which might be fragile), and in any case probably unreliable due to caching, and doesn't seem very relevant to the operator of the foreign site. Bug: T400881 Change-Id: I968fac6ee0ebbc5a2bd3244f57851eb64125c93d --- includes/filerepo/ForeignAPIRepo.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/includes/filerepo/ForeignAPIRepo.php b/includes/filerepo/ForeignAPIRepo.php index 6c5e4377537..45d6cbbd995 100644 --- a/includes/filerepo/ForeignAPIRepo.php +++ b/includes/filerepo/ForeignAPIRepo.php @@ -488,8 +488,10 @@ class ForeignAPIRepo extends FileRepo implements IForeignRepoWithMWApi { * @return string */ public static function getUserAgent() { - return MediaWikiServices::getInstance()->getHttpRequestFactory()->getUserAgent() . - " ForeignAPIRepo/" . self::VERSION; + $mediaWikiVersion = MediaWikiServices::getInstance()->getHttpRequestFactory()->getUserAgent(); + $classVersion = self::VERSION; + $contactUrl = MediaWikiServices::getInstance()->getUrlUtils()->getCanonicalServer(); + return "$mediaWikiVersion ($contactUrl) ForeignAPIRepo/$classVersion"; } /** @@ -535,9 +537,11 @@ class ForeignAPIRepo extends FileRepo implements IForeignRepoWithMWApi { public static function httpGet( $url, $timeout = 'default', $options = [], &$mtime = false ) { + $urlUtils = MediaWikiServices::getInstance()->getUrlUtils(); + $requestFactory = MediaWikiServices::getInstance()->getHttpRequestFactory(); + $options['timeout'] = $timeout; - $url = MediaWikiServices::getInstance()->getUrlUtils() - ->expand( $url, PROTO_HTTP ); + $url = $urlUtils->expand( $url, PROTO_HTTP ); wfDebug( "ForeignAPIRepo: HTTP GET: $url" ); if ( !$url ) { return false; @@ -550,8 +554,8 @@ class ForeignAPIRepo extends FileRepo implements IForeignRepoWithMWApi { $options['userAgent'] = self::getUserAgent(); - $req = MediaWikiServices::getInstance()->getHttpRequestFactory() - ->create( $url, $options, __METHOD__ ); + $req = $requestFactory->create( $url, $options, __METHOD__ ); + $req->setHeader( 'Referer', $urlUtils->getCanonicalServer() ); $status = $req->execute(); if ( $status->isOK() ) {