| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\AbuseFilter\Api; |
| 4 |
|
| 5 |
use LogicException; |
| 6 |
use MediaWiki\Api\ApiBase; |
| 7 |
use MediaWiki\Api\ApiMain; |
| 8 |
use MediaWiki\Api\ApiResult; |
| 9 |
use MediaWiki\Extension\AbuseFilter\AbuseFilterPermissionManager; |
| 10 |
use MediaWiki\Extension\AbuseFilter\AbuseLoggerFactory; |
| 11 |
use MediaWiki\Extension\AbuseFilter\FilterLookup; |
| 12 |
use MediaWiki\Extension\AbuseFilter\Parser\RuleCheckerFactory; |
| 13 |
use MediaWiki\Extension\AbuseFilter\Special\SpecialAbuseLog; |
| 14 |
use MediaWiki\Extension\AbuseFilter\VariableGenerator\VariableGeneratorFactory; |
| 15 |
use MediaWiki\Extension\AbuseFilter\Variables\LazyLoadedVariable; |
| 16 |
use MediaWiki\Extension\AbuseFilter\Variables\VariableHolder; |
| 17 |
use MediaWiki\Extension\AbuseFilter\Variables\VariablesBlobStore; |
| 18 |
use MediaWiki\Json\FormatJson; |
| 19 |
use RecentChange; |
| 20 |
use Wikimedia\ParamValidator\ParamValidator; |
| 21 |
|
| 22 |
class CheckMatch extends ApiBase { |
| 23 |
|
| 24 |
|
| 25 |
private $ruleCheckerFactory; |
| 26 |
|
| 27 |
|
| 28 |
private $afPermManager; |
| 29 |
|
| 30 |
|
| 31 |
private $afVariablesBlobStore; |
| 32 |
|
| 33 |
|
| 34 |
private $afVariableGeneratorFactory; |
| 35 |
|
| 36 |
|
| 37 |
private $filterLookup; |
| 38 |
|
| 39 |
|
| 40 |
private $abuseLoggerFactory; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public function __construct( |
| 53 |
ApiMain $main, |
| 54 |
$action, |
| 55 |
RuleCheckerFactory $ruleCheckerFactory, |
| 56 |
AbuseFilterPermissionManager $afPermManager, |
| 57 |
VariablesBlobStore $afVariablesBlobStore, |
| 58 |
VariableGeneratorFactory $afVariableGeneratorFactory, |
| 59 |
FilterLookup $filterLookup, |
| 60 |
AbuseLoggerFactory $abuseLoggerFactory |
| 61 |
) { |
| 62 |
parent::__construct( $main, $action ); |
| 63 |
$this->ruleCheckerFactory = $ruleCheckerFactory; |
| 64 |
$this->afPermManager = $afPermManager; |
| 65 |
$this->afVariablesBlobStore = $afVariablesBlobStore; |
| 66 |
$this->afVariableGeneratorFactory = $afVariableGeneratorFactory; |
| 67 |
$this->filterLookup = $filterLookup; |
| 68 |
$this->abuseLoggerFactory = $abuseLoggerFactory; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public function execute() { |
| 75 |
$performer = $this->getAuthority(); |
| 76 |
$params = $this->extractRequestParams(); |
| 77 |
$this->requireOnlyOneParameter( $params, 'vars', 'rcid', 'logid' ); |
| 78 |
|
| 79 |
|
| 80 |
if ( !$this->afPermManager->canUseTestTools( $performer ) ) { |
| 81 |
$this->dieWithError( 'apierror-abusefilter-canttest', 'permissiondenied' ); |
| 82 |
} |
| 83 |
|
| 84 |
$vars = null; |
| 85 |
if ( $params['vars'] ) { |
| 86 |
$pairs = FormatJson::decode( $params['vars'], true ); |
| 87 |
$vars = VariableHolder::newFromArray( $pairs ); |
| 88 |
} elseif ( $params['rcid'] ) { |
| 89 |
$rc = RecentChange::newFromId( $params['rcid'] ); |
| 90 |
|
| 91 |
if ( !$rc ) { |
| 92 |
$this->dieWithError( [ 'apierror-nosuchrcid', $params['rcid'] ] ); |
| 93 |
} |
| 94 |
|
| 95 |
if ( !$this->afPermManager::hasRCEntryAccess( $rc, $performer ) ) { |
| 96 |
|
| 97 |
$this->dieWithError( 'apierror-permissiondenied-generic', 'deletedrc' ); |
| 98 |
} |
| 99 |
|
| 100 |
$varGenerator = $this->afVariableGeneratorFactory->newRCGenerator( $rc, $this->getUser() ); |
| 101 |
$vars = $varGenerator->getVars(); |
| 102 |
} elseif ( $params['logid'] ) { |
| 103 |
$row = $this->getDB()->newSelectQueryBuilder() |
| 104 |
->select( '*' ) |
| 105 |
->from( 'abuse_filter_log' ) |
| 106 |
->where( [ 'afl_id' => $params['logid'] ] ) |
| 107 |
->caller( __METHOD__ ) |
| 108 |
->fetchRow(); |
| 109 |
|
| 110 |
if ( !$row ) { |
| 111 |
$this->dieWithError( [ 'apierror-abusefilter-nosuchlogid', $params['logid'] ], 'nosuchlogid' ); |
| 112 |
} |
| 113 |
|
| 114 |
$privacyLevel = $this->filterLookup->getFilter( $row->afl_filter_id, $row->afl_global ) |
| 115 |
->getPrivacyLevel(); |
| 116 |
$canSeeDetails = $this->afPermManager->canSeeLogDetailsForFilter( $performer, $privacyLevel ); |
| 117 |
if ( !$canSeeDetails ) { |
| 118 |
$this->dieWithError( 'apierror-permissiondenied-generic', 'cannotseedetails' ); |
| 119 |
} |
| 120 |
|
| 121 |
$visibility = SpecialAbuseLog::getEntryVisibilityForUser( $row, $performer, $this->afPermManager ); |
| 122 |
if ( $visibility !== SpecialAbuseLog::VISIBILITY_VISIBLE ) { |
| 123 |
|
| 124 |
|
| 125 |
$this->dieWithError( 'apierror-permissiondenied-generic', 'deletedabuselog' ); |
| 126 |
} |
| 127 |
|
| 128 |
$vars = $this->afVariablesBlobStore->loadVarDump( $row ); |
| 129 |
} |
| 130 |
if ( $vars === null ) { |
| 131 |
|
| 132 |
throw new LogicException( 'Impossible.' ); |
| 133 |
|
| 134 |
} |
| 135 |
|
| 136 |
$ruleChecker = $this->ruleCheckerFactory->newRuleChecker( $vars ); |
| 137 |
if ( !$ruleChecker->checkSyntax( $params['filter'] )->isValid() ) { |
| 138 |
$this->dieWithError( 'apierror-abusefilter-badsyntax', 'badsyntax' ); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
$usedVars = $ruleChecker->getUsedVars( $params['filter'] ); |
| 144 |
if ( $this->afPermManager->getForbiddenVariables( $this->getAuthority(), $usedVars ) ) { |
| 145 |
$this->dieWithError( 'apierror-permissiondenied-generic', 'cannotseeprotectedvariables' ); |
| 146 |
} |
| 147 |
|
| 148 |
$result = [ |
| 149 |
ApiResult::META_BC_BOOLS => [ 'result' ], |
| 150 |
'result' => $ruleChecker->checkConditions( $params['filter'] )->getResult(), |
| 151 |
]; |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
$shouldLogProtectedVarAccess = false; |
| 158 |
foreach ( $this->afPermManager->getUsedProtectedVariables( $usedVars ) as $protectedVariable ) { |
| 159 |
if ( $vars->varIsSet( $protectedVariable ) ) { |
| 160 |
$protectedVariableValue = $vars->getVarThrow( $protectedVariable ); |
| 161 |
if ( !( $protectedVariableValue instanceof LazyLoadedVariable ) && $protectedVariableValue !== null ) { |
| 162 |
$shouldLogProtectedVarAccess = true; |
| 163 |
break; |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
if ( $shouldLogProtectedVarAccess ) { |
| 169 |
|
| 170 |
|
| 171 |
if ( $vars->varIsSet( 'user_name' ) ) { |
| 172 |
$target = $vars->getComputedVariable( 'user_name' )->toNative(); |
| 173 |
} else { |
| 174 |
$target = $vars->getComputedVariable( 'accountname' )->toNative(); |
| 175 |
} |
| 176 |
$logger = $this->abuseLoggerFactory->getProtectedVarsAccessLogger(); |
| 177 |
$logger->logViewProtectedVariableValue( $this->getUser(), $target ); |
| 178 |
} |
| 179 |
|
| 180 |
$this->getResult()->addValue( |
| 181 |
null, |
| 182 |
$this->getModuleName(), |
| 183 |
$result |
| 184 |
); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
public function getAllowedParams() { |
| 192 |
return [ |
| 193 |
'filter' => [ |
| 194 |
ParamValidator::PARAM_REQUIRED => true, |
| 195 |
], |
| 196 |
'vars' => null, |
| 197 |
'rcid' => [ |
| 198 |
ParamValidator::PARAM_TYPE => 'integer' |
| 199 |
], |
| 200 |
'logid' => [ |
| 201 |
ParamValidator::PARAM_TYPE => 'integer' |
| 202 |
], |
| 203 |
]; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
protected function getExamplesMessages() { |
| 211 |
return [ |
| 212 |
'action=abusefiltercheckmatch&filter=!("autoconfirmed"%20in%20user_groups)&rcid=15' |
| 213 |
=> 'apihelp-abusefiltercheckmatch-example-1', |
| 214 |
]; |
| 215 |
} |
| 216 |
} |