fix(webui): keep listener settings, restore, and boot on one contract

Invalid bind hosts and TLS without a certificate pair can no longer be
saved. Deleting certificates while TLS is on is rejected. A bad listener
does not start HTTP instead, and shutdown unbinds before tearing down
the hook.
This commit is contained in:
DrSmoothl committed 2026-09-13 18:12:26 +08:00
1 parent 6f278dc1da
commit acb0e04bbf
7 files changed
+201 -29

No files matched your search

@@ -0,0 +1,42 @@
import { describe, it, expect } from 'vitest';
import type { OneBotManager } from '@snowluma/onebot/manager';
import { checkListenerExposure, isValidBindHost } from '../src/webui/listener-exposure';
import { initWebUI } from '../src/webui/server';
describe('isValidBindHost', () => {
it('accepts loopback, all-interfaces, and IPv6 any', () => {
expect(isValidBindHost('127.0.0.1')).toBe(true);
expect(isValidBindHost('0.0.0.0')).toBe(true);
expect(isValidBindHost('::')).toBe(true);
expect(isValidBindHost('localhost')).toBe(true);
});
it('rejects spaces, underscores, and malformed IPv6 zone ids', () => {
expect(isValidBindHost('bad host')).toBe(false);
expect(isValidBindHost('not_a_host')).toBe(false);
expect(isValidBindHost('::1%bad%extra')).toBe(false);
expect(isValidBindHost(' 127.0.0.1')).toBe(false);
});
});
describe('checkListenerExposure', () => {
it('requires a usable pair only when TLS is on', () => {
expect(checkListenerExposure({ webuiHost: '127.0.0.1', tlsEnabled: false, pairOk: false })).toEqual({ ok: true });
expect(checkListenerExposure({ webuiHost: '127.0.0.1', tlsEnabled: true, pairOk: false })).toEqual({
ok: false,
code: 'tls-pair-required',
});
});
});
describe('initWebUI boot', () => {
it('fails before listen when the bind host is invalid', async () => {
await expect(initWebUI(
5099,
{} as OneBotManager,
undefined,
undefined,
{ host: 'not_a_host' },
)).rejects.toThrow(/bind host is not a valid TCP address/);
});
});
@@ -1,5 +1,10 @@
import { describe, it, expect } from 'vitest';
import { coerceSettingsPatch } from '../src/webui/system-settings';
import {
coerceSettingsPatch,
evaluateSettingsSave,
tlsCertDeletionBlocked,
} from '../src/webui/system-settings';
import type { RuntimeConfig } from '@snowluma/common/runtime';
describe('coerceSettingsPatch', () => {
it('accepts a full valid body and maps tlsEnabled → webuiTls.enabled', () => {
@@ -37,3 +42,56 @@ describe('coerceSettingsPatch', () => {
expect(coerceSettingsPatch({ trustProxy: '' })).toEqual({ ok: true, patch: { trustProxy: '' } });
});
});
describe('evaluateSettingsSave', () => {
const current: RuntimeConfig = {
webuiPort: 5099,
webuiHost: '127.0.0.1',
webuiTls: { enabled: false },
};
it('rejects an invalid bind host on the patch', () => {
const r = evaluateSettingsSave(current, { webuiHost: 'not_a_host' }, true);
expect(r.ok).toBe(false);
if (!r.ok) expect(r.error).toMatch(/TCP/);
});
it('rejects saving an unrelated field when the persisted host is invalid', () => {
const r = evaluateSettingsSave(
{ ...current, webuiHost: 'not_a_host' },
{ webuiPort: 8080 },
true,
);
expect(r.ok).toBe(false);
});
it('rejects enabling TLS without a usable certificate pair', () => {
const r = evaluateSettingsSave(current, { webuiTls: { enabled: true } }, false);
expect(r).toEqual({ ok: false, error: '启用 TLS 前请先上传有效的证书与私钥' });
});
it('rejects leaving TLS enabled when the pair is already missing', () => {
const r = evaluateSettingsSave(
{ ...current, webuiTls: { enabled: true } },
{ webuiPort: 8080 },
false,
);
expect(r.ok).toBe(false);
});
it('allows disabling TLS even without a pair', () => {
const r = evaluateSettingsSave(
{ ...current, webuiTls: { enabled: true } },
{ webuiTls: { enabled: false } },
false,
);
expect(r).toEqual({ ok: true, patch: { webuiTls: { enabled: false } } });
});
});
describe('tlsCertDeletionBlocked', () => {
it('blocks deletion while TLS is enabled and allows it when TLS is off', () => {
expect(tlsCertDeletionBlocked(true)).toBe(true);
expect(tlsCertDeletionBlocked(false)).toBe(false);
});
});