Issues (43)

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.

MissingSubjectTripleNodeSimplifier.php (1 issue)

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
namespace PPP\Wikidata\TreeSimplifier;
4
5
use Ask\Language\Description\Conjunction;
6
use Ask\Language\Description\Disjunction;
7
use Ask\Language\Description\SomeProperty;
8
use Ask\Language\Description\ValueDescription;
9
use Ask\Language\Option\QueryOptions;
10
use InvalidArgumentException;
11
use PPP\DataModel\AbstractNode;
12
use PPP\DataModel\IntersectionNode;
13
use PPP\DataModel\MissingNode;
14
use PPP\DataModel\ResourceListNode;
15
use PPP\DataModel\TripleNode;
16
use PPP\DataModel\UnionNode;
17
use PPP\Module\TreeSimplifier\NodeSimplifier;
18
use PPP\Module\TreeSimplifier\NodeSimplifierException;
19
use PPP\Module\TreeSimplifier\NodeSimplifierFactory;
20
use PPP\Wikidata\ValueParsers\ResourceListNodeParser;
21
use PPP\Wikidata\WikibaseResourceNode;
22
use Wikibase\DataModel\Entity\EntityIdValue;
23
use Wikibase\EntityStore\EntityStore;
24
25
/**
26
 * Simplifies a triple node when the subject is missing.
27
 *
28
 * @licence AGPLv3+
29
 * @author Thomas Pellissier Tanon
30
 */
