Issues (22)

Security Analysis    no request data  

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.

src/PackagePrivate/DatabaseTermIdsResolver.php (1 issue)

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
namespace Wikibase\TermStore\MediaWiki\PackagePrivate;
4
5
use InvalidArgumentException;
6
use LogicException;
7
use Psr\Log\LoggerInterface;
8
use Psr\Log\NullLogger;
9
use stdClass;
10
use Wikimedia\Rdbms\IDatabase;
11
use Wikimedia\Rdbms\ILoadBalancer;
12
use Wikimedia\Rdbms\IResultWrapper;
13
14
/**
15
 * Term ID resolver using the normalized database schema.
16
 *
17
 * @license GPL-2.0-or-later
18
 */
19
class DatabaseTermIdsResolver implements TermIdsResolver {
20
21
	/** @var TypeIdsResolver */
22
	private $typeIdsResolver;
23
24
	/** @var ILoadBalancer */
25
	private $lb;
26
27
	/** @var bool */
28
	private $allowMasterFallback;
29
30
	/** @var LoggerInterface */
31
	private $logger;
32
33
	/** @var IDatabase */
34
	private $dbr = null;
35
36
	/** @var IDatabase */
37
	private $dbw = null;
38
39
	/** @var string[] stash of data returned from the {@link TypeIdsResolver} */
40
	private $typeNames = [];
41
42
	/**
43
	 * @param TypeIdsResolver $typeIdsResolver
44
	 * @param ILoadBalancer $lb
45
	 * @param bool $allowMasterFallback Whether to fall back to the master database if the data from
46
	 * the replica database is detected to be stale.
47
	 */
48
	public function __construct(
49
		TypeIdsResolver $typeIdsResolver,
50
		ILoadBalancer $lb,
51
		$allowMasterFallback = false,
52
		LoggerInterface $logger = null
53
	) {
54
		$this->typeIdsResolver = $typeIdsResolver;
55
		$this->lb = $lb;
56
		$this->allowMasterFallback = $allowMasterFallback;
57
		$this->logger = $logger ?: new NullLogger();
0 ignored issues
show
Documentation Bug introduced by
It seems like $logger ?: new \Psr\Log\NullLogger() can also be of type object<Psr\Log\NullLogger>. However, the property $logger is declared as type object<Psr\Log\LoggerInterface>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
58
	}
59
60
	/*
61
	 * Term data is first read from the replica; if that returns less rows than we asked for, then
62
	 * there are some new rows in the master that were not yet replicated, and we fall back to the
63
	 * master if allowed. As the internal relations of the term store never change (for example, a
64
	 * term_in_lang row will never suddenly point to a different text_in_lang), a master fallback
65
	 * should never be necessary in any other case. However, callers need to consider where they
66
	 * got the list of term IDs they pass into this method from: if it’s from a replica, they may
67
	 * still see outdated data overall.
68
	 */
69
	public function resolveTermIds( array $termIds ): array {
70
		$terms = [];
71
72
		$this->logger->debug(
73
			'{method}: getting {termCount} rows from replica',
74
			[
75
				'method' => __METHOD__,
76
				'termCount' => count( $termIds ),
77
			]
78
		);
79
		$replicaResult = $this->selectTerms( $this->getDbr(), $termIds );
80
		$this->preloadTypes( $replicaResult );
81
		$replicaTermIds = [];
82
83
		foreach ( $replicaResult as $row ) {
84
			$replicaTermIds[] = $row->wbtl_id;
85
			$this->addResultTerms( $terms, $row );
86
		}
87
88
		if ( $this->allowMasterFallback && count( $replicaTermIds ) !== count( $termIds ) ) {
89
			$this->logger->info(
90
				'{method}: replica only returned {replicaCount} out of {termCount} rows, ' .
91
					'falling back to master',
92
				[
93
					'method' => __METHOD__,
94
					'replicaCount' => count( $replicaTermIds ),
95
					'termCount' => count( $termIds ),
96
				]
97
			);
98
			$masterTermIds = array_values( array_diff( $termIds, $replicaTermIds ) );
99
			$masterResult = $this->selectTerms( $this->getDbw(), $masterTermIds );
100
			$this->preloadTypes( $masterResult );
101
			foreach ( $masterResult as $row ) {
102
				$this->addResultTerms( $terms, $row );
103
			}
104
		}
105
106
		return $terms;
107
	}
108
109
	private function selectTerms( IDatabase $db, array $termIds ): IResultWrapper {
110
		return $db->select(
111
			[ 'wbt_term_in_lang', 'wbt_text_in_lang', 'wbt_text' ],
112
			[ 'wbtl_id', 'wbtl_type_id', 'wbxl_language', 'wbx_text' ],
113
			[
114
				'wbtl_id' => $termIds,
115
				// join conditions
116
				'wbtl_text_in_lang_id=wbxl_id',
117
				'wbxl_text_id=wbx_id',
118
			],
119
			__METHOD__
120
		);
121
	}
122
123
	private function preloadTypes( IResultWrapper $result ) {
124
		$typeIds = [];
125
		foreach ( $result as $row ) {
126
			$typeId = $row->wbtl_type_id;
127
			if ( !array_key_exists( $typeId, $this->typeNames ) ) {
128
				$typeIds[$typeId] = true;
129
			}
130
		}
131
		$this->typeNames += $this->typeIdsResolver->resolveTypeIds( array_keys( $typeIds ) );
132
	}
133
134
	private function addResultTerms( array &$terms, stdClass $row ) {
135
		$type = $this->lookupType( $row->wbtl_type_id );
136
		$lang = $row->wbxl_language;
137
		$text = $row->wbx_text;
138
		$terms[$type][$lang][] = $text;
139
	}
140
141
	private function lookupType( $typeId ) {
142
		$typeName = $this->typeNames[$typeId] ?? null;
143
		if ( $typeName === null ) {
144
			throw new InvalidArgumentException(
145
				'Type ID ' . $typeId . ' was requested but not preloaded!' );
146
		}
147
		return $typeName;
148
	}
149
150
	private function getDbr() {
151
		if ( $this->dbr === null ) {
152
			$this->dbr = $this->lb->getConnection( ILoadBalancer::DB_REPLICA );
153
		}
154
155
		return $this->dbr;
156
	}
157
158
	private function getDbw() {
159
		if ( !$this->allowMasterFallback ) {
160
			throw new LogicException( 'Master fallback not allowed!' );
161
		}
162
163
		if ( $this->dbw === null ) {
164
			$this->dbw = $this->lb->getConnection( ILoadBalancer::DB_MASTER );
165
		}
166
167
		return $this->dbw;
168
	}
169
170
}
171