From 2c0ccf769ca5f2fc0745bf00f9ae93761c34fa5a Mon Sep 17 00:00:00 2001 From: Thomas Pyle Date: Thu, 28 Sep 2023 21:39:33 -0400 Subject: [PATCH] Corrects issue when collection names in imports have slashes --- .github/workflows/unit-tests.yml | 42 ++++++++++--------- packages/bruno-electron/src/ipc/collection.js | 7 ++-- .../bruno-electron/src/utils/filesystem.js | 7 +++- .../src/utils/filesystem.test.js | 26 ++++++++++++ 4 files changed, 58 insertions(+), 24 deletions(-) create mode 100644 packages/bruno-electron/src/utils/filesystem.test.js diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index 49b558f6..d3e4d261 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -1,29 +1,31 @@ name: Unit Tests on: push: - branches: [ main ] + branches: [main] pull_request: - branches: [ main ] + branches: [main] jobs: test: timeout-minutes: 60 runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - uses: actions/setup-node@v3 - with: - node-version: 16 - - name: Install dependencies - run: npm i --legacy-peer-deps - - name: Test Package bruno-query - run: npm run test --workspace=packages/bruno-query - - name: Build Package bruno-query - run: npm run build --workspace=packages/bruno-query - - name: Test Package bruno-lang - run: npm run test --workspace=packages/bruno-lang - - name: Test Package bruno-schema - run: npm run test --workspace=packages/bruno-schema - - name: Test Package bruno-app - run: npm run test --workspace=packages/bruno-app - - name: Test Package bruno-js - run: npm run test --workspace=packages/bruno-js + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: 16 + - name: Install dependencies + run: npm i --legacy-peer-deps + - name: Test Package bruno-query + run: npm run test --workspace=packages/bruno-query + - name: Build Package bruno-query + run: npm run build --workspace=packages/bruno-query + - name: Test Package bruno-lang + run: npm run test --workspace=packages/bruno-lang + - name: Test Package bruno-schema + run: npm run test --workspace=packages/bruno-schema + - name: Test Package bruno-app + run: npm run test --workspace=packages/bruno-app + - name: Test Package bruno-js + run: npm run test --workspace=packages/bruno-js + - name: Test Package bruno-electron + run: npm run test --workspace=packages/bruno-electron diff --git a/packages/bruno-electron/src/ipc/collection.js b/packages/bruno-electron/src/ipc/collection.js index 637a8d47..ae85558a 100644 --- a/packages/bruno-electron/src/ipc/collection.js +++ b/packages/bruno-electron/src/ipc/collection.js @@ -11,7 +11,8 @@ const { isDirectory, browseDirectory, createDirectory, - searchForBruFiles + searchForBruFiles, + sanitizeDirectoryName } = require('../utils/filesystem'); const { stringifyJson } = require('../utils/common'); const { openCollectionDialog, openCollection } = require('../app/collections'); @@ -315,7 +316,7 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection ipcMain.handle('renderer:import-collection', async (event, collection, collectionLocation) => { try { - let collectionName = collection.name; + let collectionName = sanitizeDirectoryName(collection.name); let collectionPath = path.join(collectionLocation, collectionName); if (fs.existsSync(collectionPath)) { @@ -359,7 +360,7 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection const uid = generateUidBasedOnHash(collectionPath); const brunoConfig = { version: '1', - name: collection.name, + name: collectionName, type: 'collection' }; const content = await stringifyJson(brunoConfig); diff --git a/packages/bruno-electron/src/utils/filesystem.js b/packages/bruno-electron/src/utils/filesystem.js index daf6eaf6..b55dfd72 100644 --- a/packages/bruno-electron/src/utils/filesystem.js +++ b/packages/bruno-electron/src/utils/filesystem.js @@ -114,6 +114,10 @@ const searchForBruFiles = (dir) => { return searchForFiles(dir, '.bru'); }; +const sanitizeDirectoryName = (name) => { + return name.replace(/[<>:"/\\|?*\x00-\x1F]+/g, '-'); +}; + module.exports = { isValidPathname, exists, @@ -127,5 +131,6 @@ module.exports = { createDirectory, browseDirectory, searchForFiles, - searchForBruFiles + searchForBruFiles, + sanitizeDirectoryName }; diff --git a/packages/bruno-electron/src/utils/filesystem.test.js b/packages/bruno-electron/src/utils/filesystem.test.js new file mode 100644 index 00000000..62d7b502 --- /dev/null +++ b/packages/bruno-electron/src/utils/filesystem.test.js @@ -0,0 +1,26 @@ +const { sanitizeDirectoryName } = require('./filesystem.js'); + +describe('sanitizeDirectoryName', () => { + it('should replace invalid characters with hyphens', () => { + const input = '<>:"/\\|?*\x00-\x1F'; + const expectedOutput = '---'; + expect(sanitizeDirectoryName(input)).toEqual(expectedOutput); + }); + + it('should not modify valid directory names', () => { + const input = 'my-directory'; + expect(sanitizeDirectoryName(input)).toEqual(input); + }); + + it('should replace multiple invalid characters with a single hyphen', () => { + const input = 'my<>invalid?directory'; + const expectedOutput = 'my-invalid-directory'; + expect(sanitizeDirectoryName(input)).toEqual(expectedOutput); + }); + + it('should handle names with slashes', () => { + const input = 'my/invalid/directory'; + const expectedOutput = 'my-invalid-directory'; + expect(sanitizeDirectoryName(input)).toEqual(expectedOutput); + }); +});