mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-26 04:18:37 +00:00
fix(security): block plaintext WebSocket connections to non-loopback addresses (#20803)
* fix(security): block plaintext WebSocket connections to non-loopback addresses Addresses CWE-319 (Cleartext Transmission of Sensitive Information). Previously, ws:// connections to remote hosts were allowed, exposing both credentials and chat data to network interception. This change blocks ALL plaintext ws:// connections to non-loopback addresses, regardless of whether explicit credentials are configured (device tokens may be loaded dynamically). Security policy: - wss:// allowed to any host - ws:// allowed only to loopback (127.x.x.x, localhost, ::1) - ws:// to LAN/tailnet/remote hosts now requires TLS Changes: - Add isSecureWebSocketUrl() validation in net.ts - Block insecure connections in GatewayClient.start() - Block insecure URLs in buildGatewayConnectionDetails() - Handle malformed URLs gracefully without crashing - Update tests to use wss:// for non-loopback URLs Fixes #12519 * fix(test): update gateway-chat mock to preserve net.js exports Use importOriginal to spread actual module exports and mock only the functions needed for testing. This ensures isSecureWebSocketUrl and other exports remain available to the code under test.
This commit is contained in:
@@ -21,6 +21,7 @@ import {
|
||||
type GatewayClientName,
|
||||
} from "../utils/message-channel.js";
|
||||
import { buildDeviceAuthPayload } from "./device-auth.js";
|
||||
import { isSecureWebSocketUrl } from "./net.js";
|
||||
import {
|
||||
type ConnectParams,
|
||||
type EventFrame,
|
||||
@@ -109,6 +110,26 @@ export class GatewayClient {
|
||||
this.opts.onConnectError?.(new Error("gateway tls fingerprint requires wss:// gateway url"));
|
||||
return;
|
||||
}
|
||||
|
||||
// Security check: block ALL plaintext ws:// to non-loopback addresses (CWE-319, CVSS 9.8)
|
||||
// This protects both credentials AND chat/conversation data from MITM attacks.
|
||||
// Device tokens may be loaded later in sendConnect(), so we block regardless of hasCredentials.
|
||||
if (!isSecureWebSocketUrl(url)) {
|
||||
// Safe hostname extraction - avoid throwing on malformed URLs in error path
|
||||
let displayHost = url;
|
||||
try {
|
||||
displayHost = new URL(url).hostname || url;
|
||||
} catch {
|
||||
// Use raw URL if parsing fails
|
||||
}
|
||||
const error = new Error(
|
||||
`SECURITY ERROR: Cannot connect to "${displayHost}" over plaintext ws://. ` +
|
||||
"Both credentials and chat data would be exposed to network interception. " +
|
||||
"Use wss:// for the gateway URL, or connect via SSH tunnel to localhost.",
|
||||
);
|
||||
this.opts.onConnectError?.(error);
|
||||
return;
|
||||
}
|
||||
// Allow node screen snapshots and other large responses.
|
||||
const wsOptions: ClientOptions = {
|
||||
maxPayload: 25 * 1024 * 1024,
|
||||
|
||||
Reference in New Issue
Block a user