Skip to content

Chrome DevTools for coding agents

fix(input): reject multiple file paths on single-file input in upload_file (#2995) (#3135)

Yuri (IURII) Trukhine65084334ffe
diff --git a/src/tools/input.ts b/src/tools/input.ts
index 4276ff20..4617c6d7 100644
--- a/src/tools/input.ts
+++ b/src/tools/input.ts
@@ -583,21 +583,28 @@ export const uploadFile = definePageTool(() => ({
try {
await handle.uploadFile(...filePaths);
- } catch {
+ } catch (err) {
+ if (
+ err instanceof Error &&
+ err.message.includes('Multiple file uploads')
+ ) {
+ throw err;
+ }
// Some sites use a proxy element to trigger file upload instead of
// a type=file element. In this case, we want to default to
// Page.waitForFileChooser() and upload the file this way.
+ let fileChooser;
try {
- const [fileChooser] = await Promise.all([
+ [fileChooser] = await Promise.all([
request.page.pptrPage.waitForFileChooser({timeout: 3000}),
handle.asLocator().click(),
]);
- await fileChooser.accept(filePaths);
} catch {
throw new Error(
`Failed to upload file. The element could not accept the file directly, and clicking it did not trigger a file chooser.`,
);
}
+ await fileChooser.accept(filePaths);
}
if (request.params.includeSnapshot) {
response.includeSnapshot();
diff --git a/tests/tools/input.test.ts b/tests/tools/input.test.ts
index 171196fb..ba774ab5 100644
--- a/tests/tools/input.test.ts
+++ b/tests/tools/input.test.ts
@@ -1638,6 +1638,44 @@ describe('input', () => {
assert.strictEqual(response.snapshotParams, undefined);
});
});
+
+ it('throws an error if multiple files are uploaded to an input without multiple attribute', async () => {
+ using firstFile = createTempFile('first file content', 'first.txt');
+ using secondFile = createTempFile('second file content', 'second.txt');
+
+ await withMcpContext(async (response, context, args) => {
+ const page = context.getSelectedMcpPage().pptrPage;
+ await page.setContent(
+ html`<form>
+ <input
+ type="file"
+ id="file-input"
+ />
+ </form>`,
+ );
+ context.getSelectedMcpPage().textSnapshot = await TextSnapshot.create(
+ context.getSelectedMcpPage(),
+ );
+
+ await assert.rejects(
+ uploadFile(args).handler(
+ {
+ params: {
+ uid: '1_2',
+ filePaths: [firstFile.path, secondFile.path],
+ },
+ page: context.getSelectedMcpPage(),
+ },
+ response,
+ context,
+ ),
+ {
+ message:
+ 'Multiple file uploads only work with <input type=file multiple>',
+ },
+ );
+ });
+ });
});
describe('press_key', () => {