Skip to content

emulate accepts empty, extra and unknown parts in geolocation and viewport and silently uses 0 or drops thememulate accepts empty, extra and unknown parts in geolocation and viewport and silently uses 0 or drops them #3097

Closed

Description

@notlakshitha

Description of the bug

After #2662, geolocationTransform and viewportTransform in src/tools/ToolDefinition.ts check that each number is finite and in range. They still convert each part with Number(...) and ignore anything they do not expect. Number("") and Number(" ") are 0, so an empty part is read as zero instead of being rejected:

  • "37.77," (trailing comma, longitude missing) emulates latitude 37.77, longitude 0.
  • "," emulates 0, 0 (Null Island).
  • "37.77,-122.42,9" drops the third part without a warning.
  • "0x10,0x20" is accepted as 16, 32, because Number parses hex literals.
  • viewport: "1280x720,mobile,bogus" ignores the unknown tag bogus, and "1280x720x2x9" ignores the fourth part.

In each case the call reports success and the page runs with a location or viewport other than the one requested, so the agent has no way to tell that its input was misread.

Possible Solution

  1. Require exactly two parts for geolocation and two or three parts for the viewport dimensions, and reject empty parts.
  2. Accept only plain decimal numbers (no hex, no blanks) by matching each part against a decimal pattern before converting it.
  3. Reject viewport tags other than mobile, touch and landscape.
const DECIMAL = /^\s*[+-]?(\d+(\.\d*)?|\.\d+)\s*$/;

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);
}

// geolocation
const parts = arg.split(',');
if (parts.length !== 2) {
  throw new Error(`Invalid geolocation "${arg}". Expected '<latitude>,<longitude>'.`);
}
const latitude = parseDecimal(parts[0], 'latitude');
const longitude = parseDecimal(parts[1], 'longitude');

I'm happy to send a PR.

Reproduction

The transforms are pure functions, so they can be called directly on main (830c693, package version 1.10.1):

import {geolocationTransform, viewportTransform} from './src/tools/ToolDefinition.js';

for (const g of ['37.77,-122.42', '37.77,', ',', '37.77,-122.42,9', '0x10,0x20']) {
  console.log(g, '->', JSON.stringify(geolocationTransform(g)));
}
for (const v of ['1280x720,mobile,bogus', '1280x720x2x9']) {
  console.log(v, '->', JSON.stringify(viewportTransform(v)));
}

Output:

37.77,-122.42    -> {"latitude":37.77,"longitude":-122.42}
37.77,           -> {"latitude":37.77,"longitude":0}
,                -> {"latitude":0,"longitude":0}
37.77,-122.42,9  -> {"latitude":37.77,"longitude":-122.42}
0x10,0x20        -> {"latitude":16,"longitude":32}
1280x720,mobile,bogus -> {"width":1280,"height":720,"isMobile":true,"isLandscape":false,"hasTouch":false}
1280x720x2x9     -> {"width":1280,"height":720,"deviceScaleFactor":2,"isMobile":false,"isLandscape":false,"hasTouch":false}

The emulate tool passes these results straight on to Emulation.setGeolocationOverride / setViewport.

Expectation

Inputs that do not match the documented formats (<latitude>,<longitude> and <width>x<height>x<devicePixelRatio>[,mobile][,touch][,landscape]) are rejected at the input-validation layer with the existing clear error messages, as "999,999" and "abc" already are.

MCP configuration

Default (npx chrome-devtools-mcp@latest); no extra flags needed.

Chrome DevTools MCP version

1.10.1 (main at 830c693)

Chrome version

Not applicable (input parsing happens before any Chrome call).

Coding agent version

Not applicable.

Model version

Not applicable.

Chat log

Not applicable; the reproduction calls the transforms directly.

Node version

v20.19.2

Operating system

Linux

Extra checklist

  • I want to provide a PR to fix this bug

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions