-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathOperation.php
291 lines (256 loc) · 8.1 KB
/
Operation.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\FilesAccessControl;
use Exception;
use OC\Files\FileInfo;
use OC\Files\Node\Folder;
use OC\Files\View;
use OCA\WorkflowEngine\Entity\File;
use OCP\EventDispatcher\Event;
use OCP\Files\Cache\ICacheEntry;
use OCP\Files\ForbiddenException;
use OCP\Files\IRootFolder;
use OCP\Files\Mount\IMountManager;
use OCP\Files\Mount\IMountPoint;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\Files\Storage\IStorage;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\WorkflowEngine\IComplexOperation;
use OCP\WorkflowEngine\IManager;
use OCP\WorkflowEngine\IRuleMatcher;
use OCP\WorkflowEngine\ISpecificOperation;
use Psr\Log\LoggerInterface;
use ReflectionClass;
use UnexpectedValueException;
class Operation implements IComplexOperation, ISpecificOperation {
protected int $nestingLevel = 0;
public function __construct(
protected readonly IManager $manager,
protected readonly IL10N $l,
protected readonly IURLGenerator $urlGenerator,
protected readonly File $fileEntity,
protected readonly IMountManager $mountManager,
protected readonly IRootFolder $rootFolder,
protected readonly LoggerInterface $logger
) {
}
/**
* @param array|ICacheEntry|null $cacheEntry
* @throws ForbiddenException
*/
public function checkFileAccess(IStorage $storage, string $path, bool $isDir = false, $cacheEntry = null): void {
if (!$this->isBlockablePath($storage, $path) || $this->isCreatingSkeletonFiles() || $this->nestingLevel !== 0) {
// Allow creating skeletons and theming
// https://github.com/nextcloud/files_accesscontrol/issues/5
// https://github.com/nextcloud/files_accesscontrol/issues/12
return;
}
$this->nestingLevel++;
$filePath = $this->translatePath($storage, $path);
$ruleMatcher = $this->manager->getRuleMatcher();
$ruleMatcher->setFileInfo($storage, $filePath, $isDir);
$node = $this->getNode($storage, $path, $cacheEntry);
if ($node !== null) {
$ruleMatcher->setEntitySubject($this->fileEntity, $node);
}
$ruleMatcher->setOperation($this);
$match = $ruleMatcher->getFlows();
$this->nestingLevel--;
if (!empty($match)) {
$e = new \RuntimeException('Access denied for path ' . $path . ' that is ' . ($isDir ? '' : 'not ') . 'a directory and matches rules: ' . json_encode($match));
$this->logger->debug($e->getMessage(), ['exception' => $e]);
// All Checks of one operation matched: prevent access
throw new ForbiddenException('Access denied by access control', false);
}
}
protected function isBlockablePath(IStorage $storage, string $path): bool {
if (property_exists($storage, 'mountPoint')) {
$hasMountPoint = $storage instanceof StorageWrapper;
if (!$hasMountPoint) {
$ref = new ReflectionClass($storage);
$prop = $ref->getProperty('mountPoint');
$hasMountPoint = $prop->isPublic();
}
if ($hasMountPoint) {
/** @var StorageWrapper $storage */
$fullPath = $storage->mountPoint . ltrim($path, '/');
} else {
$fullPath = $path;
}
} else {
$fullPath = $path;
}
if (substr_count($fullPath, '/') < 3) {
return false;
}
if (preg_match('/\.ocTransferId\d+\.part$/i', $path)) {
return false;
}
// '', admin, 'files', 'path/to/file.txt'
$segment = explode('/', $fullPath, 4);
if (isset($segment[2]) && $segment[1] === '__groupfolders' && $segment[2] === 'trash') {
// Special case, a file was deleted inside a groupfolder
return true;
}
return isset($segment[2]) && in_array($segment[2], [
'files',
'thumbnails',
'files_versions',
]);
}
/**
* For thumbnails and versions we want to check the tags of the original file
*/
protected function translatePath(IStorage $storage, string $path): string {
if (substr_count($path, '/') < 1) {
return $path;
}
// 'files', 'path/to/file.txt'
[$folder, $innerPath] = explode('/', $path, 2);
if ($folder === 'files_versions') {
$innerPath = substr($innerPath, 0, strrpos($innerPath, '.v'));
return 'files/' . $innerPath;
} elseif ($folder === 'thumbnails') {
[$fileId,] = explode('/', $innerPath, 2);
$innerPath = $storage->getCache()->getPathById((int)$fileId);
if ($innerPath !== null) {
return 'files/' . $innerPath;
}
}
return $path;
}
/**
* Check if we are in the LoginController and if so, ignore the firewall
*/
protected function isCreatingSkeletonFiles(): bool {
$exception = new Exception();
$trace = $exception->getTrace();
foreach ($trace as $step) {
if (isset($step['class']) && $step['class'] === \OC\Core\Controller\LoginController::class &&
isset($step['function']) && $step['function'] === 'tryLogin') {
return true;
}
}
return false;
}
/**
* @param string $name
* @param array $checks
* @param string $operation
* @throws UnexpectedValueException
*/
public function validateOperation(string $name, array $checks, string $operation): void {
if (empty($checks)) {
throw new UnexpectedValueException($this->l->t('No rule given'));
}
}
/**
* returns a translated name to be presented in the web interface
*
* Example: "Automated tagging" (en), "Aŭtomata etikedado" (eo)
*
* @since 18.0.0
*/
public function getDisplayName(): string {
return $this->l->t('Block access to a file');
}
/**
* returns a translated, descriptive text to be presented in the web interface.
*
* It should be short and precise.
*
* Example: "Tag based automatic deletion of files after a given time." (en)
*
* @since 18.0.0
*/
public function getDescription(): string {
return '';
}
/**
* returns the URL to the icon of the operator for display in the web interface.
*
* Usually, the implementation would utilize the `imagePath()` method of the
* `\OCP\IURLGenerator` instance and simply return its result.
*
* Example implementation: return $this->urlGenerator->imagePath('myApp', 'cat.svg');
*
* @since 18.0.0
*/
public function getIcon(): string {
return $this->urlGenerator->imagePath('files_accesscontrol', 'app.svg');
}
/**
* returns whether the operation can be used in the requested scope.
*
* Scope IDs are defined as constants in OCP\WorkflowEngine\IManager. At
* time of writing these are SCOPE_ADMIN and SCOPE_USER.
*
* For possibly unknown future scopes the recommended behaviour is: if
* user scope is permitted, the default behaviour should return `true`,
* otherwise `false`.
*
* @since 18.0.0
*/
public function isAvailableForScope(int $scope): bool {
return $scope === IManager::SCOPE_ADMIN;
}
/**
* returns the id of the entity the operator is designed for
*
* Example: 'WorkflowEngine_Entity_File'
*
* @since 18.0.0
*/
public function getEntityId(): string {
return File::class;
}
/**
* As IComplexOperation chooses the triggering events itself, a hint has
* to be shown to the user so make clear when this operation is becoming
* active. This method returns such a translated string.
*
* Example: "When a file is accessed" (en)
*
* @since 18.0.0
*/
public function getTriggerHint(): string {
return $this->l->t('File is accessed');
}
public function onEvent(string $eventName, Event $event, IRuleMatcher $ruleMatcher): void {
// Noop
}
/**
* @param array|ICacheEntry|null $cacheEntry
*/
private function getNode(IStorage $storage, string $path, $cacheEntry = null): ?Node {
/** @var IMountPoint|false $mountPoint */
$mountPoint = current($this->mountManager->findByStorageId($storage->getId()));
if (!$mountPoint) {
return null;
}
$fullPath = $mountPoint->getMountPoint() . $path;
if ($cacheEntry) {
// todo: LazyNode?
$info = new FileInfo($fullPath, $mountPoint->getStorage(), $path, $cacheEntry, $mountPoint);
$isDir = $info->getType() === \OCP\Files\FileInfo::TYPE_FOLDER;
$view = new View('');
if ($isDir) {
return new Folder($this->rootFolder, $view, $path, $info);
} else {
return new \OC\Files\Node\File($this->rootFolder, $view, $path, $info);
}
} else {
try {
return $this->rootFolder->get($fullPath);
} catch (NotFoundException $e) {
return null;
}
}
}
}