sandbox-agent/frontend/packages/inspector/src/lib/permissions.ts
Nathan Flurry d2346bafb3
Configure lefthook formatter checks (#231)
* Add lefthook formatter checks

* Fix SDK mode hydration

* Stabilize SDK mode integration test
2026-03-10 23:03:11 -07:00

48 lines
1.3 KiB
TypeScript

export const askForLocalNetworkAccess = async (): Promise<boolean> => {
try {
const status = await navigator.permissions.query({
// @ts-expect-error - local-network-access is not in standard types
name: "local-network-access",
});
if (status.state === "granted") {
return true;
}
if (status.state === "denied") {
return false;
}
// If promptable, return true - browser will prompt on first request
return true;
} catch {
// Permissions API not supported or permission not recognized - try anyway
return true;
}
};
export const isHttpsToHttpConnection = (pageUrl: string, targetUrl: string): boolean => {
try {
const page = new URL(pageUrl);
const target = new URL(targetUrl);
return page.protocol === "https:" && target.protocol === "http:";
} catch {
return false;
}
};
export const isLocalNetworkTarget = (targetUrl: string): boolean => {
try {
const url = new URL(targetUrl);
const hostname = url.hostname.toLowerCase();
return (
hostname === "localhost" ||
hostname === "127.0.0.1" ||
hostname === "::1" ||
hostname.endsWith(".local") ||
// Private IPv4 ranges
/^10\./.test(hostname) ||
/^172\.(1[6-9]|2[0-9]|3[0-1])\./.test(hostname) ||
/^192\.168\./.test(hostname)
);
} catch {
return false;
}
};