Skip to content

Commit 2528122

Browse files
refactor: use validate files on the tool level
1 parent 50cecd8 commit 2528122

12 files changed

Lines changed: 26 additions & 26 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: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ import type {
3232
ToolDefinition as ThirdPartyDeveloperToolDefinition,
3333
} from './thirdPartyDeveloper.js';
3434

35-
export interface BaseToolDefinition<
36-
Schema extends zod.ZodRawShape = zod.ZodRawShape,
37-
> {
35+
export interface BaseToolDefinition<Schema extends zod.ZodRawShape = zod.ZodRawShape> {
3836
name: string;
3937
description: string;
4038
annotations: {
@@ -48,6 +46,7 @@ export interface BaseToolDefinition<
4846
};
4947
schema: Schema;
5048
blockedByDialog: boolean;
49+
verifyFilesSchema?: Array<keyof Schema>;
5150
}
5251

5352
export interface ToolDefinition<

src/tools/extensions.ts

Lines changed: 1 addition & 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
},
@@ -80,7 +80,6 @@ export const reloadExtension = defineTool({
8080
if (!extension) {
8181
throw new Error(`Extension with ID ${id} not found.`);
8282
}
83-
await context.validatePath(extension.path);
8483
await context.installExtension(extension.path);
8584
response.appendResponseLine('Extension reloaded.');
8685
},

src/tools/input.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,9 +461,9 @@ export const uploadFile = definePageTool({
461461
includeSnapshot: includeSnapshotSchema,
462462
},
463463
blockedByDialog: true,
464-
handler: async (request, response, context) => {
464+
verifyFilesSchema: ['filePath'],
465+
handler: async (request, response, _context) => {
465466
const {uid, filePath} = request.params;
466-
await context.validatePath(filePath);
467467
const handle = (await request.page.getElementByUid(
468468
uid,
469469
)) as ElementHandle<HTMLInputElement>;

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: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,8 @@ export const getNetworkRequest = definePageTool({
115115
),
116116
},
117117
blockedByDialog: true,
118+
verifyFilesSchema: ['requestFilePath', 'responseFilePath'],
118119
handler: async (request, response, context) => {
119-
await context.validatePath(request.params.requestFilePath);
120-
await context.validatePath(request.params.responseFilePath);
121120
if (request.params.reqid) {
122121
response.attachNetworkRequest(request.params.reqid, {
123122
requestFilePath: request.params.requestFilePath,

src/tools/performance.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ export const startTrace = definePageTool({
4848
filePath: filePathSchema,
4949
},
5050
blockedByDialog: true,
51+
verifyFilesSchema: ['filePath'],
5152
handler: async (request, response, context) => {
52-
await context.validatePath(request.params.filePath);
5353
if (context.isRunningPerformanceTrace()) {
5454
response.appendResponseLine(
5555
'Error: a performance trace is already running. Use performance_stop_trace to stop it. Only one trace can be running at any given time.',
@@ -127,8 +127,8 @@ export const stopTrace = definePageTool({
127127
filePath: filePathSchema,
128128
},
129129
blockedByDialog: true,
130+
verifyFilesSchema: ['filePath'],
130131
handler: async (request, response, context) => {
131-
await context.validatePath(request.params.filePath);
132132
if (!context.isRunningPerformanceTrace()) {
133133
return;
134134
}

src/tools/screencast.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ export const startScreencast = definePageTool(args => ({
3939
),
4040
},
4141
blockedByDialog: false,
42+
verifyFilesSchema: ['filePath'],
4243
handler: async (request, response, context) => {
43-
await context.validatePath(request.params.filePath);
4444
if (context.getScreenRecorder() !== null) {
4545
response.appendResponseLine(
4646
'Error: a screencast recording is already in progress. Use screencast_stop to stop it before starting a new one.',

src/tools/screenshot.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ export const screenshot = definePageTool({
5151
),
5252
},
5353
blockedByDialog: true,
54+
verifyFilesSchema: ['filePath'],
5455
handler: async (request, response, context) => {
55-
await context.validatePath(request.params.filePath);
5656
if (request.params.uid && request.params.fullPage) {
5757
throw new Error('Providing both "uid" and "fullPage" is not allowed.');
5858
}

0 commit comments

Comments
 (0)