Chrome DevTools for coding agents
fix(daemon): keep fallback socket in private runtime dir (#2766)
Robert McConnell871a37e4b467
diff --git a/src/daemon/client.ts b/src/daemon/client.tsindex 146e1fd5..07596a2d 100644--- a/src/daemon/client.ts+++ b/src/daemon/client.ts@@ -7,6 +7,7 @@import {spawn} from 'node:child_process';import fs from 'node:fs';import net from 'node:net';+import path from 'node:path';import type {CallToolResult} from '../third_party/index.js';import {PipeTransport} from '../third_party/index.js';@@ -120,6 +121,17 @@ export async function startDaemon(mcpArgs: string[] = [], sessionId: string) {const pidFilePath = getPidFilePath(sessionId);if (fs.existsSync(pidFilePath)) {+ if (process.platform !== 'win32') {+ const pidDir = path.dirname(pidFilePath);+ if (+ fs.lstatSync(pidDir).isSymbolicLink() ||+ fs.lstatSync(pidFilePath).isSymbolicLink()+ ) {+ throw new Error(+ `Refusing to remove daemon PID file through a symbolic link: ${pidFilePath}`,+ );+ }+ }fs.unlinkSync(pidFilePath);}diff --git a/src/daemon/daemon.ts b/src/daemon/daemon.tsindex 67174260..ff06b211 100644--- a/src/daemon/daemon.ts+++ b/src/daemon/daemon.ts@@ -51,9 +51,17 @@ try {if (os.platform() !== 'win32') {// POSIX specific checkstry {- const stats = fs.statSync(pidDir);+ const stats = fs.lstatSync(pidDir);- // 1. Check Ownership: Ensure the directory is owned by the current user.+ // 1. Reject symlinked runtime directories before checking ownership.+ if (stats.isSymbolicLink()) {+ console.error(+ `[MCP Daemon] Critical error: PID directory ${pidDir} must not be a symbolic link. Possible tampering.`,+ );+ process.exit(1);+ }++ // 2. Check Ownership: Ensure the directory is owned by the current user.if (stats.uid !== currentUserUid) {console.error(`[MCP Daemon] Critical error: PID directory ${pidDir} is not owned by the current user (Expected: ${currentUserUid}, Found: ${stats.uid}). Possible tampering.`,@@ -61,7 +69,7 @@ try {process.exit(1);}- // 2. Check Permissions: Ensure the directory is not group or world-writable.+ // 3. Check Permissions: Ensure the directory is not group or world-writable.// Mode is a number, e.g., 0o700. We check if bits for group/world write are set.const mode = stats.mode;if (mode & constants.S_IWGRP || mode & constants.S_IWOTH) {diff --git a/src/daemon/utils.ts b/src/daemon/utils.tsindex 9f4a4fa4..2aff8363 100644--- a/src/daemon/utils.ts+++ b/src/daemon/utils.ts@@ -29,7 +29,6 @@ export function assertValidSessionId(sessionId: string): void {// Using these paths due to strict limits on the POSIX socket path length.export function getSocketPath(sessionId: string): string {assertValidSessionId(sessionId);- const uid = os.userInfo().uid;const username = os.userInfo().username;const suffix = sessionId ? `-${sessionId}` : '';const appName = APP_NAME + suffix;@@ -41,15 +40,7 @@ export function getSocketPath(sessionId: string): string {return path.join('\\\\.\\pipe', `${appName}-${username}`, 'server.sock');}- // 1. Try XDG_RUNTIME_DIR (Linux standard, sometimes macOS)- if (process.env.XDG_RUNTIME_DIR) {- return path.join(process.env.XDG_RUNTIME_DIR, appName, 'server.sock');- }-- // 2. macOS/Unix Fallback: Use /tmp/- // We use /tmp/ because it is much shorter than ~/Library/Application Support/- // and keeps us well under the 104-character limit.- return path.join('/tmp', `${appName}-${uid}.sock`);+ return path.join(getRuntimeHome(sessionId), 'server.sock');}export function getRuntimeHome(sessionId: string): string {diff --git a/tests/daemon/symlink.test.ts b/tests/daemon/symlink.test.tsindex 3726464f..14ee9bc7 100644--- a/tests/daemon/symlink.test.ts+++ b/tests/daemon/symlink.test.ts@@ -12,6 +12,7 @@ import path from 'node:path';import process from 'node:process';import {describe, it, afterEach, beforeEach} from 'node:test';+import {startDaemon} from '../../src/daemon/client.js';import {DAEMON_SCRIPT_PATH,getPidFilePath,@@ -89,6 +90,101 @@ describe('daemon security checks', () => {}});+ it('startDaemon should reject a symlinked runtime directory before unlinking a stale PID file', async () => {+ if (IS_WINDOWS) {+ return;+ }+ const pidFilePath = getPidFilePath(sessionId);+ const pidDir = path.dirname(pidFilePath);+ const targetDir = path.join(+ path.dirname(pidDir),+ `chrome-devtools-mcp-client-symlink-target-${sessionId}`,+ );+ const targetPidFile = path.join(targetDir, 'daemon.pid');++ try {+ fs.mkdirSync(targetDir, {recursive: true, mode: 0o700});+ fs.writeFileSync(targetPidFile, 'original content', 'utf-8');+ fs.symlinkSync(targetDir, pidDir);++ await assert.rejects(startDaemon([], sessionId), /symbolic link/);+ assert.strictEqual(+ fs.readFileSync(targetPidFile, 'utf-8'),+ 'original content',+ );+ } finally {+ try {+ fs.unlinkSync(pidDir);+ } catch {+ // ignore+ }+ try {+ fs.unlinkSync(targetPidFile);+ } catch {+ // ignore+ }+ try {+ fs.rmdirSync(targetDir);+ } catch {+ // ignore+ }+ }+ });++ it('should reject a symlinked runtime directory', async () => {+ if (IS_WINDOWS) {+ return;+ }+ const pidFilePath = getPidFilePath(sessionId);+ const pidDir = path.dirname(pidFilePath);+ const targetDir = path.join(+ path.dirname(pidDir),+ `chrome-devtools-mcp-symlink-target-${sessionId}`,+ );+ const targetPidFile = path.join(targetDir, 'daemon.pid');++ try {+ fs.mkdirSync(targetDir, {recursive: true, mode: 0o700});+ fs.writeFileSync(targetPidFile, 'original content', 'utf-8');+ fs.symlinkSync(targetDir, pidDir);++ const child = spawn(process.execPath, [DAEMON_SCRIPT_PATH], {+ env: {+ ...process.env,+ CHROME_DEVTOOLS_MCP_SESSION_ID: sessionId,+ },+ });++ const exitCode = await new Promise<number | null>(resolve => {+ child.on('exit', code => {+ resolve(code);+ });+ });++ assert.strictEqual(exitCode, 1);+ assert.strictEqual(+ fs.readFileSync(targetPidFile, 'utf-8'),+ 'original content',+ );+ } finally {+ try {+ fs.unlinkSync(pidDir);+ } catch {+ // ignore+ }+ try {+ fs.unlinkSync(targetPidFile);+ } catch {+ // ignore+ }+ try {+ fs.rmdirSync(targetDir);+ } catch {+ // ignore+ }+ }+ });+it('should fail if directory has insecure permissions (group/world writable)', async () => {if (IS_WINDOWS) {return;diff --git a/tests/daemon/utils.test.ts b/tests/daemon/utils.test.tsindex 05366cfb..24f2ad4c 100644--- a/tests/daemon/utils.test.ts+++ b/tests/daemon/utils.test.ts@@ -223,15 +223,17 @@ describe('getSocketPath', () => {});it(- 'falls back to a /tmp socket with the uid when XDG_RUNTIME_DIR is unset',+ 'falls back to the private runtime directory when XDG_RUNTIME_DIR is unset',{skip: IS_WINDOWS},() => {delete process.env['XDG_RUNTIME_DIR'];- const uid = os.userInfo().uid;- assert.strictEqual(getSocketPath(''), `/tmp/${APP_NAME}-${uid}.sock`);+ assert.strictEqual(+ getSocketPath(''),+ path.join(getRuntimeHome(''), 'server.sock'),+ );assert.strictEqual(getSocketPath(SESSION_ID),- `/tmp/${APP_NAME}-${SESSION_ID}-${uid}.sock`,+ path.join(getRuntimeHome(SESSION_ID), 'server.sock'),);},);