From 17c9813c98ab9e1cbf649920765eba96c7b030fd Mon Sep 17 00:00:00 2001 From: ramki-bruno Date: Tue, 15 Apr 2025 21:22:47 +0530 Subject: [PATCH 1/2] Regularize RegEx patterns for validating filenames - Fixed inconsistency in validating last character between bruno-electron and bruno-app. - Fixed inconsistency in sanitizing first character between bruno-electron and bruno-app. - Updated the comments to accurately reflect the patterns - Fixed inconsistencies in escaping certain characters in the patterns itself. --- packages/bruno-app/src/utils/common/regex.js | 12 ++++++------ packages/bruno-electron/src/utils/filesystem.js | 13 +++++++------ 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/packages/bruno-app/src/utils/common/regex.js b/packages/bruno-app/src/utils/common/regex.js index 6bf5d7b4d..ebdd6c453 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 dot, 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 dots, 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 a95c116eb..f623e9990 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 dots, 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 dot, 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 From b93d8e73a2274d20edef789b64650b8dc1602c5d Mon Sep 17 00:00:00 2001 From: ramki-bruno Date: Tue, 15 Apr 2025 21:45:56 +0530 Subject: [PATCH 2/2] Allow leading dot for file and folder names Co-authored-by: vishnuprasanth-j --- packages/bruno-app/src/utils/common/regex.js | 4 ++-- packages/bruno-electron/src/utils/filesystem.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/bruno-app/src/utils/common/regex.js b/packages/bruno-app/src/utils/common/regex.js index ebdd6c453..a8e4c6eee 100644 --- a/packages/bruno-app/src/utils/common/regex.js +++ b/packages/bruno-app/src/utils/common/regex.js @@ -1,6 +1,6 @@ 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, hyphen and `invalidCharacters` +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` @@ -9,7 +9,7 @@ export const variableNameRegex = /^[\w-.]*$/; export const sanitizeName = (name) => { name = name .replace(invalidCharacters, '-') // replace invalid characters with hyphens - .replace(/^[.\s\-]+/, '') // remove leading dots, spaces and 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 f623e9990..328b72d18 100644 --- a/packages/bruno-electron/src/utils/filesystem.js +++ b/packages/bruno-electron/src/utils/filesystem.js @@ -165,7 +165,7 @@ const sanitizeName = (name) => { const invalidCharacters = /[<>:"/\\|?*\x00-\x1F]/g; name = name .replace(invalidCharacters, '-') // replace invalid characters with hyphens - .replace(/^[.\s\-]+/, '') // remove leading dots, spaces and hyphens + .replace(/^[\s\-]+/, '') // remove leading spaces and hyphens .replace(/[.\s]+$/, ''); // remove trailing dots and spaces return name; }; @@ -177,7 +177,7 @@ 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, hyphen and `invalidCharacters` + 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