Debian Codegraph @

extensions/AbuseFilter/includes/Api/CheckMatch.php

raw symbols SCIP
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 /** @var RuleCheckerFactory */
25 private $ruleCheckerFactory;
26
27 /** @var AbuseFilterPermissionManager */
28 private $afPermManager;
29
30 /** @var VariablesBlobStore */
31 private $afVariablesBlobStore;
32
33 /** @var VariableGeneratorFactory */
34 private $afVariableGeneratorFactory;
35
36 /** @var FilterLookup */
37 private $filterLookup;
38
39 /** @var AbuseLoggerFactory */
40 private $abuseLoggerFactory;
41
42 /**
43 * @param ApiMain $main
44 * @param string $action
45 * @param RuleCheckerFactory $ruleCheckerFactory
46 * @param AbuseFilterPermissionManager $afPermManager
47 * @param VariablesBlobStore $afVariablesBlobStore
48 * @param VariableGeneratorFactory $afVariableGeneratorFactory
49 * @param FilterLookup $filterLookup
50 * @param AbuseLoggerFactory $abuseLoggerFactory
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 * @inheritDoc
73 */
74 public function execute() {
75 $performer = $this->getAuthority();
76 $params = $this->extractRequestParams();
77 $this->requireOnlyOneParameter( $params, 'vars', 'rcid', 'logid' );
78
79 // "Anti-DoS"
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 // T223654 - Same check as in AbuseFilterChangesList
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 // T223654 - Same check as in SpecialAbuseLog. Both the visibility of the AbuseLog entry
124 // and the corresponding revision are checked.
125 $this->dieWithError( 'apierror-permissiondenied-generic', 'deletedabuselog' );
126 }
127
128 $vars = $this->afVariablesBlobStore->loadVarDump( $row );
129 }
130 if ( $vars === null ) {
131 // @codeCoverageIgnoreStart
132 throw new LogicException( 'Impossible.' );
133 // @codeCoverageIgnoreEnd
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 // Check if the provided pattern uses protected variables. If it does, then refuse to check the pattern
142 // if the user cannot see protected filters.
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 // If the test filter pattern contains protected variables and this entry had a value set for the
154 // protected variables that were in the pattern, then log that protected variables were accessed.
155 // This is to avoid a user being able to know the value of the variable if they repeatedly try values to
156 // find the actual value through trial-and-error.
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 // Either 'user_name' or 'accountname' should be set which are not lazily loaded, so get one of
170 // them to use as the target
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 * @codeCoverageIgnore Merely declarative
189 * @inheritDoc
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 * @codeCoverageIgnore Merely declarative
208 * @inheritDoc
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 }