fix(terminal): reject file cwd values (#1204)

* fix(terminal): reject file cwd values

* test(terminal): remove stale cwd access mock

---------

Co-authored-by: Isaac Sanchez <isanchez-hawkins@arize.com>
Co-authored-by: Bohdan Triapitsyn <artmore@protonmail.com>
This commit is contained in:
Isaac Sanchez-Hawkins
2026-05-12 11:30:10 +03:00
committed by GitHub
co-authored by Isaac Sanchez Bohdan Triapitsyn
parent 89630af98d
commit 644050824e
2 changed files with 58 additions and 3 deletions
+4 -1
View File
@@ -503,7 +503,10 @@ export function createTerminalRuntime({
}
try {
await fs.promises.access(cwd);
const stats = await fs.promises.stat(cwd);
if (!stats.isDirectory()) {
return res.status(400).json({ error: 'Invalid working directory' });
}
} catch {
return res.status(400).json({ error: 'Invalid working directory' });
}
@@ -5,8 +5,23 @@ import { describe, expect, it } from 'vitest';
import { createTerminalRuntime } from './runtime.js';
function createRuntime(server) {
const app = {
function createResponse() {
return {
statusCode: 200,
body: null,
status(code) {
this.statusCode = code;
return this;
},
json(payload) {
this.body = payload;
return this;
},
};
}
function createRuntime(server, overrides = {}) {
const app = overrides.app ?? {
post() {},
get() {},
delete() {},
@@ -27,10 +42,47 @@ function createRuntime(server) {
TERMINAL_INPUT_WS_HEARTBEAT_INTERVAL_MS: 30_000,
TERMINAL_INPUT_WS_REBIND_WINDOW_MS: 1_000,
TERMINAL_INPUT_WS_MAX_REBINDS_PER_WINDOW: 3,
...overrides,
});
}
describe('terminal runtime', () => {
it('rejects regular files as terminal working directories', async () => {
const postRoutes = new Map();
const app = {
post(route, ...handlers) {
postRoutes.set(route, handlers.at(-1));
},
get() {},
delete() {},
};
const server = new EventEmitter();
const runtime = createRuntime(server, {
app,
fs: {
promises: {
stat: async () => ({ isDirectory: () => false }),
},
},
uiAuthController: { enabled: false },
buildAugmentedPath: () => '',
TERMINAL_INPUT_WS_HEARTBEAT_INTERVAL_MS: 1000,
TERMINAL_INPUT_WS_REBIND_WINDOW_MS: 1000,
});
try {
const createRoute = postRoutes.get('/api/terminal/create');
const res = createResponse();
await createRoute({ body: { cwd: '/tmp/not-a-directory' } }, res);
expect(res.statusCode).toBe(400);
expect(res.body).toEqual({ error: 'Invalid working directory' });
} finally {
await runtime.shutdown();
}
});
it('removes its websocket upgrade listener on shutdown', async () => {
const server = new EventEmitter();
const runtime = createRuntime(server);