Skip to content

Commit

Permalink
Merge pull request #40585 from nextcloud/fix/illegal-characters
Browse files Browse the repository at this point in the history
  • Loading branch information
skjnldsv committed Oct 4, 2023
2 parents c88b02a + dbeb526 commit 529bce2
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 6 deletions.
6 changes: 6 additions & 0 deletions apps/files/lib/Controller/ViewController.php
Expand Up @@ -52,6 +52,7 @@
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\Collaboration\Resources\LoadAdditionalScriptsEvent as ResourcesLoadAdditionalScriptsEvent;
use OCP\Constants;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
Expand Down Expand Up @@ -246,6 +247,11 @@ public function index($dir = '', $view = '', $fileid = null, $fileNotFound = fal
$filesSortingConfig = json_decode($this->config->getUserValue($userId, 'files', 'files_sorting_configs', '{}'), true);
$this->initialState->provideInitialState('filesSortingConfig', $filesSortingConfig);

// Forbidden file characters
/** @var string[] */
$forbiddenCharacters = $this->config->getSystemValue('forbidden_chars', []);
$this->initialState->provideInitialState('forbiddenCharacters', Constants::FILENAME_INVALID_CHARS . implode('', $forbiddenCharacters));

$event = new LoadAdditionalScriptsEvent();
$this->eventDispatcher->dispatchTyped($event);
$this->eventDispatcher->dispatchTyped(new ResourcesLoadAdditionalScriptsEvent());
Expand Down
10 changes: 10 additions & 0 deletions apps/files/src/components/FileEntry.vue
Expand Up @@ -232,12 +232,15 @@ import CustomElementRender from './CustomElementRender.vue'
import CustomSvgIconRender from './CustomSvgIconRender.vue'
import FavoriteIcon from './FavoriteIcon.vue'
import logger from '../logger.js'
import { loadState } from '@nextcloud/initial-state'
// The registered actions list
const actions = getFileActions()
Vue.directive('onClickOutside', vOnClickOutside)
const forbiddenCharacters = loadState('files', 'forbiddenCharacters', '') as string
export default Vue.extend({
name: 'FileEntry',
Expand Down Expand Up @@ -810,6 +813,13 @@ export default Vue.extend({
throw new Error(this.t('files', '{newName} already exists.', { newName: name }))
}
const toCheck = trimmedName.split('')
toCheck.forEach(char => {
if (forbiddenCharacters.indexOf(char) !== -1) {
throw new Error(this.t('files', '"{char}" is not allowed inside a file name.', { char }))
}
})
return true
},
checkIfNodeExists(name) {
Expand Down
4 changes: 2 additions & 2 deletions apps/files/src/components/NavigationQuota.vue
Expand Up @@ -51,8 +51,8 @@ export default {
computed: {
storageStatsTitle() {
const usedQuotaByte = formatFileSize(this.storageStats?.used)
const quotaByte = formatFileSize(this.storageStats?.quota)
const usedQuotaByte = formatFileSize(this.storageStats?.used, false, false)
const quotaByte = formatFileSize(this.storageStats?.quota, false, false)
// If no quota set
if (this.storageStats?.quota < 0) {
Expand Down
7 changes: 6 additions & 1 deletion apps/files/tests/Controller/ViewControllerTest.php
Expand Up @@ -51,7 +51,6 @@
use OCP\IUser;
use OCP\IUserSession;
use OCP\Share\IManager;
use OCP\Template;
use Test\TestCase;

/**
Expand Down Expand Up @@ -153,6 +152,12 @@ public function testIndexWithRegularBrowser() {
'owner' => 'MyName',
'ownerDisplayName' => 'MyDisplayName',
]);

$this->config
->expects($this->any())
->method('getSystemValue')
->with('forbidden_chars', [])
->willReturn([]);
$this->config
->method('getUserValue')
->willReturnMap([
Expand Down
11 changes: 11 additions & 0 deletions config/config.sample.php
Expand Up @@ -1933,6 +1933,17 @@
*/
'blacklisted_files' => ['.htaccess'],

/**
* Blacklist characters from being used in filenames. This is useful if you
* have a filesystem or OS which does not support certain characters like windows.
*
* Example for windows systems: ``array('?', '<', '>', ':', '*', '|', '"', chr(0), "\n", "\r")``
* see https://en.wikipedia.org/wiki/Comparison_of_file_systems#Limits
*
* Defaults to ``array()``
*/
'forbidden_chars' => [],

/**
* If you are applying a theme to Nextcloud, enter the name of the theme here.
* The default location for themes is ``nextcloud/themes/``.
Expand Down
4 changes: 2 additions & 2 deletions dist/files-main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/files-main.js.map

Large diffs are not rendered by default.

0 comments on commit 529bce2

Please sign in to comment.