Skip to content

Commit 9a6d968

Browse files
committed
Added validator to check if a given path is inside an open Collection
1 parent de350a6 commit 9a6d968

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

packages/bruno-electron/src/app/collection-watcher.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,14 @@ class CollectionWatcher {
615615
watcher?.add?.(itemPath);
616616
}
617617
}
618+
619+
getAllWatcherPaths() {
620+
return Object.entries(this.watchers)
621+
.filter(([path, watcher]) => !!watcher)
622+
.map(([path, _watcher]) => path);
623+
}
618624
}
619625

620-
module.exports = CollectionWatcher;
626+
const collectionWatcher = new CollectionWatcher();
627+
628+
module.exports = collectionWatcher;

packages/bruno-electron/src/index.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const LastOpenedCollections = require('./store/last-opened-collections');
3232
const registerNetworkIpc = require('./ipc/network');
3333
const registerCollectionsIpc = require('./ipc/collection');
3434
const registerPreferencesIpc = require('./ipc/preferences');
35-
const CollectionWatcher = require('./app/collection-watcher');
35+
const collectionWatcher = require('./app/collection-watcher');
3636
const { loadWindowState, saveBounds, saveMaximized } = require('./utils/window');
3737
const registerNotificationsIpc = require('./ipc/notifications');
3838
const registerGlobalEnvironmentsIpc = require('./ipc/global-environments');
@@ -60,7 +60,6 @@ setContentSecurityPolicy(contentSecurityPolicy.join(';') + ';');
6060
const menu = Menu.buildFromTemplate(menuTemplate);
6161

6262
let mainWindow;
63-
let collectionWatcher;
6463

6564
// Prepare the renderer once the app is ready
6665
app.on('ready', async () => {
@@ -136,7 +135,6 @@ app.on('ready', async () => {
136135
);
137136
}
138137
});
139-
collectionWatcher = new CollectionWatcher();
140138

141139
const handleBoundsChange = () => {
142140
if (!mainWindow.isMaximized()) {

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const { getEnvVars, getTreePathFromCollectionToItem, mergeVars, parseBruFileMeta
4242
const { getProcessEnvVars } = require('../store/process-env');
4343
const { getOAuth2TokenUsingAuthorizationCode, getOAuth2TokenUsingClientCredentials, getOAuth2TokenUsingPasswordCredentials, refreshOauth2Token } = require('../utils/oauth2');
4444
const { getCertsAndProxyConfig } = require('./network');
45+
const collectionWatcher = require('../app/collection-watcher');
4546

4647
const environmentSecretsStore = new EnvironmentSecretsStore();
4748
const collectionSecurityStore = new CollectionSecurityStore();
@@ -58,6 +59,16 @@ const envHasSecrets = (environment = {}) => {
5859
return secrets && secrets.length > 0;
5960
};
6061

62+
const validateCollectionSubPath = (path) => {
63+
const openCollectionPaths = collectionWatcher.getAllWatcherPaths();
64+
const isValid = openCollectionPaths.some((collectionPath) => {
65+
return path.startsWith(collectionPath);
66+
});
67+
if (!isValid) {
68+
throw new Error(`Path: ${path} should be inside a collection`);
69+
}
70+
}
71+
6172
const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollections) => {
6273
// browse directory
6374
ipcMain.handle('renderer:browse-directory', async (event, pathname, request) => {
@@ -238,6 +249,7 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
238249
throw new Error(`${request.filename}.bru is not a valid filename`);
239250
}
240251
const content = await jsonToBruViaWorker(request);
252+
validateCollectionSubPath(pathname);
241253
await writeFile(pathname, content);
242254
} catch (error) {
243255
return Promise.reject(error);

0 commit comments

Comments
 (0)