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/cache/LinkBatch.php (3 issues)

Labels
Severity

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
 * Batch query to determine page existence.
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
22
 */
23
use MediaWiki\Linker\LinkTarget;
24
use MediaWiki\MediaWikiServices;
25
26
/**
27
 * Class representing a list of titles
28
 * The execute() method checks them all for existence and adds them to a LinkCache object
29
 *
30
 * @ingroup Cache
31
 */
32
class LinkBatch {
33
	/**
34
	 * 2-d array, first index namespace, second index dbkey, value arbitrary
35
	 */
36
	public $data = [];
37
38
	/**
39
	 * For debugging which method is using this class.
40
	 */
41
	protected $caller;
42
43
	/**
44
	 * LinkBatch constructor.
45
	 * @param LinkTarget[] $arr Initial items to be added to the batch
46
	 */
47
	public function __construct( $arr = [] ) {
48
		foreach ( $arr as $item ) {
49
			$this->addObj( $item );
50
		}
51
	}
52
53
	/**
54
	 * Use ->setCaller( __METHOD__ ) to indicate which code is using this
55
	 * class. Only used in debugging output.
56
	 * @since 1.17
57
	 *
58
	 * @param string $caller
59
	 */
60
	public function setCaller( $caller ) {
61
		$this->caller = $caller;
62
	}
63
64
	/**
65
	 * @param LinkTarget $linkTarget
66
	 */
67
	public function addObj( $linkTarget ) {
68
		if ( is_object( $linkTarget ) ) {
69
			$this->add( $linkTarget->getNamespace(), $linkTarget->getDBkey() );
70
		} else {
71
			wfDebug( "Warning: LinkBatch::addObj got invalid LinkTarget object\n" );
72
		}
73
	}
74
75
	/**
76
	 * @param int $ns
77
	 * @param string $dbkey
78
	 */
79
	public function add( $ns, $dbkey ) {
80
		if ( $ns < 0 || $dbkey === '' ) {
81
			return; // T137083
82
		}
83
		if ( !array_key_exists( $ns, $this->data ) ) {
84
			$this->data[$ns] = [];
85
		}
86
87
		$this->data[$ns][strtr( $dbkey, ' ', '_' )] = 1;
88
	}
89
90
	/**
91
	 * Set the link list to a given 2-d array
92
	 * First key is the namespace, second is the DB key, value arbitrary
93
	 *
94
	 * @param array $array
95
	 */
96
	public function setArray( $array ) {
97
		$this->data = $array;
98
	}
99
100
	/**
101
	 * Returns true if no pages have been added, false otherwise.
102
	 *
103
	 * @return bool
104
	 */
105
	public function isEmpty() {
106
		return $this->getSize() == 0;
107
	}
108
109
	/**
110
	 * Returns the size of the batch.
111
	 *
112
	 * @return int
113
	 */
114
	public function getSize() {
115
		return count( $this->data );
116
	}
117
118
	/**
119
	 * Do the query and add the results to the LinkCache object
120
	 *
121
	 * @return array Mapping PDBK to ID
122
	 */
123
	public function execute() {
124
		$linkCache = MediaWikiServices::getInstance()->getLinkCache();
125
126
		return $this->executeInto( $linkCache );
127
	}
128
129
	/**
130
	 * Do the query and add the results to a given LinkCache object
131
	 * Return an array mapping PDBK to ID
132
	 *
133
	 * @param LinkCache $cache
134
	 * @return array Remaining IDs
135
	 */
136
	protected function executeInto( &$cache ) {
137
		$res = $this->doQuery();
138
		$this->doGenderQuery();
139
		$ids = $this->addResultToCache( $cache, $res );
0 ignored issues
show
It seems like $res defined by $this->doQuery() on line 137 can also be of type boolean; however, LinkBatch::addResultToCache() does only seem to accept object<ResultWrapper>, 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...
140
141
		return $ids;
142
	}
143
144
	/**
145
	 * Add a ResultWrapper containing IDs and titles to a LinkCache object.
146
	 * As normal, titles will go into the static Title cache field.
147
	 * This function *also* stores extra fields of the title used for link
148
	 * parsing to avoid extra DB queries.
149
	 *
150
	 * @param LinkCache $cache
151
	 * @param ResultWrapper $res
152
	 * @return array Array of remaining titles
153
	 */
154
	public function addResultToCache( $cache, $res ) {
155
		if ( !$res ) {
156
			return [];
157
		}
158
159
		$titleFormatter = MediaWikiServices::getInstance()->getTitleFormatter();
160
		// For each returned entry, add it to the list of good links, and remove it from $remaining
161
162
		$ids = [];
163
		$remaining = $this->data;
164
		foreach ( $res as $row ) {
165
			$title = new TitleValue( (int)$row->page_namespace, $row->page_title );
166
			$cache->addGoodLinkObjFromRow( $title, $row );
167
			$pdbk = $titleFormatter->getPrefixedDBkey( $title );
168
			$ids[$pdbk] = $row->page_id;
169
			unset( $remaining[$row->page_namespace][$row->page_title] );
170
		}
171
172
		// The remaining links in $data are bad links, register them as such
173
		foreach ( $remaining as $ns => $dbkeys ) {
174
			foreach ( $dbkeys as $dbkey => $unused ) {
175
				$title = new TitleValue( (int)$ns, (string)$dbkey );
176
				$cache->addBadLinkObj( $title );
177
				$pdbk = $titleFormatter->getPrefixedDBkey( $title );
178
				$ids[$pdbk] = 0;
179
			}
180
		}
181
182
		return $ids;
183
	}
184
185
	/**
186
	 * Perform the existence test query, return a ResultWrapper with page_id fields
187
	 * @return bool|ResultWrapper
188
	 */
189
	public function doQuery() {
190
		if ( $this->isEmpty() ) {
191
			return false;
192
		}
193
194
		// This is similar to LinkHolderArray::replaceInternal
195
		$dbr = wfGetDB( DB_REPLICA );
196
		$table = 'page';
197
		$fields = array_merge(
198
			LinkCache::getSelectFields(),
199
			[ 'page_namespace', 'page_title' ]
200
		);
201
202
		$conds = $this->constructSet( 'page', $dbr );
0 ignored issues
show
It seems like $dbr defined by wfGetDB(DB_REPLICA) on line 195 can be null; however, LinkBatch::constructSet() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
203
204
		// Do query
205
		$caller = __METHOD__;
206
		if ( strval( $this->caller ) !== '' ) {
207
			$caller .= " (for {$this->caller})";
208
		}
209
		$res = $dbr->select( $table, $fields, $conds, $caller );
0 ignored issues
show
It seems like $conds defined by $this->constructSet('page', $dbr) on line 202 can also be of type boolean; however, Database::select() does only seem to accept string, 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...
210
211
		return $res;
212
	}
213
214
	/**
215
	 * Do (and cache) {{GENDER:...}} information for userpages in this LinkBatch
216
	 *
217
	 * @return bool Whether the query was successful
218
	 */
219
	public function doGenderQuery() {
220
		if ( $this->isEmpty() ) {
221
			return false;
222
		}
223
224
		global $wgContLang;
225
		if ( !$wgContLang->needsGenderDistinction() ) {
226
			return false;
227
		}
228
229
		$genderCache = MediaWikiServices::getInstance()->getGenderCache();
230
		$genderCache->doLinkBatch( $this->data, $this->caller );
231
232
		return true;
233
	}
234
235
	/**
236
	 * Construct a WHERE clause which will match all the given titles.
237
	 *
238
	 * @param string $prefix The appropriate table's field name prefix ('page', 'pl', etc)
239
	 * @param IDatabase $db DB object to use
240
	 * @return string|bool String with SQL where clause fragment, or false if no items.
241
	 */
242
	public function constructSet( $prefix, $db ) {
243
		return $db->makeWhereFrom2d( $this->data, "{$prefix}_namespace", "{$prefix}_title" );
244
	}
245
}
246