ApiCSPReport.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. /**
  3. * Copyright © 2015 Brian Wolff
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. use MediaWiki\Logger\LoggerFactory;
  23. /**
  24. * Api module to receive and log CSP violation reports
  25. *
  26. * @ingroup API
  27. */
  28. class ApiCSPReport extends ApiBase {
  29. private $log;
  30. /**
  31. * These reports should be small. Ignore super big reports out of paranoia
  32. */
  33. const MAX_POST_SIZE = 8192;
  34. /**
  35. * Logs a content-security-policy violation report from web browser.
  36. */
  37. public function execute() {
  38. $reportOnly = $this->getParameter( 'reportonly' );
  39. $logname = $reportOnly ? 'csp-report-only' : 'csp';
  40. $this->log = LoggerFactory::getInstance( $logname );
  41. $userAgent = $this->getRequest()->getHeader( 'user-agent' );
  42. $this->verifyPostBodyOk();
  43. $report = $this->getReport();
  44. $flags = $this->getFlags( $report, $userAgent );
  45. $warningText = $this->generateLogLine( $flags, $report );
  46. $this->logReport( $flags, $warningText, [
  47. // XXX Is it ok to put untrusted data into log??
  48. 'csp-report' => $report,
  49. 'method' => __METHOD__,
  50. 'user_id' => $this->getUser()->getId() ?: 'logged-out',
  51. 'user-agent' => $userAgent,
  52. 'source' => $this->getParameter( 'source' ),
  53. ] );
  54. $this->getResult()->addValue( null, $this->getModuleName(), 'success' );
  55. }
  56. /**
  57. * Log CSP report, with a different severity depending on $flags
  58. * @param array $flags Flags for this report
  59. * @param string $logLine text of log entry
  60. * @param array $context logging context
  61. */
  62. private function logReport( $flags, $logLine, $context ) {
  63. if ( in_array( 'false-positive', $flags ) ) {
  64. // These reports probably don't matter much
  65. $this->log->debug( $logLine, $context );
  66. } else {
  67. // Normal report.
  68. $this->log->warning( $logLine, $context );
  69. }
  70. }
  71. /**
  72. * Get extra notes about the report.
  73. *
  74. * @param array $report The CSP report
  75. * @param string $userAgent
  76. * @return array
  77. */
  78. private function getFlags( $report, $userAgent ) {
  79. $reportOnly = $this->getParameter( 'reportonly' );
  80. $source = $this->getParameter( 'source' );
  81. $falsePositives = $this->getConfig()->get( 'CSPFalsePositiveUrls' );
  82. $flags = [];
  83. if ( $source !== 'internal' ) {
  84. $flags[] = 'source=' . $source;
  85. }
  86. if ( $reportOnly ) {
  87. $flags[] = 'report-only';
  88. }
  89. if (
  90. (
  91. ContentSecurityPolicy::falsePositiveBrowser( $userAgent ) &&
  92. $report['blocked-uri'] === "self"
  93. ) ||
  94. (
  95. isset( $report['blocked-uri'] ) &&
  96. $this->matchUrlPattern( $report['blocked-uri'], $falsePositives )
  97. ) ||
  98. (
  99. isset( $report['source-file'] ) &&
  100. $this->matchUrlPattern( $report['source-file'], $falsePositives )
  101. )
  102. ) {
  103. // False positive due to:
  104. // https://bugzilla.mozilla.org/show_bug.cgi?id=1026520
  105. $flags[] = 'false-positive';
  106. }
  107. return $flags;
  108. }
  109. /**
  110. * @param string $url
  111. * @param string[] $patterns
  112. * @return bool
  113. */
  114. private function matchUrlPattern( $url, array $patterns ) {
  115. if ( isset( $patterns[ $url ] ) ) {
  116. return true;
  117. }
  118. $bits = wfParseUrl( $url );
  119. unset( $bits['user'], $bits['pass'], $bits['query'], $bits['fragment'] );
  120. $bits['path'] = '';
  121. $serverUrl = wfAssembleUrl( $bits );
  122. if ( isset( $patterns[$serverUrl] ) ) {
  123. // The origin of the url matches a pattern,
  124. // e.g. "https://example.org" matches "https://example.org/foo/b?a#r"
  125. return true;
  126. }
  127. foreach ( $patterns as $pattern => $val ) {
  128. // We only use this pattern if it ends in a slash, this prevents
  129. // "/foos" from matching "/foo", and "https://good.combo.bad" matching
  130. // "https://good.com".
  131. if ( substr( $pattern, -1 ) === '/' && strpos( $url, $pattern ) === 0 ) {
  132. // The pattern starts with the same as the url
  133. // e.g. "https://example.org/foo/" matches "https://example.org/foo/b?a#r"
  134. return true;
  135. }
  136. }
  137. return false;
  138. }
  139. /**
  140. * Output an api error if post body is obviously not OK.
  141. */
  142. private function verifyPostBodyOk() {
  143. $req = $this->getRequest();
  144. $contentType = $req->getHeader( 'content-type' );
  145. if ( $contentType !== 'application/json'
  146. && $contentType !== 'application/csp-report'
  147. ) {
  148. $this->error( 'wrongformat', __METHOD__ );
  149. }
  150. if ( $req->getHeader( 'content-length' ) > self::MAX_POST_SIZE ) {
  151. $this->error( 'toobig', __METHOD__ );
  152. }
  153. }
  154. /**
  155. * Get the report from post body and turn into associative array.
  156. *
  157. * @return array
  158. */
  159. private function getReport() {
  160. $postBody = $this->getRequest()->getRawInput();
  161. if ( strlen( $postBody ) > self::MAX_POST_SIZE ) {
  162. // paranoia, already checked content-length earlier.
  163. $this->error( 'toobig', __METHOD__ );
  164. }
  165. $status = FormatJson::parse( $postBody, FormatJson::FORCE_ASSOC );
  166. if ( !$status->isGood() ) {
  167. $msg = $status->getErrors()[0]['message'];
  168. if ( $msg instanceof Message ) {
  169. $msg = $msg->getKey();
  170. }
  171. $this->error( $msg, __METHOD__ );
  172. }
  173. $report = $status->getValue();
  174. if ( !isset( $report['csp-report'] ) ) {
  175. $this->error( 'missingkey', __METHOD__ );
  176. }
  177. return $report['csp-report'];
  178. }
  179. /**
  180. * Get text of log line.
  181. *
  182. * @param array $flags of additional markers for this report
  183. * @param array $report the csp report
  184. * @return string Text to put in log
  185. */
  186. private function generateLogLine( $flags, $report ) {
  187. $flagText = '';
  188. if ( $flags ) {
  189. $flagText = '[' . implode( ', ', $flags ) . ']';
  190. }
  191. $blockedOrigin = isset( $report['blocked-uri'] )
  192. ? $this->originFromUrl( $report['blocked-uri'] )
  193. : 'n/a';
  194. $page = $report['document-uri'] ?? 'n/a';
  195. $line = isset( $report['line-number'] )
  196. ? ':' . $report['line-number']
  197. : '';
  198. $warningText = $flagText .
  199. ' Received CSP report: <' . $blockedOrigin . '>' .
  200. ' blocked from being loaded on <' . $page . '>' . $line;
  201. return $warningText;
  202. }
  203. /**
  204. * @param string $url
  205. * @return string
  206. */
  207. private function originFromUrl( $url ) {
  208. $bits = wfParseUrl( $url );
  209. unset( $bits['user'], $bits['pass'], $bits['query'], $bits['fragment'] );
  210. $bits['path'] = '';
  211. $serverUrl = wfAssembleUrl( $bits );
  212. // e.g. "https://example.org" from "https://example.org/foo/b?a#r"
  213. return $serverUrl;
  214. }
  215. /**
  216. * Stop processing the request, and output/log an error
  217. *
  218. * @param string $code error code
  219. * @param string $method method that made error
  220. * @throws ApiUsageException Always
  221. */
  222. private function error( $code, $method ) {
  223. $this->log->info( 'Error reading CSP report: ' . $code, [
  224. 'method' => $method,
  225. 'user-agent' => $this->getRequest()->getHeader( 'user-agent' )
  226. ] );
  227. // Return 400 on error for user agents to display, e.g. to the console.
  228. $this->dieWithError(
  229. [ 'apierror-csp-report', wfEscapeWikiText( $code ) ], 'cspreport-' . $code, [], 400
  230. );
  231. }
  232. public function getAllowedParams() {
  233. return [
  234. 'reportonly' => [
  235. ApiBase::PARAM_TYPE => 'boolean',
  236. ApiBase::PARAM_DFLT => false
  237. ],
  238. 'source' => [
  239. ApiBase::PARAM_TYPE => 'string',
  240. ApiBase::PARAM_DFLT => 'internal',
  241. ApiBase::PARAM_REQUIRED => false
  242. ]
  243. ];
  244. }
  245. public function mustBePosted() {
  246. return true;
  247. }
  248. public function isWriteMode() {
  249. return false;
  250. }
  251. /**
  252. * Mark as internal. This isn't meant to be used by normal api users
  253. * @return bool
  254. */
  255. public function isInternal() {
  256. return true;
  257. }
  258. /**
  259. * Even if you don't have read rights, we still want your report.
  260. * @return bool
  261. */
  262. public function isReadMode() {
  263. return false;
  264. }
  265. /**
  266. * Doesn't touch db, so max lag should be rather irrelavent.
  267. *
  268. * Also, this makes sure that reports aren't lost during lag events.
  269. * @return bool
  270. */
  271. public function shouldCheckMaxLag() {
  272. return false;
  273. }
  274. }