Issues (1798)

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.

apps/files_sharing/lib/Migration.php (3 issues)

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
 * @author Björn Schießle <[email protected]>
4
 * @author Morris Jobke <[email protected]>
5
 * @author Roeland Jago Douma <[email protected]>
6
 *
7
 * @copyright Copyright (c) 2018, ownCloud GmbH
8
 * @license AGPL-3.0
9
 *
10
 * This code is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License, version 3,
12
 * as published by the Free Software Foundation.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License, version 3,
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
21
 *
22
 */
23
24
namespace OCA\Files_Sharing;
25
26
use Doctrine\DBAL\Connection;
27
use OC\Cache\CappedMemoryCache;
28
use OCP\IDBConnection;
29
30
/**
31
 * Class Migration
32
 *
33
 * @package OCA\Files_Sharing
34
 * @group DB
35
 */
36
class Migration {
37
38
	/** @var IDBConnection */
39
	private $connection;
40
41
	/** @var  array with all shares we already saw */
42
	private $shareCache;
43
44
	/** @var string */
45
	private $table = 'share';
46
47
	public function __construct(IDBConnection $connection) {
48
		$this->connection = $connection;
49
50
		// We cache up to 10k share items (~20MB)
51
		$this->shareCache = new CappedMemoryCache(10000);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \OC\Cache\CappedMemoryCache(10000) of type object<OC\Cache\CappedMemoryCache> is incompatible with the declared type array of property $shareCache.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
52
	}
53
54
	/**
55
	 * move all re-shares to the owner in order to have a flat list of shares
56
	 * upgrade from oC 8.2 to 9.0 with the new sharing
57
	 */
58
	public function removeReShares() {
59
		$stmt = $this->getReShares();
60
61
		$owners = [];
62
		while ($share = $stmt->fetch()) {
63
			$this->shareCache[$share['id']] = $share;
64
65
			$owners[$share['id']] = [
66
					'owner' => $this->findOwner($share),
67
					'initiator' => $share['uid_owner'],
68
					'type' => $share['share_type'],
69
			];
70
71
			if (\count($owners) === 1000) {
72
				$this->updateOwners($owners);
73
				$owners = [];
74
			}
75
		}
76
77
		$stmt->closeCursor();
78
79
		if (\count($owners)) {
80
			$this->updateOwners($owners);
81
		}
82
	}
83
84
	/**
85
	 * update all owner information so that all shares have an owner
86
	 * and an initiator for the upgrade from oC 8.2 to 9.0 with the new sharing
87
	 */
88
	public function updateInitiatorInfo() {
89
		while (true) {
90
			$shares = $this->getMissingInitiator(1000);
91
92
			if (empty($shares)) {
93
				break;
94
			}
95
96
			$owners = [];
97
			foreach ($shares as $share) {
98
				$owners[$share['id']] = [
99
					'owner' => $share['uid_owner'],
100
					'initiator' => $share['uid_owner'],
101
					'type' => $share['share_type'],
102
				];
103
			}
104
			$this->updateOwners($owners);
105
		}
106
	}
107
108
	/**
109
	 * find the owner of a re-shared file/folder
110
	 *
111
	 * @param array $share
112
	 * @return array
113
	 */
114
	private function findOwner($share) {
115
		$currentShare = $share;
116
		while ($currentShare['parent'] !== null) {
117
			if (isset($this->shareCache[$currentShare['parent']])) {
118
				$currentShare = $this->shareCache[$currentShare['parent']];
119
			} else {
120
				$currentShare = $this->getShare((int)$currentShare['parent']);
121
				$this->shareCache[$currentShare['id']] = $currentShare;
122
			}
123
		}
124
125
		return $currentShare['uid_owner'];
126
	}
127
128
	/**
129
	 * Get $n re-shares from the database
130
	 *
131
	 * @param int $n The max number of shares to fetch
0 ignored issues
show
There is no parameter named $n. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
132
	 * @return \Doctrine\DBAL\Driver\Statement
133
	 */
134
	private function getReShares() {
135
		$query = $this->connection->getQueryBuilder();
136
		$query->select(['id', 'parent', 'uid_owner', 'share_type'])
137
			->from($this->table)
138
			->where($query->expr()->in(
139
				'share_type',
140
				$query->createNamedParameter(
141
					[
142
						\OCP\Share::SHARE_TYPE_USER,
143
						\OCP\Share::SHARE_TYPE_GROUP,
144
						\OCP\Share::SHARE_TYPE_LINK,
145
						\OCP\Share::SHARE_TYPE_REMOTE,
146
					],
147
					Connection::PARAM_INT_ARRAY
148
				)
149
			))
150
			->andWhere($query->expr()->in(
151
				'item_type',
152
				$query->createNamedParameter(
153
					['file', 'folder'],
154
					Connection::PARAM_STR_ARRAY
155
				)
156
			))
157
			->andWhere($query->expr()->isNotNull('parent'))
158
			->orderBy('id', 'asc');
159
		return $query->execute();
0 ignored issues
show
Bug Compatibility introduced by
The expression $query->execute(); of type Doctrine\DBAL\Driver\Statement|integer adds the type integer to the return on line 159 which is incompatible with the return type documented by OCA\Files_Sharing\Migration::getReShares of type Doctrine\DBAL\Driver\Statement.
Loading history...
160
	}
161
162
	/**
163
	 * Get $n re-shares from the database
164
	 *
165
	 * @param int $n The max number of shares to fetch
166
	 * @return array
167
	 */
168
	private function getMissingInitiator($n = 1000) {
169
		$query = $this->connection->getQueryBuilder();
170
		$query->select(['id', 'uid_owner', 'share_type'])
171
			->from($this->table)
172
			->where($query->expr()->in(
173
				'share_type',
174
				$query->createNamedParameter(
175
					[
176
						\OCP\Share::SHARE_TYPE_USER,
177
						\OCP\Share::SHARE_TYPE_GROUP,
178
						\OCP\Share::SHARE_TYPE_LINK,
179
						\OCP\Share::SHARE_TYPE_REMOTE,
180
					],
181
					Connection::PARAM_INT_ARRAY
182
				)
183
			))
184
			->andWhere($query->expr()->in(
185
				'item_type',
186
				$query->createNamedParameter(
187
					['file', 'folder'],
188
					Connection::PARAM_STR_ARRAY
189
				)
190
			))
191
			->andWhere($query->expr()->isNull('uid_initiator'))
192
			->orderBy('id', 'asc')
193
			->setMaxResults($n);
194
		$result = $query->execute();
195
		$shares = $result->fetchAll();
196
		$result->closeCursor();
197
198
		$ordered = [];
199
		foreach ($shares as $share) {
200
			$ordered[(int)$share['id']] = $share;
201
		}
202
203
		return $ordered;
204
	}
205
206
	/**
207
	 * get a specific share
208
	 *
209
	 * @param int $id
210
	 * @return array
211
	 */
212
	private function getShare($id) {
213
		$query = $this->connection->getQueryBuilder();
214
		$query->select(['id', 'parent', 'uid_owner'])
215
			->from($this->table)
216
			->where($query->expr()->eq('id', $query->createNamedParameter($id)));
217
		$result = $query->execute();
218
		$share = $result->fetchAll();
219
		$result->closeCursor();
220
221
		return $share[0];
222
	}
223
224
	/**
225
	 * update database with the new owners
226
	 *
227
	 * @param array $owners
228
	 * @throws \Exception
229
	 */
230
	private function updateOwners($owners) {
231
		$this->connection->beginTransaction();
232
233
		try {
234
			foreach ($owners as $id => $owner) {
235
				$query = $this->connection->getQueryBuilder();
236
				$query->update($this->table)
237
					->set('uid_owner', $query->createNamedParameter($owner['owner']))
238
					->set('uid_initiator', $query->createNamedParameter($owner['initiator']));
239
240
				if ((int)$owner['type'] !== \OCP\Share::SHARE_TYPE_LINK) {
241
					$query->set('parent', $query->createNamedParameter(null));
242
				}
243
244
				$query->where($query->expr()->eq('id', $query->createNamedParameter($id)));
245
246
				$query->execute();
247
			}
248
249
			$this->connection->commit();
250
		} catch (\Exception $e) {
251
			$this->connection->rollBack();
252
			throw $e;
253
		}
254
	}
255
}
256