Passed
Pull Request — master (#188)
by Blizzz
100:59 queued 89:55
created

Operation::isCreatingSkeletonFiles()   A

Complexity

Conditions 6
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 6
eloc 7
c 3
b 0
f 0
nc 3
nop 0
dl 0
loc 12
ccs 0
cts 4
cp 0
crap 42
rs 9.2222
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Morris Jobke <[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\FilesAccessControl;
23
24
use Exception;
25
use OCA\WorkflowEngine\Entity\File;
0 ignored issues
show
Bug introduced by
The type OCA\WorkflowEngine\Entity\File was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
use OCP\EventDispatcher\Event;
27
use OCP\Files\ForbiddenException;
28
use OCP\Files\Storage\IStorage;
29
use OCP\IL10N;
30
use OCP\IURLGenerator;
31
use OCP\WorkflowEngine\IComplexOperation;
32
use OCP\WorkflowEngine\IManager;
33
use OCP\WorkflowEngine\IRuleMatcher;
34
use OCP\WorkflowEngine\ISpecificOperation;
35
use ReflectionClass;
36
use UnexpectedValueException;
37
38
class Operation implements IComplexOperation, ISpecificOperation {
39
	/** @var IManager */
40
	protected $manager;
41
42
	/** @var IL10N */
43
	protected $l;
44
45
	/** @var IURLGenerator */
46
	protected $urlGenerator;
47
48
	/** @var int */
49
	protected $nestingLevel = 0;
50
51
	/**
52
	 * @param IManager $manager
53
	 * @param IL10N $l
54
	 */
55
	public function __construct(IManager $manager, IL10N $l, IURLGenerator $urlGenerator) {
56
		$this->manager = $manager;
57
		$this->l = $l;
58
		$this->urlGenerator = $urlGenerator;
59
	}
60
61
	/**
62
	 * @param IStorage $storage
63
	 * @param string $path
64
	 * @param bool $isDir
65
	 * @throws ForbiddenException
66
	 */
67
	public function checkFileAccess(IStorage $storage, string $path, bool $isDir = false): void {
68
		if (!$this->isBlockablePath($storage, $path) || $this->isCreatingSkeletonFiles() || $this->nestingLevel !== 0) {
69
			// Allow creating skeletons and theming
70
			// https://github.com/nextcloud/files_accesscontrol/issues/5
71
			// https://github.com/nextcloud/files_accesscontrol/issues/12
72
			return;
73
		}
74
75
		$this->nestingLevel++;
76
77
		$filePath = $this->translatePath($storage, $path);
78
		$ruleMatcher = $this->manager->getRuleMatcher();
79
		$ruleMatcher->setFileInfo($storage, $filePath, $isDir);
80
		$ruleMatcher->setOperation($this);
81
		$match = $ruleMatcher->getFlows();
82
83
		$this->nestingLevel--;
84
85
		if (!empty($match)) {
86
			// All Checks of one operation matched: prevent access
87
			throw new ForbiddenException('Access denied', false);
88
		}
89
	}
90
91
	protected function isBlockablePath(IStorage $storage, string $path): bool {
92
		if (property_exists($storage, 'mountPoint')) {
93
			$hasMountPoint = $storage instanceof StorageWrapper;
94
			if (!$hasMountPoint) {
95
				$ref = new ReflectionClass($storage);
96
				$prop = $ref->getProperty('mountPoint');
97
				$hasMountPoint = $prop->isPublic();
98
			}
99
100
			if ($hasMountPoint) {
101
				/** @var StorageWrapper $storage */
102
				$fullPath = $storage->mountPoint . ltrim($path, '/');
103
			} else {
104
				$fullPath = $path;
105
			}
106
		} else {
107
			$fullPath = $path;
108
		}
109
110
		if (substr_count($fullPath, '/') < 3) {
111
			return false;
112
		}
113
114
		// '', admin, 'files', 'path/to/file.txt'
115
		$segment = explode('/', $fullPath, 4);
116
117
		return isset($segment[2]) && in_array($segment[2], [
118
			'files',
119
			'thumbnails',
120
			'files_versions',
121
		]);
122
	}
123
124
	/**
125
	 * For thumbnails and versions we want to check the tags of the original file
126
	 */
127
	protected function translatePath(IStorage $storage, string $path): string {
128
		if (substr_count($path, '/') < 1) {
129
			return $path;
130
		}
131
132
		// 'files', 'path/to/file.txt'
133
		[$folder, $innerPath] = explode('/', $path, 2);
134
135
		if ($folder === 'files_versions') {
136
			$innerPath = substr($innerPath, 0, strrpos($innerPath, '.v'));
137
			return 'files/' . $innerPath;
138
		} elseif ($folder === 'thumbnails') {
139
			[$fileId,] = explode('/', $innerPath, 2);
140
			$innerPath = $storage->getCache()->getPathById($fileId);
0 ignored issues
show
Bug introduced by
$fileId of type string is incompatible with the type integer expected by parameter $id of OCP\Files\Cache\ICache::getPathById(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

140
			$innerPath = $storage->getCache()->getPathById(/** @scrutinizer ignore-type */ $fileId);
Loading history...
141
142
			if ($innerPath !== null) {
143
				return 'files/' . $innerPath;
144
			}
145
		}
146
147
		return $path;
148
	}
149
150
	/**
151
	 * Check if we are in the LoginController and if so, ignore the firewall
152
	 */
153
	protected function isCreatingSkeletonFiles(): bool {
154
		$exception = new Exception();
155
		$trace = $exception->getTrace();
156
157
		foreach ($trace as $step) {
158
			if (isset($step['class']) && $step['class'] === 'OC\Core\Controller\LoginController' &&
159
				isset($step['function']) && $step['function'] === 'tryLogin') {
160
				return true;
161
			}
162
		}
163
164
		return false;
165
	}
166
167
	/**
168
	 * @param string $name
169
	 * @param array[] $checks
170
	 * @param string $operation
171
	 * @throws UnexpectedValueException
172
	 */
173
	public function validateOperation(string $name, array $checks, string $operation): void {
174
		if (empty($checks)) {
175
			throw new UnexpectedValueException($this->l->t('No rule given'));
176
		}
177
	}
178
179
	/**
180
	 * returns a translated name to be presented in the web interface
181
	 *
182
	 * Example: "Automated tagging" (en), "Aŭtomata etikedado" (eo)
183
	 *
184
	 * @since 18.0.0
185
	 */
186
	public function getDisplayName(): string {
187
		return $this->l->t('Block access to a file');
188
	}
189
190
	/**
191
	 * returns a translated, descriptive text to be presented in the web interface.
192
	 *
193
	 * It should be short and precise.
194
	 *
195
	 * Example: "Tag based automatic deletion of files after a given time." (en)
196
	 *
197
	 * @since 18.0.0
198
	 */
199
	public function getDescription(): string {
200
		return '';
201
	}
202
203
	/**
204
	 * returns the URL to the icon of the operator for display in the web interface.
205
	 *
206
	 * Usually, the implementation would utilize the `imagePath()` method of the
207
	 * `\OCP\IURLGenerator` instance and simply return its result.
208
	 *
209
	 * Example implementation: return $this->urlGenerator->imagePath('myApp', 'cat.svg');
210
	 *
211
	 * @since 18.0.0
212
	 */
213
	public function getIcon(): string {
214
		return $this->urlGenerator->imagePath('files_accesscontrol', 'app.svg');
215
	}
216
217
	/**
218
	 * returns whether the operation can be used in the requested scope.
219
	 *
220
	 * Scope IDs are defined as constants in OCP\WorkflowEngine\IManager. At
221
	 * time of writing these are SCOPE_ADMIN and SCOPE_USER.
222
	 *
223
	 * For possibly unknown future scopes the recommended behaviour is: if
224
	 * user scope is permitted, the default behaviour should return `true`,
225
	 * otherwise `false`.
226
	 *
227
	 * @since 18.0.0
228
	 */
229
	public function isAvailableForScope(int $scope): bool {
230
		return $scope === IManager::SCOPE_ADMIN;
231
	}
232
233
	/**
234
	 * returns the id of the entity the operator is designed for
235
	 *
236
	 * Example: 'WorkflowEngine_Entity_File'
237
	 *
238
	 * @since 18.0.0
239
	 */
240
	public function getEntityId(): string {
241
		return File::class;
242
	}
243
244
	/**
245
	 * As IComplexOperation chooses the triggering events itself, a hint has
246
	 * to be shown to the user so make clear when this operation is becoming
247
	 * active. This method returns such a translated string.
248
	 *
249
	 * Example: "When a file is accessed" (en)
250
	 *
251
	 * @since 18.0.0
252
	 */
253
	public function getTriggerHint(): string {
254
		return $this->l->t('File is accessed');
255
	}
256
257
	public function onEvent(string $eventName, Event $event, IRuleMatcher $ruleMatcher): void {
258
		// Noop
259
	}
260
}
261