Repository navigation
fix(emulate): strictly validate geolocation and viewport strings - #3124
Closed
chiragjangiir wants to merge 1 commit into
Closed
chiragjangiir wants to merge 1 commit into
chiragjangiir wants to merge 1 commit into
Conversation
…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
self-requested a review
October 15, 2026 11:56
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`.
Author
|
Superseded upstream, recording it for anyone who finds this branch later (no action requested).
|
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
emulate'sgeolocationandviewportstring inputs are parsed withNumber(...), which maps""/" "to0and accepts hex literals. Malformed input therefore succeeds with silently-wrong emulation instead of failing:geolocation: "37.77,"→ emulateslatitude 37.77, longitude 0geolocation: ","→ emulates0, 0geolocation: "37.77,-122.42,9"→ third part dropped without a warninggeolocation: "0x10,0x20"→ accepted as 16, 32 (hex!)viewport: "1280x720,mobile,bogus"→ unknown tag ignoredviewport: "1280x720x2x9"→ fourth part ignoredIn 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
geolocationTransformnow requires exactly two comma-separated parts and rejects empty parts.Number(...)is applied); hex and blank parts are rejected.viewportTransformnow requires exactly 2–3x-separated dimension parts and rejects tags other thanmobile,touch, andlandscape.Testing
npm run typecheck→ passesnpm run build→ passesnode 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 cleanCompatibility
Valid inputs behave identically. Previously-silent inputs now throw a descriptive error, which is the point of the change —
emulatereporting failure is strictly more useful than silently emulating0, 0.Related issue
Fixes #3097