Skip to content

fix: escape user input in generated code - #140

Merged
JustasMonkev merged 2 commits into
mainfrom
monitor/playwright-mcp-codegen-escaping
Jul 24, 2026
Merged

JustasMonkev merged 2 commits into
mainfrom
monitor/playwright-mcp-codegen-escaping

Conversation

@JustasMonkev

@JustasMonkev JustasMonkev commented Jul 24, 2026 •

Copy link
Copy Markdown
Owner

Upstream change

Playwright MCP now escapes user-controlled strings before emitting generated Playwright code. Inputs containing apostrophes previously produced invalid JavaScript.

Why it matters here

This repository already uses its shared code-generation quoting helper for typed text and locator values, but browser_navigate and browser_press_key still interpolated URL/key input inside raw single quotes. They had the same invalid-code failure fixed upstream.

Implemented fix

  • Reuse the existing codegen.quote() helper for navigation URLs and pressed keys.
  • Add focused regression coverage that parses the generated snippets for apostrophe-containing input.

Validation

  • npm test -- tests/tools-codegen.test.ts tests/tools-navigate.test.ts
  • npm run lint
  • npm run build
  • npm test — 35 files, 379 tests passed
  • git diff --check

Summary by CodeRabbit

  • Bug Fixes

    • Improved generated browser navigation and keyboard actions to safely handle special characters in URLs and key values.
    • Prevented code-generation errors caused by apostrophes and similar characters.
  • Tests

    • Added coverage verifying generated actions remain valid when provided with special-character inputs.

@coderabbitai

coderabbitai Bot commented Jul 24, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@JustasMonkev, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 50 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 0de65c21-4e27-469c-8a42-329c952c017d

📥 Commits

Reviewing files that changed from the base of the PR and between 30480b9 and d67b740.

📒 Files selected for processing (2)
  • src/tools/keyboard.ts
  • tests/tools-codegen.test.ts
📝 Walkthrough

Walkthrough

Generated navigation and keyboard tool snippets now quote user-provided values through a shared JavaScript utility. Tests verify snippets containing apostrophes can be dynamically evaluated.

Changes

Tool code generation safety

Layer / File(s) Summary
Centralized quoting for generated tool calls
src/tools/navigate.ts, src/tools/keyboard.ts
browser_navigate and browser_press_key use javascript.quote(...) when generating Playwright calls.
Generated code execution validation
tests/tools-codegen.test.ts
Tests evaluate generated code for both tools using apostrophe-containing inputs.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: escaping user input in generated code to prevent invalid snippets.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch monitor/playwright-mcp-codegen-escaping

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@JustasMonkev
JustasMonkev marked this pull request as ready for review July 24, 2026 17:54

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/tools/keyboard.ts`:
- Line 40: Update the generated comment in the keyboard handling flow around the
response.addCode call so params.key is never emitted as raw user input; remove
that comment or safely encode the key before interpolation. Keep the quoted
params.key argument used by page.keyboard.press unchanged.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: d31c47e4-af8d-47e3-9452-de0adfca6ce8

📥 Commits

Reviewing files that changed from the base of the PR and between 718e1c8 and 30480b9.

📒 Files selected for processing (3)
  • src/tools/keyboard.ts
  • src/tools/navigate.ts
  • tests/tools-codegen.test.ts

Comment thread src/tools/keyboard.ts Resolved
@JustasMonkev
JustasMonkev merged commit d0b8811 into main Jul 24, 2026
@JustasMonkev
JustasMonkev deleted the monitor/playwright-mcp-codegen-escaping branch July 24, 2026 18:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant