Issues (213)

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.

lib/TokenManager.php (4 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
 * @copyright Copyright (c) 2016 Lukas Reschke <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OCA\Richdocuments;
23
24
use OCA\Richdocuments\Db\WopiMapper;
25
use OCA\Richdocuments\Db\Wopi;
26
use OCA\Richdocuments\Service\CapabilitiesService;
27
use OCA\Richdocuments\WOPI\Parser;
28
use OCP\Files\File;
29
use OCP\Files\IRootFolder;
30
use OCP\Files\Node;
31
use OCP\IGroupManager;
32
use OCP\IURLGenerator;
33
use OCP\IUserManager;
34
use OCP\Share\IManager;
35
use OCP\IL10N;
36
use OCP\Util;
37
38
class TokenManager {
39
	/** @var IRootFolder */
40
	private $rootFolder;
41
	/** @var IManager */
42
	private $shareManager;
43
	/** @var IURLGenerator */
44
	private $urlGenerator;
45
	/** @var Parser */
46
	private $wopiParser;
47
	/** @var AppConfig */
48
	private $appConfig;
49
	/** @var string */
50
	private $userId;
51
	/** @var WopiMapper */
52
	private $wopiMapper;
53
	/** @var IL10N */
54
	private $trans;
55
	/** @var IUserManager */
56
	private $userManager;
57
	/** @var IGroupManager */
58
	private $groupManager;
59
	/** @var CapabilitiesService */
60
	private $capabilitiesService;
61
	/** @var Helper */
62
	private $helper;
63
64
	/**
65
	 * @param IRootFolder $rootFolder
66
	 * @param IManager $shareManager
67
	 * @param IURLGenerator $urlGenerator
68
	 * @param Parser $wopiParser
69
	 * @param AppConfig $appConfig
70
	 * @param string $UserId
71
	 * @param WopiMapper $wopiMapper
72
	 * @param IL10N $trans
73
	 */
74 View Code Duplication
	public function __construct(
75
		IRootFolder $rootFolder,
76
		IManager $shareManager,
77
		IURLGenerator $urlGenerator,
78
		Parser $wopiParser,
79
		CapabilitiesService $capabilitiesService,
80
		AppConfig $appConfig,
81
		$UserId,
82
		WopiMapper $wopiMapper,
83
		IL10N $trans,
84
		IUserManager $userManager,
85
		IGroupManager $groupManager,
86
		Helper $helper
87
	) {
88
		$this->rootFolder = $rootFolder;
89
		$this->shareManager = $shareManager;
90
		$this->urlGenerator = $urlGenerator;
91
		$this->wopiParser = $wopiParser;
92
		$this->capabilitiesService = $capabilitiesService;
93
		$this->appConfig = $appConfig;
94
		$this->trans = $trans;
95
		$this->userId = $UserId;
96
		$this->wopiMapper = $wopiMapper;
97
		$this->userManager = $userManager;
98
		$this->groupManager = $groupManager;
99
		$this->helper = $helper;
100
	}
101
102
	/**
103
	 * @param string $fileId
104
	 * @param string $shareToken
105
	 * @param string $editoruid
106
	 * @return array
107
	 * @throws \Exception
108
	 */
109
	public function getToken($fileId, $shareToken = null, $editoruid = null, $direct = false, $isRemoteToken = false) {
110
		list($fileId, , $version) = Helper::parseFileId($fileId);
111
		$owneruid = null;
112
		$hideDownload = false;
113
		// if the user is not logged-in do use the sharers storage
114
		if ($shareToken !== null) {
115
			/** @var File $file */
116
			$rootFolder = $this->rootFolder;
117
			$share = $this->shareManager->getShareByToken($shareToken);
118
			$updatable = (bool)($share->getPermissions() & \OCP\Constants::PERMISSION_UPDATE);
119
			$hideDownload = $share->getHideDownload();
120
			$owneruid = $share->getShareOwner();
121
		} else if ($this->userId !== null) {
122
			try {
123
				$editoruid = $this->userId;
124
				$rootFolder = $this->rootFolder->getUserFolder($editoruid);
125
126
				$files = $rootFolder->getById((int)$fileId);
127
				$updatable = false;
128
				foreach ($files as $file) {
129
					if ($file->isUpdateable()) {
130
						$updatable = true;
131
						break;
132
					}
133
				}
134
135
				// Check if the editor (user who is accessing) is in editable group
136
				// UserCanWrite only if
137
				// 1. No edit groups are set or
138
				// 2. if they are set, it is in one of the edit groups
139
				$editGroups = array_filter(explode('|', $this->appConfig->getAppValue('edit_groups')));
140
				$editorUser = $this->userManager->get($editoruid);
141 View Code Duplication
				if ($updatable && count($editGroups) > 0 && $editorUser) {
142
					$updatable = false;
143
					foreach ($editGroups as $editGroup) {
144
						$editorGroup = $this->groupManager->get($editGroup);
145
						if ($editorGroup !== null && $editorGroup->inGroup($editorUser)) {
146
							$updatable = true;
147
							break;
148
						}
149
					}
150
				}
151
			} catch (\Exception $e) {
152
				throw $e;
153
			}
154
		} else {
155
			$rootFolder = $this->rootFolder;
156
			// no active user login while generating the token
157
			// this is required during WopiPutRelativeFile
158
			if (is_null($editoruid)) {
159
				\OC::$server->getLogger()->warning('Generating token for SaveAs without editoruid');
160
				$updatable = true;
161
			} else {
162
				// Make sure we use the user folder if available since fetching all files by id from the root might be expensive
163
				$rootFolder = $this->rootFolder->getUserFolder($editoruid);
164
165
				$updatable = false;
166
				$files = $rootFolder->getById($fileId);
167
168
				foreach ($files as $file) {
169
					if ($file->isUpdateable()) {
170
						$updatable = true;
171
						break;
172
					}
173
				}
174
			}
175
176
		}
177
		/** @var File $file */
178
		$file = $rootFolder->getById($fileId)[0];
179
		// If its a public share, use the owner from the share, otherwise check the file object
180
		if (is_null($owneruid)) {
181
			$owner = $file->getOwner();
182
			if (is_null($owner)) {
183
				// Editor UID instead of owner UID in case owner is null e.g. group folders
184
				$owneruid = $editoruid;
185
			} else {
186
				$owneruid = $owner->getUID();
187
			}
188
		}
189
190
		// force read operation to trigger possible audit logging
191
		$fp = $file->fopen('r');
192
		fclose($fp);
193
194
		$serverHost = $this->urlGenerator->getAbsoluteURL('/');//$this->request->getServerProtocol() . '://' . $this->request->getServerHost();
195
196
		$guest_name = null;
197
		if ($this->userId === null) {
198
			if ($guest_name = $this->helper->getGuestName()) {
199
				$guest_name = $this->trans->t('%s (Guest)', Util::sanitizeHTML($guest_name));
200
				$cut = 56;
201
				while (mb_strlen($guest_name) >= 64) {
202
					$guest_name = $this->trans->t('%s (Guest)', Util::sanitizeHTML(
203
						mb_substr($guest_name, 0, $cut)
204
					));
205
					$cut -= 5;
206
				}
207
			} else {
208
				$guest_name = $this->trans->t('Anonymous guest');
209
			}
210
		}
211
212
		$wopi = $this->wopiMapper->generateFileToken($fileId, $owneruid, $editoruid, $version, $updatable, $serverHost, $guest_name, 0, $hideDownload, $direct, $isRemoteToken, 0, $shareToken);
213
214
		try {
215
216
			return [
217
				$this->wopiParser->getUrlSrc($file->getMimeType())['urlsrc'], // url src might not be found ehre
218
				$wopi->getToken(),
219
				$wopi
220
			];
221
		} catch (\Exception $e) {
222
			throw $e;
223
		}
224
	}
225
226
	public function updateToRemoteToken(Wopi $wopi, $shareToken, $remoteServer, $remoteServerToken, $remoteWopi) {
227
		$uid = $remoteWopi['editorUid'] . '@' . $remoteServer;
228
		$wopi->setEditorUid($shareToken);
229
		$wopi->setCanwrite($wopi->getCanwrite() && $remoteWopi['canwrite']);
230
		$wopi->setRemoteServer($remoteServer);
231
		$wopi->setRemoteServerToken($remoteServerToken);
232
		$wopi->setGuestDisplayname($uid);
233
		$this->wopiMapper->update($wopi);
234
		return $wopi;
235
	}
236
237
	public function getTokenForTemplate(File $templateFile, $userId, $targetFileId, $direct = false) {
238
		$owneruid = $userId;
239
		$editoruid = $userId;
240
		$rootFolder = $this->rootFolder->getUserFolder($editoruid);
241
		/** @var File $targetFile */
242
		$targetFile = $rootFolder->getById($targetFileId);
243
		$targetFile = $targetFile[0] ?? null;
244
		if (!$targetFile) {
245
			// TODO: Exception
246
			return null;
247
		}
248
		$updatable = $targetFile->isUpdateable();
249
		// Check if the editor (user who is accessing) is in editable group
250
		// UserCanWrite only if
251
		// 1. No edit groups are set or
252
		// 2. if they are set, it is in one of the edit groups
253
		$editGroups = array_filter(explode('|', $this->appConfig->getAppValue('edit_groups')));
254
		$editorUser = $this->userManager->get($editoruid);
255 View Code Duplication
		if ($updatable && count($editGroups) > 0 && $editorUser) {
256
			$updatable = false;
257
			foreach($editGroups as $editGroup) {
258
				$editorGroup = $this->groupManager->get($editGroup);
259
				if ($editorGroup !== null && $editorGroup->inGroup($editorUser)) {
260
					$updatable = true;
261
					break;
262
				}
263
			}
264
		}
265
266
		$serverHost = $this->urlGenerator->getAbsoluteURL('/');
267
268
		if ($this->capabilitiesService->hasTemplateSource()) {
269
			$wopi = $this->wopiMapper->generateFileToken($targetFile->getId(), $owneruid, $editoruid, 0, $updatable, $serverHost, null, 0, false, $direct, false, $templateFile->getId());
270
		} else {
271
			// Legacy way of creating new documents from a template
272
			$wopi = $this->wopiMapper->generateFileToken($templateFile->getId(), $owneruid, $editoruid, 0, $updatable, $serverHost, null, $targetFile->getId(), $direct);
273
		}
274
275
		return [
276
			$this->wopiParser->getUrlSrc($templateFile->getMimeType())['urlsrc'],
277
			$wopi
278
		];
279
	}
280
281
	/**
282
	 * @param Node $node
283
	 * @return Wopi
284
	 */
285 View Code Duplication
	public function getRemoteToken(Node $node) {
286
		list($urlSrc, $token, $wopi) = $this->getToken($node->getId(), null, null, false, true);
0 ignored issues
show
The assignment to $urlSrc is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
The assignment to $token is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
287
		$wopi->setIsRemoteToken(true);
288
		$wopi->setRemoteServer($node->getStorage()->getRemote());
289
290
		$this->wopiMapper->update($wopi);
291
		return $wopi;
292
	}
293
294
	/**
295
	 * @param Node $node
296
	 * @return Wopi
297
	 */
298 View Code Duplication
	public function getRemoteTokenFromDirect(Node $node, $editorUid) {
299
		list($urlSrc, $token, $wopi) = $this->getToken($node->getId(), null, $editorUid, true, true);
0 ignored issues
show
The assignment to $urlSrc is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
The assignment to $token is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
300
		$wopi->setIsRemoteToken(true);
301
		$wopi->setRemoteServer($node->getStorage()->getRemote());
302
303
		$this->wopiMapper->update($wopi);
304
		return $wopi;
305
	}
306
307
308
}
309