Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🔨 chore: respect NO_PROXY for dev auth fetches (#16247)

YuTengjingbe260e6798d2
diff --git a/src/libs/better-auth/define-config.test.ts b/src/libs/better-auth/define-config.test.ts
index 76ac24d86ca..2ef14eea6aa 100644
--- a/src/libs/better-auth/define-config.test.ts
+++ b/src/libs/better-auth/define-config.test.ts
@@ -1,7 +1,9 @@
-import { describe, expect, it, vi } from 'vitest';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
const mocks = vi.hoisted(() => ({
betterAuth: vi.fn((options) => options),
+ EnvHttpProxyAgent: vi.fn((options) => ({ options })),
+ setGlobalDispatcher: vi.fn(),
}));
vi.mock('@better-auth/expo', () => ({
@@ -46,8 +48,8 @@ vi.mock('better-auth/plugins', () => ({
}));
vi.mock('undici', () => ({
- ProxyAgent: vi.fn(),
- setGlobalDispatcher: vi.fn(),
+ EnvHttpProxyAgent: mocks.EnvHttpProxyAgent,
+ setGlobalDispatcher: mocks.setGlobalDispatcher,
}));
vi.mock('@/envs/app', () => ({
@@ -103,6 +105,24 @@ vi.mock('@/server/services/user', () => ({
}));
describe('defineConfig', () => {
+ const originalEnv = process.env;
+
+ beforeEach(() => {
+ vi.clearAllMocks();
+ vi.resetModules();
+ process.env = { ...originalEnv, NODE_ENV: 'test' };
+ delete process.env.HTTP_PROXY;
+ delete process.env.http_proxy;
+ delete process.env.HTTPS_PROXY;
+ delete process.env.https_proxy;
+ delete process.env.NO_PROXY;
+ delete process.env.no_proxy;
+ });
+
+ afterEach(() => {
+ process.env = originalEnv;
+ });
+
it('should revoke existing sessions after password reset by default', async () => {
const { defineConfig } = await import('./define-config');
@@ -116,4 +136,32 @@ describe('defineConfig', () => {
}),
);
});
+
+ it('should respect NO_PROXY when configuring the development proxy dispatcher', async () => {
+ process.env.NODE_ENV = 'development';
+ process.env.HTTP_PROXY = 'http://127.0.0.1:7890';
+ process.env.HTTPS_PROXY = 'http://127.0.0.1:7890';
+ process.env.NO_PROXY = 'example.com,localhost';
+
+ await import('./define-config');
+
+ expect(mocks.EnvHttpProxyAgent).toHaveBeenCalledWith({
+ httpProxy: 'http://127.0.0.1:7890',
+ httpsProxy: 'http://127.0.0.1:7890',
+ noProxy: 'example.com,localhost,127.0.0.1,[::1]',
+ });
+ expect(mocks.setGlobalDispatcher).toHaveBeenCalledWith(
+ expect.objectContaining({
+ options: expect.objectContaining({
+ noProxy: 'example.com,localhost,127.0.0.1,[::1]',
+ }),
+ }),
+ );
+ });
+
+ it('should preserve NO_PROXY wildcard semantics', async () => {
+ const { mergeLocalNoProxy } = await import('./define-config');
+
+ expect(mergeLocalNoProxy('*')).toBe('*');
+ });
});
diff --git a/src/libs/better-auth/define-config.ts b/src/libs/better-auth/define-config.ts
index 47790e2f3f9..bad967f06e5 100644
--- a/src/libs/better-auth/define-config.ts
+++ b/src/libs/better-auth/define-config.ts
@@ -9,7 +9,7 @@ import { type BetterAuthOptions } from 'better-auth/minimal';
import { betterAuth } from 'better-auth/minimal';
import { admin, emailOTP, genericOAuth, magicLink } from 'better-auth/plugins';
import { type BetterAuthPlugin } from 'better-auth/types';
-import { ProxyAgent, setGlobalDispatcher } from 'undici';
+import { EnvHttpProxyAgent, setGlobalDispatcher } from 'undici';
import { appEnv } from '@/envs/app';
import { authEnv } from '@/envs/auth';
@@ -27,18 +27,39 @@ import { parseSSOProviders } from '@/libs/better-auth/utils/server';
import { EmailService } from '@/server/services/email';
import { UserService } from '@/server/services/user';
-// Configure HTTP proxy for OAuth provider requests in development (e.g., Google token exchange)
-// Node.js native fetch doesn't respect system proxy settings
+const LOCAL_NO_PROXY_HOSTS = ['localhost', '127.0.0.1', '[::1]'];
+
+export const mergeLocalNoProxy = (noProxy?: string): string => {
+ const entries = new Set(
+ (noProxy || '')
+ .split(/[,\s]+/)
+ .map((entry) => entry.trim())
+ .filter(Boolean),
+ );
+
+ if (entries.has('*')) return '*';
+
+ for (const host of LOCAL_NO_PROXY_HOSTS) {
+ entries.add(host);
+ }
+
+ return [...entries].join(',');
+};
+
+// Configure HTTP proxy for OAuth provider requests in development (e.g., Google token exchange).
+// Node.js native fetch doesn't respect system proxy settings. Keep localhost direct so Next can
+// fetch local Vite templates such as /index.auth.html without depending on the system proxy.
// Ref: https://github.com/better-auth/better-auth/issues/7396
if (process.env.NODE_ENV === 'development') {
- const proxyUrl =
- process.env.HTTPS_PROXY ||
- process.env.https_proxy ||
- process.env.HTTP_PROXY ||
- process.env.http_proxy;
+ const httpProxy = process.env.HTTP_PROXY || process.env.http_proxy;
+ const httpsProxy = process.env.HTTPS_PROXY || process.env.https_proxy || httpProxy;
- if (proxyUrl) {
- const proxyAgent = new ProxyAgent(proxyUrl);
+ if (httpProxy || httpsProxy) {
+ const proxyAgent = new EnvHttpProxyAgent({
+ ...(httpProxy && { httpProxy }),
+ ...(httpsProxy && { httpsProxy }),
+ noProxy: mergeLocalNoProxy(process.env.NO_PROXY || process.env.no_proxy),
+ });
setGlobalDispatcher(proxyAgent);
}
}