Skip to content

fix(emulate): strictly validate geolocation and viewport strings - #3124

Closed
chiragjangiir wants to merge 1 commit into
ChromeDevTools:mainfrom
chiragjangiir:strict-emulate-parsing
Closed

chiragjangiir wants to merge 1 commit into
ChromeDevTools:mainfrom
chiragjangiir:strict-emulate-parsing

Conversation

@chiragjangiir

Copy link
Copy Markdown

Problem

emulate's geolocation and viewport string inputs are parsed with Number(...), which maps ""/" " to 0 and accepts hex literals. Malformed input therefore succeeds with silently-wrong emulation instead of failing:

  • geolocation: "37.77," → emulates latitude 37.77, longitude 0
  • geolocation: "," → emulates 0, 0
  • geolocation: "37.77,-122.42,9" → third part dropped without a warning
  • geolocation: "0x10,0x20" → accepted as 16, 32 (hex!)
  • viewport: "1280x720,mobile,bogus" → unknown tag ignored
  • viewport: "1280x720x2x9" → fourth part ignored

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

Solution

  • geolocationTransform now requires exactly two comma-separated parts and rejects empty parts.
  • Both transforms only accept plain decimal numbers (each part must match a decimal pattern before Number(...) is applied); hex and blank parts are rejected.
  • viewportTransform now requires exactly 2–3 x-separated dimension parts and rejects tags other than mobile, touch, and landscape.
  • Error messages state the expected format at the point of failure, matching the existing message style.

Testing

  • npm run typecheck → passes
  • npm run build → passes
  • node scripts/test.js tests/tools/emulation.test.ts → all tests pass, including 9 new regression cases (trailing/leading commas, hex rejection, extra dimension parts, unknown tags, whitespace-padded decimals still accepted)
  • npm run check-format → prettier/eslint clean

Compatibility

Valid inputs behave identically. Previously-silent inputs now throw a descriptive error, which is the point of the change — emulate reporting failure is strictly more useful than silently emulating 0, 0.

Related issue

Fixes #3097

…d of silently defaulting

- Require exactly two comma-separated parts for geolocation
- Require 2-3 'x'-separated parts for viewport dimensions
- Only accept plain decimal numbers (rejects hex, empty parts)
- Reject unknown viewport tags instead of ignoring them

Number('') is 0 and Number('0x10') is 16, so malformed input
previously produced emulated locations and viewports that
differed from what the caller asked for with no error.
@OrKoN
OrKoN self-requested a review October 15, 2026 11:56
@OrKoN OrKoN closed this in #3120 Oct 16, 2026
OrKoN pushed a commit to MankhongGarden/chrome-devtools-mcp that referenced this pull request Oct 16, 2026
…rt transforms (ChromeDevTools#3097) (ChromeDevTools#3120)

Fixes ChromeDevTools#3097

Closes ChromeDevTools#3124

### Summary
- Enforce strict numeric parsing and tag validation in
`geolocationTransform` and `viewportTransform`
(`src/tools/ToolDefinition.ts`), rejecting empty segments, extra parts,
unknown boolean flags, and non-decimal literals instead of silently
coercing to `0` or dropping them.
- Add regression tests in `tests/tools/emulation.test.ts`.
@chiragjangiir

Copy link
Copy Markdown
Author

Superseded upstream, recording it for anyone who finds this branch later (no action requested).

3ea31fba — fix: reject empty, extra, and unknown parts in geolocation and viewport transforms (#3097) (#3120) — landed at 2026-10-16T07:30:52Z and touches the same two files this PR changed (src/tools/ToolDefinition.ts, tests/tools/emulation.test.ts); this PR was closed at 2026-10-16T07:34:43Z. The merged change covers the same strictness issue, so there is nothing left to carry over here.

twotnguyen pushed a commit to twotnguyen/chrome-devtools-mcp that referenced this pull request Oct 16, 2026
…vTools#3150)

Fixes ChromeDevTools#2966

`McpPage.emulate()` stores the new emulation settings before calling
`page.setViewport()` (intentionally, since ChromeDevTools#2134, so a
viewport-triggered reload uses the updated timeouts). When Chrome
rejects the viewport (e.g. `800.5x600x1` or `20000000x600x1`), the
rejected value stayed stored, so later responses printed `Emulating
viewport: ...` for a viewport the page doesn't have, and
`lighthouse_audit` failed when it called `restoreEmulation()`.

### Changes

- `src/McpPage.ts`: if `setViewport()` throws, put the previous viewport
back into the stored emulation settings (or drop it if there was none)
and rethrow. The order from ChromeDevTools#2134 (store settings, update timeouts, then
set the viewport) is unchanged, and the other settings applied in the
same call stay as they are.
- `tests/McpPage.test.ts`: two unit tests in which `setViewport`
rejects, one with no previous viewport and one with a previous viewport.

This changes only state handling in `McpPage`. It doesn't touch viewport
parsing in `ToolDefinition.ts`, so it doesn't overlap with ChromeDevTools#3120 /
ChromeDevTools#3124, which tighten input validation. The two changes work together:
validation rejects bad input earlier, and this keeps the stored state
consistent when Chrome still rejects a value.

### Testing

- `npm run test tests/McpPage.test.ts`: both new tests fail without the
fix (the stored viewport is `800.5x600` / `20000000x600`) and pass with
it. The file runs 74/74.
- `npm run check-format`: passes.
- Full `npm run test -- --test-concurrency=1` on Windows 11 / Node 22:
1386 passed, 1 failed (`ConfigWatcher` › follows symlinks, which needs
symlink privileges on Windows), and 6 files cancelled on the 120s file
timeout (e2e CLI daemon, `index.test`, `HeapSnapshotManager`). The same
tests fail the same way on unmodified `main` (9171f77) on this machine,
so the change doesn't cause them.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant