Skip to content

Commit 58ac74b

Browse files
committed
fix: FilenameValidator::isForbidden should only check forbidden files
And not forbidden basenames as this is used for different purposes. Signed-off-by: Ferdinand Thiessen <[email protected]>
1 parent 1907eee commit 58ac74b

1 file changed

Lines changed: 18 additions & 9 deletions

File tree

lib/private/Files/FilenameValidator.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,7 @@ public function validateFilename(string $filename): void {
198198
}
199199
}
200200

201-
if ($this->isForbidden($filename)) {
202-
throw new ReservedWordException();
203-
}
201+
$this->checkForbiddenName($filename);
204202

205203
$this->checkForbiddenExtension($filename);
206204

@@ -227,18 +225,25 @@ public function isForbidden(string $path): bool {
227225
return true;
228226
}
229227

228+
// Filename is not forbidden
229+
return false;
230+
}
231+
232+
protected function checkForbiddenName($filename): void {
233+
if ($this->isForbidden($filename)) {
234+
throw new ReservedWordException($this->l10n->t('"%1$s" is a forbidden file or folder name.', [$filename]));
235+
}
236+
230237
// Check for forbidden basenames - basenames are the part of the file until the first dot
231238
// (except if the dot is the first character as this is then part of the basename "hidden files")
232239
$basename = substr($filename, 0, strpos($filename, '.', 1) ?: null);
233240
$forbiddenNames = $this->getForbiddenBasenames();
234241
if (in_array($basename, $forbiddenNames)) {
235-
return true;
242+
throw new ReservedWordException($this->l10n->t('"%1$s" is a forbidden prefix for file or folder names.', [$filename]));
236243
}
237-
238-
// Filename is not forbidden
239-
return false;
240244
}
241245

246+
242247
/**
243248
* Check if a filename contains any of the forbidden characters
244249
* @param string $filename
@@ -252,7 +257,7 @@ protected function checkForbiddenCharacters(string $filename): void {
252257

253258
foreach ($this->getForbiddenCharacters() as $char) {
254259
if (str_contains($filename, $char)) {
255-
throw new InvalidCharacterInPathException($this->l10n->t('Invalid character "%1$s" in filename', [$char]));
260+
throw new InvalidCharacterInPathException($this->l10n->t('"%1$s" is not allowed inside a file or folder name.', [$char]));
256261
}
257262
}
258263
}
@@ -268,7 +273,11 @@ protected function checkForbiddenExtension(string $filename): void {
268273
$forbiddenExtensions = $this->getForbiddenExtensions();
269274
foreach ($forbiddenExtensions as $extension) {
270275
if (str_ends_with($filename, $extension)) {
271-
throw new InvalidPathException($this->l10n->t('Invalid filename extension "%1$s"', [$extension]));
276+
if (str_starts_with($extension, '.')) {
277+
throw new InvalidPathException($this->l10n->t('"%1$s" is a forbidden file type.', [$extension]));
278+
} else {
279+
throw new InvalidPathException($this->l10n->t('Filenames must not end with "%1$s".', [$extension]));
280+
}
272281
}
273282
}
274283
}

0 commit comments

Comments
 (0)