| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
> |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
use MediaWiki\Content\TextContent; |
| 30 |
use MediaWiki\Context\IContextSource; |
| 31 |
use MediaWiki\Logger\LoggerFactory; |
| 32 |
use MediaWiki\MainConfigNames; |
| 33 |
use MediaWiki\Parser\Parser; |
| 34 |
use MediaWiki\Parser\ParserOptions; |
| 35 |
use MediaWiki\Permissions\PermissionManager; |
| 36 |
use MediaWiki\Permissions\RestrictionStore; |
| 37 |
use MediaWiki\Revision\RevisionLookup; |
| 38 |
use MediaWiki\Revision\SlotRecord; |
| 39 |
use MediaWiki\Session\SessionManager; |
| 40 |
use MediaWiki\User\UserFactory; |
| 41 |
use MediaWiki\User\UserRigorOptions; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
class RawAction extends FormlessAction { |
| 50 |
|
| 51 |
private Parser $parser; |
| 52 |
private PermissionManager $permissionManager; |
| 53 |
private RevisionLookup $revisionLookup; |
| 54 |
private RestrictionStore $restrictionStore; |
| 55 |
private UserFactory $userFactory; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public function __construct( |
| 67 |
Article $article, |
| 68 |
IContextSource $context, |
| 69 |
Parser $parser, |
| 70 |
PermissionManager $permissionManager, |
| 71 |
RevisionLookup $revisionLookup, |
| 72 |
RestrictionStore $restrictionStore, |
| 73 |
UserFactory $userFactory |
| 74 |
) { |
| 75 |
parent::__construct( $article, $context ); |
| 76 |
$this->parser = $parser; |
| 77 |
$this->permissionManager = $permissionManager; |
| 78 |
$this->revisionLookup = $revisionLookup; |
| 79 |
$this->restrictionStore = $restrictionStore; |
| 80 |
$this->userFactory = $userFactory; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
public function getName() { |
| 85 |
return 'raw'; |
| 86 |
} |
| 87 |
|
| 88 |
public function requiresWrite() { |
| 89 |
return false; |
| 90 |
} |
| 91 |
|
| 92 |
public function requiresUnblock() { |
| 93 |
return false; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public function onView() { |
| 101 |
$this->getOutput()->disable(); |
| 102 |
$request = $this->getRequest(); |
| 103 |
$response = $request->response(); |
| 104 |
$config = $this->context->getConfig(); |
| 105 |
|
| 106 |
if ( $this->getOutput()->checkLastModified( |
| 107 |
$this->getWikiPage()->getTouched() |
| 108 |
) ) { |
| 109 |
|
| 110 |
return null; |
| 111 |
} |
| 112 |
|
| 113 |
$contentType = $this->getContentType(); |
| 114 |
|
| 115 |
$maxage = $request->getInt( 'maxage', $config->get( MainConfigNames::CdnMaxAge ) ); |
| 116 |
$smaxage = $request->getIntOrNull( 'smaxage' ); |
| 117 |
if ( $smaxage === null ) { |
| 118 |
if ( |
| 119 |
$contentType === 'text/css' || |
| 120 |
$contentType === 'application/json' || |
| 121 |
$contentType === 'text/javascript' |
| 122 |
) { |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
$smaxage = intval( $config->get( MainConfigNames::ForcedRawSMaxage ) ); |
| 127 |
} else { |
| 128 |
|
| 129 |
$smaxage = 0; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
$response->header( $this->getOutput()->getVaryHeader() ); |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
$privateCache = !$this->permissionManager->isEveryoneAllowed( 'read' ) && |
| 139 |
( $smaxage === 0 || SessionManager::getGlobalSession()->isPersistent() ); |
| 140 |
|
| 141 |
$privateCache = $privateCache || $this->getUser()->isRegistered(); |
| 142 |
$mode = $privateCache ? 'private' : 'public'; |
| 143 |
$response->header( |
| 144 |
'Cache-Control: ' . $mode . ', s-maxage=' . $smaxage . ', max-age=' . $maxage |
| 145 |
); |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
$title = $this->getTitle(); |
| 152 |
if ( $title->isUserConfigPage() && $contentType !== 'text/x-wiki' ) { |
| 153 |
|
| 154 |
|
| 155 |
$rootPage = strtok( $title->getText(), '/' ); |
| 156 |
$userFromTitle = $this->userFactory->newFromName( $rootPage, UserRigorOptions::RIGOR_USABLE ); |
| 157 |
if ( !$userFromTitle || !$userFromTitle->isRegistered() ) { |
| 158 |
$elevated = $this->getAuthority()->isAllowed( 'editinterface' ); |
| 159 |
$elevatedText = $elevated ? 'by elevated ' : ''; |
| 160 |
$log = LoggerFactory::getInstance( "security" ); |
| 161 |
$log->warning( |
| 162 |
"Unsafe JS/CSS/Json {$elevatedText}load - {user} loaded {title} with {ctype}", |
| 163 |
[ |
| 164 |
'user' => $this->getUser()->getName(), |
| 165 |
'title' => $title->getPrefixedDBkey(), |
| 166 |
'ctype' => $contentType, |
| 167 |
'elevated' => $elevated |
| 168 |
] |
| 169 |
); |
| 170 |
throw new HttpError( 403, wfMessage( 'unregistered-user-config' ) ); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
$pageRestrictions = $this->restrictionStore->getRestrictions( $title, 'edit' ); |
| 181 |
if ( |
| 182 |
$contentType === 'text/javascript' && |
| 183 |
!$title->isUserJsConfigPage() && |
| 184 |
!$title->inNamespace( NS_MEDIAWIKI ) && |
| 185 |
!in_array( 'sysop', $pageRestrictions ) && |
| 186 |
!in_array( 'editprotected', $pageRestrictions ) |
| 187 |
) { |
| 188 |
|
| 189 |
$log = LoggerFactory::getInstance( "security" ); |
| 190 |
$log->info( "Blocked loading unprotected JS {title} for {user}", |
| 191 |
[ |
| 192 |
'user' => $this->getUser()->getName(), |
| 193 |
'title' => $title->getPrefixedDBkey(), |
| 194 |
] |
| 195 |
); |
| 196 |
throw new HttpError( 403, wfMessage( 'unprotected-js' ) ); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
if ( $contentType === 'text/javascript' ) { |
| 205 |
if ( !( $title->isSiteJsConfigPage() || $title->isUserJsConfigPage() ) ) { |
| 206 |
$log = LoggerFactory::getInstance( "security" ); |
| 207 |
$log->info( "Did not block loading unprotected JS {title} for {user} with more restrictions", |
| 208 |
[ |
| 209 |
'user' => $this->getUser()->getName(), |
| 210 |
'title' => $title->getPrefixedDBkey(), |
| 211 |
] |
| 212 |
); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
$response->header( 'Content-type: ' . $contentType . '; charset=UTF-8' ); |
| 217 |
|
| 218 |
$text = $this->getRawText(); |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
if ( $text === false && $contentType === 'text/x-wiki' ) { |
| 225 |
$response->statusHeader( 404 ); |
| 226 |
} |
| 227 |
|
| 228 |
if ( !$this->getHookRunner()->onRawPageViewBeforeOutput( $this, $text ) ) { |
| 229 |
wfDebug( __METHOD__ . ": RawPageViewBeforeOutput hook broke raw page output." ); |
| 230 |
} |
| 231 |
|
| 232 |
echo $text; |
| 233 |
|
| 234 |
return null; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
public function getRawText() { |
| 244 |
$text = false; |
| 245 |
$title = $this->getTitle(); |
| 246 |
$request = $this->getRequest(); |
| 247 |
|
| 248 |
|
| 249 |
$rev = $this->revisionLookup->getRevisionByTitle( $title, $this->getOldId() ); |
| 250 |
if ( $rev ) { |
| 251 |
$lastMod = wfTimestamp( TS_RFC2822, $rev->getTimestamp() ); |
| 252 |
$request->response()->header( "Last-modified: $lastMod" ); |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
$slot = $this->getRequest()->getText( 'slot' ); |
| 257 |
if ( $slot ) { |
| 258 |
if ( $rev->hasSlot( $slot ) ) { |
| 259 |
$content = $rev->getContent( $slot ); |
| 260 |
} else { |
| 261 |
$content = null; |
| 262 |
} |
| 263 |
} else { |
| 264 |
$content = $rev->getContent( SlotRecord::MAIN ); |
| 265 |
} |
| 266 |
|
| 267 |
if ( $content === null ) { |
| 268 |
|
| 269 |
} elseif ( !$content instanceof TextContent && !method_exists( $content, 'getText' ) ) { |
| 270 |
|
| 271 |
wfHttpError( |
| 272 |
415, |
| 273 |
"Unsupported Media Type", "The requested page uses the content model `" |
| 274 |
. $content->getModel() . "` which is not supported via this interface." |
| 275 |
); |
| 276 |
die(); |
| 277 |
} else { |
| 278 |
|
| 279 |
$section = $request->getIntOrNull( 'section' ); |
| 280 |
if ( $section !== null ) { |
| 281 |
$content = $content->getSection( $section ); |
| 282 |
} |
| 283 |
|
| 284 |
if ( $content !== null && $content !== false ) { |
| 285 |
|
| 286 |
$text = $content->getText(); |
| 287 |
} |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
if ( $text !== false && $text !== '' && $request->getRawVal( 'templates' ) === 'expand' ) { |
| 292 |
$text = $this->parser->preprocess( |
| 293 |
$text, |
| 294 |
$title, |
| 295 |
ParserOptions::newFromContext( $this->getContext() ) |
| 296 |
); |
| 297 |
} |
| 298 |
|
| 299 |
return $text; |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
public function getOldId() { |
| 308 |
$oldId = $this->getRequest()->getInt( 'oldid' ); |
| 309 |
$rl = $this->revisionLookup; |
| 310 |
switch ( $this->getRequest()->getText( 'direction' ) ) { |
| 311 |
case 'next': |
| 312 |
|
| 313 |
$nextRev = null; |
| 314 |
if ( $oldId ) { |
| 315 |
$oldRev = $rl->getRevisionById( $oldId ); |
| 316 |
if ( $oldRev ) { |
| 317 |
$nextRev = $rl->getNextRevision( $oldRev ); |
| 318 |
} |
| 319 |
} |
| 320 |
$oldId = $nextRev ? $nextRev->getId() : -1; |
| 321 |
break; |
| 322 |
case 'prev': |
| 323 |
|
| 324 |
$prevRev = null; |
| 325 |
if ( !$oldId ) { |
| 326 |
|
| 327 |
$oldId = $this->getWikiPage()->getLatest(); |
| 328 |
} |
| 329 |
$oldRev = $rl->getRevisionById( $oldId ); |
| 330 |
if ( $oldRev ) { |
| 331 |
$prevRev = $rl->getPreviousRevision( $oldRev ); |
| 332 |
} |
| 333 |
$oldId = $prevRev ? $prevRev->getId() : -1; |
| 334 |
break; |
| 335 |
case 'cur': |
| 336 |
$oldId = 0; |
| 337 |
break; |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
return $oldId; |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
public function getContentType() { |
| 350 |
|
| 351 |
$ctype = $this->getRequest()->getRawVal( 'ctype' ); |
| 352 |
|
| 353 |
if ( $ctype == '' ) { |
| 354 |
|
| 355 |
$gen = $this->getRequest()->getRawVal( 'gen' ); |
| 356 |
if ( $gen == 'js' ) { |
| 357 |
$ctype = 'text/javascript'; |
| 358 |
} elseif ( $gen == 'css' ) { |
| 359 |
$ctype = 'text/css'; |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
static $allowedCTypes = [ |
| 364 |
'text/x-wiki', |
| 365 |
'text/javascript', |
| 366 |
'text/css', |
| 367 |
|
| 368 |
'application/x-zope-edit', |
| 369 |
'application/json' |
| 370 |
]; |
| 371 |
if ( $ctype == '' || !in_array( $ctype, $allowedCTypes ) ) { |
| 372 |
$ctype = 'text/x-wiki'; |
| 373 |
} |
| 374 |
|
| 375 |
return $ctype; |
| 376 |
} |
| 377 |
} |