Completed
Push — master ( 9e8584...db8089 )
by Phil
24:40 queued 12:33
created

ReadOnlyCachePermissionsMask::formatCacheEntry()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 12
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 23
rs 8.5906
1
<?php
2
/**
3
 * @author Morris Jobke <[email protected]>
4
 * @author Robin Appelman <[email protected]>
5
 * @author Ilja Neumann <[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 OC\Files\Cache\Wrapper;
25
26
use OCP\Constants;
27
28
/**
29
 * Works together with ReadOnlyJail class
30
 * to allow file creation outside of users files dir.
31
 *
32
 * @package OC\Files\Cache\Wrapper
33
 */
34
class ReadOnlyCachePermissionsMask extends CacheWrapper {
35
	/**
36
	 * @var int
37
	 */
38
	protected $mask;
39
40
	/**
41
	 * System internal paths which should not be protected
42
	 * @var array
43
	 */
44
	private $whitelist = [
45
		'uploads',
46
		'cache',
47
		'files_zsync'
48
	];
49
50
	/**
51
	 * @param \OCP\Files\Cache\ICache $cache
52
	 * @param int $mask
53
	 */
54
	public function __construct($cache, $mask) {
55
		parent::__construct($cache);
56
		$this->mask = $mask;
57
	}
58
59
	/**
60
	 * @param \OCP\Files\Cache\ICacheEntry $entry
61
	 * @return \OCP\Files\Cache\ICacheEntry
62
	 */
63
	protected function formatCacheEntry($entry) {
64
		$storageId = $entry->getStorageId();
65
66
		// Give all permissions to whitelisted "internal" paths and their
67
		// subdirectories
68
		if ($this->isHomeStorage($storageId)) {
69
			foreach ($this->whitelist as $path) {
70
				if ($this->startsWith($entry->getPath(), $path)) {
71
					$entry['permissions'] = Constants::PERMISSION_ALL;
72
					return $entry;
73
				}
74
			}
75
		}
76
77
		// Allow creation of skeleton files
78
		if ($this->isHomeStorage($storageId) && $entry->getPath() === '') {
79
			$entry['permissions'] = Constants::PERMISSION_CREATE;
80
			$this->mask = Constants::PERMISSION_CREATE;
81
		}
82
83
		$entry['permissions'] &= $this->mask;
84
		return $entry;
85
	}
86
87
	private function isHomeStorage($storageId) {
88
		return \substr($storageId, 0, \strlen('home::')) === 'home::';
89
	}
90
91
	private function startsWith($haystack, $needle) {
92
		return (\substr($haystack, 0, \strlen($needle)) === $needle);
93
	}
94
}
95