From 8996b35dd11d58e0e100a8eea9dd4fa4605c6898 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Venti=E3=81=AE=E9=85=92?= <3788365896@qq.com> Date: Wed, 13 May 2026 10:33:47 +0800 Subject: [PATCH] fix: enable wss connections and add certificate verification toggle in WebSocket Client (#1823) * fix: enable wss connections and add certificate verification toggle in WebSocket Client Previous behavior: the WebSocket Client only supported unencrypted ws:// connections; wss:// URLs were not handled and would fail to connect, providing no option to disable TLS certificate validation for development or self-signed certificates. Fixed behavior: secure WebSocket connections (wss://) are now fully supported. A verifyCertificate configuration option has been introduced, defaulting to true. When set to false, certificate errors (e.g., self-signed certificates) are ignored, enabling safe local testing while maintaining strict validation by default for production. * fix: improve wss certificate verification and add tests Previous behavior: wsOptions used 'any' type without type safety, verifyCertificate was added to napcat-protocol without implementation, and no tests existed for the new config option. Fixed behavior: wsOptions now uses proper WebSocket.ClientOptions type for compile-time safety, verifyCertificate removed from napcat-protocol (no WebSocket client implementation exists there), and comprehensive tests added to validate config loading and default values. --- packages/napcat-onebot/config/config.ts | 1 + .../napcat-onebot/network/websocket-client.ts | 10 +++- packages/napcat-test/schema.test.ts | 51 +++++++++++++++++++ .../src/components/network_edit/ws_client.tsx | 14 +++-- .../src/types/onebot_conf.d.ts | 1 + 5 files changed, 72 insertions(+), 5 deletions(-) diff --git a/packages/napcat-onebot/config/config.ts b/packages/napcat-onebot/config/config.ts index 114654c55..71e1edcf5 100644 --- a/packages/napcat-onebot/config/config.ts +++ b/packages/napcat-onebot/config/config.ts @@ -59,6 +59,7 @@ const WebsocketClientConfigSchema = Type.Object({ token: Type.String({ default: '' }), debug: Type.Boolean({ default: false }), heartInterval: Type.Number({ default: 30000 }), + verifyCertificate: Type.Optional(Type.Boolean({ default: true })), }); const PluginConfigSchema = Type.Object({ diff --git a/packages/napcat-onebot/network/websocket-client.ts b/packages/napcat-onebot/network/websocket-client.ts index b19c477cf..fddb82aa8 100644 --- a/packages/napcat-onebot/network/websocket-client.ts +++ b/packages/napcat-onebot/network/websocket-client.ts @@ -73,7 +73,7 @@ export class OB11WebSocketClientAdapter extends IOB11NetworkAdapter { // this.logger.logDebug('[OneBot] [WebSocket Client] 收到pong'); }); diff --git a/packages/napcat-test/schema.test.ts b/packages/napcat-test/schema.test.ts index 9cd9443b9..99f8c06df 100644 --- a/packages/napcat-test/schema.test.ts +++ b/packages/napcat-test/schema.test.ts @@ -97,6 +97,57 @@ describe('NapCat Configuration Loaders', () => { expect(loaded.musicSignUrl).toBe(''); }); + test('OneBotConfig websocketClients should apply verifyCertificate default', () => { + const partialConfig: Partial = { + network: { + httpServers: [], + httpSseServers: [], + httpClients: [], + websocketServers: [], + websocketClients: [{ + name: 'test-ws-client', + enable: true, + url: 'wss://example.com', + messagePostFormat: 'array', + reportSelfMessage: false, + reconnectInterval: 5000, + token: '', + debug: false, + heartInterval: 30000, + }], + plugins: [], + }, + }; + const loaded = loadOneBotConfig(partialConfig); + expect(loaded.network.websocketClients[0]?.verifyCertificate).toBe(true); + }); + + test('OneBotConfig websocketClients should preserve explicit verifyCertificate false', () => { + const partialConfig: Partial = { + network: { + httpServers: [], + httpSseServers: [], + httpClients: [], + websocketServers: [], + websocketClients: [{ + name: 'test-ws-client', + enable: true, + url: 'wss://example.com', + messagePostFormat: 'array', + reportSelfMessage: false, + reconnectInterval: 5000, + token: '', + debug: false, + heartInterval: 30000, + verifyCertificate: false, + }], + plugins: [], + }, + }; + const loaded = loadOneBotConfig(partialConfig); + expect(loaded.network.websocketClients[0]?.verifyCertificate).toBe(false); + }); + test('NapcatConfig should compile and apply defaults', () => { let compiled: ReturnType | undefined; expect(() => { diff --git a/packages/napcat-webui-frontend/src/components/network_edit/ws_client.tsx b/packages/napcat-webui-frontend/src/components/network_edit/ws_client.tsx index 03a08be3b..4cbece6e8 100644 --- a/packages/napcat-webui-frontend/src/components/network_edit/ws_client.tsx +++ b/packages/napcat-webui-frontend/src/components/network_edit/ws_client.tsx @@ -2,11 +2,11 @@ import GenericForm, { random_token } from './generic_form'; import type { Field } from './generic_form'; export interface WebsocketClientFormProps { - data?: OneBotConfig['network']['websocketClients'][0] - onClose: () => void + data?: OneBotConfig['network']['websocketClients'][0]; + onClose: () => void; onSubmit: ( data: OneBotConfig['network']['websocketClients'][0] - ) => Promise + ) => Promise; } type WebsocketClientFormType = OneBotConfig['network']['websocketClients']; @@ -26,6 +26,7 @@ const WebsocketClientForm: React.FC = ({ debug: false, heartInterval: 30000, reconnectInterval: 30000, + verifyCertificate: true, }; const fields: Field<'websocketClients'>[] = [ @@ -99,6 +100,13 @@ const WebsocketClientForm: React.FC = ({ isRequired: true, colSpan: 1, }, + { + name: 'verifyCertificate', + label: 'SSL证书验证', + type: 'switch', + description: '是否验证SSL证书(wss连接)。关闭后可连接自签名证书的服务器,但会降低安全性', + colSpan: 2, + }, ]; return ( diff --git a/packages/napcat-webui-frontend/src/types/onebot_conf.d.ts b/packages/napcat-webui-frontend/src/types/onebot_conf.d.ts index 7b168e715..52a26a0dc 100644 --- a/packages/napcat-webui-frontend/src/types/onebot_conf.d.ts +++ b/packages/napcat-webui-frontend/src/types/onebot_conf.d.ts @@ -42,6 +42,7 @@ interface WebsocketClientConfig extends AdapterConfig { token: string; debug: boolean; heartInterval: number; + verifyCertificate?: boolean; } interface HttpSseServerConfig extends HttpServerConfig {