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
This commit is contained in:
Gergő Tisza 2025-08-11 00:42:50 +02:00
parent 82648f1d67
commit 9bd9289c6c

View file

@ -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() ) {