fix(onebot): enforce WebSocket access policy

Apply the configured access policy before server-side WebSocket sessions are established across both listener modes.

Preserve established client compatibility and runtime configuration changes, with regression coverage for accepted and rejected connections.
This commit is contained in:
DrSmoothl committed 2026-08-01 17:09:04 +08:00
1 parent 8940f8864b
commit 3c772df818
5 files changed
+64 -3

No files matched your search

@@ -7,8 +7,12 @@ const { FakeWebSocketServer, servers } = vi.hoisted(() => {
class FakeWebSocketServer extends EventEmitter {
closeCallback: ((error?: Error) => void) | null = null;
constructor(_options: unknown) {
readonly options: {
verifyClient?: (info: { req: { headers: Record<string, string>; url?: string } }) => boolean;
};
constructor(options: unknown) {
super();
this.options = options as typeof this.options;
servers.push(this);
}
close(callback?: (error?: Error) => void): void {
@@ -73,6 +77,19 @@ describe('WsServerAdapter bind/release lifecycle promises', () => {
expect(adapter.describeStatus().status).toBe('ok');
});
it('provides standalone upgrade authentication before accepting a connection', () => {
const adapter = new WsServerAdapter('ws', {
...config(),
accessToken: 'expected-token',
}, CTX);
void adapter.open();
const verify = servers[0].options.verifyClient;
expect(verify).toBeTypeOf('function');
expect(verify?.({ req: { headers: {}, url: '/?access_token=wrong-token' } })).toBe(false);
expect(verify?.({ req: { headers: {}, url: '/?access_token=expected-token' } })).toBe(true);
});
it('rejects bind errors and exposes them as degraded', async () => {
const adapter = new WsServerAdapter('ws', config(), CTX);
const opening = adapter.open();