Skip to content

Commit 316b632

Browse files
authored
refactor: add sanitization for collection names and improve directory name handling (#3559)
1 parent dc469af commit 316b632

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

packages/bruno-electron/src/ipc/collection.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const {
2020
normalizeWslPath,
2121
normalizeAndResolvePath,
2222
safeToRename,
23+
sanitizeCollectionName,
2324
isWindowsOS,
2425
isValidFilename,
2526
hasSubDirectories,
@@ -68,6 +69,8 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
6869
'renderer:create-collection',
6970
async (event, collectionName, collectionFolderName, collectionLocation) => {
7071
try {
72+
collectionFolderName = sanitizeDirectoryName(collectionFolderName);
73+
collectionName = sanitizeCollectionName(collectionName);
7174
const dirPath = path.join(collectionLocation, collectionFolderName);
7275
if (fs.existsSync(dirPath)) {
7376
const files = fs.readdirSync(dirPath);
@@ -105,6 +108,7 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
105108
ipcMain.handle(
106109
'renderer:clone-collection',
107110
async (event, collectionName, collectionFolderName, collectionLocation, previousPath) => {
111+
collectionFolderName = sanitizeCollectionName(collectionFolderName);
108112
const dirPath = path.join(collectionLocation, collectionFolderName);
109113
if (fs.existsSync(dirPath)) {
110114
throw new Error(`collection: ${dirPath} already exists`);
@@ -150,6 +154,7 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
150154
// rename collection
151155
ipcMain.handle('renderer:rename-collection', async (event, newName, collectionPathname) => {
152156
try {
157+
newName = sanitizeCollectionName(newName);
153158
const brunoJsonFilePath = path.join(collectionPathname, 'bruno.json');
154159
const content = fs.readFileSync(brunoJsonFilePath, 'utf8');
155160
const json = JSON.parse(content);
@@ -442,6 +447,8 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
442447

443448
// new folder
444449
ipcMain.handle('renderer:new-folder', async (event, pathname) => {
450+
const resolvedFolderName = sanitizeDirectoryName(path.basename(pathname));
451+
pathname = path.join(path.dirname(pathname), resolvedFolderName);
445452
try {
446453
if (!fs.existsSync(pathname)) {
447454
fs.mkdirSync(pathname);
@@ -500,7 +507,7 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
500507

501508
ipcMain.handle('renderer:import-collection', async (event, collection, collectionLocation) => {
502509
try {
503-
let collectionName = sanitizeDirectoryName(collection.name);
510+
let collectionName = sanitizeCollectionName(collection.name);
504511
let collectionPath = path.join(collectionLocation, collectionName);
505512

506513
if (fs.existsSync(collectionPath)) {
@@ -516,6 +523,7 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
516523
fs.writeFileSync(filePath, content);
517524
}
518525
if (item.type === 'folder') {
526+
item.name = sanitizeDirectoryName(item.name);
519527
const folderPath = path.join(currentPath, item.name);
520528
fs.mkdirSync(folderPath);
521529

packages/bruno-electron/src/utils/filesystem.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,12 @@ const searchForBruFiles = (dir) => {
161161
return searchForFiles(dir, '.bru');
162162
};
163163

164+
const sanitizeCollectionName = (name) => {
165+
return name.trim();
166+
}
167+
164168
const sanitizeDirectoryName = (name) => {
165-
return name.replace(/[<>:"/\\|?*\x00-\x1F]+/g, '-');
169+
return name.replace(/[<>:"/\\|?*\x00-\x1F]+/g, '-').trim();
166170
};
167171

168172
const isWindowsOS = () => {
@@ -227,6 +231,7 @@ module.exports = {
227231
searchForFiles,
228232
searchForBruFiles,
229233
sanitizeDirectoryName,
234+
sanitizeCollectionName,
230235
isWindowsOS,
231236
safeToRename,
232237
isValidFilename,

0 commit comments

Comments
 (0)