Skip to content

Commit 837aa4a

Browse files
committed
app: runCmd: Add user and static plugin type to runScript
This function was not updated to account for these two plugin type paths. Also the script exits with 1 when enountering a wrong path. Otherwise the script just hangs.
1 parent 0c704be commit 837aa4a

File tree

2 files changed

+79
-3
lines changed

2 files changed

+79
-3
lines changed

app/electron/runCmd.test.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
import { describe, expect, it } from '@jest/globals';
18+
import path from 'path';
1819
import { checkPermissionSecret, validateCommandData } from './runCmd';
1920

2021
describe('checkPermissionSecret', () => {
@@ -220,3 +221,68 @@ describe('validateCommandData', () => {
220221
).toBe(true);
221222
});
222223
});
224+
225+
describe('runScript', () => {
226+
const originalArgv = process.argv;
227+
const originalExit = process.exit;
228+
const originalConsoleError = console.error;
229+
const originalResourcesPath = process.resourcesPath;
230+
231+
let exitMock: jest.Mock;
232+
let consoleErrorMock: jest.Mock;
233+
beforeEach(() => {
234+
jest.resetModules();
235+
// @ts-ignore this is fine for tests
236+
process.resourcesPath = '/resources';
237+
jest.mock('./plugin-management', () => ({
238+
defaultPluginsDir: jest.fn(() => '/plugins/default'),
239+
defaultUserPluginsDir: jest.fn(() => '/plugins/user'),
240+
}));
241+
242+
exitMock = jest.fn() as any;
243+
// @ts-expect-error overriding for test
244+
process.exit = exitMock;
245+
consoleErrorMock = jest.fn();
246+
console.error = consoleErrorMock;
247+
});
248+
249+
afterEach(() => {
250+
process.argv = originalArgv;
251+
process.exit = originalExit;
252+
console.error = originalConsoleError;
253+
// @ts-ignore
254+
process.resourcesPath = originalResourcesPath;
255+
jest.unmock('./plugin-management');
256+
jest.restoreAllMocks();
257+
});
258+
259+
const testScriptImport = async (scriptPath: string) => {
260+
const resolvedPath = path.resolve(scriptPath);
261+
process.argv = ['node', resolvedPath];
262+
jest.doMock(resolvedPath, () => ({}), { virtual: true });
263+
const runCmdModule = await import('./runCmd');
264+
runCmdModule.runScript();
265+
expect(exitMock).not.toHaveBeenCalled();
266+
};
267+
268+
it('imports the script when path is inside defaultPluginsDir', () =>
269+
testScriptImport('/plugins/default/my-script.js'));
270+
271+
it('imports the script when path is inside defaultUserPluginsDir', () =>
272+
testScriptImport('/plugins/user/my-script.js'));
273+
274+
it('imports the script when path is inside static .plugins dir', () =>
275+
testScriptImport('/resources/.plugins/my-script.js'));
276+
277+
it('exits with error when script is outside allowed directories', async () => {
278+
const scriptPath = path.resolve('/not-allowed/my-script.js');
279+
process.argv = ['node', scriptPath];
280+
jest.doMock(scriptPath, () => ({}), { virtual: true });
281+
282+
const runCmdModule = await import('./runCmd');
283+
runCmdModule.runScript();
284+
285+
expect(consoleErrorMock).toHaveBeenCalledTimes(1);
286+
expect(exitMock).toHaveBeenCalledWith(1);
287+
});
288+
});

app/electron/runCmd.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ const COMMANDS_WITH_CONSENT = {
141141
'scriptjs minikube/manage-minikube.js',
142142
],
143143
};
144+
144145
/**
145146
* Adds the runCmd consent for the plugin.
146147
*
@@ -323,12 +324,21 @@ export function handleRunCommand(
323324
*/
324325
export function runScript() {
325326
const baseDir = path.resolve(defaultPluginsDir());
327+
const userPluginsDir = path.resolve(defaultUserPluginsDir());
328+
const staticPluginsDir = path.resolve(path.join(process.resourcesPath, '.plugins'));
326329
const scriptPath = path.resolve(process.argv[1]);
327330

328-
if (!scriptPath.startsWith(baseDir)) {
329-
console.error(`Invalid script path: ${scriptPath}. Must be within ${baseDir}.`);
330-
return;
331+
if (
332+
!scriptPath.startsWith(baseDir) &&
333+
!scriptPath.startsWith(userPluginsDir) &&
334+
!scriptPath.startsWith(staticPluginsDir)
335+
) {
336+
console.error(
337+
`Invalid script path: ${scriptPath}. Must be within ${baseDir}, ${userPluginsDir}, or ${staticPluginsDir}.`
338+
);
339+
process.exit(1);
331340
}
341+
332342
import(scriptPath);
333343
}
334344

0 commit comments

Comments
 (0)