Issues (1401)

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.

repo/includes/Rdf/FullStatementRdfBuilder.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 Wikibase\Repo\Rdf;
4
5
use Wikibase\DataModel\Entity\EntityDocument;
6
use Wikibase\DataModel\Entity\EntityId;
7
use Wikibase\DataModel\Reference;
8
use Wikibase\DataModel\Statement\Statement;
9
use Wikibase\DataModel\Statement\StatementList;
10
use Wikibase\DataModel\Statement\StatementListProvider;
11
use Wikimedia\Purtle\RdfWriter;
12
13
/**
14
 * Fully reified RDF mapping for wikibase statements, including deprecated and non-"best"
15
 * statements, ranks, qualifiers, and references. This modells statements as identifiable objects
16
 * and does not output a direct property to value mapping as the TruthyStatementRdfBuilder does. If
17
 * both forms (direct and full) are desired, use TruthyStatementRdfBuilder in addition to
18
 * FullStatementRdfBuilder.
19
 *
20
 * @see TruthyStatementRdfBuilder
21
 *
22
 * @license GPL-2.0-or-later
23
 * @author Daniel Kinzler
24
 * @author Stas Malyshev
25
 */
26
class FullStatementRdfBuilder implements EntityRdfBuilder {
27
28
	/**
29
	 * @var DedupeBag
30
	 */
31
	private $dedupeBag;
32
33
	/**
34
	 * @var bool
35
	 */
36
	private $produceQualifiers = true;
37
38
	/**
39
	 * @var bool
40
	 */
41
	private $produceReferences = true;
42
43
	/**
44
	 * @var RdfVocabulary
45
	 */
46
	private $vocabulary;
47
48
	/**
49
	 * @var RdfWriter
50
	 */
51
	private $statementWriter;
52
53
	/**
54
	 * @var RdfWriter
55
	 */
56
	private $referenceWriter;
57
58
	/**
59
	 * @var SnakRdfBuilder
60
	 */
61
	private $snakBuilder;
62
63
	public function __construct( RdfVocabulary $vocabulary, RdfWriter $writer, SnakRdfBuilder $snakBuilder ) {
64
		$this->vocabulary = $vocabulary;
65
66
		// Note: since we process references as nested structures, they need a separate
67
		// rdf writer, so outputting references doesn't destroy the state of the statement writer.
68
		$this->statementWriter = $writer;
69
		$this->referenceWriter = $writer->sub();
70
71
		$this->snakBuilder = $snakBuilder;
72
73
		$this->dedupeBag = new NullDedupeBag();
74
	}
75
76
	public function setDedupeBag( DedupeBag $dedupeBag ) {
77
		$this->dedupeBag = $dedupeBag;
78
	}
79
80
	/**
81
	 * @return boolean
82
	 */
83
	public function getProduceQualifiers() {
84
		return $this->produceQualifiers;
85
	}
86
87
	/**
88
	 * @param boolean $produceQualifiers
89
	 */
90
	public function setProduceQualifiers( $produceQualifiers ) {
91
		$this->produceQualifiers = $produceQualifiers;
92
	}
93
94
	/**
95
	 * @return boolean
96
	 */
97
	public function getProduceReferences() {
98
		return $this->produceReferences;
99
	}
100
101
	/**
102
	 * @param boolean $produceReferences
103
	 */
104
	public function setProduceReferences( $produceReferences ) {
105
		$this->produceReferences = $produceReferences;
106
	}
107
108
	/**
109
	 * Adds Statements to the RDF graph.
110
	 *
111
	 * @param EntityId $entityId
112
	 * @param StatementList $statementList
113
	 */
114
	public function addStatements( EntityId $entityId, StatementList $statementList ) {
115
		$bestList = [];
116
117
		// FIXME: This is expensive, share the result with TruthyStatementRdfBuilder!
118
		foreach ( $statementList->getPropertyIds() as $propertyId ) {
119
			$bestStatements = $statementList->getByPropertyId( $propertyId )->getBestStatements();
120
			foreach ( $bestStatements->toArray() as $statement ) {
121
				$bestList[$statement->getGuid()] = true;
122
			}
123
		}
124
125
		foreach ( $statementList->toArray() as $statement ) {
126
			$this->addStatement( $entityId, $statement, isset( $bestList[$statement->getGuid()] ) );
127
		}
128
	}
129
130
	/**
131
	 * Adds the given Statement from the given Entity to the RDF graph.
132
	 *
133
	 * @param EntityId $entityId
134
	 * @param Statement $statement
135
	 * @param bool $isBest Is this best ranked statement?
136
	 */
137
	private function addStatement( EntityId $entityId, Statement $statement, $isBest ) {
138
		$statementLName = $this->vocabulary->getStatementLName( $statement );
139
140
		$entityRepository = $this->vocabulary->getEntityRepositoryName( $entityId );
141
142
		$this->addMainSnak( $entityId, $statementLName, $statement, $isBest );
143
144
		// XXX: separate builder for qualifiers?
145
		if ( $this->produceQualifiers ) {
146
			// this assumes statement was added by addMainSnak
147
			foreach ( $statement->getQualifiers() as $q ) {
148
				$propertyRepository = $this->vocabulary->getEntityRepositoryName( $q->getPropertyId() );
149
				$this->snakBuilder->addSnak(
150
					$this->statementWriter,
151
					$this->vocabulary->statementNamespaceNames[$entityRepository][RdfVocabulary::NS_VALUE],
152
					$q,
153
					$this->vocabulary->propertyNamespaceNames[$propertyRepository][RdfVocabulary::NSP_QUALIFIER],
154
					$statementLName
155
				);
156
			}
157
		}
158
159
		// XXX: separate builder for references?
160
		if ( $this->produceReferences ) {
161
			$entityRepository = $this->vocabulary->getEntityRepositoryName( $entityId );
162
			/** @var Reference $reference */
163
			foreach ( $statement->getReferences() as $reference ) { //FIXME: split body into separate method
164
				$hash = $reference->getSnaks()->getHash();
165
				$refLName = $hash;
166
167
				$this->statementWriter->about(
168
					$this->vocabulary->statementNamespaceNames[$entityRepository][RdfVocabulary::NS_STATEMENT],
169
					$statementLName
170
				)
171
					->say( RdfVocabulary::NS_PROV, 'wasDerivedFrom' )->is(
172
						$this->vocabulary->statementNamespaceNames[$entityRepository][RdfVocabulary::NS_REFERENCE],
173
						$refLName
174
					);
175
				if ( $this->dedupeBag->alreadySeen( $hash, 'R' ) !== false ) {
176
					continue;
177
				}
178
179
				$this->referenceWriter->about(
180
					$this->vocabulary->statementNamespaceNames[$entityRepository][RdfVocabulary::NS_REFERENCE],
181
					$refLName
182
				)
183
					->a( RdfVocabulary::NS_ONTOLOGY, 'Reference' );
184
185
				foreach ( $reference->getSnaks() as $refSnak ) {
186
					$propertyRepository = $this->vocabulary->getEntityRepositoryName( $refSnak->getPropertyId() );
187
					$this->snakBuilder->addSnak(
188
						$this->referenceWriter,
189
						$this->vocabulary->statementNamespaceNames[$entityRepository][RdfVocabulary::NS_VALUE],
190
						$refSnak,
191
						$this->vocabulary->propertyNamespaceNames[$propertyRepository][RdfVocabulary::NSP_REFERENCE],
192
						$refLName
193
					);
194
				}
195
			}
196
		}
197
	}
198
199
	/**
200
	 * Adds the given Statement's main Snak to the RDF graph.
201
	 *
202
	 * @param EntityId $entityId
203
	 * @param string $statementLName
204
	 * @param Statement $statement
205
	 * @param bool $isBest Is this best ranked statement?
206
	 */
207
	private function addMainSnak( EntityId $entityId, $statementLName, Statement $statement, $isBest ) {
208
		$snak = $statement->getMainSnak();
209
210
		$entityLName = $this->vocabulary->getEntityLName( $entityId );
211
		$entityRepository = $this->vocabulary->getEntityRepositoryName( $entityId );
212
		$propertyId = $snak->getPropertyId();
213
		$propertyLName = $this->vocabulary->getEntityLName( $propertyId );
214
		$propertyRepository = $this->vocabulary->getEntityRepositoryName( $propertyId );
215
216
		$this->statementWriter->about(
217
			$this->vocabulary->entityNamespaceNames[$entityRepository],
218
			$entityLName
219
		)
220
			->say(
221
				$this->vocabulary->propertyNamespaceNames[$propertyRepository][RdfVocabulary::NSP_CLAIM],
222
				$propertyLName
223
			)
224
			->is( $this->vocabulary->statementNamespaceNames[$entityRepository][RdfVocabulary::NS_STATEMENT], $statementLName );
225
226
		$this->statementWriter->about(
227
			$this->vocabulary->statementNamespaceNames[$entityRepository][RdfVocabulary::NS_STATEMENT],
228
			$statementLName
229
		)
230
			->a( RdfVocabulary::NS_ONTOLOGY, 'Statement' );
231
232
		$rank = $statement->getRank();
233
		if ( isset( RdfVocabulary::$rankMap[$rank] ) ) {
234
			if ( $isBest ) {
235
				$this->statementWriter->a( RdfVocabulary::NS_ONTOLOGY, RdfVocabulary::WIKIBASE_RANK_BEST );
236
			}
237
			$this->statementWriter->about(
238
				$this->vocabulary->statementNamespaceNames[$entityRepository][RdfVocabulary::NS_STATEMENT],
239
				$statementLName
240
			)
241
				->say( RdfVocabulary::NS_ONTOLOGY, 'rank' )->is( RdfVocabulary::NS_ONTOLOGY, RdfVocabulary::$rankMap[$rank] );
242
		} else {
243
			wfLogWarning( "Unknown rank $rank encountered for $entityId:{$statement->getGuid()}" );
244
		}
245
246
		$this->snakBuilder->addSnak(
247
			$this->statementWriter,
248
			$this->vocabulary->statementNamespaceNames[$entityRepository][RdfVocabulary::NS_VALUE],
249
			$snak,
250
			$this->vocabulary->propertyNamespaceNames[$propertyRepository][RdfVocabulary::NSP_CLAIM_STATEMENT],
251
			$statementLName
252
		);
253
	}
254
255
	/**
256
	 * Add fully reified statements for the given entity to the RDF graph.
257
	 * This may include qualifiers and references, depending on calls to
258
	 * setProduceQualifiers() resp. setProduceReferences().
259
	 *
260
	 * @param EntityDocument $entity the entity to output.
261
	 */
262
	public function addEntity( EntityDocument $entity ) {
263
		$entityId = $entity->getId();
264
265
		if ( $entity instanceof StatementListProvider ) {
266
			$this->addStatements( $entityId, $entity->getStatements() );
0 ignored issues
show
It seems like $entityId defined by $entity->getId() on line 263 can be null; however, Wikibase\Repo\Rdf\FullSt...uilder::addStatements() 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...
267
		}
268
	}
269
270
	/**
271
	 * Does nothing, since Statements should not be part of entity stubs.
272
	 *
273
	 * @see EntityRdfBuilder::addEntityStub
274
	 *
275
	 * @param EntityDocument $entity the entity to output.
276
	 */
277
	public function addEntityStub( EntityDocument $entity ) {
278
		// noop
279
	}
280
281
}
282