Skip to content

Commit

Permalink
chore(files): Move to @nextcloud/files functions for filename sanit…
Browse files Browse the repository at this point in the history
…izing

Signed-off-by: Ferdinand Thiessen <[email protected]>
  • Loading branch information
susnux committed Jul 3, 2024
1 parent da3914a commit 5e160c0
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 22 deletions.
4 changes: 0 additions & 4 deletions apps/files/lib/Controller/ViewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,6 @@ 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
$forbiddenCharacters = \OCP\Util::getForbiddenFileNameChars();
$this->initialState->provideInitialState('forbiddenCharacters', $forbiddenCharacters);

$event = new LoadAdditionalScriptsEvent();
$this->eventDispatcher->dispatchTyped($event);
$this->eventDispatcher->dispatchTyped(new ResourcesLoadAdditionalScriptsEvent());
Expand Down
19 changes: 2 additions & 17 deletions apps/files/src/components/FileEntry/FileEntryName.vue
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ import type { PropType } from 'vue'
import { showError, showSuccess } from '@nextcloud/dialogs'
import { emit } from '@nextcloud/event-bus'
import { FileType, NodeStatus, Permission } from '@nextcloud/files'
import { loadState } from '@nextcloud/initial-state'
import { FileType, NodeStatus, Permission, isFilenameValid } from '@nextcloud/files'
import { translate as t } from '@nextcloud/l10n'
import axios, { isAxiosError } from '@nextcloud/axios'
import { defineComponent } from 'vue'
Expand All @@ -54,8 +53,6 @@ import { useNavigation } from '../../composables/useNavigation'
import { useRenamingStore } from '../../store/renaming.ts'
import logger from '../../logger.js'
const forbiddenCharacters = loadState<string[]>('files', 'forbiddenCharacters', [])
export default defineComponent({
name: 'FileEntryName',
Expand Down Expand Up @@ -210,24 +207,12 @@ export default defineComponent({
},
isFileNameValid(name: string) {
const trimmedName = name.trim()
if (trimmedName === '.' || trimmedName === '..') {
if (!isFilenameValid(name)) {
throw new Error(t('files', '"{name}" is an invalid file name.', { name }))
} else if (trimmedName.length === 0) {
throw new Error(t('files', 'File name cannot be empty.'))
} else if (trimmedName.indexOf('/') !== -1) {
throw new Error(t('files', '"/" is not allowed inside a file name.'))
} else if (trimmedName.match(window.OC.config.blacklist_files_regex)) {
throw new Error(t('files', '"{name}" is not an allowed filetype.', { name }))
} else if (this.checkIfNodeExists(name)) {
throw new Error(t('files', '{newName} already exists.', { newName: name }))
}
const char = forbiddenCharacters.find((char) => trimmedName.includes(char))
if (char) {
throw new Error(t('files', '"{char}" is not allowed inside a file name.', { char }))
}
return true
},
Expand Down
2 changes: 1 addition & 1 deletion apps/files/src/views/FilesList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export default defineComponent({
const { currentView } = useNavigation()
const enableGridView = (loadState('core', 'config', [])['enable_non-accessible_features'] ?? true)
const forbiddenCharacters = loadState<string[]>('files', 'forbiddenCharacters', [])
const forbiddenCharacters = (window as unknown as { '_oc_config': { 'forbidden_filename_characters': string }})._oc_config.forbidden_filename_characters
return {
currentView,
Expand Down

0 comments on commit 5e160c0

Please sign in to comment.