Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions src/ToolHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,12 @@ export class ToolHandler {

response.setRedactNetworkHeaders(this.serverArgs.redactNetworkHeaders);
try {
if (this.tool.verifyFilesSchema) {
for (const key of this.tool.verifyFilesSchema) {
const filePath = params[key];
await context.validatePath(filePath as string);
}
}
if (isPageScopedTool(this.tool)) {
const pageId =
typeof params.pageId === 'number' ? params.pageId : undefined;
Expand Down
1 change: 1 addition & 0 deletions src/tools/ToolDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export interface BaseToolDefinition<
};
schema: Schema;
blockedByDialog: boolean;
verifyFilesSchema: Array<keyof Schema>;
}

export interface ToolDefinition<
Expand Down
2 changes: 2 additions & 0 deletions src/tools/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export const listConsoleMessages = definePageTool(cliArgs => {
),
},
blockedByDialog: false,
verifyFilesSchema: [],
handler: async (request, response) => {
response.setIncludeConsoleData(true, {
pageSize: request.params.pageSize,
Expand All @@ -105,6 +106,7 @@ export const getConsoleMessage = definePageTool({
),
},
blockedByDialog: false,
verifyFilesSchema: [],
handler: async (request, response) => {
response.attachConsoleMessage(request.params.msgid);
},
Expand Down
1 change: 1 addition & 0 deletions src/tools/emulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export const emulate = definePageTool({
),
},
blockedByDialog: true,
verifyFilesSchema: [],
handler: async (request, response, context) => {
const page = request.page;
await context.emulate(request.params, page.pptrPage);
Expand Down
7 changes: 5 additions & 2 deletions src/tools/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ export const installExtension = defineTool({
.describe('Absolute path to the unpacked extension folder.'),
},
blockedByDialog: false,
verifyFilesSchema: ['path'],
handler: async (request, response, context) => {
const {path} = request.params;
await context.validatePath(path);
const id = await context.installExtension(path);
response.appendResponseLine(`Extension installed. Id: ${id}`);
},
Expand All @@ -41,6 +41,7 @@ export const uninstallExtension = defineTool({
id: zod.string().describe('ID of the extension to uninstall.'),
},
blockedByDialog: false,
verifyFilesSchema: [],
handler: async (request, response, context) => {
const {id} = request.params;
await context.uninstallExtension(id);
Expand All @@ -58,6 +59,7 @@ export const listExtensions = defineTool({
},
schema: {},
blockedByDialog: false,
verifyFilesSchema: [],
handler: async (_request, response, _context) => {
response.setListExtensions();
},
Expand All @@ -74,13 +76,13 @@ export const reloadExtension = defineTool({
id: zod.string().describe('ID of the extension to reload.'),
},
blockedByDialog: false,
verifyFilesSchema: [],
handler: async (request, response, context) => {
const {id} = request.params;
const extension = await context.getExtension(id);
if (!extension) {
throw new Error(`Extension with ID ${id} not found.`);
}
await context.validatePath(extension.path);
await context.installExtension(extension.path);
response.appendResponseLine('Extension reloaded.');
},
Expand All @@ -97,6 +99,7 @@ export const triggerExtensionAction = defineTool({
id: zod.string().describe('ID of the extension to trigger the action for.'),
},
blockedByDialog: false,
verifyFilesSchema: [],
handler: async (request, response, context) => {
const {id} = request.params;
await context.triggerExtensionAction(id);
Expand Down
12 changes: 10 additions & 2 deletions src/tools/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export const click = definePageTool({
includeSnapshot: includeSnapshotSchema,
},
blockedByDialog: true,
verifyFilesSchema: [],
handler: async (request, response) => {
const uid = request.params.uid;
const handle = await request.page.getElementByUid(uid);
Expand Down Expand Up @@ -154,6 +155,7 @@ export const clickAt = definePageTool({
includeSnapshot: includeSnapshotSchema,
},
blockedByDialog: true,
verifyFilesSchema: [],
handler: async (request, response) => {
const page = request.page;
const result = await page.waitForEventsAfterAction(async () => {
Expand Down Expand Up @@ -189,6 +191,7 @@ export const hover = definePageTool({
includeSnapshot: includeSnapshotSchema,
},
blockedByDialog: true,
verifyFilesSchema: [],
handler: async (request, response) => {
const uid = request.params.uid;
const handle = await request.page.getElementByUid(uid);
Expand Down Expand Up @@ -316,6 +319,7 @@ export const fill = definePageTool({
includeSnapshot: includeSnapshotSchema,
},
blockedByDialog: true,
verifyFilesSchema: [],
handler: async (request, response, context) => {
const page = request.page;
const result = await page.waitForEventsAfterAction(async () => {
Expand Down Expand Up @@ -346,6 +350,7 @@ export const typeText = definePageTool({
submitKey: submitKeySchema,
},
blockedByDialog: true,
verifyFilesSchema: [],
handler: async (request, response) => {
const page = request.page;
const result = await page.waitForEventsAfterAction(async () => {
Expand Down Expand Up @@ -376,6 +381,7 @@ export const drag = definePageTool({
includeSnapshot: includeSnapshotSchema,
},
blockedByDialog: true,
verifyFilesSchema: [],
handler: async (request, response) => {
const fromHandle = await request.page.getElementByUid(
request.params.from_uid,
Expand Down Expand Up @@ -423,6 +429,7 @@ export const fillForm = definePageTool({
includeSnapshot: includeSnapshotSchema,
},
blockedByDialog: true,
verifyFilesSchema: [],
handler: async (request, response, context) => {
const page = request.page;
let lastResult: WaitForEventsResult = {};
Expand Down Expand Up @@ -461,9 +468,9 @@ export const uploadFile = definePageTool({
includeSnapshot: includeSnapshotSchema,
},
blockedByDialog: true,
handler: async (request, response, context) => {
verifyFilesSchema: ['filePath'],
handler: async (request, response, _context) => {
const {uid, filePath} = request.params;
await context.validatePath(filePath);
const handle = (await request.page.getElementByUid(
uid,
)) as ElementHandle<HTMLInputElement>;
Expand Down Expand Up @@ -512,6 +519,7 @@ export const pressKey = definePageTool({
includeSnapshot: includeSnapshotSchema,
},
blockedByDialog: true,
verifyFilesSchema: [],
handler: async (request, response) => {
const page = request.page;
const tokens = parseKey(request.params.key);
Expand Down
3 changes: 1 addition & 2 deletions src/tools/lighthouse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export const lighthouseAudit = definePageTool({
.describe('Directory for reports. If omitted, uses temporary files.'),
},
blockedByDialog: true,
verifyFilesSchema: ['outputDirPath'],
handler: async (request, response, context) => {
const page = request.page;
const categories = [
Expand All @@ -59,8 +60,6 @@ export const lighthouseAudit = definePageTool({
outputDirPath,
} = request.params;

await context.validatePath(outputDirPath);

const flags: Flags = {
onlyCategories: categories,
output: formats,
Expand Down
13 changes: 6 additions & 7 deletions src/tools/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ export const takeHeapSnapshot = definePageTool({
.describe('A path to a .heapsnapshot file to save the heapsnapshot to.'),
},
blockedByDialog: true,
handler: async (request, response, context) => {
verifyFilesSchema: ['filePath'],
handler: async (request, response) => {
const page = request.page;
await context.validatePath(request.params.filePath);

await page.pptrPage.captureHeapSnapshot({
path: ensureExtension(request.params.filePath, '.heapsnapshot'),
Expand All @@ -50,8 +50,8 @@ export const getHeapSnapshotSummary = defineTool({
filePath: zod.string().describe('A path to a .heapsnapshot file to read.'),
},
blockedByDialog: false,
verifyFilesSchema: ['filePath'],
handler: async (request, response, context) => {
await context.validatePath(request.params.filePath);
const stats = await context.getHeapSnapshotStats(request.params.filePath);
const staticData = await context.getHeapSnapshotStaticData(
request.params.filePath,
Expand Down Expand Up @@ -82,8 +82,8 @@ export const getHeapSnapshotDetails = defineTool({
.describe('The page size for pagination of aggregates.'),
},
blockedByDialog: false,
verifyFilesSchema: ['filePath'],
handler: async (request, response, context) => {
await context.validatePath(request.params.filePath);
const aggregates = await context.getHeapSnapshotAggregates(
request.params.filePath,
);
Expand Down Expand Up @@ -111,8 +111,8 @@ export const getHeapSnapshotClassNodes = defineTool({
pageSize: zod.number().optional().describe('The page size for pagination.'),
},
blockedByDialog: false,
verifyFilesSchema: ['filePath'],
handler: async (request, response, context) => {
await context.validatePath(request.params.filePath);
const nodes = await context.getHeapSnapshotNodesById(
request.params.filePath,
request.params.id,
Expand All @@ -135,15 +135,14 @@ export const getHeapSnapshotRetainers = defineTool({
conditions: ['experimentalMemory'],
},
blockedByDialog: false,
verifyFilesSchema: ['filePath'],
schema: {
filePath: zod.string().describe('A path to a .heapsnapshot file to read.'),
nodeId: zod.number().describe('The node ID to get retainers for.'),
pageIdx: zod.number().optional().describe('The page index for pagination.'),
pageSize: zod.number().optional().describe('The page size for pagination.'),
},
handler: async (request, response, context) => {
await context.validatePath(request.params.filePath);

const retainers = await context.getHeapSnapshotRetainers(
request.params.filePath,
request.params.nodeId,
Expand Down
4 changes: 2 additions & 2 deletions src/tools/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export const listNetworkRequests = definePageTool({
),
},
blockedByDialog: false,
verifyFilesSchema: [],
handler: async (request, response, context) => {
const data = await request.page.getDevToolsData();
response.attachDevToolsData(data);
Expand Down Expand Up @@ -115,9 +116,8 @@ export const getNetworkRequest = definePageTool({
),
},
blockedByDialog: true,
verifyFilesSchema: ['requestFilePath', 'responseFilePath'],
handler: async (request, response, context) => {
await context.validatePath(request.params.requestFilePath);
await context.validatePath(request.params.responseFilePath);
if (request.params.reqid) {
response.attachNetworkRequest(request.params.reqid, {
requestFilePath: request.params.requestFilePath,
Expand Down
8 changes: 8 additions & 0 deletions src/tools/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export const listPages = defineTool(args => {
},
schema: {},
blockedByDialog: false,
verifyFilesSchema: [],
handler: async (_request, response) => {
response.setIncludePages(true);
response.setListThirdPartyDeveloperTools();
Expand Down Expand Up @@ -112,6 +113,7 @@ export const selectPage = defineTool({
.describe('Whether to focus the page and bring it to the top.'),
},
blockedByDialog: false,
verifyFilesSchema: [],
handler: async (request, response, context) => {
const page = context.getPageById(request.params.pageId);
context.selectPage(page);
Expand All @@ -137,6 +139,7 @@ export const closePage = defineTool({
.describe('The ID of the page to close. Call list_pages to list pages.'),
},
blockedByDialog: false,
verifyFilesSchema: [],
handler: async (request, response, context) => {
try {
await context.closePage(request.params.pageId);
Expand Down Expand Up @@ -189,6 +192,7 @@ export const newPage = defineTool(args => {
...timeoutSchema,
},
blockedByDialog: false,
verifyFilesSchema: [],
handler: async (request, response, context) => {
const page = await context.newPage(
request.params.background,
Expand Down Expand Up @@ -256,6 +260,7 @@ export const navigatePage = definePageTool(args => {
...timeoutSchema,
},
blockedByDialog: false,
verifyFilesSchema: [],
handler: async (request, response) => {
const page = request.page;
const options = {
Expand Down Expand Up @@ -391,6 +396,7 @@ export const resizePage = definePageTool({
height: zod.number().describe('Page height'),
},
blockedByDialog: false,
verifyFilesSchema: [],
handler: async (request, response, _context) => {
const page = request.page;

Expand Down Expand Up @@ -436,6 +442,7 @@ export const handleDialog = definePageTool({
.describe('Optional prompt text to enter into the dialog.'),
},
blockedByDialog: false,
verifyFilesSchema: [],
handler: async (request, response, _context) => {
const page = request.page;
const dialog = page.getDialog();
Expand Down Expand Up @@ -487,6 +494,7 @@ export const getTabId = definePageTool({
),
},
blockedByDialog: false,
verifyFilesSchema: [],
handler: async (request, response, context) => {
const page = context.getPageById(request.params.pageId);
const tabId = (page.pptrPage as unknown as CdpPage)._tabId;
Expand Down
5 changes: 3 additions & 2 deletions src/tools/performance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ export const startTrace = definePageTool({
filePath: filePathSchema,
},
blockedByDialog: true,
verifyFilesSchema: ['filePath'],
handler: async (request, response, context) => {
await context.validatePath(request.params.filePath);
if (context.isRunningPerformanceTrace()) {
response.appendResponseLine(
'Error: a performance trace is already running. Use performance_stop_trace to stop it. Only one trace can be running at any given time.',
Expand Down Expand Up @@ -127,8 +127,8 @@ export const stopTrace = definePageTool({
filePath: filePathSchema,
},
blockedByDialog: true,
verifyFilesSchema: ['filePath'],
handler: async (request, response, context) => {
await context.validatePath(request.params.filePath);
if (!context.isRunningPerformanceTrace()) {
return;
}
Expand Down Expand Up @@ -163,6 +163,7 @@ export const analyzeInsight = definePageTool({
),
},
blockedByDialog: false,
verifyFilesSchema: [],
handler: async (request, response, context) => {
const lastRecording = context.recordedTraces().at(-1);
if (!lastRecording) {
Expand Down
3 changes: 2 additions & 1 deletion src/tools/screencast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export const startScreencast = definePageTool(args => ({
),
},
blockedByDialog: false,
verifyFilesSchema: ['filePath'],
handler: async (request, response, context) => {
await context.validatePath(request.params.filePath);
if (context.getScreenRecorder() !== null) {
response.appendResponseLine(
'Error: a screencast recording is already in progress. Use screencast_stop to stop it before starting a new one.',
Expand Down Expand Up @@ -103,6 +103,7 @@ export const stopScreencast = definePageTool({
},
schema: {},
blockedByDialog: false,
verifyFilesSchema: [],
handler: async (_request, response, context) => {
const data = context.getScreenRecorder();
if (!data) {
Expand Down
2 changes: 1 addition & 1 deletion src/tools/screenshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ export const screenshot = definePageTool({
),
},
blockedByDialog: true,
verifyFilesSchema: ['filePath'],
handler: async (request, response, context) => {
await context.validatePath(request.params.filePath);
if (request.params.uid && request.params.fullPage) {
throw new Error('Providing both "uid" and "fullPage" is not allowed.');
}
Expand Down
Loading
Loading