Skip to content

Commit 2689143

Browse files
authored
Merge pull request #1837 from nextcloud-libraries/fix/sanitize-guest-nick-stable6
[stable6] fix: display guest name validity
2 parents 76b76d4 + 5c0886e commit 2689143

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

l10n/messages.pot

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,21 @@ msgid ""
22
msgstr ""
33
"Content-Type: text/plain; charset=UTF-8\n"
44

5+
msgid "\"{char}\" is not allowed inside a name."
6+
msgstr ""
7+
8+
msgid "\"{extension}\" is not an allowed name."
9+
msgstr ""
10+
511
msgid "\"{name}\" is an invalid folder name."
612
msgstr ""
713

814
msgid "\"{name}\" is not an allowed folder name"
915
msgstr ""
1016

17+
msgid "\"{segment}\" is a reserved name and not allowed."
18+
msgstr ""
19+
1120
msgid "\"/\" is not allowed inside a folder name."
1221
msgstr ""
1322

@@ -76,6 +85,9 @@ msgstr ""
7685
msgid "Home"
7786
msgstr ""
7887

88+
msgid "Invalid name."
89+
msgstr ""
90+
7991
msgid "Modified"
8092
msgstr ""
8193

@@ -88,6 +100,15 @@ msgstr ""
88100
msgid "Name"
89101
msgstr ""
90102

103+
msgid "Names must not be empty."
104+
msgstr ""
105+
106+
msgid "Names must not end with \"{extension}\"."
107+
msgstr ""
108+
109+
msgid "Names must not start with a dot."
110+
msgstr ""
111+
91112
msgid "New"
92113
msgstr ""
93114

lib/components/PublicAuthPrompt.vue

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ import { getBuilder } from '@nextcloud/browser-storage'
3939
import { setGuestNickname } from '@nextcloud/auth'
4040
import { showError } from '@nextcloud/dialogs'
4141
42-
import NcButton from '@nextcloud/vue/components/NcButton'
4342
import NcDialog from '@nextcloud/vue/components/NcDialog'
4443
import NcNoteCard from '@nextcloud/vue/components/NcNoteCard'
4544
import NcTextField from '@nextcloud/vue/components/NcTextField'
4645
4746
import { t } from '../utils/l10n.ts'
47+
import { getGuestNameValidity } from '../utils/guestNameValidity.ts'
4848
4949
const storage = getBuilder('public').build()
5050
@@ -158,6 +158,19 @@ export default defineComponent({
158158
},
159159
immediate: true,
160160
},
161+
162+
name() {
163+
// Check validity of the new name
164+
const newName = this.name.trim?.() || ''
165+
const input = (this.$refs.input as Vue|undefined)?.$el.querySelector('input')
166+
if (!input) {
167+
return
168+
}
169+
170+
const validity = getGuestNameValidity(newName)
171+
input.setCustomValidity(validity)
172+
input.reportValidity()
173+
},
161174
},
162175
163176
methods: {

lib/utils/guestNameValidity.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*!
2+
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
3+
* SPDX-License-Identifier: AGPL-3.0-or-later
4+
*/
5+
import { InvalidFilenameError, InvalidFilenameErrorReason, validateFilename } from '@nextcloud/files'
6+
import { t } from '../utils/l10n.ts'
7+
8+
/**
9+
* Get the validity of a filename (empty if valid).
10+
* This can be used for `setCustomValidity` on input elements
11+
*
12+
* @param name The filename
13+
*/
14+
export function getGuestNameValidity(name: string): string {
15+
if (name.trim() === '') {
16+
return t('Names must not be empty.')
17+
}
18+
19+
if (name.startsWith('.')) {
20+
return t('Names must not start with a dot.')
21+
}
22+
23+
try {
24+
validateFilename(name)
25+
return ''
26+
} catch (error) {
27+
if (!(error instanceof InvalidFilenameError)) {
28+
throw error
29+
}
30+
31+
switch (error.reason) {
32+
case InvalidFilenameErrorReason.Character:
33+
return t('"{char}" is not allowed inside a name.', { char: error.segment })
34+
case InvalidFilenameErrorReason.ReservedName:
35+
return t('"{segment}" is a reserved name and not allowed.', { segment: error.segment })
36+
case InvalidFilenameErrorReason.Extension:
37+
if (error.segment.match(/\.[a-z]/i)) {
38+
return t('"{extension}" is not an allowed name.', { extension: error.segment })
39+
}
40+
return t('Names must not end with "{extension}".', { extension: error.segment })
41+
default:
42+
return t('Invalid name.')
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)