Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/playwright-core/src/tools/backend/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ export class Context {

redactSecrets(text: string): string {
for (const [secretName, secretValue] of Object.entries(this.config.secrets ?? {})) {
if (!secretValue)
if (!secretValue || secretValue.length < 4)

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.

please avoid configuring guessable values such as abc as secrets rather than silently disabling redaction for short values (see microsoft/playwright-mcp#1106 (comment))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Devin Rousso (@dcrousso)

please avoid configuring guessable values such as abc as secrets rather than silently disabling redaction for short values

Removed the length < 4 skip. Empty values are still ignored. The short-secret test is gone. 36971b002

continue;
text = text.replaceAll(secretValue, `<secret>${secretName}</secret>`);
}
Expand Down
13 changes: 9 additions & 4 deletions packages/playwright-core/src/tools/backend/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,16 @@ export const uploadFile = defineTabTool({

response.addCode(`await fileChooser.setFiles(${JSON.stringify(paths)})`);

tab.clearModalState(modalState);
await tab.waitForCompletion(async () => {
if (paths)
if (paths) {
try {
await modalState.fileChooser.setFiles(paths);

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.

awaiting setFiles() outside waitForCompletion() makes uploads whose change handler opens a dialog time out instead of returning the dialog for handling

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Devin Rousso (@dcrousso)

awaiting setFiles() outside waitForCompletion() makes uploads whose change handler opens a dialog time out instead of returning the dialog for handling

Same change: setFiles runs inside waitForCompletion after the chooser is cleared, so a dialog from the change handler is returned. If setFiles throws, the chooser is restored. 36971b002

});
} catch (e) {
response.addError(e instanceof Error ? e.message : String(e));
return;
}
}
tab.clearModalState(modalState);
await tab.waitForCompletion(async () => {});

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.

starting the completion wait after setFiles() misses requests triggered by the upload on WebKit, so the tool can return before those requests finish

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Devin Rousso (@dcrousso)

starting the completion wait after setFiles() misses requests triggered by the upload on WebKit

setFiles is back inside waitForCompletion. The file chooser is cleared first so the wait actually runs. 36971b002

},

clearsModalState: 'fileChooser',
Expand Down
6 changes: 4 additions & 2 deletions packages/playwright-core/src/tools/backend/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ const route = defineTool({
},

handle: async (context, params, response) => {
const addHeaders = params.headers ? Object.fromEntries(params.headers.map(h => {
const addHeaders = params.headers ? Object.fromEntries(params.headers.flatMap(h => {
const colonIndex = h.indexOf(':');
return [h.substring(0, colonIndex).trim(), h.substring(colonIndex + 1).trim()];
if (colonIndex <= 0)
Comment thread
SebTardif marked this conversation as resolved.
Resolved
return [];
return [[h.substring(0, colonIndex).trim(), h.substring(colonIndex + 1).trim()]];
})) : undefined;
const removeHeaders = params.removeHeaders ? params.removeHeaders.split(',').map(h => h.trim()) : undefined;

Expand Down
37 changes: 37 additions & 0 deletions tests/mcp/files.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,43 @@ test('browser_file_upload', async ({ client, server }, testInfo) => {
}
});

test('browser_file_upload keeps chooser when setFiles fails', async ({ client, server }, testInfo) => {
server.setContent('/', `<input type="file" />`, 'text/html');

await client.callTool({
name: 'browser_navigate',
arguments: { url: server.PREFIX },
});

await client.callTool({
name: 'browser_click',
arguments: {
element: 'Textbox',
target: 'e2',
},
});

const missing = testInfo.outputPath('missing.txt');
const failed = await client.callTool({
name: 'browser_file_upload',
arguments: { paths: [missing] },
});
expect(failed).toHaveResponse({
isError: true,
modalState: expect.stringContaining(`[File chooser]`),
});

const filePath = testInfo.outputPath('retry.txt');
await fs.writeFile(filePath, 'retry');
const retried = await client.callTool({
name: 'browser_file_upload',
arguments: { paths: [filePath] },
});
expect(retried).toHaveResponse({
modalState: undefined,
});
});

test('clicking on download link emits download', async ({ startClient, server }, testInfo) => {
const { client } = await startClient({
config: { outputDir: testInfo.outputPath('output') },
Expand Down
36 changes: 36 additions & 0 deletions tests/mcp/route.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,42 @@ test('browser_route modifies request headers', async ({ client, server }) => {
expect(receivedHeaders['x-custom-header']).toBe('test-value');
});

test('browser_route ignores header lines without a colon', async ({ client, server }) => {
let receivedHeaders: Record<string, string> = {};
server.setRoute('/api/check', (req, res) => {
receivedHeaders = req.headers as Record<string, string>;
res.writeHead(200);
res.end('ok');
});

server.setContent('/', `
<button onclick="fetch('/api/check').then(() => document.body.textContent = 'Done')">Fetch</button>
`, 'text/html');

await client.callTool({
name: 'browser_navigate',
arguments: { url: server.PREFIX },
});

await client.callTool({
name: 'browser_route',
arguments: {
pattern: '**/api/check',
headers: ['NotAHeader', 'X-Custom-Header: test-value'],
},
});

const requestPromise = server.waitForRequest('/api/check');
await client.callTool({
name: 'browser_click',
arguments: { element: 'Fetch button', target: 'e2' },
});

await requestPromise;
expect(receivedHeaders['x-custom-header']).toBe('test-value');
expect(receivedHeaders['']).toBeUndefined();
});

test('browser_route_list shows active routes', async ({ client, server }) => {
await client.callTool({
name: 'browser_navigate',
Expand Down
26 changes: 26 additions & 0 deletions tests/mcp/secrets.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,32 @@ await page.getByRole('textbox', { name: 'Password' }).fill(process.env['X-PASSWO
});
});

test('short secret values are not redacted', async ({ startClient, server }) => {
const secretsFile = test.info().outputPath('secrets.env');
await fs.promises.writeFile(secretsFile, 'X-TINY=e');

const { client } = await startClient({
args: ['--secrets', secretsFile],
});

server.setContent('/', `
<!DOCTYPE html>
<html>
<body><p>hello</p></body>
</html>
`, 'text/html');

const response = await client.callTool({
name: 'browser_navigate',
arguments: { url: server.PREFIX },
});

expect(response).toHaveResponse({
snapshot: expect.stringContaining('hello'),
});
expect(JSON.stringify(response)).not.toContain('<secret>X-TINY</secret>');
});

test('empty secret value is ignored', async ({ startClient, server }) => {
const secretsFile = test.info().outputPath('secrets.env');
await fs.promises.writeFile(secretsFile, 'EMPTY_SECRET=\nX-PASSWORD=password123');
Expand Down