Skip to content

Chrome DevTools for coding agents

fix: validate type_text submitKey before typing (#3185)

patricksonc8615d705dbd
diff --git a/src/tools/input.ts b/src/tools/input.ts
index 4617c6d7..cfea3d43 100644
--- a/src/tools/input.ts
+++ b/src/tools/input.ts
@@ -7,7 +7,7 @@
import {TimeoutError, zod} from '../third_party/index.js';
import type {ElementHandle, KeyInput} from '../third_party/index.js';
import type {TextSnapshotNode} from '../types.js';
-import {parseKey} from '../utils/keyboard.js';
+import {invalidKeyMessage, isValidKey, parseKey} from '../utils/keyboard.js';
import {logger} from '../utils/logger.js';
import type {WaitForEventsResult} from '../utils/WaitForHelper.js';
@@ -27,6 +27,10 @@ const includeSnapshotSchema = zod
const submitKeySchema = zod
.string()
+ // An empty string means no submit key, as before.
+ .refine((key): boolean => key === '' || isValidKey(key), {
+ error: issue => invalidKeyMessage(String(issue.input)),
+ })
.optional()
.describe(
'Optional key to press after typing. E.g., "Enter", "Tab", "Escape"',
@@ -442,16 +446,16 @@ export const typeText = definePageTool(() => ({
verifyFilesSchema: {},
handler: async (request, response) => {
const page = request.page;
+ const submitKey = request.params.submitKey;
const result = await page.waitForEventsAfterAction(async () => {
await page.pptrPage.keyboard.type(request.params.text);
- if (request.params.submitKey) {
- await page.pptrPage.keyboard.press(
- request.params.submitKey as KeyInput,
- );
+ // submitKeySchema has already validated the key; isValidKey narrows the type.
+ if (submitKey && isValidKey(submitKey)) {
+ await page.pptrPage.keyboard.press(submitKey);
}
});
response.appendResponseLine(
- `Typed text "${request.params.text}${request.params.submitKey ? ` + ${request.params.submitKey}` : ''}"`,
+ `Typed text "${request.params.text}${submitKey ? ` + ${submitKey}` : ''}"`,
);
response.attachWaitForResult(result);
},
diff --git a/src/utils/keyboard.ts b/src/utils/keyboard.ts
index 51b0a3f7..194d14b6 100644
--- a/src/utils/keyboard.ts
+++ b/src/utils/keyboard.ts
@@ -265,13 +265,25 @@ const validKeys = new Set([
'VolumeUp',
]);
+/**
+ * Whether the string is a single valid key name (no `+` combinations).
+ */
+export function isValidKey(key: string): key is KeyInput {
+ return validKeys.has(key);
+}
+
+/**
+ * Error message for a string that is not a valid key name.
+ */
+export function invalidKeyMessage(key: string): string {
+ return `${key} is invalid. Valid keys are: ${Array.from(validKeys.values()).join(',')}.`;
+}
+
function throwIfInvalidKey(key: string): KeyInput {
- if (validKeys.has(key)) {
- return key as KeyInput;
+ if (isValidKey(key)) {
+ return key;
}
- throw new Error(
- `${key} is invalid. Valid keys are: ${Array.from(validKeys.values()).join(',')}.`,
- );
+ throw new Error(invalidKeyMessage(key));
}
/**
diff --git a/tests/tools/input.test.ts b/tests/tools/input.test.ts
index ba774ab5..0b82680b 100644
--- a/tests/tools/input.test.ts
+++ b/tests/tools/input.test.ts
@@ -883,32 +883,6 @@ describe('input', () => {
});
});
- it('errors on invalid submit key', async () => {
- await withMcpContext(async (response, context, args) => {
- const page = context.getSelectedMcpPage().pptrPage;
- await page.setContent(html`<textarea></textarea>`);
- await page.click('textarea');
- context.getSelectedMcpPage().textSnapshot = await TextSnapshot.create(
- context.getSelectedMcpPage(),
- );
- try {
- await typeText(args).handler(
- {
- params: {
- text: 'test',
- submitKey: 'XXX',
- },
- page: context.getSelectedMcpPage(),
- },
- response,
- context,
- );
- } catch (err) {
- assert.strictEqual(err.message, 'Unknown key: "XXX"');
- }
- });
- });
-
it('reproduction: fill isolation', async () => {
await withMcpContext(async (_response, context, args) => {
const page = context.getSelectedMcpPage().pptrPage;
diff --git a/tests/utils/keyboard.test.ts b/tests/utils/keyboard.test.ts
index 061c07b7..b7a1d179 100644
--- a/tests/utils/keyboard.test.ts
+++ b/tests/utils/keyboard.test.ts
@@ -7,7 +7,7 @@
import assert from 'node:assert';
import {describe, it} from 'node:test';
-import {parseKey} from '../../src/utils/keyboard.js';
+import {isValidKey, parseKey} from '../../src/utils/keyboard.js';
describe('parseKey', () => {
it('parses single keys', () => {
@@ -149,3 +149,17 @@ describe('parseKey', () => {
);
});
});
+
+describe('isValidKey', () => {
+ it('accepts single key names', () => {
+ for (const key of ['Enter', 'Tab', 'Escape', 'a', 'A', '5', ' ']) {
+ assert.strictEqual(isValidKey(key), true, key);
+ }
+ });
+
+ it('rejects unknown names and combinations', () => {
+ for (const key of ['XXX', 'enter', 'Shift+Enter', ' Enter', '']) {
+ assert.strictEqual(isValidKey(key), false, key);
+ }
+ });
+});