diff --git a/packages/bruno-app/src/utils/common/regex.js b/packages/bruno-app/src/utils/common/regex.js index 6bf5d7b4d..a8e4c6eee 100644 --- a/packages/bruno-app/src/utils/common/regex.js +++ b/packages/bruno-app/src/utils/common/regex.js @@ -1,16 +1,16 @@ const invalidCharacters = /[<>:"/\\|?*\x00-\x1F]/g; // replace invalid characters with hyphens const reservedDeviceNames = /^(CON|PRN|AUX|NUL|COM[0-9]|LPT[0-9])$/i; -const firstCharacter = /^[^.\s\-\<>:"/\\|?*\x00-\x1F]/; // no dot, space, or hyphen at start -const middleCharacters = /^[^<>:"/\\|?*\x00-\x1F]*$/; // no invalid characters -const lastCharacter = /^[^.\s\-\<>:"/\\|?*\x00-\x1F]/; // no dot or space at end, hyphen allowed +const firstCharacter = /^[^\s\-<>:"/\\|?*\x00-\x1F]/; // no space, hyphen and `invalidCharacters` +const middleCharacters = /^[^<>:"/\\|?*\x00-\x1F]*$/; // no `invalidCharacters` +const lastCharacter = /[^.\s<>:"/\\|?*\x00-\x1F]$/; // no dot, space and `invalidCharacters` export const variableNameRegex = /^[\w-.]*$/; export const sanitizeName = (name) => { name = name - .replace(invalidCharacters, '-') // replace invalid characters with hyphens - .replace(/^[.\s-]+/, '') // remove leading dots, hyphens and spaces - .replace(/[.\s]+$/, ''); // remove trailing dots and spaces (keep trailing hyphens) + .replace(invalidCharacters, '-') // replace invalid characters with hyphens + .replace(/^[\s\-]+/, '') // remove leading spaces and hyphens + .replace(/[.\s]+$/, ''); // remove trailing dots and spaces return name; }; diff --git a/packages/bruno-electron/src/utils/filesystem.js b/packages/bruno-electron/src/utils/filesystem.js index 7593d79ed..28b479013 100644 --- a/packages/bruno-electron/src/utils/filesystem.js +++ b/packages/bruno-electron/src/utils/filesystem.js @@ -164,9 +164,9 @@ const searchForBruFiles = (dir) => { const sanitizeName = (name) => { const invalidCharacters = /[<>:"/\\|?*\x00-\x1F]/g; name = name - .replace(invalidCharacters, '-') // replace invalid characters with hyphens - .replace(/^[.\s]+/, '') // remove leading dots and and spaces - .replace(/[.\s]+$/, ''); // remove trailing dots and spaces (keep trailing hyphens) + .replace(invalidCharacters, '-') // replace invalid characters with hyphens + .replace(/^[\s\-]+/, '') // remove leading spaces and hyphens + .replace(/[.\s]+$/, ''); // remove trailing dots and spaces return name; }; @@ -175,10 +175,11 @@ const isWindowsOS = () => { } const validateName = (name) => { + const invalidCharacters = /[<>:"/\\|?*\x00-\x1F]/g; // keeping this for informational purpose const reservedDeviceNames = /^(CON|PRN|AUX|NUL|COM[0-9]|LPT[0-9])$/i; - const firstCharacter = /^[^.\s\-\<>:"/\\|?*\x00-\x1F]/; // no dot, space, or hyphen at start - const middleCharacters = /^[^<>:"/\\|?*\x00-\x1F]*$/; // no invalid characters - const lastCharacter = /[^.\s]$/; // no dot or space at end, hyphen allowed + const firstCharacter = /^[^\s\-<>:"/\\|?*\x00-\x1F]/; // no space, hyphen and `invalidCharacters` + const middleCharacters = /^[^<>:"/\\|?*\x00-\x1F]*$/; // no `invalidCharacters` + const lastCharacter = /[^.\s<>:"/\\|?*\x00-\x1F]$/; // no dot, space and `invalidCharacters` if (name.length > 255) return false; // max name length if (reservedDeviceNames.test(name)) return false; // windows reserved names