Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

5.6 KB
Raw Blame
import type { Socket } from "net";
import { config } from "../../../../config";
import type { TLSSocket } from "tls";
import * as undici from "undici";
import { interceptors } from "undici";
import { CookieJar } from "tough-cookie";
import { cookie } from "http-cookie-agent/undici";
import IPAddr from "ipaddr.js";

// Node's certificate verification errors, which rejectUnauthorized: false
// suppresses. CRL and OpenSSL hostname codes are omitted: Node never checks those.
export const TLS_CERT_ERROR_CODES = new Set([
  "UNABLE_TO_GET_ISSUER_CERT",
  "UNABLE_TO_GET_ISSUER_CERT_LOCALLY",
  "UNABLE_TO_VERIFY_LEAF_SIGNATURE",
  "UNABLE_TO_DECRYPT_CERT_SIGNATURE",
  "UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY",
  "CERT_SIGNATURE_FAILURE",
  "CERT_NOT_YET_VALID",
  "CERT_HAS_EXPIRED",
  "ERROR_IN_CERT_NOT_BEFORE_FIELD",
  "ERROR_IN_CERT_NOT_AFTER_FIELD",
  "DEPTH_ZERO_SELF_SIGNED_CERT",
  "SELF_SIGNED_CERT_IN_CHAIN",
  "CERT_CHAIN_TOO_LONG",
  "INVALID_CA",
  "PATH_LENGTH_EXCEEDED",
  "INVALID_PURPOSE",
  "CERT_UNTRUSTED",
  "CERT_REJECTED",
  "UNSPECIFIED",
  "ERR_TLS_CERT_ALTNAME_INVALID",
]);
export class InsecureConnectionError extends Error {
  constructor() {
    super("Connection violated security rules.");
  }
}

export function isIPPrivate(address: string): boolean {
  if (!IPAddr.isValid(address)) return false;

  const addr = IPAddr.parse(address);
  return addr.range() !== "unicast";
}

/**
 * Reject private IP literals and localhost names before dispatch, on the
 * initial request and every redirect hop. With PROXY_SERVER set, the socket
 * check below never sees the destination, so this is the only local guard.
 * Other hostnames are resolved proxy-side and must be filtered by the proxy.
 */
export const rejectPrivateIPLiteralTargets: undici.Dispatcher.DispatcherComposeInterceptor =
  dispatch => (options, handler) => {
    if (config.ALLOW_LOCAL_WEBHOOKS === true || options.origin === undefined) {
      return dispatch(options, handler);
    }

    let privateTarget = false;

    try {
      const origin =
        options.origin instanceof URL
          ? options.origin
          : new URL(options.origin.toString());
      const hostname = origin.hostname.replace(/^\[|\]$/g, "");
      privateTarget =
        isIPPrivate(hostname) || /(^|\.)localhost\.?$/.test(hostname);
    } catch {
      // Let Undici report malformed origins through its normal path.
    }

    if (privateTarget) {
      const error = new InsecureConnectionError();
      const compatibleHandler = handler as typeof handler & {
        onResponseError?: (controller: unknown, error: Error) => void;
      };

      if (typeof compatibleHandler.onError === "function") {
        compatibleHandler.onError(error);
      } else if (typeof compatibleHandler.onResponseError === "function") {
        compatibleHandler.onResponseError(null, error);
      } else {
        throw error;
      }

      return true;
    }

    return dispatch(options, handler);
  };

function createBaseAgent(skipTlsVerification: boolean) {
  const baseAgent = config.PROXY_SERVER
    ? new undici.ProxyAgent({
        uri: config.PROXY_SERVER.includes("://")
          ? config.PROXY_SERVER
          : "http://" + config.PROXY_SERVER,
        token: config.PROXY_USERNAME
          ? `Basic ${Buffer.from(config.PROXY_USERNAME + ":" + (config.PROXY_PASSWORD ?? "")).toString("base64")}`
          : undefined,
        requestTls: {
          rejectUnauthorized: !skipTlsVerification, // Only bypass SSL verification if explicitly requested
        },
      })
    : new undici.Agent({
        connect: {
          rejectUnauthorized: !skipTlsVerification, // Only bypass SSL verification if explicitly requested
        },
      });

  // Add redirect interceptor for handling redirects
  return baseAgent.compose(
    rejectPrivateIPLiteralTargets,
    interceptors.redirect({ maxRedirections: 5000 }),
  );
}

function attachSecurityCheck(agent: undici.Dispatcher) {
  agent.on("connect", (_, targets) => {
    const client: undici.Client = targets.slice(-1)[0] as undici.Client;
    const socketSymbol = Object.getOwnPropertySymbols(client).find(
      x => x.description === "socket",
    )!;
    const socket: Socket | TLSSocket = (client as any)[socketSymbol];

    if (
      socket.remoteAddress &&
      isIPPrivate(socket.remoteAddress) &&
      config.ALLOW_LOCAL_WEBHOOKS !== true
    ) {
      socket.destroy(new InsecureConnectionError());
    }
  });
}

function makeSecureDispatcher(skipTlsVerification: boolean) {
  const agent = createBaseAgent(skipTlsVerification);
  attachSecurityCheck(agent);
  return agent;
}

// Dispatcher WITHOUT cookie handling (for webhooks - avoids empty cookie header bug)
function makeSecureDispatcherNoCookies(skipTlsVerification: boolean) {
  const agent = createBaseAgent(skipTlsVerification);
  attachSecurityCheck(agent);
  return agent;
}

const secureDispatcher = makeSecureDispatcher(false);
const secureDispatcherSkipTlsVerification = makeSecureDispatcher(true);
const secureDispatcherNoCookies = makeSecureDispatcherNoCookies(false);
const secureDispatcherNoCookiesSkipTlsVerification =
  makeSecureDispatcherNoCookies(true);

export const getSecureDispatcher = (skipTlsVerification: boolean = false) => {
  const dispatcher = skipTlsVerification
    ? secureDispatcherSkipTlsVerification
    : secureDispatcher;

  return dispatcher.compose(cookie({ jar: new CookieJar() }));
};

// Use this for webhook delivery to avoid sending empty cookie headers
export const getSecureDispatcherNoCookies = (
  skipTlsVerification: boolean = false,
) =>
  skipTlsVerification
    ? secureDispatcherNoCookiesSkipTlsVerification
    : secureDispatcherNoCookies;