| 1 |
<?php |
| 2 |
|
| 3 |
f |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\Api; |
| 24 |
|
| 25 |
use MediaWiki\Json\FormatJson; |
| 26 |
use MediaWiki\Logger\LoggerFactory; |
| 27 |
use MediaWiki\MainConfigNames; |
| 28 |
use MediaWiki\Request\ContentSecurityPolicy; |
| 29 |
use MediaWiki\Utils\UrlUtils; |
| 30 |
use Psr\Log\LoggerInterface; |
| 31 |
use Wikimedia\ParamValidator\ParamValidator; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
class ApiCSPReport extends ApiBase { |
| 39 |
|
| 40 |
private LoggerInterface $log; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
private const MAX_POST_SIZE = 8192; |
| 46 |
|
| 47 |
private UrlUtils $urlUtils; |
| 48 |
|
| 49 |
public function __construct( |
| 50 |
ApiMain $main, |
| 51 |
string $action, |
| 52 |
UrlUtils $urlUtils |
| 53 |
) { |
| 54 |
parent::__construct( $main, $action ); |
| 55 |
$this->urlUtils = $urlUtils; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public function execute() { |
| 62 |
$reportOnly = $this->getParameter( 'reportonly' ); |
| 63 |
$logname = $reportOnly ? 'csp-report-only' : 'csp'; |
| 64 |
$this->log = LoggerFactory::getInstance( $logname ); |
| 65 |
$userAgent = $this->getRequest()->getHeader( 'user-agent' ); |
| 66 |
|
| 67 |
$this->verifyPostBodyOk(); |
| 68 |
$report = $this->getReport(); |
| 69 |
$flags = $this->getFlags( $report, $userAgent ); |
| 70 |
|
| 71 |
$warningText = $this->generateLogLine( $flags, $report ); |
| 72 |
$this->logReport( $flags, $warningText, [ |
| 73 |
|
| 74 |
'csp-report' => $report, |
| 75 |
'method' => __METHOD__, |
| 76 |
'user_id' => $this->getUser()->getId() ?: 'logged-out', |
| 77 |
'user-agent' => $userAgent, |
| 78 |
'source' => $this->getParameter( 'source' ), |
| 79 |
] ); |
| 80 |
$this->getResult()->addValue( null, $this->getModuleName(), 'success' ); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
private function logReport( $flags, $logLine, $context ) { |
| 90 |
if ( in_array( 'false-positive', $flags ) ) { |
| 91 |
|
| 92 |
$this->log->debug( $logLine, $context ); |
| 93 |
} else { |
| 94 |
|
| 95 |
$this->log->warning( $logLine, $context ); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
private function getFlags( $report, $userAgent ) { |
| 107 |
$reportOnly = $this->getParameter( 'reportonly' ); |
| 108 |
$source = $this->getParameter( 'source' ); |
| 109 |
$falsePositives = $this->getConfig()->get( MainConfigNames::CSPFalsePositiveUrls ); |
| 110 |
|
| 111 |
$flags = []; |
| 112 |
if ( $source !== 'internal' ) { |
| 113 |
$flags[] = 'source=' . $source; |
| 114 |
} |
| 115 |
if ( $reportOnly ) { |
| 116 |
$flags[] = 'report-only'; |
| 117 |
} |
| 118 |
|
| 119 |
if ( |
| 120 |
( |
| 121 |
ContentSecurityPolicy::falsePositiveBrowser( $userAgent ) && |
| 122 |
$report['blocked-uri'] === "self" |
| 123 |
) || |
| 124 |
( |
| 125 |
isset( $report['blocked-uri'] ) && |
| 126 |
$this->matchUrlPattern( $report['blocked-uri'], $falsePositives ) |
| 127 |
) || |
| 128 |
( |
| 129 |
isset( $report['source-file'] ) && |
| 130 |
$this->matchUrlPattern( $report['source-file'], $falsePositives ) |
| 131 |
) |
| 132 |
) { |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
$flags[] = 'false-positive'; |
| 137 |
} |
| 138 |
return $flags; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
private function matchUrlPattern( $url, array $patterns ) { |
| 147 |
if ( isset( $patterns[ $url ] ) ) { |
| 148 |
return true; |
| 149 |
} |
| 150 |
|
| 151 |
$bits = $this->urlUtils->parse( $url ); |
| 152 |
if ( !$bits ) { |
| 153 |
return false; |
| 154 |
} |
| 155 |
|
| 156 |
unset( $bits['user'], $bits['pass'], $bits['query'], $bits['fragment'] ); |
| 157 |
$bits['path'] = ''; |
| 158 |
$serverUrl = UrlUtils::assemble( $bits ); |
| 159 |
if ( isset( $patterns[$serverUrl] ) ) { |
| 160 |
|
| 161 |
|
| 162 |
return true; |
| 163 |
} |
| 164 |
foreach ( $patterns as $pattern => $val ) { |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
if ( str_ends_with( $pattern, '/' ) && str_starts_with( $url, $pattern ) ) { |
| 169 |
|
| 170 |
|
| 171 |
return true; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
return false; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
private function verifyPostBodyOk() { |
| 182 |
$req = $this->getRequest(); |
| 183 |
$contentType = $req->getHeader( 'content-type' ); |
| 184 |
if ( $contentType !== 'application/json' |
| 185 |
&& $contentType !== 'application/csp-report' |
| 186 |
) { |
| 187 |
$this->error( 'wrongformat', __METHOD__ ); |
| 188 |
} |
| 189 |
if ( $req->getHeader( 'content-length' ) > self::MAX_POST_SIZE ) { |
| 190 |
$this->error( 'toobig', __METHOD__ ); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
private function getReport() { |
| 200 |
$postBody = $this->getRequest()->getRawInput(); |
| 201 |
if ( strlen( $postBody ) > self::MAX_POST_SIZE ) { |
| 202 |
|
| 203 |
$this->error( 'toobig', __METHOD__ ); |
| 204 |
} |
| 205 |
$status = FormatJson::parse( $postBody, FormatJson::FORCE_ASSOC ); |
| 206 |
if ( !$status->isGood() ) { |
| 207 |
$msg = $status->getMessages()[0]->getKey(); |
| 208 |
$this->error( $msg, __METHOD__ ); |
| 209 |
} |
| 210 |
|
| 211 |
$report = $status->getValue(); |
| 212 |
|
| 213 |
if ( !isset( $report['csp-report'] ) ) { |
| 214 |
$this->error( 'missingkey', __METHOD__ ); |
| 215 |
} |
| 216 |
return $report['csp-report']; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
private function generateLogLine( $flags, $report ) { |
| 227 |
$flagText = ''; |
| 228 |
if ( $flags ) { |
| 229 |
$flagText = '[' . implode( ', ', $flags ) . ']'; |
| 230 |
} |
| 231 |
|
| 232 |
$blockedOrigin = isset( $report['blocked-uri'] ) |
| 233 |
? $this->originFromUrl( $report['blocked-uri'] ) |
| 234 |
: 'n/a'; |
| 235 |
$page = $report['document-uri'] ?? 'n/a'; |
| 236 |
$line = isset( $report['line-number'] ) |
| 237 |
? ':' . $report['line-number'] |
| 238 |
: ''; |
| 239 |
return $flagText . |
| 240 |
' Received CSP report: <' . $blockedOrigin . '>' . |
| 241 |
' blocked from being loaded on <' . $page . '>' . $line; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
private function originFromUrl( $url ) { |
| 249 |
$bits = $this->urlUtils->parse( $url ) ?? []; |
| 250 |
unset( $bits['user'], $bits['pass'], $bits['query'], $bits['fragment'] ); |
| 251 |
$bits['path'] = ''; |
| 252 |
|
| 253 |
return UrlUtils::assemble( $bits ); |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
private function error( $code, $method ) { |
| 264 |
$this->log->info( 'Error reading CSP report: ' . $code, [ |
| 265 |
'method' => $method, |
| 266 |
'user-agent' => $this->getRequest()->getHeader( 'user-agent' ) |
| 267 |
] ); |
| 268 |
|
| 269 |
$this->dieWithError( |
| 270 |
[ 'apierror-csp-report', wfEscapeWikiText( $code ) ], 'cspreport-' . $code, [], 400 |
| 271 |
); |
| 272 |
} |
| 273 |
|
| 274 |
public function getAllowedParams() { |
| 275 |
return [ |
| 276 |
'reportonly' => [ |
| 277 |
ParamValidator::PARAM_TYPE => 'boolean', |
| 278 |
ParamValidator::PARAM_DEFAULT => false |
| 279 |
], |
| 280 |
'source' => [ |
| 281 |
ParamValidator::PARAM_TYPE => 'string', |
| 282 |
ParamValidator::PARAM_DEFAULT => 'internal', |
| 283 |
ParamValidator::PARAM_REQUIRED => false |
| 284 |
] |
| 285 |
]; |
| 286 |
} |
| 287 |
|
| 288 |
public function mustBePosted() { |
| 289 |
return true; |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
public function isInternal() { |
| 297 |
return true; |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
public function isReadMode() { |
| 305 |
return false; |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
public function shouldCheckMaxLag() { |
| 315 |
return false; |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
class_alias( ApiCSPReport::class, 'ApiCSPReport' ); |