Inject the condition limit into AbuseFilterParser

Change-Id: I487ba25ca3f3ac4b84c3afaf88b35678944cdb4d
This commit is contained in:
Daimona Eaytoy 2021-01-01 17:07:14 +01:00
parent bac48babd8
commit aafd3bcfcd
6 changed files with 39 additions and 16 deletions

View file

@ -81,6 +81,9 @@ class AbuseFilterParser extends AFPTransitionBase {
/** @var KeywordsManager */
protected $keywordsManager;
/** @var int */
private $conditionsLimit;
/** @var UserVisibleWarning[] */
protected $warnings = [];
@ -116,6 +119,7 @@ class AbuseFilterParser extends AFPTransitionBase {
* @param BagOStuff $cache Used to cache the AST (in CachingParser) and the tokens
* @param LoggerInterface $logger Used for debugging
* @param KeywordsManager $keywordsManager
* @param int $conditionsLimit
* @param AbuseFilterVariableHolder|null $vars
*/
public function __construct(
@ -123,6 +127,7 @@ class AbuseFilterParser extends AFPTransitionBase {
BagOStuff $cache,
LoggerInterface $logger,
KeywordsManager $keywordsManager,
int $conditionsLimit,
AbuseFilterVariableHolder $vars = null
) {
$this->contLang = $contLang;
@ -130,6 +135,7 @@ class AbuseFilterParser extends AFPTransitionBase {
$this->logger = $logger;
$this->statsd = new NullStatsdDataFactory;
$this->keywordsManager = $keywordsManager;
$this->conditionsLimit = $conditionsLimit;
$this->resetState();
if ( $vars ) {
$this->mVariables = $vars;
@ -193,11 +199,9 @@ class AbuseFilterParser extends AFPTransitionBase {
* @throws AFPException
*/
protected function raiseCondCount( $val = 1 ) {
global $wgAbuseFilterConditionLimit;
$this->mCondCount += $val;
if ( $this->condLimitEnabled && $this->mCondCount > $wgAbuseFilterConditionLimit ) {
if ( $this->condLimitEnabled && $this->mCondCount > $this->conditionsLimit ) {
throw new AFPException( 'Condition limit reached.' );
}
}

View file

@ -26,25 +26,31 @@ class ParserFactory {
/** @var string */
private $parserClass;
/** @var int */
private $conditionsLimit;
/**
* @param Language $contLang
* @param BagOStuff $cache
* @param LoggerInterface $logger
* @param KeywordsManager $keywordsManager
* @param string $parserClass
* @param int $conditionsLimit
*/
public function __construct(
Language $contLang,
BagOStuff $cache,
LoggerInterface $logger,
KeywordsManager $keywordsManager,
string $parserClass
string $parserClass,
int $conditionsLimit
) {
$this->contLang = $contLang;
$this->cache = $cache;
$this->logger = $logger;
$this->keywordsManager = $keywordsManager;
$this->parserClass = $parserClass;
$this->conditionsLimit = $conditionsLimit;
}
/**
@ -53,6 +59,13 @@ class ParserFactory {
*/
public function newParser( AbuseFilterVariableHolder $vars = null ) : AbuseFilterParser {
$class = "\MediaWiki\Extension\AbuseFilter\Parser\\{$this->parserClass}";
return new $class( $this->contLang, $this->cache, $this->logger, $this->keywordsManager, $vars );
return new $class(
$this->contLang,
$this->cache,
$this->logger,
$this->keywordsManager,
$this->conditionsLimit,
$vars
);
}
}

View file

@ -109,7 +109,8 @@ return [
ObjectCache::getLocalServerInstance( 'hash' ),
LoggerFactory::getInstance( 'AbuseFilter' ),
$services->getService( KeywordsManager::SERVICE_NAME ),
$services->getMainConfig()->get( 'AbuseFilterParserClass' )
$services->getMainConfig()->get( 'AbuseFilterParserClass' ),
$services->getMainConfig()->get( 'AbuseFilterConditionLimit' )
);
},
FilterLookup::SERVICE_NAME => function ( MediaWikiServices $services ): FilterLookup {

View file

@ -66,9 +66,9 @@ class ParserEquivsetTest extends MediaWikiIntegrationTestCase {
$logger = new \Psr\Log\NullLogger();
$keywordsManager = AbuseFilterServices::getKeywordsManager();
$parser = new AbuseFilterParser( $contLang, $cache, $logger, $keywordsManager );
$parser = new AbuseFilterParser( $contLang, $cache, $logger, $keywordsManager, 1000 );
$parser->toggleConditionLimit( false );
$cachingParser = new AbuseFilterCachingParser( $contLang, $cache, $logger, $keywordsManager );
$cachingParser = new AbuseFilterCachingParser( $contLang, $cache, $logger, $keywordsManager, 1000 );
$cachingParser->toggleConditionLimit( false );
$parsers = [ $parser, $cachingParser ];
} else {

View file

@ -150,7 +150,8 @@ class ParserTest extends ParserTestCase {
$this->getLanguageMock(),
new EmptyBagOStuff(),
new NullLogger(),
new KeywordsManager( $this->createMock( AbuseFilterHookRunner::class ) )
new KeywordsManager( $this->createMock( AbuseFilterHookRunner::class ) ),
1000
];
$parser = new AbuseFilterParser( ...$constrArgs );
@ -787,7 +788,8 @@ class ParserTest extends ParserTestCase {
$this->getLanguageMock(),
new EmptyBagOStuff(),
new NullLogger(),
$this->createMock( KeywordsManager::class )
$this->createMock( KeywordsManager::class ),
1000
);
$parser->toggleConditionLimit( false );
try {
@ -1079,7 +1081,8 @@ class ParserTest extends ParserTestCase {
$this->getLanguageMock(),
new EmptyBagOStuff(),
new NullLogger(),
new KeywordsManager( $this->createMock( AbuseFilterHookRunner::class ) )
new KeywordsManager( $this->createMock( AbuseFilterHookRunner::class ) ),
1000
] )
->setMethods( [ 'logEmptyOperand' ] )
->getMock();
@ -1105,7 +1108,8 @@ class ParserTest extends ParserTestCase {
$this->getLanguageMock(),
new EmptyBagOStuff(),
new NullLogger(),
$this->createMock( KeywordsManager::class )
$this->createMock( KeywordsManager::class ),
1000
);
$parser->toggleConditionLimit( false );
}
@ -1168,7 +1172,8 @@ class ParserTest extends ParserTestCase {
$this->getLanguageMock(),
new EmptyBagOStuff(),
new NullLogger(),
new KeywordsManager( $this->createMock( AbuseFilterHookRunner::class ) )
new KeywordsManager( $this->createMock( AbuseFilterHookRunner::class ) ),
1000
] )
->setMethods( [ 'logEmptyOperand' ] )
->getMock();
@ -1286,7 +1291,7 @@ class ParserTest extends ParserTestCase {
$keywordsManager = $this->createMock( KeywordsManager::class );
$vars = new AbuseFilterVariableHolder( $keywordsManager );
$parser = new AbuseFilterParser( $lang, $cache, $logger, $keywordsManager, $vars );
$parser = new AbuseFilterParser( $lang, $cache, $logger, $keywordsManager, 1000, $vars );
$this->assertEquals( $vars, $parser->mVariables, 'Variables should be initialized' );
$pVars = TestingAccessWrapper::newFromObject( $parser->mVariables );
$this->assertSame( $logger, $pVars->logger, 'VarHolder logger should be initialized' );

View file

@ -48,9 +48,9 @@ abstract class ParserTestCase extends MediaWikiUnitTestCase {
$logger = new \Psr\Log\NullLogger();
$keywordsManager = new KeywordsManager( $this->createMock( AbuseFilterHookRunner::class ) );
$parser = new AbuseFilterParser( $contLang, $cache, $logger, $keywordsManager );
$parser = new AbuseFilterParser( $contLang, $cache, $logger, $keywordsManager, 1000 );
$parser->toggleConditionLimit( false );
$cachingParser = new AbuseFilterCachingParser( $contLang, $cache, $logger, $keywordsManager );
$cachingParser = new AbuseFilterCachingParser( $contLang, $cache, $logger, $keywordsManager, 1000 );
$cachingParser->toggleConditionLimit( false );
return [ $parser, $cachingParser ];
}