minor updates based on code review

This commit is contained in:
Cynthia Long
2026-03-10 19:12:14 +00:00
parent 5103046713
commit 589562ab65
@@ -113,12 +113,13 @@ export class BrowserManager {
throw signal.reason ?? new Error('Operation cancelled'); throw signal.reason ?? new Error('Operation cancelled');
} }
if (!this.checkNavigationRestrictions(toolName, args)) { const errorMessage = this.checkNavigationRestrictions(toolName, args);
if (errorMessage) {
return { return {
content: [ content: [
{ {
type: 'text', type: 'text',
text: `Tool '${toolName}' is not permitted for the requested URL/domain based on your current browser settings.`, text: errorMessage,
}, },
], ],
isError: true, isError: true,
@@ -464,36 +465,38 @@ export class BrowserManager {
} }
/** /**
* Check navigation restrictions based on tools and the args sending * Check navigation restrictions based on tools and the args sent
* along with it. * along with them.
*
* @returns error message if failed, undefined if passed.
*/ */
private checkNavigationRestrictions( private checkNavigationRestrictions(
toolName: string, toolName: string,
args: Record<string, unknown>, args: Record<string, unknown>,
): boolean { ): string | undefined {
const pageNavigationTools = ['navigate_page', 'new_page']; const pageNavigationTools = ['navigate_page', 'new_page'];
if (!pageNavigationTools.includes(toolName)) { if (!pageNavigationTools.includes(toolName)) {
return true; return undefined;
} }
const allowedDomains = const allowedDomains =
this.config.getBrowserAgentConfig().customConfig.allowedDomains; this.config.getBrowserAgentConfig().customConfig.allowedDomains;
if (!allowedDomains || allowedDomains.length === 0) { if (!allowedDomains || allowedDomains.length === 0) {
return true; return undefined;
} }
const url = args['url']; const url = args['url'];
if (!url) { if (!url) {
return true; return undefined;
} }
if (typeof url !== 'string') { if (typeof url !== 'string') {
throw new Error('Invalid URL: URL must be a string.'); return `Invalid URL: URL must be a string.`;
} }
try { try {
const parsedUrl = new URL(url); const parsedUrl = new URL(url);
const urlHostname = parsedUrl.hostname; const urlHostname = parsedUrl.hostname.replace(/\.$/, '');
for (const domainPattern of allowedDomains) { for (const domainPattern of allowedDomains) {
if (domainPattern.startsWith('*.')) { if (domainPattern.startsWith('*.')) {
@@ -502,19 +505,19 @@ export class BrowserManager {
urlHostname === baseDomain || urlHostname === baseDomain ||
urlHostname.endsWith(`.${baseDomain}`) urlHostname.endsWith(`.${baseDomain}`)
) { ) {
return true; return undefined;
} }
} else { } else {
if (urlHostname === domainPattern) { if (urlHostname === domainPattern) {
return true; return undefined;
} }
} }
} }
} catch { } catch {
throw new Error('Invalid URL: Malformed URL string.'); return `Invalid URL: Malformed URL string.`;
} }
// If none matched, then deny // If none matched, then deny
return false; return `Tool '${toolName}' is not permitted for the requested URL/domain based on your current browser settings.`;
} }
} }