feat(onebot): make the websocket frame cap configurable and diagnosable (#631)

- expose DEFAULT_MAX_PAYLOAD_BYTES; an inbound frame over the cap now reports its
  declared byte size, message type and the effective limit, recovered by walking
  the buffered frame stream so the number is right when a chunk carries several
  frames, the oversized header spans chunks, or control frames are interleaved;
  when the walk cannot reach a verdict the error names the cap without a size
- truncate close reasons to the RFC 6455 control-frame room on a UTF-8 boundary
  (an over-long reason previously produced an invalid control frame)
- add optional maxPayloadBytes (default 100 MiB unchanged) to the three adapters
  that read WebSocket frames: wsServers, wsClients and httpServers with
  enableWebSocket. httpClients rejects the field because it has no WebSocket
  transport, and the value is part of the binding signature so a change rebinds
  the transport instead of being saved without taking effect
- keep the existing 1009 log lines and append the actionable hint instead of
  replacing the template
- WebUI field (including httpServers once the embedded listener is on) and README
  docs for the 100 MiB => ~445 s (7:26) inline PCM consequence
This commit is contained in:
KazusaUwU authored and GitHub committed 2026-11-01 20:29:16 +08:00
1 parent bd40ac3522
commit 8d9f44b6b0
16 files changed
+1035 -77

No files matched your search

+134
View File
@@ -148,6 +148,44 @@ describe('assertValidOneBotConfig', () => {
expect(() => assertValidOneBotConfig(ws)).toThrow(/reconnectIntervalMs/);
});
it('bounds maxPayloadBytes when one is configured', () => {
for (const invalid of [0, 1023, 2 * 1024 * 1024 * 1024 + 1, 1024.5, '104857600']) {
const config = makeDefaultOneBotConfig();
config.networks.wsServers[0].maxPayloadBytes = invalid as unknown as number;
expect(() => assertValidOneBotConfig(config), String(invalid)).toThrow(
/maxPayloadBytes/,
);
}
const accepted = makeDefaultOneBotConfig();
accepted.networks.wsServers[0].maxPayloadBytes = 1024;
expect(() => assertValidOneBotConfig(accepted)).not.toThrow();
// The third WebSocket-capable adapter: httpServers with the embedded WS.
const httpWs = makeDefaultOneBotConfig();
httpWs.networks.httpServers[0].enableWebSocket = true;
httpWs.networks.httpServers[0].maxPayloadBytes = 2048;
expect(() => assertValidOneBotConfig(httpWs)).not.toThrow();
});
it('rejects maxPayloadBytes on the pure HTTP client adapter', () => {
const config = makeDefaultOneBotConfig();
config.networks.httpClients.push({
name: 'mirror',
url: 'http://127.0.0.1:9000',
messageFormat: 'array',
reportSelfMessage: false,
});
// HttpPostAdapter has no WebSocket transport, so the value could only be
// saved and silently ignored.
(config.networks.httpClients[0] as unknown as Record<string, unknown>).maxPayloadBytes =
1024;
expect(() => assertValidOneBotConfig(config)).toThrow(
/maxPayloadBytes is only supported by WebSocket adapters/,
);
});
it('rejects server paths that cannot match a URL pathname', () => {
for (const invalidPath of ['api', '/api?token=x', '/api#fragment', ' /api']) {
const config = makeDefaultOneBotConfig();
@@ -365,6 +403,102 @@ describe('loadOneBotConfig', () => {
expect(reloaded.networks.httpClients[0].reportSelfMessage).toBe(true);
});
it('round-trips maxPayloadBytes (both directions) and omits it when unset', () => {
const uin = '10020';
const config = makeDefaultOneBotConfig();
config.networks.wsServers[0].maxPayloadBytes = 512 * 1024 * 1024;
config.networks.wsClients.push({
name: 'astrbot',
url: 'ws://127.0.0.1:6199/ws',
messageFormat: 'array',
reportSelfMessage: false,
maxPayloadBytes: 256 * 1024 * 1024,
});
saveOneBotConfig(uin, config);
const onDisk = JSON.parse(
fs.readFileSync(path.join(tempDir, 'config', `onebot_${uin}.json`), 'utf8'),
);
expect(onDisk.networks.wsServers[0].maxPayloadBytes).toBe(512 * 1024 * 1024);
expect(onDisk.networks.wsClients[0].maxPayloadBytes).toBe(256 * 1024 * 1024);
// Unset adapters must not gain the key, so existing configs stay byte-stable.
expect(onDisk.networks.httpServers[0].maxPayloadBytes).toBeUndefined();
const reloaded = loadOneBotConfig(uin);
expect(reloaded.networks.wsServers[0].maxPayloadBytes).toBe(512 * 1024 * 1024);
expect(reloaded.networks.wsClients[0]!.maxPayloadBytes).toBe(256 * 1024 * 1024);
const prepared = prepareOneBotConfigForRestore(onDisk, 'per-uin');
const preparedNetworks = prepared.value.networks as {
wsClients: Array<Record<string, unknown>>;
};
expect(preparedNetworks.wsClients[0]!.maxPayloadBytes).toBe(256 * 1024 * 1024);
const junk = structuredClone(onDisk) as {
networks: { wsClients: Array<Record<string, unknown>> };
};
junk.networks.wsClients[0]!.maxPayloadBytes = 0;
expect(() => prepareOneBotConfigForRestore(junk, 'per-uin')).toThrow(/maxPayloadBytes/);
});
it('persists maxPayloadBytes for httpServers and drops it for httpClients', () => {
const uin = '10021';
const config = makeDefaultOneBotConfig();
config.networks.httpServers[0].enableWebSocket = true;
config.networks.httpServers[0].maxPayloadBytes = 4096;
saveOneBotConfig(uin, config);
const onDisk = JSON.parse(
fs.readFileSync(path.join(tempDir, 'config', `onebot_${uin}.json`), 'utf8'),
);
expect(onDisk.networks.httpServers[0].maxPayloadBytes).toBe(4096);
expect(loadOneBotConfig(uin).networks.httpServers[0].maxPayloadBytes).toBe(4096);
expect(() => prepareOneBotConfigForRestore(onDisk, 'per-uin')).not.toThrow();
// A restore source that carries the key on a non-WS adapter is rejected
// rather than accepted into a config where it can never apply.
const withHttpClient = structuredClone(onDisk) as {
networks: { httpClients: Array<Record<string, unknown>> };
};
withHttpClient.networks.httpClients.push({
name: 'mirror',
url: 'http://127.0.0.1:9000',
maxPayloadBytes: 4096,
});
expect(() => prepareOneBotConfigForRestore(withHttpClient, 'per-uin')).toThrow(
/maxPayloadBytes is not supported/,
);
// A hand-written file is parsed permissively, but the key is not carried
// into the canonical config (nor written back).
const dir = path.join(tempDir, 'config');
fs.writeFileSync(
path.join(dir, 'onebot_10022.json'),
JSON.stringify({
mode: 'snapshot',
networks: {
httpServers: [],
httpClients: [
{
name: 'mirror',
url: 'http://127.0.0.1:9000',
messageFormat: 'array',
reportSelfMessage: false,
maxPayloadBytes: 4096,
},
],
wsServers: [],
wsClients: [],
},
}),
'utf8',
);
const loaded = loadOneBotConfig('10022');
expect(
(loaded.networks.httpClients[0] as unknown as Record<string, unknown>).maxPayloadBytes,
).toBeUndefined();
});
it('replays a saved per-UIN snapshot without reviving deleted global adapters', () => {
const dir = path.join(tempDir, 'config');
fs.mkdirSync(dir, { recursive: true });