Skip to content

Chrome DevTools for coding agents

refactor: reuse Zod schemas and validate integer viewport dimensions (#3367)

Nikolay Vitkov3c609da38a39
diff --git a/src/tools/ToolDefinition.ts b/src/tools/ToolDefinition.ts
index 227f8be2..a5f74ecf 100644
--- a/src/tools/ToolDefinition.ts
+++ b/src/tools/ToolDefinition.ts
@@ -698,10 +698,20 @@ export function zodTransform<Input, Output>(
try {
return transform(arg);
} catch (error) {
- ctx.addIssue({
- code: 'custom',
- message: error instanceof Error ? error.message : String(error),
- });
+ if (error instanceof zod.ZodError) {
+ for (const issue of error.issues) {
+ ctx.addIssue({
+ code: 'custom',
+ message: issue.message,
+ path: issue.path,
+ });
+ }
+ } else {
+ ctx.addIssue({
+ code: 'custom',
+ message: error instanceof Error ? error.message : String(error),
+ });
+ }
return zod.NEVER;
}
};
@@ -713,28 +723,51 @@ export const timeoutSchema: {
timeout: zod
.number()
.int()
- .transform(
- zodTransform(value => {
- return value <= 0 ? undefined : value;
- }),
- )
+ .transform(value => {
+ return value <= 0 ? undefined : value;
+ })
.optional()
.describe(
`Maximum wait time in milliseconds. If set to 0, the default timeout will be used.`,
),
};
-const DECIMAL = /^[+-]?(\d+(\.\d*)?|\.\d+)$/;
-const VALID_VIEWPORT_TAGS = new Set(['mobile', 'touch', 'landscape']);
-
-function parseDecimal(part: string | undefined, name: string): number {
- if (part === undefined || !DECIMAL.test(part)) {
- throw new Error(
- `Invalid ${name} "${part ?? ''}". Expected a decimal number.`,
- );
- }
- return Number(part);
-}
+const integerStringSchema = zod
+ .string()
+ .regex(/^[+-]?\d+$/, 'Expected an integer')
+ .pipe(zod.coerce.number());
+
+const decimalStringSchema = zod
+ .string()
+ .regex(/^[+-]?\d+(\.\d+)?$/, 'Expected a number')
+ .pipe(zod.coerce.number());
+
+/* eslint-disable @local/enforce-zod-schema */
+const geolocationSchema = zod.object({
+ latitude: decimalStringSchema.pipe(zod.number().min(-90).max(90)),
+ longitude: decimalStringSchema.pipe(zod.number().min(-180).max(180)),
+});
+
+const VIEWPORT_FORMAT =
+ '<width>x<height>x<devicePixelRatio>[,mobile][,touch][,landscape]';
+
+const viewportTagSchema = zod.enum(['mobile', 'touch', 'landscape'], {
+ error: issue =>
+ `Invalid viewport option "${String(issue.input)}". Expected "mobile", "touch", or "landscape" in '${VIEWPORT_FORMAT}'.`,
+});
+
+// CDP's Emulation.setDeviceMetricsOverride takes integer width and height.
+const viewportSchema = zod.object({
+ width: integerStringSchema.pipe(zod.number().int().positive()),
+ height: integerStringSchema.pipe(zod.number().int().positive()),
+ deviceScaleFactor: decimalStringSchema
+ .pipe(zod.number().positive())
+ .optional(),
+ isMobile: zod.boolean().optional(),
+ isLandscape: zod.boolean().optional(),
+ hasTouch: zod.boolean().optional(),
+});
+/* eslint-enable @local/enforce-zod-schema */
export function viewportTransform(arg: string | undefined):
| {
@@ -751,51 +784,22 @@ export function viewportTransform(arg: string | undefined):
}
const [dimensions, ...tags] = arg.split(',');
for (const tag of tags) {
- if (!VALID_VIEWPORT_TAGS.has(tag)) {
- throw new Error(
- `Invalid viewport tag "${tag}". Expected format '<width>x<height>x<devicePixelRatio>[,mobile][,touch][,landscape]'.`,
- );
- }
+ viewportTagSchema.parse(tag);
}
- const isMobile = tags.includes('mobile');
- const hasTouch = tags.includes('touch');
- const isLandscape = tags.includes('landscape');
const dimParts = dimensions.split('x');
if (dimParts.length !== 2 && dimParts.length !== 3) {
throw new Error(
- `Invalid viewport dimensions "${dimensions}". Expected format '<width>x<height>x<devicePixelRatio>[,mobile][,touch][,landscape]'.`,
- );
- }
- const width = parseDecimal(dimParts[0], 'viewport width');
- if (!Number.isFinite(width) || width <= 0) {
- throw new Error(
- `Invalid viewport width "${width}". Expected format '<width>x<height>x<devicePixelRatio>[,mobile][,touch][,landscape]' with a positive width.`,
- );
- }
- const height = parseDecimal(dimParts[1], 'viewport height');
- if (!Number.isFinite(height) || height <= 0) {
- throw new Error(
- `Invalid viewport height "${height}". Expected format '<width>x<height>x<devicePixelRatio>[,mobile][,touch][,landscape]' with a positive height.`,
+ `Invalid viewport dimensions "${dimensions}". Expected format '${VIEWPORT_FORMAT}'.`,
);
}
- let deviceScaleFactor: number | undefined;
- if (dimParts.length === 3) {
- const dpr = parseDecimal(dimParts[2], 'devicePixelRatio');
- if (!Number.isFinite(dpr) || dpr <= 0) {
- throw new Error(
- `Invalid devicePixelRatio "${dpr}". Expected a positive number.`,
- );
- }
- deviceScaleFactor = dpr;
- }
- return {
- width,
- height,
- deviceScaleFactor,
- isMobile,
- isLandscape,
- hasTouch,
- };
+ return viewportSchema.parse({
+ width: dimParts[0],
+ height: dimParts[1],
+ deviceScaleFactor: dimParts[2],
+ isMobile: tags.includes('mobile'),
+ isLandscape: tags.includes('landscape'),
+ hasTouch: tags.includes('touch'),
+ });
}
export function geolocationTransform(
@@ -810,22 +814,10 @@ export function geolocationTransform(
`Invalid geolocation "${arg}". Expected '<latitude>,<longitude>'.`,
);
}
- const latitude = parseDecimal(parts[0], 'latitude');
- if (!Number.isFinite(latitude) || latitude < -90 || latitude > 90) {
- throw new Error(
- `Invalid latitude "${latitude}". Latitude must be a number between -90 and 90.`,
- );
- }
- const longitude = parseDecimal(parts[1], 'longitude');
- if (!Number.isFinite(longitude) || longitude < -180 || longitude > 180) {
- throw new Error(
- `Invalid longitude "${longitude}". Longitude must be a number between -180 and 180.`,
- );
- }
- return {
- latitude,
- longitude,
- };
+ return geolocationSchema.parse({
+ latitude: parts[0],
+ longitude: parts[1],
+ });
}
export function isSlimTool(
diff --git a/tests/McpServer.test.ts b/tests/McpServer.test.ts
index 4e6d73f1..c51bd22c 100644
--- a/tests/McpServer.test.ts
+++ b/tests/McpServer.test.ts
@@ -154,9 +154,21 @@ describe('McpServer', () => {
{pageId: 1, geolocation: '0x0'},
/^Input validation error: Invalid arguments for tool emulate: geolocation: Invalid geolocation "0x0"\. Expected '<latitude>,<longitude>'\.$/,
],
+ [
+ {pageId: 1, geolocation: 'abc,11.5'},
+ /^Input validation error: Invalid arguments for tool emulate: geolocation\.latitude: Expected a number$/,
+ ],
[
{pageId: 1, viewport: '0x0'},
- /^Input validation error: Invalid arguments for tool emulate: viewport: Invalid viewport width "0"\./,
+ /^Input validation error: Invalid arguments for tool emulate: viewport\.width: Too small: expected number to be >0/,
+ ],
+ [
+ {pageId: 1, viewport: '1280.5x720'},
+ /^Input validation error: Invalid arguments for tool emulate: viewport\.width: Expected an integer$/,
+ ],
+ [
+ {pageId: 1, viewport: '1280x720,bogus'},
+ /^Input validation error: Invalid arguments for tool emulate: viewport: Invalid viewport option "bogus"\. Expected "mobile", "touch", or "landscape" in '<width>x<height>x<devicePixelRatio>\[,mobile\]\[,touch\]\[,landscape\]'\.$/,
],
[
{pageId: 1, extraHttpHeaders: 'notjson'},
diff --git a/tests/tools/emulation.test.ts b/tests/tools/emulation.test.ts
index f6faa47a..aef7663d 100644
--- a/tests/tools/emulation.test.ts
+++ b/tests/tools/emulation.test.ts
@@ -80,6 +80,25 @@ describe('emulation', () => {
assert.throws(() => viewportTransform('800x0'));
});
+ it('throws when width or height is not an integer', () => {
+ assert.throws(
+ () => viewportTransform('1280.5x720'),
+ /Expected an integer/,
+ );
+ assert.throws(
+ () => viewportTransform('1280x720.5'),
+ /Expected an integer/,
+ );
+ assert.throws(
+ () => viewportTransform('1280.0x720'),
+ /Expected an integer/,
+ );
+ assert.throws(
+ () => viewportTransform('1280x720.'),
+ /Expected an integer/,
+ );
+ });
+
it('throws when devicePixelRatio is not positive', () => {
assert.throws(() => viewportTransform('1024x768x0'));
});
@@ -557,11 +576,81 @@ describe('emulation', () => {
'Invalid geolocation "0x0". Expected \'<latitude>,<longitude>\'.',
);
+ const nonNumericGeoResult = schema.safeParse({
+ pageId: 1,
+ geolocation: 'abc,11.5',
+ });
+ assert.strictEqual(nonNumericGeoResult.success, false);
+ assert.deepStrictEqual(nonNumericGeoResult.error.issues[0].path, [
+ 'geolocation',
+ 'latitude',
+ ]);
+ assert.strictEqual(
+ nonNumericGeoResult.error.issues[0].message,
+ 'Expected a number',
+ );
+
const viewportResult = schema.safeParse({pageId: 1, viewport: '0x0'});
assert.strictEqual(viewportResult.success, false);
+ assert.deepStrictEqual(viewportResult.error.issues[0].path, [
+ 'viewport',
+ 'width',
+ ]);
assert.strictEqual(
viewportResult.error.issues[0].message,
- 'Invalid viewport width "0". Expected format \'<width>x<height>x<devicePixelRatio>[,mobile][,touch][,landscape]\' with a positive width.',
+ 'Too small: expected number to be >0',
+ );
+
+ const nonNumericViewportResult = schema.safeParse({
+ pageId: 1,
+ viewport: 'abcx720',
+ });
+ assert.strictEqual(nonNumericViewportResult.success, false);
+ assert.deepStrictEqual(nonNumericViewportResult.error.issues[0].path, [
+ 'viewport',
+ 'width',
+ ]);
+ assert.strictEqual(
+ nonNumericViewportResult.error.issues[0].message,
+ 'Expected an integer',
+ );
+
+ const fractionalViewportResult = schema.safeParse({
+ pageId: 1,
+ viewport: '1280.5x720',
+ });
+ assert.strictEqual(fractionalViewportResult.success, false);
+ assert.deepStrictEqual(fractionalViewportResult.error.issues[0].path, [
+ 'viewport',
+ 'width',
+ ]);
+ assert.strictEqual(
+ fractionalViewportResult.error.issues[0].message,
+ 'Expected an integer',
+ );
+
+ const invalidTagResult = schema.safeParse({
+ pageId: 1,
+ viewport: '1280x720,bogus',
+ });
+ assert.strictEqual(invalidTagResult.success, false);
+ assert.deepStrictEqual(invalidTagResult.error.issues[0].path, [
+ 'viewport',
+ ]);
+ assert.strictEqual(
+ invalidTagResult.error.issues[0].message,
+ 'Invalid viewport option "bogus". Expected "mobile", "touch", or "landscape" in \'<width>x<height>x<devicePixelRatio>[,mobile][,touch][,landscape]\'.',
+ );
+
+ const emptyTagResult = schema.safeParse({
+ pageId: 1,
+ viewport: '1280x720,',
+ });
+ assert.strictEqual(emptyTagResult.success, false);
+ assert.deepStrictEqual(emptyTagResult.error.issues[0].path, ['viewport']);
+ assert.strictEqual(
+ emptyTagResult.error.issues[0].message,
+ 'Invalid viewport option "". Expected "mobile", "touch", or "landscape" in \'<width>x<height>x<devicePixelRatio>[,mobile][,touch][,landscape]\'.',
);
const invalidJsonResult = schema.safeParse({