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
|
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OC\Files;
use OCP\Files\EmptyFileNameException;
use OCP\Files\FileNameTooLongException;
use OCP\Files\IFilenameValidator;
use OCP\Files\InvalidCharacterInPathException;
use OCP\Files\InvalidPathException;
use OCP\Files\ReservedWordException;
use OCP\IConfig;
use OCP\IL10N;
use OCP\L10N\IFactory;
use Psr\Log\LoggerInterface;
/**
* @since 30.0.0
*/
class FilenameValidator implements IFilenameValidator {
private IL10N $l10n;
/**
* @var list<string>
*/
private array $forbiddenNames = [];
/**
* @var list<string>
*/
private array $forbiddenCharacters = [];
/**
* @var list<string>
*/
private array $forbiddenExtensions = [];
public function __construct(
IFactory $l10nFactory,
private IConfig $config,
private LoggerInterface $logger,
) {
$this->l10n = $l10nFactory->get('core');
}
/**
* Get a list of reserved filenames that must not be used
* This list should be checked case-insensitive, all names are returned lowercase.
* @return list<string>
* @since 30.0.0
*/
public function getForbiddenExtensions(): array {
if (empty($this->forbiddenExtensions)) {
$forbiddenExtensions = $this->config->getSystemValue('forbidden_filename_extensions', ['.filepart']);
if (!is_array($forbiddenExtensions)) {
$this->logger->error('Invalid system config value for "forbidden_filename_extensions" is ignored.');
$forbiddenExtensions = ['.filepart'];
}
// Always forbid .part files as they are used internally
$forbiddenExtensions = array_merge($forbiddenExtensions, ['.part']);
// The list is case insensitive so we provide it always lowercase
$forbiddenExtensions = array_map('mb_strtolower', $forbiddenExtensions);
$this->forbiddenExtensions = array_values($forbiddenExtensions);
}
return $this->forbiddenExtensions;
}
/**
* Get a list of forbidden filename extensions that must not be used
* This list should be checked case-insensitive, all names are returned lowercase.
* @return list<string>
* @since 30.0.0
*/
public function getForbiddenFilenames(): array {
if (empty($this->forbiddenNames)) {
$forbiddenNames = $this->config->getSystemValue('forbidden_filenames', ['.htaccess']);
if (!is_array($forbiddenNames)) {
$this->logger->error('Invalid system config value for "forbidden_filenames" is ignored.');
$forbiddenNames = ['.htaccess'];
}
// Handle legacy config option
// TODO: Drop with Nextcloud 34
$legacyForbiddenNames = $this->config->getSystemValue('blacklisted_files', []);
if (!is_array($legacyForbiddenNames)) {
$this->logger->error('Invalid system config value for "blacklisted_files" is ignored.');
$legacyForbiddenNames = [];
}
if (!empty($legacyForbiddenNames)) {
$this->logger->warning('System config option "blacklisted_files" is deprecated and will be removed in Nextcloud 34, use "forbidden_filenames" instead.');
}
$forbiddenNames = array_merge($legacyForbiddenNames, $forbiddenNames);
// The list is case insensitive so we provide it always lowercase
$forbiddenNames = array_map('mb_strtolower', $forbiddenNames);
$this->forbiddenNames = array_values($forbiddenNames);
}
return $this->forbiddenNames;
}
/**
* Get a list of characters forbidden in filenames
*
* Note: Characters in the range [0-31] are always forbidden,
* even if not inside this list (see OCP\Files\Storage\IStorage::verifyPath).
*
* @return list<string>
* @since 30.0.0
*/
public function getForbiddenCharacters(): array {
if (empty($this->forbiddenCharacters)) {
// Get always forbidden characters
$forbiddenCharacters = str_split(\OCP\Constants::FILENAME_INVALID_CHARS);
if ($forbiddenCharacters === false) {
$forbiddenCharacters = [];
}
// Get admin defined invalid characters
$additionalChars = $this->config->getSystemValue('forbidden_filename_characters', []);
if (!is_array($additionalChars)) {
$this->logger->error('Invalid system config value for "forbidden_filename_characters" is ignored.');
$additionalChars = [];
}
$forbiddenCharacters = array_merge($forbiddenCharacters, $additionalChars);
// Handle legacy config option
// TODO: Drop with Nextcloud 34
$legacyForbiddenCharacters = $this->config->getSystemValue('forbidden_chars', []);
if (!is_array($legacyForbiddenCharacters)) {
$this->logger->error('Invalid system config value for "forbidden_chars" is ignored.');
$legacyForbiddenCharacters = [];
}
if (!empty($legacyForbiddenCharacters)) {
$this->logger->warning('System config option "forbidden_chars" is deprecated and will be removed in Nextcloud 34, use "forbidden_filename_characters" instead.');
}
$forbiddenCharacters = array_merge($legacyForbiddenCharacters, $forbiddenCharacters);
$this->forbiddenCharacters = array_values($forbiddenCharacters);
}
return $this->forbiddenCharacters;
}
/**
* @inheritdoc
*/
public function isFilenameValid(string $filename): bool {
try {
$this->validateFilename($filename);
} catch (\OCP\Files\InvalidPathException) {
return false;
}
return true;
}
/**
* @inheritdoc
*/
public function validateFilename(string $filename): void {
$trimmed = trim($filename);
if ($trimmed === '') {
throw new EmptyFileNameException();
}
// the special directories . and .. would cause never ending recursion
if ($trimmed === '.' || $trimmed === '..') {
throw new ReservedWordException();
}
// 255 characters is the limit on common file systems (ext/xfs)
// oc_filecache has a 250 char length limit for the filename
if (isset($filename[250])) {
throw new FileNameTooLongException();
}
if ($this->isForbidden($filename)) {
throw new ReservedWordException();
}
$this->checkForbiddenExtension($filename);
$this->checkForbiddenCharacters($filename);
}
/**
* Check if the filename is forbidden
* @param string $path Path to check the filename
* @return bool True if invalid name, False otherwise
*/
public function isForbidden(string $path): bool {
$filename = basename($path);
$filename = mb_strtolower($filename);
if ($filename === '') {
return false;
}
// The name part without extension
$basename = substr($filename, 0, strpos($filename, '.', 1) ?: null);
// Check for forbidden filenames
$forbiddenNames = $this->getForbiddenFilenames();
if (in_array($basename, $forbiddenNames)) {
return true;
}
// Filename is not forbidden
return false;
}
/**
* Check if a filename contains any of the forbidden characters
* @param string $filename
* @throws InvalidCharacterInPathException
*/
protected function checkForbiddenCharacters(string $filename): void {
$sanitizedFileName = filter_var($filename, FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW);
if ($sanitizedFileName !== $filename) {
throw new InvalidCharacterInPathException();
}
foreach ($this->getForbiddenCharacters() as $char) {
if (str_contains($filename, $char)) {
throw new InvalidCharacterInPathException($char);
}
}
}
/**
* Check if a filename has a forbidden filename extension
* @param string $filename The filename to validate
* @throws InvalidPathException
*/
protected function checkForbiddenExtension(string $filename): void {
$filename = mb_strtolower($filename);
// Check for forbidden filename exten<sions
$forbiddenExtensions = $this->getForbiddenExtensions();
foreach ($forbiddenExtensions as $extension) {
if (str_ends_with($filename, $extension)) {
throw new InvalidPathException($this->l10n->t('Invalid filename extension "%1$s"', [$extension]));
}
}
}
};
|