Skip to content

Commit 2e039c0

Browse files
refactor: use validate files on the tool level (ChromeDevTools#2152)
Fixes ChromeDevTools#2138 Closes ChromeDevTools#2150
1 parent 50cecd8 commit 2e039c0

20 files changed

Lines changed: 66 additions & 23 deletions

src/ToolHandler.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,12 @@ export class ToolHandler {
220220

221221
response.setRedactNetworkHeaders(this.serverArgs.redactNetworkHeaders);
222222
try {
223+
if (this.tool.verifyFilesSchema) {
224+
for (const key of this.tool.verifyFilesSchema) {
225+
const filePath = params[key];
226+
await context.validatePath(filePath as string);
227+
}
228+
}
223229
if (isPageScopedTool(this.tool)) {
224230
const pageId =
225231
typeof params.pageId === 'number' ? params.pageId : undefined;

src/tools/ToolDefinition.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export interface BaseToolDefinition<
4848
};
4949
schema: Schema;
5050
blockedByDialog: boolean;
51+
verifyFilesSchema: Array<keyof Schema>;
5152
}
5253

5354
export interface ToolDefinition<

src/tools/console.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export const listConsoleMessages = definePageTool(cliArgs => {
7979
),
8080
},
8181
blockedByDialog: false,
82+
verifyFilesSchema: [],
8283
handler: async (request, response) => {
8384
response.setIncludeConsoleData(true, {
8485
pageSize: request.params.pageSize,
@@ -105,6 +106,7 @@ export const getConsoleMessage = definePageTool({
105106
),
106107
},
107108
blockedByDialog: false,
109+
verifyFilesSchema: [],
108110
handler: async (request, response) => {
109111
response.attachConsoleMessage(request.params.msgid);
110112
},

src/tools/emulation.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ export const emulate = definePageTool({
100100
),
101101
},
102102
blockedByDialog: true,
103+
verifyFilesSchema: [],
103104
handler: async (request, response, context) => {
104105
const page = request.page;
105106
await context.emulate(request.params, page.pptrPage);

src/tools/extensions.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ export const installExtension = defineTool({
2222
.describe('Absolute path to the unpacked extension folder.'),
2323
},
2424
blockedByDialog: false,
25+
verifyFilesSchema: ['path'],
2526
handler: async (request, response, context) => {
2627
const {path} = request.params;
27-
await context.validatePath(path);
2828
const id = await context.installExtension(path);
2929
response.appendResponseLine(`Extension installed. Id: ${id}`);
3030
},
@@ -41,6 +41,7 @@ export const uninstallExtension = defineTool({
4141
id: zod.string().describe('ID of the extension to uninstall.'),
4242
},
4343
blockedByDialog: false,
44+
verifyFilesSchema: [],
4445
handler: async (request, response, context) => {
4546
const {id} = request.params;
4647
await context.uninstallExtension(id);
@@ -58,6 +59,7 @@ export const listExtensions = defineTool({
5859
},
5960
schema: {},
6061
blockedByDialog: false,
62+
verifyFilesSchema: [],
6163
handler: async (_request, response, _context) => {
6264
response.setListExtensions();
6365
},
@@ -74,13 +76,13 @@ export const reloadExtension = defineTool({
7476
id: zod.string().describe('ID of the extension to reload.'),
7577
},
7678
blockedByDialog: false,
79+
verifyFilesSchema: [],
7780
handler: async (request, response, context) => {
7881
const {id} = request.params;
7982
const extension = await context.getExtension(id);
8083
if (!extension) {
8184
throw new Error(`Extension with ID ${id} not found.`);
8285
}
83-
await context.validatePath(extension.path);
8486
await context.installExtension(extension.path);
8587
response.appendResponseLine('Extension reloaded.');
8688
},
@@ -97,6 +99,7 @@ export const triggerExtensionAction = defineTool({
9799
id: zod.string().describe('ID of the extension to trigger the action for.'),
98100
},
99101
blockedByDialog: false,
102+
verifyFilesSchema: [],
100103
handler: async (request, response, context) => {
101104
const {id} = request.params;
102105
await context.triggerExtensionAction(id);

src/tools/input.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ export const click = definePageTool({
103103
includeSnapshot: includeSnapshotSchema,
104104
},
105105
blockedByDialog: true,
106+
verifyFilesSchema: [],
106107
handler: async (request, response) => {
107108
const uid = request.params.uid;
108109
const handle = await request.page.getElementByUid(uid);
@@ -154,6 +155,7 @@ export const clickAt = definePageTool({
154155
includeSnapshot: includeSnapshotSchema,
155156
},
156157
blockedByDialog: true,
158+
verifyFilesSchema: [],
157159
handler: async (request, response) => {
158160
const page = request.page;
159161
const result = await page.waitForEventsAfterAction(async () => {
@@ -189,6 +191,7 @@ export const hover = definePageTool({
189191
includeSnapshot: includeSnapshotSchema,
190192
},
191193
blockedByDialog: true,
194+
verifyFilesSchema: [],
192195
handler: async (request, response) => {
193196
const uid = request.params.uid;
194197
const handle = await request.page.getElementByUid(uid);
@@ -316,6 +319,7 @@ export const fill = definePageTool({
316319
includeSnapshot: includeSnapshotSchema,
317320
},
318321
blockedByDialog: true,
322+
verifyFilesSchema: [],
319323
handler: async (request, response, context) => {
320324
const page = request.page;
321325
const result = await page.waitForEventsAfterAction(async () => {
@@ -346,6 +350,7 @@ export const typeText = definePageTool({
346350
submitKey: submitKeySchema,
347351
},
348352
blockedByDialog: true,
353+
verifyFilesSchema: [],
349354
handler: async (request, response) => {
350355
const page = request.page;
351356
const result = await page.waitForEventsAfterAction(async () => {
@@ -376,6 +381,7 @@ export const drag = definePageTool({
376381
includeSnapshot: includeSnapshotSchema,
377382
},
378383
blockedByDialog: true,
384+
verifyFilesSchema: [],
379385
handler: async (request, response) => {
380386
const fromHandle = await request.page.getElementByUid(
381387
request.params.from_uid,
@@ -423,6 +429,7 @@ export const fillForm = definePageTool({
423429
includeSnapshot: includeSnapshotSchema,
424430
},
425431
blockedByDialog: true,
432+
verifyFilesSchema: [],
426433
handler: async (request, response, context) => {
427434
const page = request.page;
428435
let lastResult: WaitForEventsResult = {};
@@ -461,9 +468,9 @@ export const uploadFile = definePageTool({
461468
includeSnapshot: includeSnapshotSchema,
462469
},
463470
blockedByDialog: true,
464-
handler: async (request, response, context) => {
471+
verifyFilesSchema: ['filePath'],
472+
handler: async (request, response, _context) => {
465473
const {uid, filePath} = request.params;
466-
await context.validatePath(filePath);
467474
const handle = (await request.page.getElementByUid(
468475
uid,
469476
)) as ElementHandle<HTMLInputElement>;
@@ -512,6 +519,7 @@ export const pressKey = definePageTool({
512519
includeSnapshot: includeSnapshotSchema,
513520
},
514521
blockedByDialog: true,
522+
verifyFilesSchema: [],
515523
handler: async (request, response) => {
516524
const page = request.page;
517525
const tokens = parseKey(request.params.key);

src/tools/lighthouse.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export const lighthouseAudit = definePageTool({
4444
.describe('Directory for reports. If omitted, uses temporary files.'),
4545
},
4646
blockedByDialog: true,
47+
verifyFilesSchema: ['outputDirPath'],
4748
handler: async (request, response, context) => {
4849
const page = request.page;
4950
const categories = [
@@ -59,8 +60,6 @@ export const lighthouseAudit = definePageTool({
5960
outputDirPath,
6061
} = request.params;
6162

62-
await context.validatePath(outputDirPath);
63-
6463
const flags: Flags = {
6564
onlyCategories: categories,
6665
output: formats,

src/tools/memory.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ export const takeHeapSnapshot = definePageTool({
2323
.describe('A path to a .heapsnapshot file to save the heapsnapshot to.'),
2424
},
2525
blockedByDialog: true,
26-
handler: async (request, response, context) => {
26+
verifyFilesSchema: ['filePath'],
27+
handler: async (request, response) => {
2728
const page = request.page;
28-
await context.validatePath(request.params.filePath);
2929

3030
await page.pptrPage.captureHeapSnapshot({
3131
path: ensureExtension(request.params.filePath, '.heapsnapshot'),
@@ -50,8 +50,8 @@ export const getHeapSnapshotSummary = defineTool({
5050
filePath: zod.string().describe('A path to a .heapsnapshot file to read.'),
5151
},
5252
blockedByDialog: false,
53+
verifyFilesSchema: ['filePath'],
5354
handler: async (request, response, context) => {
54-
await context.validatePath(request.params.filePath);
5555
const stats = await context.getHeapSnapshotStats(request.params.filePath);
5656
const staticData = await context.getHeapSnapshotStaticData(
5757
request.params.filePath,
@@ -82,8 +82,8 @@ export const getHeapSnapshotDetails = defineTool({
8282
.describe('The page size for pagination of aggregates.'),
8383
},
8484
blockedByDialog: false,
85+
verifyFilesSchema: ['filePath'],
8586
handler: async (request, response, context) => {
86-
await context.validatePath(request.params.filePath);
8787
const aggregates = await context.getHeapSnapshotAggregates(
8888
request.params.filePath,
8989
);
@@ -111,8 +111,8 @@ export const getHeapSnapshotClassNodes = defineTool({
111111
pageSize: zod.number().optional().describe('The page size for pagination.'),
112112
},
113113
blockedByDialog: false,
114+
verifyFilesSchema: ['filePath'],
114115
handler: async (request, response, context) => {
115-
await context.validatePath(request.params.filePath);
116116
const nodes = await context.getHeapSnapshotNodesById(
117117
request.params.filePath,
118118
request.params.id,
@@ -135,15 +135,14 @@ export const getHeapSnapshotRetainers = defineTool({
135135
conditions: ['experimentalMemory'],
136136
},
137137
blockedByDialog: false,
138+
verifyFilesSchema: ['filePath'],
138139
schema: {
139140
filePath: zod.string().describe('A path to a .heapsnapshot file to read.'),
140141
nodeId: zod.number().describe('The node ID to get retainers for.'),
141142
pageIdx: zod.number().optional().describe('The page index for pagination.'),
142143
pageSize: zod.number().optional().describe('The page size for pagination.'),
143144
},
144145
handler: async (request, response, context) => {
145-
await context.validatePath(request.params.filePath);
146-
147146
const retainers = await context.getHeapSnapshotRetainers(
148147
request.params.filePath,
149148
request.params.nodeId,

src/tools/network.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export const listNetworkRequests = definePageTool({
7171
),
7272
},
7373
blockedByDialog: false,
74+
verifyFilesSchema: [],
7475
handler: async (request, response, context) => {
7576
const data = await request.page.getDevToolsData();
7677
response.attachDevToolsData(data);
@@ -115,9 +116,8 @@ export const getNetworkRequest = definePageTool({
115116
),
116117
},
117118
blockedByDialog: true,
119+
verifyFilesSchema: ['requestFilePath', 'responseFilePath'],
118120
handler: async (request, response, context) => {
119-
await context.validatePath(request.params.requestFilePath);
120-
await context.validatePath(request.params.responseFilePath);
121121
if (request.params.reqid) {
122122
response.attachNetworkRequest(request.params.reqid, {
123123
requestFilePath: request.params.requestFilePath,

src/tools/pages.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export const listPages = defineTool(args => {
8585
},
8686
schema: {},
8787
blockedByDialog: false,
88+
verifyFilesSchema: [],
8889
handler: async (_request, response) => {
8990
response.setIncludePages(true);
9091
response.setListThirdPartyDeveloperTools();
@@ -112,6 +113,7 @@ export const selectPage = defineTool({
112113
.describe('Whether to focus the page and bring it to the top.'),
113114
},
114115
blockedByDialog: false,
116+
verifyFilesSchema: [],
115117
handler: async (request, response, context) => {
116118
const page = context.getPageById(request.params.pageId);
117119
context.selectPage(page);
@@ -137,6 +139,7 @@ export const closePage = defineTool({
137139
.describe('The ID of the page to close. Call list_pages to list pages.'),
138140
},
139141
blockedByDialog: false,
142+
verifyFilesSchema: [],
140143
handler: async (request, response, context) => {
141144
try {
142145
await context.closePage(request.params.pageId);
@@ -189,6 +192,7 @@ export const newPage = defineTool(args => {
189192
...timeoutSchema,
190193
},
191194
blockedByDialog: false,
195+
verifyFilesSchema: [],
192196
handler: async (request, response, context) => {
193197
const page = await context.newPage(
194198
request.params.background,
@@ -256,6 +260,7 @@ export const navigatePage = definePageTool(args => {
256260
...timeoutSchema,
257261
},
258262
blockedByDialog: false,
263+
verifyFilesSchema: [],
259264
handler: async (request, response) => {
260265
const page = request.page;
261266
const options = {
@@ -391,6 +396,7 @@ export const resizePage = definePageTool({
391396
height: zod.number().describe('Page height'),
392397
},
393398
blockedByDialog: false,
399+
verifyFilesSchema: [],
394400
handler: async (request, response, _context) => {
395401
const page = request.page;
396402

@@ -436,6 +442,7 @@ export const handleDialog = definePageTool({
436442
.describe('Optional prompt text to enter into the dialog.'),
437443
},
438444
blockedByDialog: false,
445+
verifyFilesSchema: [],
439446
handler: async (request, response, _context) => {
440447
const page = request.page;
441448
const dialog = page.getDialog();
@@ -487,6 +494,7 @@ export const getTabId = definePageTool({
487494
),
488495
},
489496
blockedByDialog: false,
497+
verifyFilesSchema: [],
490498
handler: async (request, response, context) => {
491499
const page = context.getPageById(request.params.pageId);
492500
const tabId = (page.pptrPage as unknown as CdpPage)._tabId;

0 commit comments

Comments
 (0)