| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Api; |
| 4 |
|
| 5 |
use MediaWiki\Api\ApiCSPReport; |
| 6 |
use MediaWiki\Api\ApiMain; |
| 7 |
use MediaWiki\Api\ApiResult; |
| 8 |
use MediaWiki\Context\DerivativeContext; |
| 9 |
use MediaWiki\Context\RequestContext; |
| 10 |
use MediaWiki\MainConfigNames; |
| 11 |
use MediaWiki\Request\FauxRequest; |
| 12 |
use MediaWikiIntegrationTestCase; |
| 13 |
use Psr\Log\AbstractLogger; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
class ApiCSPReportTest extends MediaWikiIntegrationTestCase { |
| 21 |
|
| 22 |
public function testInternalReportonly() { |
| 23 |
$params = [ |
| 24 |
'reportonly' => '1', |
| 25 |
'source' => 'internal', |
| 26 |
]; |
| 27 |
$cspReport = [ |
| 28 |
'document-uri' => 'https://doc.test/path', |
| 29 |
'referrer' => 'https://referrer.test/path', |
| 30 |
'violated-directive' => 'connet-src', |
| 31 |
'disposition' => 'report', |
| 32 |
'blocked-uri' => 'https://blocked.test/path?query', |
| 33 |
'line-number' => 4, |
| 34 |
'column-number' => 2, |
| 35 |
'source-file' => 'https://source.test/path?query', |
| 36 |
]; |
| 37 |
|
| 38 |
$log = $this->doExecute( $params, $cspReport ); |
| 39 |
|
| 40 |
$this->assertEquals( |
| 41 |
[ |
| 42 |
[ |
| 43 |
'[report-only] Received CSP report: ' . |
| 44 |
'<https://blocked.test> blocked from being loaded on <https://doc.test/path>:4', |
| 45 |
[ |
| 46 |
'method' => ApiCSPReport::class . '::execute', |
| 47 |
'user_id' => 'logged-out', |
| 48 |
'user-agent' => 'Test/0.0', |
| 49 |
'source' => 'internal' |
| 50 |
] |
| 51 |
], |
| 52 |
], |
| 53 |
$log, |
| 54 |
'logged messages' |
| 55 |
); |
| 56 |
} |
| 57 |
|
| 58 |
public function testFalsePositiveOriginMatch() { |
| 59 |
$params = [ |
| 60 |
'reportonly' => '1', |
| 61 |
'source' => 'internal', |
| 62 |
]; |
| 63 |
$cspReport = [ |
| 64 |
'document-uri' => 'https://doc.test/path', |
| 65 |
'referrer' => 'https://referrer.test/path', |
| 66 |
'violated-directive' => 'connet-src', |
| 67 |
'disposition' => 'report', |
| 68 |
'blocked-uri' => 'https://blocked.test/path/file?query', |
| 69 |
'line-number' => 4, |
| 70 |
'column-number' => 2, |
| 71 |
'source-file' => 'https://source.test/path/file?query', |
| 72 |
]; |
| 73 |
|
| 74 |
$this->overrideConfigValue( |
| 75 |
MainConfigNames::CSPFalsePositiveUrls, |
| 76 |
[ 'https://blocked.test/path/' => true ] |
| 77 |
); |
| 78 |
$log = $this->doExecute( $params, $cspReport ); |
| 79 |
|
| 80 |
$this->assertSame( |
| 81 |
[], |
| 82 |
$log, |
| 83 |
'logged messages' |
| 84 |
); |
| 85 |
} |
| 86 |
|
| 87 |
private function doExecute( array $params, array $cspReport ) { |
| 88 |
$log = []; |
| 89 |
$logger = $this->createMock( AbstractLogger::class ); |
| 90 |
$logger->method( 'warning' )->willReturnCallback( |
| 91 |
static function ( $msg, $ctx ) use ( &$log ) { |
| 92 |
unset( $ctx['csp-report'] ); |
| 93 |
$log[] = [ $msg, $ctx ]; |
| 94 |
} |
| 95 |
); |
| 96 |
$this->setLogger( 'csp-report-only', $logger ); |
| 97 |
|
| 98 |
$postBody = json_encode( [ 'csp-report' => $cspReport ] ); |
| 99 |
$req = $this->getMockBuilder( FauxRequest::class ) |
| 100 |
->onlyMethods( [ 'getRawInput' ] ) |
| 101 |
->setConstructorArgs( [ $params, true ] ) |
| 102 |
->getMock(); |
| 103 |
$req->method( 'getRawInput' )->willReturn( $postBody ); |
| 104 |
$req->setHeaders( [ |
| 105 |
'Content-Type' => 'application/csp-report', |
| 106 |
'User-Agent' => 'Test/0.0' |
| 107 |
] ); |
| 108 |
|
| 109 |
$services = $this->getServiceContainer(); |
| 110 |
$context = new DerivativeContext( RequestContext::getMain() ); |
| 111 |
$context->setRequest( $req ); |
| 112 |
$main = new ApiMain( $context ); |
| 113 |
$api = $this->getMockBuilder( ApiCSPReport::class ) |
| 114 |
->setConstructorArgs( [ $main, 'mock', $services->getUrlUtils() ] ) |
| 115 |
->onlyMethods( [ 'getParameter', 'getRequest', 'getResult' ] ) |
| 116 |
->getMock(); |
| 117 |
$api->method( 'getParameter' )->willReturnCallback( |
| 118 |
static function ( $key ) use ( $req ) { |
| 119 |
return $req->getRawVal( $key ); |
| 120 |
} |
| 121 |
); |
| 122 |
$api->method( 'getRequest' )->willReturn( $req ); |
| 123 |
$api->method( 'getResult' )->willReturn( new ApiResult( false ) ); |
| 124 |
|
| 125 |
$api->execute(); |
| 126 |
return $log; |
| 127 |
} |
| 128 |
} |