feat(webui): add SNOWLUMA_WEBUI_TRUST_PROXY for reverse-proxy deploys
The previous commit tightened login rate-limiting to key off the real
TCP socket peer, which fixed the X-Forwarded-For bypass but had the
known caveat of collapsing every client into a single global bucket
when the WebUI runs behind nginx / Caddy / a SaaS CDN.
Give operators an explicit, audited way to opt back into per-client
keying without re-introducing the spoof:
* `SNOWLUMA_WEBUI_TRUST_PROXY` env var. Modes:
- unset / empty → keep socket peer (safe default, unchanged)
- "loopback" → trust X-Real-IP / X-Forwarded-For when the
immediate socket peer is 127.0.0.1 / ::1
(nginx + WebUI on the same host)
- "1" / "true" / → trust headers from any peer (only safe behind
"all" a SaaS proxy that strips client-set headers)
- comma-separated → trust headers only when the socket peer
IP list matches one of these (proxy on another box)
* When trusting headers we prefer X-Real-IP (single value the proxy
overwrites unconditionally) and fall back to the first hop of XFF.
We deliberately do NOT walk the full XFF chain — without a fully
trusted proxy chain the leftmost value is still spoofable; a
single-tier proxy is the only setup this app realistically sees.
* Boot log prints "login rate-limit keyed by: …" so operators can
verify the mode took effect, plus an extra WARN for "all" reminding
them it's only safe with header-stripping in front.
Logic lives in webui/client-ip.ts as a pure pickClientIp() that takes
a socket-IP resolver, so the trust-proxy matrix can be exercised
without standing up a server — see webui-client-ip.test.ts for the
11 new unit tests covering parse + dispatch + loopback / list / all /
none modes.
Smoke-tested with SNOWLUMA_WEBUI_TRUST_PROXY=loopback:
- 5× failed login w/ X-Forwarded-For: 1.1.1.1 → 401
- 6th attempt same XFF → 429 (locked, per-XFF)
- 6th attempt X-Forwarded-For: 2.2.2.2 → 401 (fresh bucket)
- X-Real-IP overrides XFF when both present
This commit is contained in:
1 parent
d66f0b2c6b
commit
ffe0c57abf
3 files changed
+209
-13
No files matched your search
@@ -1,7 +1,7 @@
|
||||
import { serve } from '@hono/node-server';
|
||||
import { getConnInfo } from '@hono/node-server/conninfo';
|
||||
import { serveStatic } from '@hono/node-server/serve-static';
|
||||
import { Hono, type Context } from 'hono';
|
||||
import { describeTrustProxy, makeClientIpResolver, parseTrustProxy } from './client-ip';
|
||||
import { createLogger, getRecentLogs, subscribeLogs } from '../utils/logger';
|
||||
import { randomBytes } from 'crypto';
|
||||
import type { OneBotManager } from '../onebot/manager';
|
||||
@@ -66,19 +66,13 @@ function purgeExpiredTokens() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the client IP from the underlying TCP socket via the official
|
||||
* @hono/node-server conninfo helper. We deliberately do NOT trust
|
||||
* `X-Forwarded-For` — the WebUI listens on a local port with no required
|
||||
* reverse proxy, so accepting that header would let any client rotate it
|
||||
* to bypass the per-IP login lockout.
|
||||
* 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.
|
||||
*/
|
||||
function getClientIp(c: Context): string {
|
||||
try {
|
||||
return getConnInfo(c).remote.address ?? '127.0.0.1';
|
||||
} catch {
|
||||
return '127.0.0.1';
|
||||
}
|
||||
}
|
||||
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`, {
|
||||
@@ -109,6 +103,10 @@ export async function initWebUI(
|
||||
} else if (auth.mustChangePassword()) {
|
||||
log.warn('password change is still required');
|
||||
}
|
||||
log.info('login rate-limit keyed by: %s', describeTrustProxy(trustProxyMode));
|
||||
if (trustProxyMode.kind === 'all') {
|
||||
log.warn('SNOWLUMA_WEBUI_TRUST_PROXY=1 — only safe behind a reverse proxy that strips client-set X-Real-IP / X-Forwarded-For');
|
||||
}
|
||||
|
||||
const app = new Hono();
|
||||
|
||||
|
||||
Reference in new issue
Block a user