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 { 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 {
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
@@ -40,7 +47,7 @@ export function validateOneBotAccessTokenChanges(
|
||||
next: OneBotConfig,
|
||||
options: OneBotAccessTokenPolicyOptions,
|
||||
): void {
|
||||
const allowEmpty = isLoopbackClientIp(options.clientIp);
|
||||
const allowEmptyFromClient = isLoopbackClientIp(options.clientIp);
|
||||
for (const kind of INBOUND_KINDS) {
|
||||
const previousList = previous?.networks[kind] ?? [];
|
||||
const nextList = next.networks[kind];
|
||||
@@ -50,9 +57,9 @@ export function validateOneBotAccessTokenChanges(
|
||||
if (sameTokenAtPreviousIdentity(previousList, nextList, adapter.name, token, index)) continue;
|
||||
|
||||
if (!token) {
|
||||
if (!allowEmpty) {
|
||||
if (!allowEmptyFromClient && !isLoopbackBindHost(adapter.host)) {
|
||||
throw new OneBotAccessTokenPolicyError(
|
||||
`节点“${adapter.name}”仅能从本机页面保存无令牌配置;请生成令牌后重试。`,
|
||||
`节点“${adapter.name}”未绑定本机地址,远程保存必须填写令牌;请生成令牌或将主机改为 127.0.0.1。`,
|
||||
);
|
||||
}
|
||||
continue;
|
||||
|
||||
Reference in new issue
Block a user