Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
π Honor HTTP_PROXY/HTTPS_PROXY for outbound fetch (#2897)
Luke Vella60437dc24044
diff --git a/apps/web/package.json b/apps/web/package.jsonindex 6b11a5858..a9045275e 100644--- a/apps/web/package.json+++ b/apps/web/package.json@@ -98,6 +98,7 @@"smoothscroll-polyfill": "^0.4.4","superjson": "^2.0.0","tailwindcss": "^4.1.18",+ "undici": "^7.29.0","zod": "^4.3.6","zustand": "^5.0.14","zxcvbn": "^4.4.2"diff --git a/apps/web/src/instrumentation.ts b/apps/web/src/instrumentation.tsindex 598fac0ed..85569497b 100644--- a/apps/web/src/instrumentation.ts+++ b/apps/web/src/instrumentation.ts@@ -4,6 +4,12 @@ export const onRequestError = Sentry.captureRequestError;export async function register() {if (process.env.NEXT_RUNTIME === "nodejs") {+ // Before Sentry, so its outbound traffic is proxied too. The import must+ // stay dynamic: a top-level import would be hoisted into the Edge bundle,+ // where undici doesn't exist.+ const { setupOutboundProxy } = await import("@/lib/outbound-proxy");+ setupOutboundProxy();+await import("../sentry.server.config");}diff --git a/apps/web/src/lib/outbound-proxy.test.ts b/apps/web/src/lib/outbound-proxy.test.tsnew file mode 100644index 000000000..01846545b--- /dev/null+++ b/apps/web/src/lib/outbound-proxy.test.ts@@ -0,0 +1,115 @@+import type { Server } from "node:http";+import { createServer } from "node:http";+import type { AddressInfo } from "node:net";+import { getGlobalDispatcher, setGlobalDispatcher } from "undici";+import { afterEach, beforeEach, expect, test, vi } from "vitest";+import { setupOutboundProxy } from "./outbound-proxy";++vi.mock("@rallly/logger", () => ({+ createLogger: () => ({ info: vi.fn() }),+}));++const PROXY_ENV_VARS = [+ "HTTP_PROXY",+ "HTTPS_PROXY",+ "http_proxy",+ "https_proxy",+ "NO_PROXY",+ "no_proxy",+];++const savedEnv: Record<string, string | undefined> = {};+const originalDispatcher = getGlobalDispatcher();+let server: Server | undefined;++beforeEach(() => {+ for (const name of PROXY_ENV_VARS) {+ savedEnv[name] = process.env[name];+ delete process.env[name];+ }+});++afterEach(async () => {+ for (const name of PROXY_ENV_VARS) {+ if (savedEnv[name] === undefined) {+ delete process.env[name];+ } else {+ process.env[name] = savedEnv[name];+ }+ }+ setGlobalDispatcher(originalDispatcher);+ if (server) {+ await new Promise((resolve) => server?.close(resolve));+ server = undefined;+ }+});++/**+ * A local server that behaves as a forward proxy for both proxying styles:+ * CONNECT tunneling (what undici uses today) and absolute-form requests+ * (what curl uses for plain HTTP), so the test doesn't break if undici+ * changes mechanics across majors. Every request is answered with "proxied"+ * and the target host is recorded.+ */+function startProxyServer() {+ const targets: string[] = [];++ const proxy = createServer((req, res) => {+ targets.push(new URL(req.url as string).host);+ res.setHeader("connection", "close");+ res.end("proxied");+ });++ proxy.on("connect", (req, clientSocket) => {+ targets.push(req.url as string);+ clientSocket.write("HTTP/1.1 200 Connection Established\r\n\r\n");+ clientSocket.once("data", () => {+ clientSocket.end(+ "HTTP/1.1 200 OK\r\ncontent-length: 7\r\nconnection: close\r\n\r\nproxied",+ );+ });+ clientSocket.on("error", () => {});+ });++ server = proxy;++ return new Promise<{ port: number; targets: string[] }>((resolve) => {+ proxy.listen(0, "127.0.0.1", () => {+ resolve({ port: (proxy.address() as AddressInfo).port, targets });+ });+ });+}++/**+ * The load-bearing assumption behind setupOutboundProxy is that the INSTALLED+ * undici package's setGlobalDispatcher registers the dispatcher that Node's+ * BUNDLED undici (global fetch) consults β they share a Symbol.for-registered+ * global. If a Node upgrade ships an undici whose global-dispatcher symbol+ * version no longer matches the installed package's, the proxy setup becomes+ * a silent no-op. The target host is unresolvable (.invalid), so this fetch+ * can only succeed by going through the proxy.+ */+test("Node's global fetch routes through the proxy from HTTP_PROXY", async () => {+ const { port, targets } = await startProxyServer();+ process.env.HTTP_PROXY = `http://127.0.0.1:${port}`;++ setupOutboundProxy();++ const res = await fetch("http://rallly-proxy-contract-check.invalid/ping", {+ signal: AbortSignal.timeout(5000),+ });++ expect(res.status).toBe(200);+ expect(await res.text()).toBe("proxied");+ expect(targets).toContainEqual(+ expect.stringContaining("rallly-proxy-contract-check.invalid"),+ );+});++test("leaves the global dispatcher untouched when no proxy variables are set", () => {+ const before = getGlobalDispatcher();++ setupOutboundProxy();++ expect(getGlobalDispatcher()).toBe(before);+});diff --git a/apps/web/src/lib/outbound-proxy.ts b/apps/web/src/lib/outbound-proxy.tsnew file mode 100644index 000000000..ed8ac4604--- /dev/null+++ b/apps/web/src/lib/outbound-proxy.ts@@ -0,0 +1,56 @@+import "server-only";++import { createLogger } from "@rallly/logger";+import { EnvHttpProxyAgent, setGlobalDispatcher } from "undici";++const logger = createLogger("outbound-proxy");++const PROXY_ENV_VARS = [+ "HTTP_PROXY",+ "HTTPS_PROXY",+ "http_proxy",+ "https_proxy",+] as const;++// Proxy URLs may carry credentials (http://user:pass@proxy) β never log them.+function sanitize(value: string) {+ try {+ const url = new URL(value);+ url.username = "";+ url.password = "";+ return url.href;+ } catch {+ return "<invalid URL>";+ }+}++/**+ * Node's built-in fetch ignores proxy environment variables, so on networks+ * with no direct egress (self-hosted behind a corporate proxy) outbound calls+ * never reach the proxy at all. EnvHttpProxyAgent honors HTTP_PROXY /+ * HTTPS_PROXY / NO_PROXY in both cases. Node consults the global dispatcher+ * on every fetch, but anything fetched before this runs goes direct β call it+ * first at boot.+ *+ * Node 24's experimental NODE_USE_ENV_PROXY does the same thing natively;+ * when it stabilizes, this module can be deleted in its favor.+ */+export function setupOutboundProxy() {+ const configured = PROXY_ENV_VARS.filter((name) => process.env[name]);++ if (configured.length === 0) {+ return;+ }++ setGlobalDispatcher(new EnvHttpProxyAgent());++ logger.info(+ {+ proxies: Object.fromEntries(+ configured.map((name) => [name, sanitize(process.env[name] as string)]),+ ),+ noProxy: process.env.NO_PROXY ?? process.env.no_proxy ?? null,+ },+ "Outbound proxy enabled: fetch will route through the configured proxy",+ );+}diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 61cba55bc..883a5ac9a 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -94,7 +94,7 @@ importers:version: 8.1.0([email protected])'@vercel/analytics':specifier: ^1.5.0- version: 1.6.1([email protected](@babel/[email protected])(@opentelemetry/[email protected])(@playwright/[email protected])(@types/[email protected])([email protected]([email protected]))([email protected]))([email protected])+ version: 1.6.1([email protected](@opentelemetry/[email protected])(@playwright/[email protected])(@types/[email protected])([email protected]([email protected]))([email protected]))([email protected])dayjs:specifier: ^1.11.13version: 1.11.19@@ -133,7 +133,7 @@ importers:version: 6.0.0(@types/[email protected])([email protected])next-seo:specifier: ^6.1.0- version: 6.8.0([email protected](@babel/[email protected])(@opentelemetry/[email protected])(@playwright/[email protected])(@types/[email protected])([email protected]([email protected]))([email protected]))([email protected]([email protected]))([email protected])+ version: 6.8.0([email protected](@opentelemetry/[email protected])(@playwright/[email protected])(@types/[email protected])([email protected]([email protected]))([email protected]))([email protected]([email protected]))([email protected])pg:specifier: ^8.17.1version: 8.18.0@@ -423,6 +423,9 @@ importers:tailwindcss:specifier: ^4.1.18version: 4.1.18+ undici:+ specifier: ^7.29.0+ version: 7.29.0zod:specifier: ^4.3.6version: 4.3.6@@ -11224,8 +11227,8 @@ packages:resolution: {integrity: sha512-Zz+aZWSj8LE6zoxD+xrjh4VfkIG8Ya6LvYkZqtUQGJPZjYl53ypCaUwWqo7eI0x66KBGeRo+mlBEkMSeSZ38Nw==}- resolution: {integrity: sha512-MJZrkjyd7DeC+uPZh+5/YaMDxFiiEEaDgbUSVMXayofAkDWF1088CDo+2RPg7B1BuS1qf1vgNE7xqwPxE0DuSQ==}+ resolution: {integrity: sha512-IDxfleLmmbSskfWSUATiN1nfn2rDuvnMOqb5CWR92iIfojA0Ud+ulOAAEQ57LPr9rWmsreUyf5lwyao+7GNNVw==}engines: {node: '>=20.18.1'}@@ -18356,7 +18359,7 @@ snapshots:dependencies:uncrypto: 0.1.3- '@vercel/[email protected]([email protected](@babel/[email protected])(@opentelemetry/[email protected])(@playwright/[email protected])(@types/[email protected])([email protected]([email protected]))([email protected]))([email protected])':+ '@vercel/[email protected]([email protected](@opentelemetry/[email protected])(@playwright/[email protected])(@types/[email protected])([email protected]([email protected]))([email protected]))([email protected])':optionalDependencies:next: 16.3.0(@babel/[email protected])(@opentelemetry/[email protected])(@playwright/[email protected])(@types/[email protected])([email protected]([email protected]))([email protected])react: 19.2.6@@ -19017,7 +19020,7 @@ snapshots:parse5: 7.3.0parse5-htmlparser2-tree-adapter: 7.1.0parse5-parser-stream: 7.1.2- undici: 7.20.0+ undici: 7.29.0whatwg-mimetype: 4.0.0@@ -22108,7 +22111,7 @@ snapshots:react: 19.2.6react-dom: 19.2.6([email protected])- [email protected]([email protected](@babel/[email protected])(@opentelemetry/[email protected])(@playwright/[email protected])(@types/[email protected])([email protected]([email protected]))([email protected]))([email protected]([email protected]))([email protected]):+ [email protected]([email protected](@opentelemetry/[email protected])(@playwright/[email protected])(@types/[email protected])([email protected]([email protected]))([email protected]))([email protected]([email protected]))([email protected]):dependencies:next: 16.3.0(@babel/[email protected])(@opentelemetry/[email protected])(@playwright/[email protected])(@types/[email protected])([email protected]([email protected]))([email protected])react: 19.2.6@@ -24428,7 +24431,7 @@ snapshots:- [email protected]: {}+ [email protected]: {}