fix(webui): allow empty OneBot tokens on loopback bind hosts
Empty inbound tokens were gated on the WebUI page hostname / client IP, so a remote panel visit blocked clearing the token even when the listener was 127.0.0.1. Trust the bind host as well: loopback listeners are not reachable from outside, while 0.0.0.0 still requires a local WebUI session.
This commit is contained in:
7 files changed
+76
-12
No files matched your search
@@ -2,6 +2,13 @@ import { assessAccessToken } from '@snowluma/common/access-token';
|
|||||||
import type { OneBotConfig } from '@snowluma/onebot/types';
|
import type { OneBotConfig } from '@snowluma/onebot/types';
|
||||||
import { isLoopbackClientIp } from './client-ip';
|
import { isLoopbackClientIp } from './client-ip';
|
||||||
|
|
||||||
|
function isLoopbackBindHost(host: string | undefined): boolean {
|
||||||
|
const value = host?.trim().toLowerCase().replace(/^\[|\]$/gu, '').replace(/\.$/u, '') ?? '';
|
||||||
|
if (!value) return false;
|
||||||
|
if (value === 'localhost' || value.endsWith('.localhost')) return true;
|
||||||
|
return isLoopbackClientIp(value);
|
||||||
|
}
|
||||||
|
|
||||||
export class OneBotAccessTokenPolicyError extends Error {
|
export class OneBotAccessTokenPolicyError extends Error {
|
||||||
constructor(message: string) {
|
constructor(message: string) {
|
||||||
super(message);
|
super(message);
|
||||||
@@ -40,7 +47,7 @@ export function validateOneBotAccessTokenChanges(
|
|||||||
next: OneBotConfig,
|
next: OneBotConfig,
|
||||||
options: OneBotAccessTokenPolicyOptions,
|
options: OneBotAccessTokenPolicyOptions,
|
||||||
): void {
|
): void {
|
||||||
const allowEmpty = isLoopbackClientIp(options.clientIp);
|
const allowEmptyFromClient = isLoopbackClientIp(options.clientIp);
|
||||||
for (const kind of INBOUND_KINDS) {
|
for (const kind of INBOUND_KINDS) {
|
||||||
const previousList = previous?.networks[kind] ?? [];
|
const previousList = previous?.networks[kind] ?? [];
|
||||||
const nextList = next.networks[kind];
|
const nextList = next.networks[kind];
|
||||||
@@ -50,9 +57,9 @@ export function validateOneBotAccessTokenChanges(
|
|||||||
if (sameTokenAtPreviousIdentity(previousList, nextList, adapter.name, token, index)) continue;
|
if (sameTokenAtPreviousIdentity(previousList, nextList, adapter.name, token, index)) continue;
|
||||||
|
|
||||||
if (!token) {
|
if (!token) {
|
||||||
if (!allowEmpty) {
|
if (!allowEmptyFromClient && !isLoopbackBindHost(adapter.host)) {
|
||||||
throw new OneBotAccessTokenPolicyError(
|
throw new OneBotAccessTokenPolicyError(
|
||||||
`节点“${adapter.name}”仅能从本机页面保存无令牌配置;请生成令牌后重试。`,
|
`节点“${adapter.name}”未绑定本机地址,远程保存必须填写令牌;请生成令牌或将主机改为 127.0.0.1。`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
@@ -14,17 +14,46 @@ function validate(
|
|||||||
}
|
}
|
||||||
|
|
||||||
describe('OneBot access token save policy', () => {
|
describe('OneBot access token save policy', () => {
|
||||||
it('rejects clearing an inbound token from a non-loopback client', () => {
|
it('rejects clearing an inbound token from a non-loopback client when the bind host is public', () => {
|
||||||
const previous = makeDefaultOneBotConfig();
|
const previous = makeDefaultOneBotConfig();
|
||||||
const next = structuredClone(previous);
|
const next = structuredClone(previous);
|
||||||
|
next.networks.httpServers[0].host = '0.0.0.0';
|
||||||
|
next.networks.httpServers[0].accessToken = undefined;
|
||||||
|
|
||||||
|
expect(() => validate(previous, next, '192.0.2.10')).toThrow(OneBotAccessTokenPolicyError);
|
||||||
|
expect(() => validate(previous, next, '192.0.2.10')).toThrow(/令牌/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('allows a remote client to clear a token when the inbound host is loopback', () => {
|
||||||
|
const previous = makeDefaultOneBotConfig();
|
||||||
|
const next = structuredClone(previous);
|
||||||
|
next.networks.httpServers[0].host = '127.0.0.1';
|
||||||
|
next.networks.httpServers[0].accessToken = undefined;
|
||||||
|
|
||||||
|
expect(() => validate(previous, next, '192.0.2.10')).not.toThrow();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('treats localhost as a loopback bind host for empty inbound tokens', () => {
|
||||||
|
const previous = makeDefaultOneBotConfig();
|
||||||
|
const next = structuredClone(previous);
|
||||||
|
next.networks.wsServers[0].host = 'localhost';
|
||||||
|
next.networks.wsServers[0].accessToken = undefined;
|
||||||
|
|
||||||
|
expect(() => validate(previous, next, '192.0.2.10')).not.toThrow();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not treat a missing bind host as loopback', () => {
|
||||||
|
const previous = makeDefaultOneBotConfig();
|
||||||
|
const next = structuredClone(previous);
|
||||||
|
next.networks.httpServers[0].host = undefined;
|
||||||
next.networks.httpServers[0].accessToken = undefined;
|
next.networks.httpServers[0].accessToken = undefined;
|
||||||
|
|
||||||
expect(() => validate(previous, next, '192.0.2.10')).toThrow(OneBotAccessTokenPolicyError);
|
expect(() => validate(previous, next, '192.0.2.10')).toThrow(OneBotAccessTokenPolicyError);
|
||||||
expect(() => validate(previous, next, '192.0.2.10')).toThrow(/本机/);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('treats every inbound token as new when no readable prior config exists', () => {
|
it('treats every inbound token as new when no readable prior config exists', () => {
|
||||||
const next = makeDefaultOneBotConfig();
|
const next = makeDefaultOneBotConfig();
|
||||||
|
next.networks.httpServers[0].host = '0.0.0.0';
|
||||||
next.networks.httpServers[0].accessToken = undefined;
|
next.networks.httpServers[0].accessToken = undefined;
|
||||||
|
|
||||||
expect(() => validate(null, next, '192.0.2.10')).toThrow(OneBotAccessTokenPolicyError);
|
expect(() => validate(null, next, '192.0.2.10')).toThrow(OneBotAccessTokenPolicyError);
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ import {
|
|||||||
accessTokenFeedback,
|
accessTokenFeedback,
|
||||||
type AccessTokenFeedback,
|
type AccessTokenFeedback,
|
||||||
} from '@/lib/access-token-feedback';
|
} from '@/lib/access-token-feedback';
|
||||||
import { isLoopbackHostname } from '@/lib/transport-security';
|
import { allowEmptyInboundAccessToken } from '@/lib/transport-security';
|
||||||
import type {
|
import type {
|
||||||
HttpClientNetwork,
|
HttpClientNetwork,
|
||||||
HttpServerNetwork,
|
HttpServerNetwork,
|
||||||
@@ -87,9 +87,9 @@ export function NodeEditDialog<K extends NetworkKind>(props: NodeEditDialogProps
|
|||||||
const isInboundServer = kind === 'httpServers' || kind === 'wsServers';
|
const isInboundServer = kind === 'httpServers' || kind === 'wsServers';
|
||||||
const tokenChanged = (draft.accessToken ?? '') !== (initial.accessToken ?? '');
|
const tokenChanged = (draft.accessToken ?? '') !== (initial.accessToken ?? '');
|
||||||
const enforceTokenPolicy = isInboundServer && (!isEdit || tokenChanged);
|
const enforceTokenPolicy = isInboundServer && (!isEdit || tokenChanged);
|
||||||
const allowEmptyToken = typeof window !== 'undefined'
|
|
||||||
&& isLoopbackHostname(window.location.hostname);
|
|
||||||
const inbound = isInboundServer ? draft as HttpServerNetwork | WsServerNetwork : null;
|
const inbound = isInboundServer ? draft as HttpServerNetwork | WsServerNetwork : null;
|
||||||
|
const allowEmptyToken = typeof window !== 'undefined'
|
||||||
|
&& allowEmptyInboundAccessToken(window.location.hostname, inbound?.host);
|
||||||
const tokenFeedback = useMemo(
|
const tokenFeedback = useMemo(
|
||||||
() => enforceTokenPolicy
|
() => enforceTokenPolicy
|
||||||
? accessTokenFeedback(
|
? accessTokenFeedback(
|
||||||
@@ -180,7 +180,7 @@ export function NodeEditDialog<K extends NetworkKind>(props: NodeEditDialogProps
|
|||||||
<div className="p-4">
|
<div className="p-4">
|
||||||
<TokenField
|
<TokenField
|
||||||
label="授权 Token"
|
label="授权 Token"
|
||||||
placeholder={isInboundServer ? '留空则关闭鉴权(仅允许本机保存)' : '按对端要求填写'}
|
placeholder={isInboundServer ? '留空则关闭鉴权(监听本机或从本机打开页面时可保存)' : '按对端要求填写'}
|
||||||
value={draft.accessToken}
|
value={draft.accessToken}
|
||||||
onChange={(v) => patch({ accessToken: v || undefined } as Partial<AnyAdapter<K>>)}
|
onChange={(v) => patch({ accessToken: v || undefined } as Partial<AnyAdapter<K>>)}
|
||||||
onGenerate={() => patch({ accessToken: generateAccessToken() } as Partial<AnyAdapter<K>>)}
|
onGenerate={() => patch({ accessToken: generateAccessToken() } as Partial<AnyAdapter<K>>)}
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ export function accessTokenFeedback(
|
|||||||
: {
|
: {
|
||||||
valid: false,
|
valid: false,
|
||||||
tone: 'error',
|
tone: 'error',
|
||||||
message: '当前为远程访问,请使用右侧按钮生成令牌后保存。',
|
message: '未绑定本机地址时,远程访问必须填写令牌;请生成令牌或将主机改为 127.0.0.1。',
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,17 @@ export function isLoopbackHostname(hostname: string): boolean {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Empty inbound OneBot tokens are safe when the listener is loopback, or
|
||||||
|
* when the operator opened WebUI on loopback (they can bind 0.0.0.0
|
||||||
|
* without a token on purpose). A remote WebUI plus a public bind host
|
||||||
|
* must keep a token. */
|
||||||
|
export function allowEmptyInboundAccessToken(
|
||||||
|
webuiHostname: string,
|
||||||
|
bindHost: string | undefined,
|
||||||
|
): boolean {
|
||||||
|
return isLoopbackHostname(webuiHostname) || isLoopbackHostname(bindHost ?? '');
|
||||||
|
}
|
||||||
|
|
||||||
export function shouldWarnAboutInsecureRemoteAccess(location: {
|
export function shouldWarnAboutInsecureRemoteAccess(location: {
|
||||||
protocol: string;
|
protocol: string;
|
||||||
hostname: string;
|
hostname: string;
|
||||||
|
|||||||
@@ -35,8 +35,12 @@ describe('OneBot access token editor policy', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('only permits an empty inbound token on loopback', () => {
|
it('only permits an empty inbound token in a trusted bind or local-page context', () => {
|
||||||
expect(accessTokenFeedback('', [], false)).toMatchObject({ valid: false, tone: 'error' });
|
expect(accessTokenFeedback('', [], false)).toEqual({
|
||||||
|
valid: false,
|
||||||
|
tone: 'error',
|
||||||
|
message: '未绑定本机地址时,远程访问必须填写令牌;请生成令牌或将主机改为 127.0.0.1。',
|
||||||
|
});
|
||||||
expect(accessTokenFeedback('', [], true)).toMatchObject({ valid: true, tone: 'warning' });
|
expect(accessTokenFeedback('', [], true)).toMatchObject({ valid: true, tone: 'warning' });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
import { describe, expect, it } from 'vitest';
|
import { describe, expect, it } from 'vitest';
|
||||||
import {
|
import {
|
||||||
|
allowEmptyInboundAccessToken,
|
||||||
isLoopbackHostname,
|
isLoopbackHostname,
|
||||||
shouldWarnAboutInsecureRemoteAccess,
|
shouldWarnAboutInsecureRemoteAccess,
|
||||||
} from '../src/lib/transport-security';
|
} from '../src/lib/transport-security';
|
||||||
@@ -27,6 +28,18 @@ describe('WebUI transport security notice', () => {
|
|||||||
expect(isLoopbackHostname(hostname)).toBe(false);
|
expect(isLoopbackHostname(hostname)).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('allows an empty inbound token when the bind host is loopback, even from a remote WebUI', () => {
|
||||||
|
expect(allowEmptyInboundAccessToken('192.168.1.20', '127.0.0.1')).toBe(true);
|
||||||
|
expect(allowEmptyInboundAccessToken('panel.example.com', 'localhost')).toBe(true);
|
||||||
|
expect(allowEmptyInboundAccessToken('192.168.1.20', '0.0.0.0')).toBe(false);
|
||||||
|
expect(allowEmptyInboundAccessToken('192.168.1.20', '')).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('still allows an empty inbound token when the WebUI itself is opened on loopback', () => {
|
||||||
|
expect(allowEmptyInboundAccessToken('127.0.0.1', '0.0.0.0')).toBe(true);
|
||||||
|
expect(allowEmptyInboundAccessToken('localhost', undefined)).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
it('warns only when a non-loopback page is accessed without HTTPS', () => {
|
it('warns only when a non-loopback page is accessed without HTTPS', () => {
|
||||||
expect(shouldWarnAboutInsecureRemoteAccess({ protocol: 'http:', hostname: '192.168.1.20' }))
|
expect(shouldWarnAboutInsecureRemoteAccess({ protocol: 'http:', hostname: '192.168.1.20' }))
|
||||||
.toBe(true);
|
.toBe(true);
|
||||||
|
|||||||
Reference in new issue
Block a user