StringUtils: Add a utility for checking if a string is a valid regex
[lhc/web/wiklou.git] / includes / actions / RawAction.php
1 <?php
2 /**
3 * Raw page text accessor
4 *
5 * Copyright © 2004 Gabriel Wicke <wicke@wikidev.net>
6 * http://wikidev.net/
7 *
8 * Based on HistoryAction and SpecialExport
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 *
25 * @author Gabriel Wicke <wicke@wikidev.net>
26 * @file
27 */
28
29 use MediaWiki\Logger\LoggerFactory;
30 use MediaWiki\MediaWikiServices;
31
32 /**
33 * A simple method to retrieve the plain source of an article,
34 * using "action=raw" in the GET request string.
35 *
36 * @ingroup Actions
37 */
38 class RawAction extends FormlessAction {
39 public function getName() {
40 return 'raw';
41 }
42
43 public function requiresWrite() {
44 return false;
45 }
46
47 public function requiresUnblock() {
48 return false;
49 }
50
51 /**
52 * @suppress SecurityCheck-XSS Non html mime type
53 * @return string|null
54 */
55 function onView() {
56 $this->getOutput()->disable();
57 $request = $this->getRequest();
58 $response = $request->response();
59 $config = $this->context->getConfig();
60
61 if ( !$request->checkUrlExtension() ) {
62 return null;
63 }
64
65 if ( $this->getOutput()->checkLastModified( $this->page->getTouched() ) ) {
66 return null; // Client cache fresh and headers sent, nothing more to do.
67 }
68
69 $contentType = $this->getContentType();
70
71 $maxage = $request->getInt( 'maxage', $config->get( 'CdnMaxAge' ) );
72 $smaxage = $request->getIntOrNull( 'smaxage' );
73 if ( $smaxage === null ) {
74 if (
75 $contentType == 'text/css' ||
76 $contentType == 'application/json' ||
77 $contentType == 'text/javascript'
78 ) {
79 // CSS/JSON/JS raw content has its own CDN max age configuration.
80 // Note: Title::getCdnUrls() includes action=raw for css/json/js
81 // pages, so if using the canonical url, this will get HTCP purges.
82 $smaxage = intval( $config->get( 'ForcedRawSMaxage' ) );
83 } else {
84 // No CDN cache for anything else
85 $smaxage = 0;
86 }
87 }
88
89 // Set standard Vary headers so cache varies on cookies and such (T125283)
90 $response->header( $this->getOutput()->getVaryHeader() );
91
92 // Output may contain user-specific data;
93 // vary generated content for open sessions on private wikis
94 $privateCache = !User::isEveryoneAllowed( 'read' ) &&
95 ( $smaxage == 0 || MediaWiki\Session\SessionManager::getGlobalSession()->isPersistent() );
96 // Don't accidentally cache cookies if user is logged in (T55032)
97 $privateCache = $privateCache || $this->getUser()->isLoggedIn();
98 $mode = $privateCache ? 'private' : 'public';
99 $response->header(
100 'Cache-Control: ' . $mode . ', s-maxage=' . $smaxage . ', max-age=' . $maxage
101 );
102
103 // In the event of user JS, don't allow loading a user JS/CSS/Json
104 // subpage that has no registered user associated with, as
105 // someone could register the account and take control of the
106 // JS/CSS/Json page.
107 $title = $this->getTitle();
108 if ( $title->isUserConfigPage() && $contentType !== 'text/x-wiki' ) {
109 // not using getRootText() as we want this to work
110 // even if subpages are disabled.
111 $rootPage = strtok( $title->getText(), '/' );
112 $userFromTitle = User::newFromName( $rootPage, 'usable' );
113 if ( !$userFromTitle || $userFromTitle->getId() === 0 ) {
114 $elevated = MediaWikiServices::getInstance()->getPermissionManager()
115 ->userHasRight( $this->getUser(), 'editinterface' );
116 $elevatedText = $elevated ? 'by elevated ' : '';
117 $log = LoggerFactory::getInstance( "security" );
118 $log->warning(
119 "Unsafe JS/CSS/Json {$elevatedText}load - {user} loaded {title} with {ctype}",
120 [
121 'user' => $this->getUser()->getName(),
122 'title' => $title->getPrefixedDBkey(),
123 'ctype' => $contentType,
124 'elevated' => $elevated
125 ]
126 );
127 $msg = wfMessage( 'unregistered-user-config' );
128 throw new HttpError( 403, $msg );
129 }
130 }
131
132 // Don't allow loading non-protected pages as javascript.
133 // In future we may further restrict this to only CONTENT_MODEL_JAVASCRIPT
134 // in NS_MEDIAWIKI or NS_USER, as well as including other config types,
135 // but for now be more permissive. Allowing protected pages outside of
136 // NS_USER and NS_MEDIAWIKI in particular should be considered a temporary
137 // allowance.
138 if (
139 $contentType === 'text/javascript' &&
140 !$title->isUserJsConfigPage() &&
141 !$title->inNamespace( NS_MEDIAWIKI ) &&
142 !in_array( 'sysop', $title->getRestrictions( 'edit' ) ) &&
143 !in_array( 'editprotected', $title->getRestrictions( 'edit' ) )
144 ) {
145
146 $log = LoggerFactory::getInstance( "security" );
147 $log->info( "Blocked loading unprotected JS {title} for {user}",
148 [
149 'user' => $this->getUser()->getName(),
150 'title' => $title->getPrefixedDBkey(),
151 ]
152 );
153 throw new HttpError( 403, wfMessage( 'unprotected-js' ) );
154 }
155
156 $response->header( 'Content-type: ' . $contentType . '; charset=UTF-8' );
157
158 $text = $this->getRawText();
159
160 // Don't return a 404 response for CSS or JavaScript;
161 // 404s aren't generally cached and it would create
162 // extra hits when user CSS/JS are on and the user doesn't
163 // have the pages.
164 if ( $text === false && $contentType == 'text/x-wiki' ) {
165 $response->statusHeader( 404 );
166 }
167
168 // Avoid PHP 7.1 warning of passing $this by reference
169 $rawAction = $this;
170 if ( !Hooks::run( 'RawPageViewBeforeOutput', [ &$rawAction, &$text ] ) ) {
171 wfDebug( __METHOD__ . ": RawPageViewBeforeOutput hook broke raw page output.\n" );
172 }
173
174 echo $text;
175
176 return null;
177 }
178
179 /**
180 * Get the text that should be returned, or false if the page or revision
181 * was not found.
182 *
183 * @return string|bool
184 */
185 public function getRawText() {
186 $text = false;
187 $title = $this->getTitle();
188 $request = $this->getRequest();
189
190 // Get it from the DB
191 $rev = Revision::newFromTitle( $title, $this->getOldId() );
192 if ( $rev ) {
193 $lastmod = wfTimestamp( TS_RFC2822, $rev->getTimestamp() );
194 $request->response()->header( "Last-modified: $lastmod" );
195
196 // Public-only due to cache headers
197 $content = $rev->getContent();
198
199 if ( $content === null ) {
200 // revision not found (or suppressed)
201 $text = false;
202 } elseif ( !$content instanceof TextContent ) {
203 // non-text content
204 wfHttpError( 415, "Unsupported Media Type", "The requested page uses the content model `"
205 . $content->getModel() . "` which is not supported via this interface." );
206 die();
207 } else {
208 // want a section?
209 $section = $request->getIntOrNull( 'section' );
210 if ( $section !== null ) {
211 $content = $content->getSection( $section );
212 }
213
214 if ( $content === null || $content === false ) {
215 // section not found (or section not supported, e.g. for JS, JSON, and CSS)
216 $text = false;
217 } else {
218 $text = $content->getText();
219 }
220 }
221 }
222
223 if ( $text !== false && $text !== '' && $request->getRawVal( 'templates' ) === 'expand' ) {
224 $text = MediaWikiServices::getInstance()->getParser()->preprocess(
225 $text,
226 $title,
227 ParserOptions::newFromContext( $this->getContext() )
228 );
229 }
230
231 return $text;
232 }
233
234 /**
235 * Get the ID of the revision that should used to get the text.
236 *
237 * @return int
238 */
239 public function getOldId() {
240 $oldid = $this->getRequest()->getInt( 'oldid' );
241 switch ( $this->getRequest()->getText( 'direction' ) ) {
242 case 'next':
243 # output next revision, or nothing if there isn't one
244 $nextid = 0;
245 if ( $oldid ) {
246 $nextid = $this->getTitle()->getNextRevisionID( $oldid );
247 }
248 $oldid = $nextid ?: -1;
249 break;
250 case 'prev':
251 # output previous revision, or nothing if there isn't one
252 if ( !$oldid ) {
253 # get the current revision so we can get the penultimate one
254 $oldid = $this->page->getLatest();
255 }
256 $previd = $this->getTitle()->getPreviousRevisionID( $oldid );
257 $oldid = $previd ?: -1;
258 break;
259 case 'cur':
260 $oldid = 0;
261 break;
262 }
263
264 return $oldid;
265 }
266
267 /**
268 * Get the content type to use for the response
269 *
270 * @return string
271 */
272 public function getContentType() {
273 // Optimisation: Avoid slow getVal(), this isn't user-generated content.
274 $ctype = $this->getRequest()->getRawVal( 'ctype' );
275
276 if ( $ctype == '' ) {
277 // Legacy compatibilty
278 $gen = $this->getRequest()->getRawVal( 'gen' );
279 if ( $gen == 'js' ) {
280 $ctype = 'text/javascript';
281 } elseif ( $gen == 'css' ) {
282 $ctype = 'text/css';
283 }
284 }
285
286 $allowedCTypes = [
287 'text/x-wiki',
288 'text/javascript',
289 'text/css',
290 // FIXME: Should we still allow Zope editing? External editing feature was dropped
291 'application/x-zope-edit',
292 'application/json'
293 ];
294 if ( $ctype == '' || !in_array( $ctype, $allowedCTypes ) ) {
295 $ctype = 'text/x-wiki';
296 }
297
298 return $ctype;
299 }
300 }