Skip to content

Commit e4e4446

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 e4e4446

File tree

2 files changed

+77
-3
lines changed

2 files changed

+77
-3
lines changed

app/electron/runCmd.test.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,67 @@ describe('validateCommandData', () => {
220220
).toBe(true);
221221
});
222222
});
223+
224+
describe('runScript', () => {
225+
const originalArgv = process.argv;
226+
const originalExit = process.exit;
227+
const originalConsoleError = console.error;
228+
const originalResourcesPath = process.resourcesPath;
229+
230+
let exitMock: jest.Mock;
231+
let consoleErrorMock: jest.Mock;
232+
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+
process.argv = ['node', scriptPath];
261+
jest.doMock(scriptPath, () => ({}), { virtual: true });
262+
const runCmdModule = await import('./runCmd');
263+
await runCmdModule.runScript();
264+
expect(exitMock).not.toHaveBeenCalled();
265+
};
266+
267+
it('imports the script when path is inside defaultPluginsDir', () =>
268+
testScriptImport('/plugins/default/my-script.js'));
269+
270+
it('imports the script when path is inside defaultUserPluginsDir', () =>
271+
testScriptImport('/plugins/user/my-script.js'));
272+
273+
it('imports the script when path is inside static .plugins dir', () =>
274+
testScriptImport('/resources/.plugins/my-script.js'));
275+
276+
it('exits with error when script is outside allowed directories', async () => {
277+
process.argv = ['node', '/not-allowed/my-script.js'];
278+
jest.doMock('/not-allowed/my-script.js', () => ({}), { virtual: true });
279+
280+
const runCmdModule = await import('./runCmd');
281+
await runCmdModule.runScript();
282+
283+
expect(consoleErrorMock).toHaveBeenCalledTimes(1);
284+
expect(exitMock).toHaveBeenCalledWith(1);
285+
});
286+
});

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)