Skip to content

Chrome DevTools for coding agents

fix(tools): validate JSON object tool parameters in Zod schemas (#3383)

Nikolay Vitkov186d82cbd662
diff --git a/src/tools/ToolDefinition.ts b/src/tools/ToolDefinition.ts
index dde134f7..efd005db 100644
--- a/src/tools/ToolDefinition.ts
+++ b/src/tools/ToolDefinition.ts
@@ -824,6 +824,17 @@ export function geolocationTransform(
});
}
+const jsonObjectSchema = zod.record(zod.string(), zod.unknown());
+
+export function jsonObjectTransform(
+ arg: string | undefined,
+): Record<string, unknown> | undefined {
+ if (!arg) {
+ return undefined;
+ }
+ return jsonObjectSchema.parse(JSON.parse(arg));
+}
+
export function isSlimTool(
tool: Pick<BaseToolDefinition, 'annotations'>,
): boolean {
diff --git a/src/tools/thirdPartyDeveloper.ts b/src/tools/thirdPartyDeveloper.ts
index 706e20d3..917c1ebc 100644
--- a/src/tools/thirdPartyDeveloper.ts
+++ b/src/tools/thirdPartyDeveloper.ts
@@ -7,7 +7,12 @@
import {zod, ajv, type JSONSchema7} from '../third_party/index.js';
import {ToolCategory} from './categories.js';
-import {definePageTool, type PageToolFactory} from './ToolDefinition.js';
+import {
+ definePageTool,
+ jsonObjectTransform,
+ zodTransform,
+ type PageToolFactory,
+} from './ToolDefinition.js';
export interface ToolDefinition {
name: string;
@@ -79,6 +84,7 @@ export const executeThirdPartyDeveloperTool: PageToolFactory = definePageTool(
toolName: zod.string().describe('The name of the tool to execute'),
params: zod
.string()
+ .transform(zodTransform(jsonObjectTransform))
.optional()
.describe('The JSON-stringified parameters to pass to the tool'),
},
@@ -87,24 +93,7 @@ export const executeThirdPartyDeveloperTool: PageToolFactory = definePageTool(
verifyFilesSchema: {},
handler: async (request, response) => {
const toolName = request.params.toolName;
- let params: Record<string, unknown> = {};
- if (request.params.params) {
- try {
- const parsed = JSON.parse(request.params.params);
- if (
- typeof parsed === 'object' &&
- parsed !== null &&
- !Array.isArray(parsed)
- ) {
- params = parsed;
- } else {
- throw new Error('Parsed params is not an object');
- }
- } catch (e) {
- const errorMessage = e instanceof Error ? e.message : String(e);
- throw new Error(`Failed to parse params as JSON: ${errorMessage}`);
- }
- }
+ const params = request.params.params ?? {};
const toolGroups = await request.page.discoverThirdPartyDeveloperTools();
let tool;
diff --git a/src/tools/webmcp.ts b/src/tools/webmcp.ts
index 02fca423..fad86fd3 100644
--- a/src/tools/webmcp.ts
+++ b/src/tools/webmcp.ts
@@ -7,7 +7,12 @@
import {zod} from '../third_party/index.js';
import {ToolCategory} from './categories.js';
-import {definePageTool, type PageToolFactory} from './ToolDefinition.js';
+import {
+ definePageTool,
+ jsonObjectTransform,
+ zodTransform,
+ type PageToolFactory,
+} from './ToolDefinition.js';
export const listWebMcpTools: PageToolFactory = definePageTool(() => ({
name: 'list_webmcp_tools',
@@ -36,6 +41,7 @@ export const executeWebMcpTool: PageToolFactory = definePageTool(() => ({
toolName: zod.string().describe('The name of the WebMCP tool to execute'),
input: zod
.string()
+ .transform(zodTransform(jsonObjectTransform))
.optional()
.describe('The JSON-stringified parameters to pass to the WebMCP tool'),
},
@@ -44,25 +50,7 @@ export const executeWebMcpTool: PageToolFactory = definePageTool(() => ({
verifyFilesSchema: {},
handler: async (request, response) => {
const toolName = request.params.toolName;
-
- let input: Record<string, unknown> = {};
- if (request.params.input) {
- try {
- const parsed = JSON.parse(request.params.input);
- if (
- typeof parsed === 'object' &&
- parsed !== null &&
- !Array.isArray(parsed)
- ) {
- input = parsed;
- } else {
- throw new Error('Parsed input is not an object');
- }
- } catch (e) {
- const errorMessage = e instanceof Error ? e.message : String(e);
- throw new Error(`Failed to parse input as JSON: ${errorMessage}`);
- }
- }
+ const input = request.params.input ?? {};
const tools = request.page.getWebMcpTools();
const tool = tools.find(t => t.name === toolName);
diff --git a/tests/tools/thirdPartyDeveloper.test.ts b/tests/tools/thirdPartyDeveloper.test.ts
index 41a5c224..a2f81d19 100644
--- a/tests/tools/thirdPartyDeveloper.test.ts
+++ b/tests/tools/thirdPartyDeveloper.test.ts
@@ -13,10 +13,12 @@ import type {ParsedArguments} from '../../src/config/mcp-options.js';
import type {McpContext} from '../../src/McpContext.js';
import type {McpResponse} from '../../src/McpResponse.js';
import {TextSnapshot} from '../../src/TextSnapshot.js';
+import {zod} from '../../src/third_party/index.js';
import {
executeThirdPartyDeveloperTool,
listThirdPartyDeveloperTools,
} from '../../src/tools/thirdPartyDeveloper.js';
+import {jsonObjectTransform} from '../../src/tools/ToolDefinition.js';
import {createHandlerMocks} from '../mocks.js';
import {getTextContent, withMcpContext} from '../utils.js';
@@ -340,7 +342,7 @@ describe('thirdPartyDeveloperTools', () => {
{
params: {
toolName: 'test-tool',
- params: JSON.stringify({arg: 'value'}),
+ params: {arg: 'value'},
},
page,
},
@@ -379,7 +381,7 @@ describe('thirdPartyDeveloperTools', () => {
{
params: {
toolName: 'missing-tool',
- params: JSON.stringify({}),
+ params: {},
},
page,
},
@@ -394,21 +396,48 @@ describe('thirdPartyDeveloperTools', () => {
sinon.assert.notCalled(page.executeThirdPartyDeveloperTool);
});
- it('rejects JSON array params', async () => {
- const {page, context, response, args} = createHandlerMocks();
- await assert.rejects(
- executeThirdPartyDeveloperTool(args).handler(
- {
- params: {
- toolName: 'test-tool',
- params: '[]',
- },
- page,
+ it('parses valid JSON object params and rejects non-object or invalid JSON in schema', () => {
+ const {args} = createHandlerMocks();
+ const schema = zod.object(executeThirdPartyDeveloperTool(args).schema);
+
+ assert.strictEqual(jsonObjectTransform(undefined), undefined);
+ assert.strictEqual(jsonObjectTransform(''), undefined);
+ assert.deepStrictEqual(
+ schema.safeParse({
+ pageId: 1,
+ toolName: 'test-tool',
+ params: JSON.stringify({arg: 'value'}),
+ }),
+ {
+ success: true,
+ data: {
+ pageId: 1,
+ toolName: 'test-tool',
+ params: {arg: 'value'},
},
- response,
- context,
- ),
- {message: /Parsed params is not an object/},
+ },
+ );
+
+ const arrayResult = schema.safeParse({
+ pageId: 1,
+ toolName: 'test-tool',
+ params: '[]',
+ });
+ assert.strictEqual(arrayResult.success, false);
+ assert.strictEqual(
+ arrayResult.error.issues[0].message,
+ 'Invalid input: expected record, received array',
+ );
+
+ const invalidJsonResult = schema.safeParse({
+ pageId: 1,
+ toolName: 'test-tool',
+ params: 'invalid',
+ });
+ assert.strictEqual(invalidJsonResult.success, false);
+ assert.match(
+ invalidJsonResult.error.issues[0].message,
+ /Unexpected token 'i', "invalid" is not valid JSON/,
);
});
@@ -442,7 +471,7 @@ describe('thirdPartyDeveloperTools', () => {
{
params: {
toolName: 'test-tool',
- params: JSON.stringify({}), // Missing required 'arg'
+ params: {}, // Missing required 'arg'
},
page,
},
@@ -483,7 +512,7 @@ describe('thirdPartyDeveloperTools', () => {
{
params: {
toolName: 'test-tool',
- params: JSON.stringify({}),
+ params: {},
},
page: context.getSelectedMcpPage(),
},
@@ -567,7 +596,7 @@ describe('thirdPartyDeveloperTools', () => {
{
params: {
toolName: 'test-tool',
- params: JSON.stringify({element: {uid: 'some-uid'}}),
+ params: {element: {uid: 'some-uid'}},
},
page,
},
@@ -619,7 +648,7 @@ describe('thirdPartyDeveloperTools', () => {
{
params: {
toolName: 'test-tool',
- params: JSON.stringify({}),
+ params: {},
},
page: context.getSelectedMcpPage(),
},
@@ -666,7 +695,7 @@ describe('thirdPartyDeveloperTools', () => {
{
params: {
toolName: 'test-tool',
- params: JSON.stringify({}),
+ params: {},
},
page: context.getSelectedMcpPage(),
},
@@ -715,7 +744,7 @@ describe('thirdPartyDeveloperTools', () => {
{
params: {
toolName: 'test-tool',
- params: JSON.stringify({}),
+ params: {},
},
page: context.getSelectedMcpPage(),
},
@@ -783,7 +812,7 @@ describe('thirdPartyDeveloperTools', () => {
{
params: {
toolName: 'test-tool',
- params: JSON.stringify({}),
+ params: {},
},
page,
},
@@ -847,7 +876,7 @@ describe('thirdPartyDeveloperTools', () => {
{
params: {
toolName: 'test-tool',
- params: JSON.stringify({}),
+ params: {},
},
page,
},
@@ -900,7 +929,7 @@ describe('thirdPartyDeveloperTools', () => {
{
params: {
toolName: 'test-tool',
- params: JSON.stringify({}),
+ params: {},
},
page,
},
@@ -954,7 +983,7 @@ describe('thirdPartyDeveloperTools', () => {
{
params: {
toolName: 'test-tool',
- params: JSON.stringify({}),
+ params: {},
},
page,
},
@@ -1018,7 +1047,7 @@ describe('thirdPartyDeveloperTools', () => {
{
params: {
toolName: 'test-tool',
- params: JSON.stringify({}),
+ params: {},
},
page,
},
@@ -1067,7 +1096,7 @@ describe('thirdPartyDeveloperTools', () => {
{
params: {
toolName: 'test-tool',
- params: JSON.stringify({}),
+ params: {},
},
page,
},
@@ -1118,7 +1147,7 @@ describe('thirdPartyDeveloperTools', () => {
{
params: {
toolName: 'test-tool',
- params: JSON.stringify({}),
+ params: {},
},
page,
},
@@ -1135,7 +1164,7 @@ describe('thirdPartyDeveloperTools', () => {
{
params: {
toolName: 'test-tool',
- params: JSON.stringify({}),
+ params: {},
},
page,
},
diff --git a/tests/tools/webmcp.test.ts b/tests/tools/webmcp.test.ts
index c5df6a29..5eb8c101 100644
--- a/tests/tools/webmcp.test.ts
+++ b/tests/tools/webmcp.test.ts
@@ -10,6 +10,7 @@ import {describe, it} from 'node:test';
import sinon from 'sinon';
import type {McpPage} from '../../src/McpPage.js';
+import {zod} from '../../src/third_party/index.js';
import {listPages, navigatePage, selectPage} from '../../src/tools/pages.js';
import {executeWebMcpTool} from '../../src/tools/webmcp.js';
import {createHandlerMocks, createMockWebMCPTool} from '../mocks.js';
@@ -81,7 +82,7 @@ describe('webmcp', () => {
await toolsAddedPromise;
await executeWebMcpTool(args).handler(
- {params: {toolName: 'test_tool', input: JSON.stringify({})}, page},
+ {params: {toolName: 'test_tool', input: {}}, page},
response,
context,
);
@@ -102,7 +103,7 @@ describe('webmcp', () => {
async () => {
await executeWebMcpTool(args).handler(
{
- params: {toolName: 'missing-tool', input: JSON.stringify({})},
+ params: {toolName: 'missing-tool', input: {}},
page: context.getSelectedMcpPage(),
},
response,
@@ -124,7 +125,7 @@ describe('webmcp', () => {
await assert.rejects(
executeWebMcpTool(args).handler(
{
- params: {toolName: 'debug_tool', input: '{}'},
+ params: {toolName: 'debug_tool', input: {}},
page,
},
response,
@@ -149,7 +150,7 @@ describe('webmcp', () => {
await executeWebMcpTool(args).handler(
{
- params: {toolName: 'my_tool', input: '{"key":"val"}'},
+ params: {toolName: 'my_tool', input: {key: 'val'}},
page,
},
response,
@@ -238,43 +239,46 @@ describe('webmcp', () => {
);
});
- it('rejects JSON array input', async () => {
- const {page, context, response, args} = createHandlerMocks();
- await assert.rejects(
- executeWebMcpTool(args).handler(
- {
- params: {toolName: 'test_tool', input: '[]'},
- page,
+ it('rejects JSON array and invalid JSON input in schema', () => {
+ const {args} = createHandlerMocks();
+ const schema = zod.object(executeWebMcpTool(args).schema);
+
+ assert.deepStrictEqual(
+ schema.safeParse({
+ pageId: 1,
+ toolName: 'test_tool',
+ input: '{"key":"val"}',
+ }),
+ {
+ success: true,
+ data: {
+ pageId: 1,
+ toolName: 'test_tool',
+ input: {key: 'val'},
},
- response,
- context,
- ),
- {message: /Parsed input is not an object/},
+ },
);
- });
- it('throws if input is invalid', async () => {
- await withMcpContext(
- async (response, context, args) => {
- await assert.rejects(
- async () => {
- const page = context.getSelectedMcpPage();
- await setupWebMcpTool(page);
+ const arrayResult = schema.safeParse({
+ pageId: 1,
+ toolName: 'test_tool',
+ input: '[]',
+ });
+ assert.strictEqual(arrayResult.success, false);
+ assert.strictEqual(
+ arrayResult.error.issues[0].message,
+ 'Invalid input: expected record, received array',
+ );
- await executeWebMcpTool(args).handler(
- {params: {toolName: 'test_tool', input: 'invalid'}, page},
- response,
- context,
- );
- },
- {
- message:
- /Failed to parse input as JSON: Unexpected token 'i', "invalid" is not valid JSON/,
- },
- );
- },
- {args: ['--enable-features=WebMCP,DevToolsWebMCPSupport']},
- {categoryExperimentalWebmcp: true},
+ const invalidJsonResult = schema.safeParse({
+ pageId: 1,
+ toolName: 'test_tool',
+ input: 'invalid',
+ });
+ assert.strictEqual(invalidJsonResult.success, false);
+ assert.match(
+ invalidJsonResult.error.issues[0].message,
+ /Unexpected token 'i', "invalid" is not valid JSON/,
);
});
});