[FileUpload] Allow special characters in file names (#217361)

## Summary

This PR adds the ability for file names to have special characters.
Closes: #210312
This commit is contained in:
Krzysztof Kowalczyk 2025-04-08 13:38:34 +02:00 committed by GitHub
parent 8c08db126c
commit 94f210dc4c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 3 additions and 24 deletions

View file

@ -18,13 +18,13 @@ describe('parseFileName', () => {
test(' Something_* really -=- strange.abc.wav', () => {
expect(parseFileName(' Something_* really -=- strange.abc.wav')).toEqual({
name: 'Something__ really ___ strange_abc',
name: 'Something_* really -=- strange.abc',
});
});
test('!@#$%^&*()', () => {
expect(parseFileName('!@#$%^&*()')).toEqual({
name: '__________',
name: '!@#$%^&*()',
});
});

View file

@ -14,9 +14,6 @@ interface Result {
export function parseFileName(fileName: string): Result {
const withoutExt = fileName.substring(0, fileName.lastIndexOf('.')) || fileName;
return {
name: withoutExt
.trim()
.slice(0, 256)
.replace(/[^a-z0-9\s]/gi, '_'), // replace invalid chars
name: withoutExt.trim().slice(0, 256),
};
}

View file

@ -9,38 +9,20 @@
import { schema, Type } from '@kbn/config-schema';
const ALPHA_NUMERIC_WITH_SPACES_REGEX = /^[a-z0-9\s_]+$/i;
const ALPHA_NUMERIC_WITH_SPACES_EXT_REGEX = /^[a-z0-9\s\._]+$/i;
function alphanumericValidation(v: string) {
return ALPHA_NUMERIC_WITH_SPACES_REGEX.test(v)
? undefined
: 'Only alphanumeric characters are allowed as file names';
}
function alphanumericWithExtValidation(v: string) {
return ALPHA_NUMERIC_WITH_SPACES_EXT_REGEX.test(v)
? undefined
: 'Only alphanumeric characters, spaces (" "), dots (".") and underscores ("_") are allowed';
}
export const fileName = schema.string({
minLength: 1,
maxLength: 256,
validate: alphanumericValidation,
});
export const fileNameWithExt = schema.string({
minLength: 1,
maxLength: 256,
validate: alphanumericWithExtValidation,
});
export const fileAlt = schema.maybe(
schema.string({
minLength: 1,
maxLength: 256,
validate: alphanumericValidation,
})
);