Issues (4122)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

includes/parser/ParserCache.php (6 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Cache for outputs of the PHP parser
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
 * @ingroup Cache Parser
22
 */
23
24
/**
25
 * @ingroup Cache Parser
26
 * @todo document
27
 */
28
class ParserCache {
29
	/** @var BagOStuff */
30
	private $mMemc;
31
	/**
32
	 * Get an instance of this object
33
	 *
34
	 * @return ParserCache
35
	 */
36
	public static function singleton() {
37
		static $instance;
38
		if ( !isset( $instance ) ) {
39
			global $parserMemc;
40
			$instance = new ParserCache( $parserMemc );
41
		}
42
		return $instance;
43
	}
44
45
	/**
46
	 * Setup a cache pathway with a given back-end storage mechanism.
47
	 *
48
	 * This class use an invalidation strategy that is compatible with
49
	 * MultiWriteBagOStuff in async replication mode.
50
	 *
51
	 * @param BagOStuff $memCached
52
	 * @throws MWException
53
	 */
54
	protected function __construct( BagOStuff $memCached ) {
55
		$this->mMemc = $memCached;
56
	}
57
58
	/**
59
	 * @param WikiPage $article
60
	 * @param string $hash
61
	 * @return mixed|string
62
	 */
63
	protected function getParserOutputKey( $article, $hash ) {
64
		global $wgRequest;
65
66
		// idhash seem to mean 'page id' + 'rendering hash' (r3710)
67
		$pageid = $article->getId();
68
		$renderkey = (int)( $wgRequest->getVal( 'action' ) == 'render' );
69
70
		$key = wfMemcKey( 'pcache', 'idhash', "{$pageid}-{$renderkey}!{$hash}" );
71
		return $key;
72
	}
73
74
	/**
75
	 * @param WikiPage $page
76
	 * @return mixed|string
77
	 */
78
	protected function getOptionsKey( $page ) {
79
		return wfMemcKey( 'pcache', 'idoptions', $page->getId() );
80
	}
81
82
	/**
83
	 * @param WikiPage $page
84
	 * @since 1.28
85
	 */
86
	public function deleteOptionsKey( $page ) {
87
		$this->mMemc->delete( $this->getOptionsKey( $page ) );
88
	}
89
90
	/**
91
	 * Provides an E-Tag suitable for the whole page. Note that $article
92
	 * is just the main wikitext. The E-Tag has to be unique to the whole
93
	 * page, even if the article itself is the same, so it uses the
94
	 * complete set of user options. We don't want to use the preference
95
	 * of a different user on a message just because it wasn't used in
96
	 * $article. For example give a Chinese interface to a user with
97
	 * English preferences. That's why we take into account *all* user
98
	 * options. (r70809 CR)
99
	 *
100
	 * @param WikiPage $article
101
	 * @param ParserOptions $popts
102
	 * @return string
103
	 */
104
	public function getETag( $article, $popts ) {
105
		return 'W/"' . $this->getParserOutputKey( $article,
106
			$popts->optionsHash( ParserOptions::legacyOptions(), $article->getTitle() ) ) .
107
				"--" . $article->getTouched() . '"';
108
	}
109
110
	/**
111
	 * Retrieve the ParserOutput from ParserCache, even if it's outdated.
112
	 * @param WikiPage $article
113
	 * @param ParserOptions $popts
114
	 * @return ParserOutput|bool False on failure
115
	 */
116
	public function getDirty( $article, $popts ) {
117
		$value = $this->get( $article, $popts, true );
118
		return is_object( $value ) ? $value : false;
119
	}
120
121
	/**
122
	 * Generates a key for caching the given article considering
123
	 * the given parser options.
124
	 *
125
	 * @note Which parser options influence the cache key
126
	 * is controlled via ParserOutput::recordOption() or
127
	 * ParserOptions::addExtraKey().
128
	 *
129
	 * @note Used by Article to provide a unique id for the PoolCounter.
130
	 * It would be preferable to have this code in get()
131
	 * instead of having Article looking in our internals.
132
	 *
133
	 * @todo Document parameter $useOutdated
134
	 *
135
	 * @param WikiPage $article
136
	 * @param ParserOptions $popts
137
	 * @param bool $useOutdated (default true)
138
	 * @return bool|mixed|string
139
	 */
140
	public function getKey( $article, $popts, $useOutdated = true ) {
141
		global $wgCacheEpoch;
142
143
		if ( $popts instanceof User ) {
144
			wfWarn( "Use of outdated prototype ParserCache::getKey( &\$article, &\$user )\n" );
145
			$popts = ParserOptions::newFromUser( $popts );
146
		}
147
148
		// Determine the options which affect this article
149
		$casToken = null;
150
		$optionsKey = $this->mMemc->get(
151
			$this->getOptionsKey( $article ), $casToken, BagOStuff::READ_VERIFIED );
152
		if ( $optionsKey instanceof CacheTime ) {
153
			if ( !$useOutdated && $optionsKey->expired( $article->getTouched() ) ) {
154
				wfIncrStats( "pcache.miss.expired" );
155
				$cacheTime = $optionsKey->getCacheTime();
156
				wfDebugLog( "ParserCache",
157
					"Parser options key expired, touched " . $article->getTouched()
158
					. ", epoch $wgCacheEpoch, cached $cacheTime\n" );
159
				return false;
160 View Code Duplication
			} elseif ( !$useOutdated && $optionsKey->isDifferentRevision( $article->getLatest() ) ) {
161
				wfIncrStats( "pcache.miss.revid" );
162
				$revId = $article->getLatest();
163
				$cachedRevId = $optionsKey->getCacheRevisionId();
164
				wfDebugLog( "ParserCache",
165
					"ParserOutput key is for an old revision, latest $revId, cached $cachedRevId\n"
166
				);
167
				return false;
168
			}
169
170
			// $optionsKey->mUsedOptions is set by save() by calling ParserOutput::getUsedOptions()
171
			$usedOptions = $optionsKey->mUsedOptions;
172
			wfDebug( "Parser cache options found.\n" );
173
		} else {
174
			if ( !$useOutdated ) {
175
				return false;
176
			}
177
			$usedOptions = ParserOptions::legacyOptions();
178
		}
179
180
		return $this->getParserOutputKey(
181
			$article,
182
			$popts->optionsHash( $usedOptions, $article->getTitle() )
0 ignored issues
show
It seems like $usedOptions defined by $optionsKey->mUsedOptions on line 171 can also be of type boolean; however, ParserOptions::optionsHash() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
183
		);
184
	}
185
186
	/**
187
	 * Retrieve the ParserOutput from ParserCache.
188
	 * false if not found or outdated.
189
	 *
190
	 * @param WikiPage|Article $article
191
	 * @param ParserOptions $popts
192
	 * @param bool $useOutdated (default false)
193
	 *
194
	 * @return ParserOutput|bool False on failure
195
	 */
196
	public function get( $article, $popts, $useOutdated = false ) {
197
		global $wgCacheEpoch;
198
199
		$canCache = $article->checkTouched();
200
		if ( !$canCache ) {
201
			// It's a redirect now
202
			return false;
203
		}
204
205
		$touched = $article->getTouched();
206
207
		$parserOutputKey = $this->getKey( $article, $popts, $useOutdated );
0 ignored issues
show
It seems like $article defined by parameter $article on line 196 can also be of type object<Article>; however, ParserCache::getKey() does only seem to accept object<WikiPage>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
208
		if ( $parserOutputKey === false ) {
209
			wfIncrStats( 'pcache.miss.absent' );
210
			return false;
211
		}
212
213
		$casToken = null;
214
		/** @var ParserOutput $value */
215
		$value = $this->mMemc->get( $parserOutputKey, $casToken, BagOStuff::READ_VERIFIED );
216
		if ( !$value ) {
217
			wfDebug( "ParserOutput cache miss.\n" );
218
			wfIncrStats( "pcache.miss.absent" );
219
			return false;
220
		}
221
222
		wfDebug( "ParserOutput cache found.\n" );
223
224
		// The edit section preference may not be the appropiate one in
225
		// the ParserOutput, as we are not storing it in the parsercache
226
		// key. Force it here. See bug 31445.
227
		$value->setEditSectionTokens( $popts->getEditSection() );
228
229
		$wikiPage = method_exists( $article, 'getPage' )
230
			? $article->getPage()
0 ignored issues
show
The method getPage does only exist in Article, but not in WikiPage.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
231
			: $article;
232
233
		if ( !$useOutdated && $value->expired( $touched ) ) {
234
			wfIncrStats( "pcache.miss.expired" );
235
			$cacheTime = $value->getCacheTime();
236
			wfDebugLog( "ParserCache",
237
				"ParserOutput key expired, touched $touched, "
238
				. "epoch $wgCacheEpoch, cached $cacheTime\n" );
239
			$value = false;
240 View Code Duplication
		} elseif ( !$useOutdated && $value->isDifferentRevision( $article->getLatest() ) ) {
241
			wfIncrStats( "pcache.miss.revid" );
242
			$revId = $article->getLatest();
243
			$cachedRevId = $value->getCacheRevisionId();
244
			wfDebugLog( "ParserCache",
245
				"ParserOutput key is for an old revision, latest $revId, cached $cachedRevId\n"
246
			);
247
			$value = false;
248
		} elseif (
249
			Hooks::run( 'RejectParserCacheValue', [ $value, $wikiPage, $popts ] ) === false
250
		) {
251
			wfIncrStats( 'pcache.miss.rejected' );
252
			wfDebugLog( "ParserCache",
253
				"ParserOutput key valid, but rejected by RejectParserCacheValue hook handler.\n"
254
			);
255
			$value = false;
256
		} else {
257
			wfIncrStats( "pcache.hit" );
258
		}
259
260
		return $value;
261
	}
262
263
	/**
264
	 * @param ParserOutput $parserOutput
265
	 * @param WikiPage $page
266
	 * @param ParserOptions $popts
267
	 * @param string $cacheTime Time when the cache was generated
268
	 * @param int $revId Revision ID that was parsed
269
	 */
270
	public function save( $parserOutput, $page, $popts, $cacheTime = null, $revId = null ) {
271
		$expire = $parserOutput->getCacheExpiry();
272
		if ( $expire > 0 && !$this->mMemc instanceof EmptyBagOStuff ) {
273
			$cacheTime = $cacheTime ?: wfTimestampNow();
274
			if ( !$revId ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $revId of type integer|null is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
275
				$revision = $page->getRevision();
276
				$revId = $revision ? $revision->getId() : null;
277
			}
278
279
			$optionsKey = new CacheTime;
280
			$optionsKey->mUsedOptions = $parserOutput->getUsedOptions();
281
			$optionsKey->updateCacheExpiry( $expire );
282
283
			$optionsKey->setCacheTime( $cacheTime );
0 ignored issues
show
It seems like $cacheTime defined by $cacheTime ?: wfTimestampNow() on line 273 can also be of type false; however, CacheTime::setCacheTime() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
284
			$parserOutput->setCacheTime( $cacheTime );
0 ignored issues
show
It seems like $cacheTime defined by $cacheTime ?: wfTimestampNow() on line 273 can also be of type false; however, CacheTime::setCacheTime() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
285
			$optionsKey->setCacheRevisionId( $revId );
286
			$parserOutput->setCacheRevisionId( $revId );
287
288
			$parserOutputKey = $this->getParserOutputKey( $page,
289
				$popts->optionsHash( $optionsKey->mUsedOptions, $page->getTitle() ) );
290
291
			// Save the timestamp so that we don't have to load the revision row on view
292
			$parserOutput->setTimestamp( $page->getTimestamp() );
293
294
			$msg = "Saved in parser cache with key $parserOutputKey" .
295
				" and timestamp $cacheTime" .
296
				" and revision id $revId" .
297
				"\n";
298
299
			$parserOutput->mText .= "\n<!-- $msg -->\n";
300
			wfDebug( $msg );
301
302
			// Save the parser output
303
			$this->mMemc->set( $parserOutputKey, $parserOutput, $expire );
304
305
			// ...and its pointer
306
			$this->mMemc->set( $this->getOptionsKey( $page ), $optionsKey, $expire );
307
308
			Hooks::run(
309
				'ParserCacheSaveComplete',
310
				[ $this, $parserOutput, $page->getTitle(), $popts, $revId ]
311
			);
312
		} elseif ( $expire <= 0 ) {
313
			wfDebug( "Parser output was marked as uncacheable and has not been saved.\n" );
314
		}
315
	}
316
}
317