31
class MissingSubjectTripleNodeSimplifier implements NodeSimplifier {
32
33
	const QUERY_LIMIT = 50;
34
35
	/**
36
	 * @var NodeSimplifierFactory
37
	 */
38
	private $nodeSimplifierFactory;
39
40
	/**
41
	 * @var EntityStore
42
	 */
43
	private $entityStore;
44
45
	/**
46
	 * @var ResourceListNodeParser
47
	 */
48
	private $resourceListNodeParser;
49
50
	/**
51
	 * @param NodeSimplifierFactory $nodeSimplifierFactory
52
	 * @param EntityStore $entityStore
53
	 * @param ResourceListNodeParser $resourceListNodeParser
54
	 */
55 22
	public function __construct(NodeSimplifierFactory $nodeSimplifierFactory, EntityStore $entityStore, ResourceListNodeParser $resourceListNodeParser) {
56 22
		$this->nodeSimplifierFactory = $nodeSimplifierFactory;
57 22
		$this->entityStore = $entityStore;
58 22
		$this->resourceListNodeParser = $resourceListNodeParser;
59 22
	}
60
61
	/**
62
	 * @see AbstractNode::isSimplifierFor
63
	 */
64 7
	public function isSimplifierFor(AbstractNode $node) {
65 7
		return $this->isTripleWithMissingSubject($node) || $this->isTripleWithMissingSubjectOperator($node);
66
	}
67
68 7
	private function isTripleWithMissingSubject(AbstractNode $node) {
69 7
		return $node instanceof TripleNode &&
70 7
			$node->getSubject() instanceof MissingNode;
71
	}
72
73 5
	private function isTripleWithMissingSubjectOperator(AbstractNode $node) {
74 5
		if(!($node instanceof IntersectionNode || $node instanceof UnionNode)) {
75 4
			return false;
76
		}
77
78 2
		foreach($node->getOperands() as $operand) {
79 2
			if(!$this->isSimplifierFor($operand)) {
80 1
				return false;
81
			}
82 1
		}
83
84 1
		return true;
85
	}
86
87
	/**
88
	 * @see NodeSimplifier::doSimplification
89
	 */
90 14
	public function simplify(AbstractNode $node) {
91
		try {
92 14
			$query = $this->buildQueryForNode($node);
93 14
		} catch(EmptyQueryException $e) {
94 4
			return new ResourceListNode();
95
		}
96
97 6
		$entityIds = $this->entityStore->getItemIdForQueryLookup()->getItemIdsForQuery($query, new QueryOptions(self::QUERY_LIMIT, 0));
98
99 6
		return $this->formatQueryResult($entityIds);
100
	}
101
102 14
	private function buildQueryForNode(AbstractNode $node) {
103 14
		if($node instanceof UnionNode) {
104 5
			return $this->buildQueryForUnion($node);
105 13
		} else if($node instanceof IntersectionNode) {
106 3
			return $this->buildQueryForIntersection($node);
107 13
		} else if($node instanceof TripleNode) {
108 12
			return $this->buildQueryForTriple($node);
109
		} else {
110 1
			throw new InvalidArgumentException('Unsupported Node');
111
		}
112
	}
113
114 5
	private function buildQueryForUnion(UnionNode $unionNode) {
115 5
		$queries = array();
116
117 5
		foreach($unionNode->getOperands() as $operandNode) {
118
			try {
119 4
				$queries[] = $this->buildQueryForNode($operandNode);
120 4
			} catch(EmptyQueryException $e) {
121
				//May be ignored: we are in a union
122
			}
123 4
		}
124
125 4
		switch(count($queries)) {
126 4
			case 0:
127 2
				throw new EmptyQueryException();
128 2
			case 1:
129 1
				return reset($queries);
130 1
			default:
131 1
				return new Disjunction($queries);
132 1
		}
133
	}
134
135 3
	private function buildQueryForIntersection(IntersectionNode $intersectionNode) {
136 3
		$queries = array();
137
138 3
		foreach($intersectionNode->getOperands() as $operandNode) {
139 3
			$queries[] = $this->buildQueryForNode($operandNode);
140 2
		}
141
142 2
		switch(count($queries)) {
143 2
			case 1:
144 1
				return reset($queries);
145 1
			default:
146 1
				return new Conjunction($queries);
147 1
		}
148
	}
149
150 12
	private function buildQueryForTriple(TripleNode $triple) {
151 12
		if(!($triple->getSubject()->equals(new MissingNode()))) {
152 3
			throw new InvalidArgumentException('Triple whose subject is not missing given.');
153
		}
154
155 9
		$simplifier = $this->nodeSimplifierFactory->newNodeSimplifier();
156 9
		$predicate = $simplifier->simplify($triple->getPredicate());
157 9
		$object = $simplifier->simplify($triple->getObject());
158 9
		if(!($predicate instanceof ResourceListNode && $object instanceof ResourceListNode)) {
159
			throw new NodeSimplifierException('Invalid triple');
160
		}
161
162 9
		$propertyNodes = $this->resourceListNodeParser->parse($predicate, 'wikibase-property');
163 9
		$queryParameters = array();
164
165 9
		foreach($this->bagsPropertiesPerType($propertyNodes) as $objectType => $propertyNodes) {
166 7
			$objectNodes = $this->resourceListNodeParser->parse($object, $objectType);
167
168 7
			foreach($propertyNodes as $propertyNode) {
169
				try {
170 7
					$queryParameters[] = $this->buildQueryForProperty($propertyNode, $objectNodes);
171 7
				} catch(EmptyQueryException $e) {
172
					//May be ignored: we are in a union
173
				}
174 7
			}
175 9
		}
176
177 9
		switch(count($queryParameters)) {
178 9
			case 0:
179 3
				throw new EmptyQueryException();
180 6
			case 1:
181 4
				return reset($queryParameters);
182 2
			default:
183 2
				return new Disjunction($queryParameters);
184 2
		}
185
	}
186
187 9
	private function bagsPropertiesPerType($propertyNodes) {
188 9
		$propertyNodesPerType = array();
189
190
		/** @var WikibaseResourceNode $propertyNode */
191 9
		foreach($propertyNodes as $propertyNode) {
192 7
			$objectType = $this->entityStore->getPropertyLookup()->getPropertyForId(
193 7
				$propertyNode->getDataValue()->getEntityId()
194 7
			)->getDataTypeId();
195 7
			$propertyNodesPerType[$objectType][] = $propertyNode;
196 9
		}
197
198 9
		return $propertyNodesPerType;
199
	}
200
201 7
	private function buildQueryForProperty(WikibaseResourceNode $predicate, ResourceListNode $objectList) {
202 7
		return new SomeProperty(
203 7
			new EntityIdValue($predicate->getDataValue()->getEntityId()),
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface DataValues\DataValue as the method getEntityId() does only exist in the following implementations of said interface: Wikibase\DataModel\Entity\EntityIdValue.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
204 7
			$this->buildValueDescriptionsForObjects($objectList)
205 6
		);
206
	}
207
208 7
	private function buildValueDescriptionsForObjects(ResourceListNode $objectList) {
209 7
		$valueDescriptions = array();
210
211
		/** @var WikibaseResourceNode $object */
212 7
		foreach($objectList as $object) {
213 6
			$valueDescriptions[] = new ValueDescription($object->getDataValue());
214 7
		}
215
216 7
		switch(count($valueDescriptions)) {
217 7
			case 0:
218 1
				throw new EmptyQueryException();
219 6
			case 1:
220 5
				return reset($valueDescriptions);
221 1
			default:
222 1
				return new Disjunction($valueDescriptions);
223 1
		}
224
	}
225
226 6
	private function formatQueryResult(array $subjectIds) {
227 6
		$nodes = array();
228
229 6
		foreach($subjectIds as $subjectId) {
230 6
			$nodes[] = new WikibaseResourceNode('', new EntityIdValue($subjectId));
231 6
		}
232
233 6
		return new ResourceListNode($nodes);
234
	}
235
}
236