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.
35 lines
1.3 KiB
TypeScript
35 lines
1.3 KiB
TypeScript
const LOOPBACK_V4 = /^127\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/u;
|
|
|
|
function isLoopbackIpv4(hostname: string): boolean {
|
|
const match = LOOPBACK_V4.exec(hostname);
|
|
if (!match) return false;
|
|
return match.slice(1).every((part) => Number(part) <= 255);
|
|
}
|
|
|
|
export function isLoopbackHostname(hostname: string): boolean {
|
|
const host = hostname.trim().toLowerCase().replace(/^\[|\]$/gu, '').replace(/\.$/u, '');
|
|
if (host === 'localhost' || host.endsWith('.localhost')) return true;
|
|
if (isLoopbackIpv4(host)) return true;
|
|
if (host === '::1' || host === '0:0:0:0:0:0:0:1') return true;
|
|
if (host.startsWith('::ffff:')) return isLoopbackIpv4(host.slice('::ffff:'.length));
|
|
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: {
|
|
protocol: string;
|
|
hostname: string;
|
|
}): boolean {
|
|
return location.protocol !== 'https:' && !isLoopbackHostname(location.hostname);
|
|
}
|