fix(cdp): backslash-escape key/code in Input.dispatchKeyEvent (#433) (#437)

The keyDown/keyUp paths interpolated `key`/`code` into the generated
KeyboardEvent snippet with single-quote escaping only. Chrome sends
`key: "\"` (U+005C) for the backslash key, which became `key:'\'` — the
backslash escaped the closing quote, ran the string literal on, and made
the whole page.evaluate a syntax error, so the keydown was silently
dropped. Any key/code carrying a backslash broke dispatch.

Escape backslash before single-quote (backslash first, so the quote-escape
backslashes are not themselves doubled), matching the sibling text path in
the same function. Adds a regression test asserting the backslash key
arrives at a keydown listener alongside an ordinary key.
This commit is contained in:
Andrey Mnatsakanov authored and GitHub committed 2026-07-19 19:11:17 +00:00
1 parent b2e4bb49a7
commit 57949a0efc
2 files changed
+112 -4

No files matched your search

+8 -4
View File
@@ -151,8 +151,12 @@ pub async fn handle(
var evt = globalThis.__obscura_markTrusted(new KeyboardEvent('keydown', {{bubbles:true,cancelable:true,key:'{key}',code:'{code}'}}));\
target.dispatchEvent(evt);\
}})()",
key = key.replace('\'', "\\'"),
code = code.replace('\'', "\\'"),
// Escape backslash BEFORE single-quote (as the text
// path below does) so a key like "\" — Chrome's
// backslash key — doesn't escape the closing quote
// and produce a syntax error that drops the event.
key = key.replace('\\', "\\\\").replace('\'', "\\'"),
code = code.replace('\\', "\\\\").replace('\'', "\\'"),
);
page.evaluate(&js);
@@ -194,8 +198,8 @@ pub async fn handle(
var evt = globalThis.__obscura_markTrusted(new KeyboardEvent('keyup', {{bubbles:true,key:'{key}',code:'{code}'}}));\
target.dispatchEvent(evt);\
}})()",
key = key.replace('\'', "\\'"),
code = code.replace('\'', "\\'"),
key = key.replace('\\', "\\\\").replace('\'', "\\'"),
code = code.replace('\\', "\\\\").replace('\'', "\\'"),
);
page.evaluate(&js);
}