ProxyLookup: inject hook container

Change-Id: I97cdb4ed7ea1dea52cefa8cdce62fc904825a770
This commit is contained in:
DannyS712 2021-03-30 18:43:18 +00:00
parent ce2acaad93
commit 1c6e823158
3 changed files with 28 additions and 23 deletions

View file

@ -19,6 +19,8 @@
* @file
*/
use MediaWiki\HookContainer\HookContainer;
use MediaWiki\HookContainer\HookRunner;
use Wikimedia\IPSet;
/**
@ -26,28 +28,31 @@ use Wikimedia\IPSet;
*/
class ProxyLookup {
/**
* @var string[]
*/
/** @var string[] */
private $proxyServers;
/**
* @var string[]
*/
/** @var string[] */
private $proxyServersComplex;
/**
* @var IPSet|null
*/
/** @var IPSet|null */
private $proxyIPSet;
/** @var HookRunner */
private $hookRunner;
/**
* @param string[] $proxyServers Simple list of IPs
* @param string[] $proxyServersComplex Complex list of IPs/ranges
* @param HookContainer $hookContainer
*/
public function __construct( $proxyServers, $proxyServersComplex ) {
public function __construct(
$proxyServers,
$proxyServersComplex,
HookContainer $hookContainer
) {
$this->proxyServers = $proxyServers;
$this->proxyServersComplex = $proxyServersComplex;
$this->hookRunner = new HookRunner( $hookContainer );
}
/**
@ -79,7 +84,7 @@ class ProxyLookup {
*/
public function isTrustedProxy( $ip ) {
$trusted = $this->isConfiguredProxy( $ip );
Hooks::runner()->onIsTrustedProxy( $ip, $trusted );
$this->hookRunner->onIsTrustedProxy( $ip, $trusted );
return $trusted;
}
}

View file

@ -1097,7 +1097,8 @@ return [
$mainConfig = $services->getMainConfig();
return new ProxyLookup(
$mainConfig->get( 'CdnServers' ),
$mainConfig->get( 'CdnServersNoPurge' )
$mainConfig->get( 'CdnServersNoPurge' ),
$services->getHookContainer()
);
},

View file

@ -396,17 +396,15 @@ class WebRequestTest extends MediaWikiIntegrationTestCase {
$this->setServerVars( $input );
$this->setMwGlobals( [
'wgUsePrivateIPs' => $private,
'wgHooks' => [
'IsTrustedProxy' => [
static function ( &$ip, &$trusted ) use ( $xffList ) {
$trusted = $trusted || in_array( $ip, $xffList );
return true;
}
]
]
] );
$this->setService( 'ProxyLookup', new ProxyLookup( [], $cdn ) );
$hookContainer = $this->createHookContainer( [
'IsTrustedProxy' => static function ( &$ip, &$trusted ) use ( $xffList ) {
$trusted = $trusted || in_array( $ip, $xffList );
return true;
}
] );
$this->setService( 'ProxyLookup', new ProxyLookup( [], $cdn, $hookContainer ) );
$request = new WebRequest();
$result = $request->getIP();
@ -590,9 +588,10 @@ class WebRequestTest extends MediaWikiIntegrationTestCase {
'wgCdnServers' => [],
'wgCdnServersNoPurge' => [],
'wgUsePrivateIPs' => false,
'wgHooks' => [],
] );
$this->setService( 'ProxyLookup', new ProxyLookup( [], [] ) );
$hookContainer = $this->createHookContainer();
$this->setService( 'ProxyLookup', new ProxyLookup( [], [], $hookContainer ) );
$request = new WebRequest();
# Next call should throw an exception about lacking an IP