Skip to content

Chrome DevTools for coding agents

fix(input): validate press_key and submitKey parameters in Zod schema (#3382)

Nikolay Vitkov375a269ee090
diff --git a/src/ToolHandler.ts b/src/ToolHandler.ts
index b1496b44..ead3fa13 100644
--- a/src/ToolHandler.ts
+++ b/src/ToolHandler.ts
@@ -555,7 +555,7 @@ export class ToolHandler {
} finally {
void ClearcutLogger.get()?.logToolInvocation({
toolName: tool.name,
- params,
+ params: rawArgs,
schema: tool.schema,
success,
latencyMs: Date.now() - startTime,
diff --git a/src/telemetry/transformation.ts b/src/telemetry/transformation.ts
index d385daf9..43d97363 100644
--- a/src/telemetry/transformation.ts
+++ b/src/telemetry/transformation.ts
@@ -247,7 +247,7 @@ export function sanitizeParams(
): ShapeOutput<zod.ZodRawShape> {
const transformed: ShapeOutput<zod.ZodRawShape> = {};
for (const [name, value] of Object.entries(params)) {
- if (PARAM_BLOCKLIST.has(name)) {
+ if (value === undefined || PARAM_BLOCKLIST.has(name)) {
continue;
}
const zodType = getZodType(schema[name]);
diff --git a/src/tools/input.ts b/src/tools/input.ts
index 76c8d1f9..91cfb2c9 100644
--- a/src/tools/input.ts
+++ b/src/tools/input.ts
@@ -11,13 +11,14 @@ import {
type KeyInput,
} from '../third_party/index.js';
import type {TextSnapshotNode} from '../types.js';
-import {invalidKeyMessage, isValidKey, parseKey} from '../utils/keyboard.js';
+import {parseKey, throwIfInvalidKey} from '../utils/keyboard.js';
import {logger} from '../utils/logger.js';
import type {WaitForEventsResult} from '../utils/WaitForHelper.js';
import {ToolCategory} from './categories.js';
import {
definePageTool,
+ zodTransform,
type ContextPage,
type PageToolFactory,
} from './ToolDefinition.js';
@@ -35,9 +36,11 @@ const includeSnapshotSchema = zod
const submitKeySchema = zod
.string()
// An empty string means no submit key, as before.
- .refine((key): boolean => key === '' || isValidKey(key), {
- error: issue => invalidKeyMessage(String(issue.input)),
- })
+ .transform(
+ zodTransform((key): KeyInput | undefined =>
+ key === '' ? undefined : throwIfInvalidKey(key),
+ ),
+ )
.optional()
.describe(
'Optional key to press after typing. E.g., "Enter", "Tab", "Escape"',
@@ -461,8 +464,7 @@ export const typeText: PageToolFactory = definePageTool(() => ({
const submitKey = request.params.submitKey;
const result = await page.waitForEventsAfterAction(async () => {
await page.pptrPage.keyboard.type(request.params.text);
- // submitKeySchema has already validated the key; isValidKey narrows the type.
- if (submitKey && isValidKey(submitKey)) {
+ if (submitKey) {
await page.pptrPage.keyboard.press(submitKey);
}
});
@@ -642,6 +644,7 @@ export const pressKey: PageToolFactory = definePageTool(() => ({
schema: {
key: zod
.string()
+ .transform(zodTransform(parseKey))
.describe(
'A key or a combination (e.g., "Enter", "Control+A", "Control++", "Control+Shift+R"). Modifiers: Control, Shift, Alt, Meta',
),
@@ -652,8 +655,7 @@ export const pressKey: PageToolFactory = definePageTool(() => ({
verifyFilesSchema: {},
handler: async (request, response) => {
const page = request.page;
- const tokens = parseKey(request.params.key);
- const [key, ...modifiers] = tokens;
+ const [key, ...modifiers] = request.params.key;
const result = await page.waitForEventsAfterAction(async () => {
const heldModifiers: KeyInput[] = [];
@@ -674,7 +676,7 @@ export const pressKey: PageToolFactory = definePageTool(() => ({
});
response.appendResponseLine(
- `Successfully pressed key: ${request.params.key}`,
+ `Successfully pressed key: ${[...modifiers, key].join('+')}`,
);
response.attachWaitForResult(result);
if (request.params.includeSnapshot) {
diff --git a/src/utils/keyboard.ts b/src/utils/keyboard.ts
index 194d14b6..0f91b06f 100644
--- a/src/utils/keyboard.ts
+++ b/src/utils/keyboard.ts
@@ -272,14 +272,11 @@ export function isValidKey(key: string): key is KeyInput {
return validKeys.has(key);
}
-/**
- * Error message for a string that is not a valid key name.
- */
-export function invalidKeyMessage(key: string): string {
+function invalidKeyMessage(key: string): string {
return `${key} is invalid. Valid keys are: ${Array.from(validKeys.values()).join(',')}.`;
}
-function throwIfInvalidKey(key: string): KeyInput {
+export function throwIfInvalidKey(key: string): KeyInput {
if (isValidKey(key)) {
return key;
}
@@ -305,7 +302,8 @@ export function parseKey(keyInput: string): [KeyInput, ...KeyInput[]] {
result.push(throwIfInvalidKey(key));
}
- if (result.length === 0) {
+ const primaryKey = result.at(-1);
+ if (primaryKey === undefined) {
throw new Error(`Key ${keyInput} could not be parsed.`);
}
@@ -313,5 +311,5 @@ export function parseKey(keyInput: string): [KeyInput, ...KeyInput[]] {
throw new Error(`Key ${keyInput} contains duplicate keys.`);
}
- return [result.at(-1), ...result.slice(0, -1)] as [KeyInput, ...KeyInput[]];
+ return [primaryKey, ...result.slice(0, -1)];
}
diff --git a/tests/ToolHandler.test.ts b/tests/ToolHandler.test.ts
index aa35802e..add8f1de 100644
--- a/tests/ToolHandler.test.ts
+++ b/tests/ToolHandler.test.ts
@@ -18,6 +18,9 @@ import type {McpContext} from '../src/McpContext.js';
import {McpPage} from '../src/McpPage.js';
import {McpResponse, type DataFormat} from '../src/McpResponse.js';
import {ClearcutLogger} from '../src/telemetry/ClearcutLogger.js';
+import {FilePersistence} from '../src/telemetry/persistence.js';
+import {McpClient, WatchdogMessageType} from '../src/telemetry/types.js';
+import {WatchdogClient} from '../src/telemetry/WatchdogClient.js';
import {
ProtocolError,
ProtocolErrorCode,
@@ -25,6 +28,8 @@ import {
} from '../src/third_party/index.js';
import {TOOL_CALL_TIMEOUT_MS, ToolHandler} from '../src/ToolHandler.js';
import {ToolCategory} from '../src/tools/categories.js';
+import {emulate} from '../src/tools/emulation.js';
+import {pressKey, typeText} from '../src/tools/input.js';
import {
defineBrowserlessTool,
definePageTool,
@@ -805,6 +810,101 @@ describe('ToolHandler', () => {
}
});
+ it('sanitizes raw validated args for tools with transformed schemas', async () => {
+ sinon.stub(Date, 'now').returns(1000);
+ sinon
+ .stub(McpResponse.prototype, 'handle')
+ .resolves({content: [], structuredContent: {}});
+ const config = parseConfig(['--no-page-id-routing']);
+ const mockContext = createMockMcpContext();
+ mockContext.ensureBrowser.resolves(
+ getMockBrowser({process: sinon.createStubInstance(ChildProcess)}),
+ );
+ mockContext.getSelectedMcpPage.returns(sinon.createStubInstance(McpPage));
+ mockContext.getDevToolsData.resolves({});
+
+ const mockPersistence = sinon.createStubInstance(FilePersistence, {
+ loadState: Promise.resolve({lastToolCall: new Date().toISOString()}),
+ });
+ const mockWatchdogClient = sinon.createStubInstance(WatchdogClient);
+ ClearcutLogger.initialize({
+ persistence: mockPersistence,
+ appVersion: '1.0.0',
+ watchdogClient: mockWatchdogClient,
+ });
+ await Promise.resolve();
+
+ const testCases: Array<{
+ tool: DefinedPageTool;
+ rawArgs: Record<string, unknown>;
+ expectedParams: Record<string, unknown>;
+ }> = [
+ {
+ tool: pressKey(config.resolved),
+ rawArgs: {key: 'Control+Shift+C'},
+ expectedParams: {
+ press_key_params: {
+ key_length: 20,
+ },
+ },
+ },
+ {
+ tool: typeText(config.resolved),
+ rawArgs: {text: 'hello', submitKey: ''},
+ expectedParams: {
+ type_text_params: {
+ text_length: 5,
+ submit_key_length: 0,
+ },
+ },
+ },
+ {
+ tool: emulate(config.resolved),
+ rawArgs: {
+ viewport: '400x400x1',
+ geolocation: '37.7749,-122.4194',
+ extraHttpHeaders: '{"X-Custom":"1"}',
+ },
+ expectedParams: {
+ emulate_params: {
+ viewport_length: 10,
+ geolocation_length: 20,
+ extra_http_headers_length: 20,
+ },
+ },
+ },
+ ];
+
+ for (const {tool, rawArgs, expectedParams} of testCases) {
+ mockWatchdogClient.send.resetHistory();
+ const toolHandler = new ToolHandler(
+ tool,
+ config,
+ async () => mockContext,
+ sinon.spy(),
+ );
+
+ const result = await toolHandler.handle(rawArgs);
+
+ assert.strictEqual(result.isError, undefined);
+ sinon.assert.calledOnceWithExactly(mockWatchdogClient.send, {
+ type: WatchdogMessageType.LOG_EVENT,
+ payload: {
+ mcp_client: McpClient.MCP_CLIENT_UNSPECIFIED,
+ tool_invocation: {
+ tool_name: tool.name,
+ success: true,
+ latency_ms: 50,
+ context: {
+ is_devtools_open: false,
+ },
+ tool_params: expectedParams,
+ },
+ },
+ });
+ }
+ });
+
it('rejects unknown registered tool arguments and sets additionalProperties to false', () => {
const tool: ToolDefinition = {
name: 'strict_tool',
diff --git a/tests/telemetry/transformation.test.ts b/tests/telemetry/transformation.test.ts
index 99d81655..7777fa6b 100644
--- a/tests/telemetry/transformation.test.ts
+++ b/tests/telemetry/transformation.test.ts
@@ -137,6 +137,25 @@ describe('sanitizeParams', () => {
});
});
+ it('skips undefined parameter values', () => {
+ const schema = {
+ myString: zod.string(),
+ optionalString: zod.string().optional(),
+ };
+
+ const sanitized = sanitizeParams(
+ {
+ myString: 'hello',
+ optionalString: undefined,
+ },
+ schema,
+ );
+
+ assert.deepStrictEqual(sanitized, {
+ my_string_length: 5,
+ });
+ });
+
it('bucketizes string lengths correctly', () => {
const schema = {
str0: zod.string(),
diff --git a/tests/tools/input.test.ts b/tests/tools/input.test.ts
index ed823d52..1169d711 100644
--- a/tests/tools/input.test.ts
+++ b/tests/tools/input.test.ts
@@ -11,7 +11,7 @@ import sinon from 'sinon';
import {McpResponse} from '../../src/McpResponse.js';
import {TextSnapshot} from '../../src/TextSnapshot.js';
-import {TimeoutError} from '../../src/third_party/index.js';
+import {TimeoutError, zod} from '../../src/third_party/index.js';
import {
click,
hover,
@@ -1821,7 +1821,7 @@ describe('input', () => {
await pressKey(args).handler(
{
params: {
- key: 'Control+Shift+C',
+ key: ['C', 'Control', 'Shift'],
},
page: context.getSelectedMcpPage(),
},
@@ -1829,6 +1829,10 @@ describe('input', () => {
context,
);
+ assert.strictEqual(
+ response.responseLines[0],
+ 'Successfully pressed key: Control+Shift+C',
+ );
assert.deepStrictEqual(await page.evaluate('logs'), [
'dControl',
'dShift',
@@ -1840,6 +1844,68 @@ describe('input', () => {
});
});
+ it('parses valid keys and rejects invalid keys in press_key and type_text schemas', () => {
+ const {args} = createHandlerMocks();
+ const pressKeySchema = zod.object(pressKey(args).schema);
+ assert.deepStrictEqual(
+ pressKeySchema.safeParse({pageId: 1, key: 'Control+Shift+C'}),
+ {
+ success: true,
+ data: {
+ pageId: 1,
+ key: ['C', 'Control', 'Shift'],
+ },
+ },
+ );
+ const invalidPressKey = pressKeySchema.safeParse({
+ pageId: 1,
+ key: 'NotAKey',
+ });
+ assert.strictEqual(invalidPressKey.success, false);
+ assert.match(
+ invalidPressKey.error.issues[0].message,
+ /^NotAKey is invalid\. Valid keys are: /,
+ );
+
+ const typeTextSchema = zod.object(typeText(args).schema);
+ assert.deepStrictEqual(
+ typeTextSchema.safeParse({
+ pageId: 1,
+ text: 'hello',
+ submitKey: 'Enter',
+ }),
+ {
+ success: true,
+ data: {
+ pageId: 1,
+ text: 'hello',
+ submitKey: 'Enter',
+ },
+ },
+ );
+ assert.deepStrictEqual(
+ typeTextSchema.safeParse({pageId: 1, text: 'hello', submitKey: ''}),
+ {
+ success: true,
+ data: {
+ pageId: 1,
+ text: 'hello',
+ submitKey: undefined,
+ },
+ },
+ );
+ const invalidSubmitKey = typeTextSchema.safeParse({
+ pageId: 1,
+ text: 'hello',
+ submitKey: 'NotAKey',
+ });
+ assert.strictEqual(invalidSubmitKey.success, false);
+ assert.match(
+ invalidSubmitKey.error.issues[0].message,
+ /^NotAKey is invalid\. Valid keys are: /,
+ );
+ });
+
it('releases held modifiers when the main key press fails', async () => {
await withMcpContext(async (response, context, args) => {
const page = context.getSelectedMcpPage().pptrPage;
@@ -1865,7 +1931,7 @@ describe('input', () => {
pressKey(args).handler(
{
params: {
- key: 'Control+Shift+C',
+ key: ['C', 'Control', 'Shift'],
},
page: context.getSelectedMcpPage(),
},