MediaWiki  master
RawAction.php
Go to the documentation of this file.
1 <?php
35 
42 class RawAction extends FormlessAction {
43 
45  private $parser;
46 
48  private $permissionManager;
49 
51  private $revisionLookup;
52 
54  private $restrictionStore;
55 
64  public function __construct(
65  Article $article,
67  Parser $parser,
68  PermissionManager $permissionManager,
69  RevisionLookup $revisionLookup,
70  RestrictionStore $restrictionStore
71  ) {
72  parent::__construct( $article, $context );
73  $this->parser = $parser;
74  $this->permissionManager = $permissionManager;
75  $this->revisionLookup = $revisionLookup;
76  $this->restrictionStore = $restrictionStore;
77  }
78 
79  public function getName() {
80  return 'raw';
81  }
82 
83  public function requiresWrite() {
84  return false;
85  }
86 
87  public function requiresUnblock() {
88  return false;
89  }
90 
95  public function onView() {
96  $this->getOutput()->disable();
97  $request = $this->getRequest();
98  $response = $request->response();
99  $config = $this->context->getConfig();
100 
101  if ( $this->getOutput()->checkLastModified(
102  $this->getWikiPage()->getTouched()
103  ) ) {
104  return null; // Client cache fresh and headers sent, nothing more to do.
105  }
106 
107  $contentType = $this->getContentType();
108 
109  $maxage = $request->getInt( 'maxage', $config->get( MainConfigNames::CdnMaxAge ) );
110  $smaxage = $request->getIntOrNull( 'smaxage' );
111  if ( $smaxage === null ) {
112  if (
113  $contentType == 'text/css' ||
114  $contentType == 'application/json' ||
115  $contentType == 'text/javascript'
116  ) {
117  // CSS/JSON/JS raw content has its own CDN max age configuration.
118  // Note: HtmlCacheUpdater::getUrls() includes action=raw for css/json/js
119  // pages, so if using the canonical url, this will get HTCP purges.
120  $smaxage = intval( $config->get( MainConfigNames::ForcedRawSMaxage ) );
121  } else {
122  // No CDN cache for anything else
123  $smaxage = 0;
124  }
125  }
126 
127  // Set standard Vary headers so cache varies on cookies and such (T125283)
128  $response->header( $this->getOutput()->getVaryHeader() );
129 
130  // Output may contain user-specific data;
131  // vary generated content for open sessions on private wikis
132  $privateCache = !$this->permissionManager->isEveryoneAllowed( 'read' ) &&
133  ( $smaxage == 0 || MediaWiki\Session\SessionManager::getGlobalSession()->isPersistent() );
134  // Don't accidentally cache cookies if user is registered (T55032)
135  $privateCache = $privateCache || $this->getUser()->isRegistered();
136  $mode = $privateCache ? 'private' : 'public';
137  $response->header(
138  'Cache-Control: ' . $mode . ', s-maxage=' . $smaxage . ', max-age=' . $maxage
139  );
140 
141  // In the event of user JS, don't allow loading a user JS/CSS/Json
142  // subpage that has no registered user associated with, as
143  // someone could register the account and take control of the
144  // JS/CSS/Json page.
145  $title = $this->getTitle();
146  if ( $title->isUserConfigPage() && $contentType !== 'text/x-wiki' ) {
147  // not using getRootText() as we want this to work
148  // even if subpages are disabled.
149  $rootPage = strtok( $title->getText(), '/' );
150  $userFromTitle = User::newFromName( $rootPage, 'usable' );
151  if ( !$userFromTitle || !$userFromTitle->isRegistered() ) {
152  $elevated = $this->getAuthority()->isAllowed( 'editinterface' );
153  $elevatedText = $elevated ? 'by elevated ' : '';
154  $log = LoggerFactory::getInstance( "security" );
155  $log->warning(
156  "Unsafe JS/CSS/Json {$elevatedText}load - {user} loaded {title} with {ctype}",
157  [
158  'user' => $this->getUser()->getName(),
159  'title' => $title->getPrefixedDBkey(),
160  'ctype' => $contentType,
161  'elevated' => $elevated
162  ]
163  );
164  throw new HttpError( 403, wfMessage( 'unregistered-user-config' ) );
165  }
166  }
167 
168  // Don't allow loading non-protected pages as javascript.
169  // In future we may further restrict this to only CONTENT_MODEL_JAVASCRIPT
170  // in NS_MEDIAWIKI or NS_USER, as well as including other config types,
171  // but for now be more permissive. Allowing protected pages outside of
172  // NS_USER and NS_MEDIAWIKI in particular should be considered a temporary
173  // allowance.
174  $pageRestrictions = $this->restrictionStore->getRestrictions( $title, 'edit' );
175  if (
176  $contentType === 'text/javascript' &&
177  !$title->isUserJsConfigPage() &&
178  !$title->inNamespace( NS_MEDIAWIKI ) &&
179  !in_array( 'sysop', $pageRestrictions ) &&
180  !in_array( 'editprotected', $pageRestrictions )
181  ) {
182 
183  $log = LoggerFactory::getInstance( "security" );
184  $log->info( "Blocked loading unprotected JS {title} for {user}",
185  [
186  'user' => $this->getUser()->getName(),
187  'title' => $title->getPrefixedDBkey(),
188  ]
189  );
190  throw new HttpError( 403, wfMessage( 'unprotected-js' ) );
191  }
192 
193  $response->header( 'Content-type: ' . $contentType . '; charset=UTF-8' );
194 
195  $text = $this->getRawText();
196 
197  // Don't return a 404 response for CSS or JavaScript;
198  // 404s aren't generally cached and it would create
199  // extra hits when user CSS/JS are on and the user doesn't
200  // have the pages.
201  if ( $text === false && $contentType == 'text/x-wiki' ) {
202  $response->statusHeader( 404 );
203  }
204 
205  if ( !$this->getHookRunner()->onRawPageViewBeforeOutput( $this, $text ) ) {
206  wfDebug( __METHOD__ . ": RawPageViewBeforeOutput hook broke raw page output." );
207  }
208 
209  echo $text;
210 
211  return null;
212  }
213 
220  public function getRawText() {
221  $text = false;
222  $title = $this->getTitle();
223  $request = $this->getRequest();
224 
225  // Get it from the DB
226  $rev = $this->revisionLookup->getRevisionByTitle( $title, $this->getOldId() );
227  if ( $rev ) {
228  $lastmod = wfTimestamp( TS_RFC2822, $rev->getTimestamp() );
229  $request->response()->header( "Last-modified: $lastmod" );
230 
231  // Public-only due to cache headers
232  $content = $rev->getContent( SlotRecord::MAIN );
233 
234  if ( $content === null ) {
235  // revision not found (or suppressed)
236  } elseif ( !$content instanceof TextContent ) {
237  // non-text content
238  wfHttpError( 415, "Unsupported Media Type", "The requested page uses the content model `"
239  . $content->getModel() . "` which is not supported via this interface." );
240  die();
241  } else {
242  // want a section?
243  $section = $request->getIntOrNull( 'section' );
244  if ( $section !== null ) {
245  $content = $content->getSection( $section );
246  }
247 
248  if ( $content === null || $content === false ) {
249  // section not found (or section not supported, e.g. for JS, JSON, and CSS)
250  } else {
251  $text = $content->getText();
252  }
253  }
254  }
255 
256  if ( $text !== false && $text !== '' && $request->getRawVal( 'templates' ) === 'expand' ) {
257  $text = $this->parser->preprocess(
258  $text,
259  $title,
261  );
262  }
263 
264  return $text;
265  }
266 
272  public function getOldId() {
273  $oldid = $this->getRequest()->getInt( 'oldid' );
274  $rl = $this->revisionLookup;
275  switch ( $this->getRequest()->getText( 'direction' ) ) {
276  case 'next':
277  # output next revision, or nothing if there isn't one
278  $nextRev = null;
279  if ( $oldid ) {
280  $oldRev = $rl->getRevisionById( $oldid );
281  if ( $oldRev ) {
282  $nextRev = $rl->getNextRevision( $oldRev );
283  }
284  }
285  $oldid = $nextRev ? $nextRev->getId() : -1;
286  break;
287  case 'prev':
288  # output previous revision, or nothing if there isn't one
289  $prevRev = null;
290  if ( !$oldid ) {
291  # get the current revision so we can get the penultimate one
292  $oldid = $this->getWikiPage()->getLatest();
293  }
294  $oldRev = $rl->getRevisionById( $oldid );
295  if ( $oldRev ) {
296  $prevRev = $rl->getPreviousRevision( $oldRev );
297  }
298  $oldid = $prevRev ? $prevRev->getId() : -1;
299  break;
300  case 'cur':
301  $oldid = 0;
302  break;
303  }
304 
305  // @phan-suppress-next-line PhanTypeMismatchReturnNullable RevisionRecord::getId does not return null here
306  return $oldid;
307  }
308 
314  public function getContentType() {
315  // Optimisation: Avoid slow getVal(), this isn't user-generated content.
316  $ctype = $this->getRequest()->getRawVal( 'ctype' );
317 
318  if ( $ctype == '' ) {
319  // Legacy compatibility
320  $gen = $this->getRequest()->getRawVal( 'gen' );
321  if ( $gen == 'js' ) {
322  $ctype = 'text/javascript';
323  } elseif ( $gen == 'css' ) {
324  $ctype = 'text/css';
325  }
326  }
327 
328  $allowedCTypes = [
329  'text/x-wiki',
330  'text/javascript',
331  'text/css',
332  // FIXME: Should we still allow Zope editing? External editing feature was dropped
333  'application/x-zope-edit',
334  'application/json'
335  ];
336  if ( $ctype == '' || !in_array( $ctype, $allowedCTypes ) ) {
337  $ctype = 'text/x-wiki';
338  }
339 
340  return $ctype;
341  }
342 }
const NS_MEDIAWIKI
Definition: Defines.php:72
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfHttpError( $code, $label, $desc)
Provide a simple HTTP error.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
getWikiPage()
Get a WikiPage object.
Definition: Action.php:203
IContextSource null $context
IContextSource if specified; otherwise we'll use the Context from the Page.
Definition: Action.php:57
getHookRunner()
Definition: Action.php:268
getTitle()
Shortcut to get the Title object from the page.
Definition: Action.php:224
getContext()
Get the IContextSource in use here.
Definition: Action.php:130
getOutput()
Get the OutputPage being used for this instance.
Definition: Action.php:154
getUser()
Shortcut to get the User being used for this instance.
Definition: Action.php:164
getAuthority()
Shortcut to get the Authority executing this instance.
Definition: Action.php:174
getRequest()
Get the WebRequest being used for this instance.
Definition: Action.php:144
Legacy class representing an editable page and handling UI for some page actions.
Definition: Article.php:49
An action which just does something, without showing a form first.
Show an error that looks like an HTTP server error.
Definition: HttpError.php:32
PSR-3 logger instance factory.
A class containing constants representing the names of configuration variables.
A service class for checking permissions To obtain an instance, use MediaWikiServices::getInstance()-...
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:40
static getGlobalSession()
If PHP's session_id() has been set, returns that session.
static newFromContext(IContextSource $context)
Get a ParserOptions object from a IContextSource object.
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition: Parser.php:97
A simple method to retrieve the plain source of an article, using "action=raw" in the GET request str...
Definition: RawAction.php:42
getContentType()
Get the content type to use for the response.
Definition: RawAction.php:314
getRawText()
Get the text that should be returned, or false if the page or revision was not found.
Definition: RawAction.php:220
getName()
Return the name of the action this object responds to.
Definition: RawAction.php:79
requiresWrite()
Whether this action requires the wiki not to be locked.
Definition: RawAction.php:83
requiresUnblock()
Whether this action can still be executed by a blocked user.
Definition: RawAction.php:87
__construct(Article $article, IContextSource $context, Parser $parser, PermissionManager $permissionManager, RevisionLookup $revisionLookup, RestrictionStore $restrictionStore)
Definition: RawAction.php:64
getOldId()
Get the ID of the revision that should used to get the text.
Definition: RawAction.php:272
Content object implementation for representing flat text.
Definition: TextContent.php:40
static newFromName( $name, $validate='valid')
Definition: User.php:587
Interface for objects which can provide a MediaWiki context on request.
Service for looking up page revisions.
$content
Definition: router.php:76