Skip to content

Chrome DevTools for coding agents

fix: keep temp files under the temp dir (#3372)

kevind666acf7136b
diff --git a/src/utils/files.ts b/src/utils/files.ts
index 086e6bb8..4976f1fc 100644
--- a/src/utils/files.ts
+++ b/src/utils/files.ts
@@ -9,6 +9,18 @@ import os from 'node:os';
import path from 'node:path';
export async function getTempFilePath(filename: string): Promise<string> {
+ // Reject names with directory components (e.g. `../x`) instead of writing
+ // the file somewhere other than the temp directory.
+ if (
+ filename !== path.basename(filename) ||
+ filename === '' ||
+ filename === '.' ||
+ filename === '..'
+ ) {
+ throw new Error(
+ `Invalid temp file name "${filename}": it must not contain a directory.`,
+ );
+ }
const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'chrome-devtools-mcp-'));
const filepath = path.join(dir, filename);
diff --git a/tests/utils.ts b/tests/utils.ts
index a016e81e..faf7b8af 100644
--- a/tests/utils.ts
+++ b/tests/utils.ts
@@ -37,6 +37,7 @@ import {HeapSnapshotManager} from '../src/processors/HeapSnapshotManager.js';
import {TaskRunner} from '../src/utils/TaskRunner.js';
import {TextSnapshot} from '../src/TextSnapshot.js';
import {DevTools, type CallToolResult} from '../src/third_party/index.js';
+import {getTempFilePath} from '../src/utils/files.js';
import {stableIdSymbol} from '../src/utils/id.js';
import {
@@ -611,3 +612,16 @@ export function createTempFile(
},
};
}
+
+export async function createDisposableTempFile(filename: string): Promise<{
+ path: string;
+ [Symbol.dispose]: () => void;
+}> {
+ const filepath = await getTempFilePath(filename);
+ return {
+ path: filepath,
+ [Symbol.dispose](): void {
+ fs.rmSync(path.dirname(filepath), {recursive: true, force: true});
+ },
+ };
+}
diff --git a/tests/utils/files.test.ts b/tests/utils/files.test.ts
index 41d17012..171313f6 100644
--- a/tests/utils/files.test.ts
+++ b/tests/utils/files.test.ts
@@ -10,8 +10,8 @@ import os from 'node:os';
import path from 'node:path';
import {describe, it} from 'node:test';
-import {resolveCanonicalPath} from '../../src/utils/files.js';
-import {createTempDir} from '../utils.js';
+import {getTempFilePath, resolveCanonicalPath} from '../../src/utils/files.js';
+import {createDisposableTempFile, createTempDir} from '../utils.js';
async function createSymlinkOrSkip(
t: it.TestContext,
@@ -161,3 +161,19 @@ describe('resolveCanonicalPath', () => {
);
});
});
+
+describe('getTempFilePath', () => {
+ it('keeps the file under the created temp directory', async () => {
+ using tempFile = await createDisposableTempFile('shot.png');
+ const dir = path.dirname(tempFile.path);
+ assert.strictEqual(path.basename(tempFile.path), 'shot.png');
+ assert.ok(path.basename(dir).startsWith('chrome-devtools-mcp-'));
+ assert.strictEqual(path.relative(dir, tempFile.path), 'shot.png');
+ });
+
+ for (const name of ['../escape.png', 'sub/shot.png', '..', '.', '']) {
+ it(`rejects "${name}" instead of writing outside the temp directory`, async () => {
+ await assert.rejects(getTempFilePath(name), /Invalid temp file name/);
+ });
+ }
+});