Issues (9)

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/MongoDB/MongoDBEntityDatabase.php (4 issues)

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
namespace Wikibase\EntityStore\MongoDB;
4
5
use Doctrine\MongoDB\Database;
6
use Doctrine\MongoDB\Query\Expr;
7
use Wikibase\DataModel\Entity\EntityDocument;
8
use Wikibase\DataModel\Entity\EntityId;
9
use Wikibase\EntityStore\EntityDocumentLookup;
10
use Wikibase\EntityStore\EntityDocumentSaver;
11
12
/**
13
 * Internal class
14
 *
15
 * @licence GPLv2+
16
 * @author Thomas Pellissier Tanon
17
 */
18
class MongoDBEntityDatabase implements EntityDocumentLookup, EntityDocumentSaver {
19
20
	/**
21
	 * @var Database
22
	 */
23
	private $database;
24
25
	/**
26
	 * @var MongoDBDocumentBuilder
27
	 */
28
	private $documentBuilder;
29
30
	/**
31
	 * @param Database $database
32
	 * @param MongoDBDocumentBuilder $documentBuilder
33
	 */
34 5
	public function __construct( Database $database, MongoDBDocumentBuilder $documentBuilder ) {
35 5
		$this->database = $database;
36 5
		$this->documentBuilder = $documentBuilder;
37 5
	}
38
39
	/**
40
	 * @see EntityDocumentLookup::getEntityDocumentForId
41
	 */
42 3
	public function getEntityDocumentForId( EntityId $entityId ) {
43 3
		$document = $this->database
44 3
			->selectCollection( $entityId->getEntityType() )
45 3
			->findOne( $this->buildGetEntityForIdQuery( $entityId ) );
0 ignored issues
show
$this->buildGetEntityForIdQuery($entityId) is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
46
47 3
		return ( $document === null ) ? null : $this->documentBuilder->buildEntityForDocument( $document );
48
	}
49
50
	/**
51
	 * @see EntityDocumentLookup::getEntityDocumentsForIds
52
	 */
53 2
	public function getEntityDocumentsForIds( array $entityIds ) {
54 2
		$entities = [];
55
56 2
		foreach( $this->splitEntityIdsPerType( $entityIds ) as $type => $entityIdsForType ) {
57 2
			$entities = array_merge(
58 2
				$entities,
59 2
				$this->getEntitiesInCollection( $entityIdsForType, $type )
60 2
			);
61 2
		}
62
63 2
		return $entities;
64
	}
65
66 2
	private function splitEntityIdsPerType( array $entityIds ) {
67 2
		$entityIdsPerType = [];
68
69
		/** @var EntityId $entityId */
70 2
		foreach( $entityIds as $entityId ) {
71 2
			$entityIdsPerType[$entityId->getEntityType()][] = $entityId;
72 2
		}
73
74 2
		return $entityIdsPerType;
75
	}
76
77 2
	private function getEntitiesInCollection( array $entityIds, $collectionName ) {
78 2
		$documents = $this->database
79 2
			->selectCollection( $collectionName )
80 2
			->find( $this->buildGetEntitiesForIdsQuery( $entityIds ) );
0 ignored issues
show
$this->buildGetEntitiesForIdsQuery($entityIds) is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
81
82 2
		$entities = [];
83 2
		foreach( $documents as $document ) {
84 1
			$entities[] = $this->documentBuilder->buildEntityForDocument( $document );
85 2
		}
86 2
		return $entities;
87
	}
88
89
	/**
90
	 * @see EntityDocumentSaver::saveEntityDocument
91
	 */
92 2
	public function saveEntityDocument( EntityDocument $entityDocument ) {
93 2
		$this->database->selectCollection( $entityDocument->getType() )->upsert(
94 2
			$this->buildGetEntityForIdQuery( $entityDocument->getId() ),
0 ignored issues
show
It seems like $entityDocument->getId() can be null; however, buildGetEntityForIdQuery() 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...
$this->buildGetEntityFor...ntityDocument->getId()) is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
95 2
			$this->documentBuilder->buildDocumentForEntity( $entityDocument )
96 2
		);
97 2
	}
98
99 4
	private function buildGetEntityForIdQuery( EntityId $entityId ) {
100 4
		$expr = new Expr();
101 4
		return $expr->field( '_id' )->equals( $entityId->getSerialization() )->getQuery();
102
	}
103
104 2
	private function buildGetEntitiesForIdsQuery( array $entityIds ) {
105 2
		$expr = new Expr();
106 2
		return $expr->field( '_id' )->in( $this->serializeEntityIds( $entityIds ) )->getQuery();
107
	}
108
109 2
	private function serializeEntityIds( array $entityIds ) {
110 2
		$serializations = [];
111
112
		/** @var EntityId $entityId */
113 2
		foreach( $entityIds as $entityId ) {
114 2
			$serializations[] = $entityId->getSerialization();
115 2
		}
116
117 2
		return $serializations;
118
	}
119
}
120