Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

1.6 KB
Raw Blame
const SAFE_EXTERNAL_URL_SCHEMES: ReadonlySet<string> = new Set([
  "http:",
  "https:",
  "mailto:",
]);

/**
 * Whether a URL is safe to hand to the host's "open externally" capability,
 * which ultimately reaches `shell.openExternal` and dispatches to whatever app
 * the OS has registered for the scheme. Restricting to web and mail schemes
 * stops a tampered or attacker-supplied value from triggering `file:`, `smb:`,
 * `data:`, `javascript:`, `ms-msdt:`, or custom app deep-link schemes.
 */
export function isSafeExternalUrl(url: string): boolean {
  let parsed: URL;
  try {
    parsed = new URL(url);
  } catch {
    return false;
  }
  return SAFE_EXTERNAL_URL_SCHEMES.has(parsed.protocol);
}

/**
 * Whether a URL from untrusted code (the freeform-canvas sandbox) may be
 * opened externally: absolute https URLs on posthog.com or a subdomain only.
 */
export function isSafePostHogUrl(url: string): boolean {
  let parsed: URL;
  try {
    parsed = new URL(url);
  } catch {
    return false;
  }
  return (
    parsed.protocol === "https:" &&
    (parsed.hostname === "posthog.com" ||
      parsed.hostname.endsWith(".posthog.com"))
  );
}

export function isSafeGitHubPullRequestUrl(url: string): boolean {
  let parsed: URL;
  try {
    parsed = new URL(url);
  } catch {
    return false;
  }
  return (
    parsed.protocol === "https:" &&
    parsed.hostname === "github.com" &&
    !parsed.username &&
    !parsed.password &&
    !parsed.port &&
    !parsed.search &&
    /^\/[a-z0-9-]+\/[a-z0-9_.-]+\/pull\/[1-9]\d*(?:\/(?:files|commits|checks))?\/?$/i.test(
      parsed.pathname,
    )
  );
}