From ad21e19dc86da339e850f1f2132e57974adcac3f Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Thu, 18 Jun 2026 14:26:32 +0800 Subject: [PATCH] feat(webui): source trustProxy from runtime config (env still overrides) Move the trust-proxy/client-ip resolver from a module-level direct read of SNOWLUMA_WEBUI_TRUST_PROXY into initWebUI, fed by runtime.json's trustProxy (loadRuntimeConfig already merges the env override on top). Default '' parses identically to the old undefined (trust socket peer only), so behavior is unchanged when unset. --- packages/core/src/index.ts | 1 + packages/core/src/webui/server.ts | 17 ++++++++--------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index d7188d3b..4668352e 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -51,6 +51,7 @@ async function main() { await initWebUI(runtimeConfig.webuiPort || 5099, oneBotManager, hookManager, notificationManager, { host: runtimeConfig.webuiHost, tlsEnabled: runtimeConfig.webuiTls?.enabled, + trustProxy: runtimeConfig.trustProxy, }); } catch (err) { log.error('Failed to start WebUI: ', err); diff --git a/packages/core/src/webui/server.ts b/packages/core/src/webui/server.ts index 14b1f764..7b8b29c5 100644 --- a/packages/core/src/webui/server.ts +++ b/packages/core/src/webui/server.ts @@ -80,14 +80,6 @@ function purgeExpiredTokens() { } } -/** - * Resolve the client IP for per-IP rate limiting. Default is the TCP - * socket peer (cannot be spoofed by the client). Operators behind a - * reverse proxy must opt in via the `SNOWLUMA_WEBUI_TRUST_PROXY` env - * var; see `./client-ip.ts` for the accepted values. - */ -const trustProxyMode = parseTrustProxy(process.env.SNOWLUMA_WEBUI_TRUST_PROXY); -const getClientIp = makeClientIpResolver(trustProxyMode); async function fetchQqAvatar(uin: string): Promise<{ body: Uint8Array; contentType: string }> { const response = await fetch(`https://q1.qlogo.cn/g?b=qq&nk=${encodeURIComponent(uin)}&s=100`, { @@ -107,8 +99,15 @@ export async function initWebUI( oneBotManager: OneBotManager, hookManager?: HookManager, notificationManager?: NotificationManager, - listener: { host?: string; tlsEnabled?: boolean } = {}, + listener: { host?: string; tlsEnabled?: boolean; trustProxy?: string } = {}, ): Promise<{ port: number }> { + // Resolve the client IP for per-IP rate limiting from the configured + // trust-proxy directive (runtime.json `trustProxy`, env-overridable via + // SNOWLUMA_WEBUI_TRUST_PROXY which loadRuntimeConfig already merged in). + // Default ('') = trust the TCP socket peer only (cannot be spoofed). + const trustProxyMode = parseTrustProxy(listener.trustProxy); + const getClientIp = makeClientIpResolver(trustProxyMode); + const auth = WebuiAuth.load(); const initialPassword = auth.takeInitialPassword(); if (auth.isDevMode()) {