Skip to content

Commit 77e5621

Browse files
committed
fix(gateway): preserve TLS pin transport policy
Punchcard-Session: cobalt-brook-meadow-m9
1 parent 652d16a commit 77e5621

6 files changed

Lines changed: 74 additions & 20 deletions

File tree

‎packages/gateway-client/src/client.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import type {
1919
import { resolveGatewayStartupRetryAfterMs } from "@openclaw/gateway-protocol/startup-unavailable";
2020
import { MIN_CLIENT_PROTOCOL_VERSION, PROTOCOL_VERSION } from "@openclaw/gateway-protocol/version";
2121
import { isLoopbackIpAddress, type ParsedIpAddress } from "@openclaw/net-policy/ip";
22+
import { isWssUrl } from "@openclaw/net-policy/url-protocol";
2223
import { WebSocket, type ClientOptions, type CertMeta } from "ws";
2324
import {
2425
isSensitiveUrlQueryParamName,
@@ -518,7 +519,8 @@ export class GatewayClient {
518519

519520
private createSocket(handlers: GatewayProtocolSocketHandlers): GatewayProtocolSocket {
520521
const url = this.opts.url ?? DEFAULT_GATEWAY_CLIENT_URL;
521-
if (this.opts.tlsFingerprint && !url.startsWith("wss://")) {
522+
const usesTls = isWssUrl(url);
523+
if (this.opts.tlsFingerprint && !usesTls) {
522524
throw new GatewayClientSocketFactoryConfigurationError(
523525
"gateway tls fingerprint requires wss:// gateway url",
524526
);
@@ -560,7 +562,7 @@ export class GatewayClient {
560562
handshakeTimeout: handshakeTimeoutMs,
561563
...(this.opts.origin ? { origin: this.opts.origin } : {}),
562564
};
563-
if (url.startsWith("wss://") && this.opts.tlsFingerprint) {
565+
if (usesTls && this.opts.tlsFingerprint) {
564566
wsOptions.rejectUnauthorized = false;
565567
wsOptions.checkServerIdentity = (_hostValue: string, cert: CertMeta) => {
566568
const fingerprintValue =
@@ -606,7 +608,7 @@ export class GatewayClient {
606608
this.transportValidated = false;
607609
ws.on("open", () => {
608610
handlers.open();
609-
if (url.startsWith("wss://") && this.opts.tlsFingerprint) {
611+
if (usesTls && this.opts.tlsFingerprint) {
610612
const tlsError = this.validateTlsFingerprint();
611613
if (tlsError) {
612614
handlers.error(tlsError);

‎packages/gateway-client/src/protocol-client.socket-factory.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,27 @@ describe("GatewayProtocolClient socket factory recovery", () => {
212212
});
213213

214214
describe("GatewayClient socket factory recovery", () => {
215+
it("accepts uppercase WSS URLs with a TLS fingerprint", () => {
216+
const onConnectError = vi.fn<(error: Error) => void>();
217+
const beforeConnect = vi.fn(() => {
218+
throw new Error("stop after transport policy");
219+
});
220+
const client = new GatewayClient({
221+
url: "WSS://gateway.example:18789",
222+
tlsFingerprint: "deadbeef",
223+
onConnectError,
224+
hostDeps: { beforeConnect },
225+
});
226+
227+
client.start();
228+
229+
expect(beforeConnect).toHaveBeenCalledOnce();
230+
expect(onConnectError).toHaveBeenCalledExactlyOnceWith(
231+
expect.objectContaining({ message: "stop after transport policy" }),
232+
);
233+
client.stop();
234+
});
235+
215236
it("retries transient node-host setup failures without requiring another start", async () => {
216237
vi.useFakeTimers();
217238
const onConnectError = vi.fn<(error: Error) => void>();

‎packages/net-policy/src/url-protocol.test.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { describe, expect, it } from "vitest";
2-
import { hasHttpUrlPrefix, isHttpsUrl, isHttpUrl, isWebSocketUrl } from "./url-protocol.js";
2+
import {
3+
hasHttpUrlPrefix,
4+
isHttpsUrl,
5+
isHttpUrl,
6+
isWebSocketUrl,
7+
isWssUrl,
8+
} from "./url-protocol.js";
39

410
describe("hasHttpUrlPrefix", () => {
511
it.each([
@@ -27,17 +33,20 @@ describe("parsed URL protocol predicates", () => {
2733
expect(isHttpUrl(url)).toBe(http);
2834
expect(isHttpsUrl(url)).toBe(https);
2935
expect(isWebSocketUrl(url)).toBe(websocket);
36+
expect(isWssUrl(url)).toBe(url.protocol === "wss:");
3037
});
3138

3239
it("returns false for malformed and relative strings", () => {
3340
expect(isHttpUrl("https://")).toBe(false);
3441
expect(isHttpsUrl("/relative")).toBe(false);
3542
expect(isWebSocketUrl("not a URL")).toBe(false);
43+
expect(isWssUrl("not a URL")).toBe(false);
3644
});
3745

3846
it("parses string inputs without changing URL constructor behavior", () => {
3947
expect(isHttpUrl(" HTTP://user:[email protected]:8080/path ")).toBe(true);
4048
expect(isHttpsUrl(" HTTPS://example.com ")).toBe(true);
4149
expect(isWebSocketUrl(" WSS://example.com:9443/socket ")).toBe(true);
50+
expect(isWssUrl(" WSS://example.com:9443/socket ")).toBe(true);
4251
});
4352
});

‎packages/net-policy/src/url-protocol.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,7 @@ export function isWebSocketUrl(value: string | URL): boolean {
2828
const url = parseUrl(value);
2929
return url?.protocol === "ws:" || url?.protocol === "wss:";
3030
}
31+
32+
export function isWssUrl(value: string | URL): boolean {
33+
return parseUrl(value)?.protocol === "wss:";
34+
}

‎src/gateway/client-bootstrap.test.ts‎

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -149,19 +149,12 @@ describe("resolveGatewayClientBootstrap", () => {
149149
expect(mockState.loadGatewayTlsRuntime).not.toHaveBeenCalled();
150150
});
151151

152-
it.each([
153-
{
154-
label: "CLI URL overrides",
155-
url: "wss://override.example/ws",
152+
it("does not inherit the configured remote pin for CLI URL overrides", async () => {
153+
const url = "wss://override.example/ws";
154+
mockState.buildGatewayConnectionDetails.mockReturnValue({
155+
url,
156156
urlSource: "cli --url",
157-
},
158-
{
159-
label: "plaintext remote URLs",
160-
url: "ws://gateway.example/ws",
161-
urlSource: "config gateway.remote.url",
162-
},
163-
])("does not inherit the configured remote pin for $label", async ({ url, urlSource }) => {
164-
mockState.buildGatewayConnectionDetails.mockReturnValue({ url, urlSource });
157+
});
165158

166159
const result = await resolveGatewayClientBootstrap({
167160
config: {
@@ -173,14 +166,38 @@ describe("resolveGatewayClientBootstrap", () => {
173166
},
174167
},
175168
} as never,
176-
gatewayUrl: urlSource === "cli --url" ? url : undefined,
169+
gatewayUrl: url,
177170
env: process.env,
178171
});
179172

180173
expect(result.tlsFingerprint).toBeUndefined();
181174
expect(mockState.loadGatewayTlsRuntime).not.toHaveBeenCalled();
182175
});
183176

177+
it("preserves the configured remote pin so plaintext targets fail closed", async () => {
178+
const url = "ws://127.0.0.1:18789";
179+
mockState.buildGatewayConnectionDetails.mockReturnValue({
180+
url,
181+
urlSource: "config gateway.remote.url",
182+
});
183+
184+
const result = await resolveGatewayClientBootstrap({
185+
config: {
186+
gateway: {
187+
mode: "remote",
188+
remote: {
189+
url,
190+
tlsFingerprint: "sha256:remote",
191+
},
192+
},
193+
} as never,
194+
env: process.env,
195+
});
196+
197+
expect(result.tlsFingerprint).toBe("sha256:remote");
198+
expect(mockState.loadGatewayTlsRuntime).not.toHaveBeenCalled();
199+
});
200+
184201
it("uses the local pin when remote mode falls back to the configured local gateway", async () => {
185202
const tlsConfig = { enabled: true };
186203
mockState.buildGatewayConnectionDetails.mockReturnValue({

‎src/gateway/tls-fingerprint.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { isWssUrl } from "@openclaw/net-policy/url-protocol";
12
import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce";
23
import type { GatewayTlsConfig } from "../config/types.gateway.js";
34
import type { OpenClawConfig } from "../config/types.openclaw.js";
@@ -17,9 +18,6 @@ export async function resolveGatewayConnectionTlsFingerprint(params: {
1718
if (explicitTlsFingerprint) {
1819
return explicitTlsFingerprint;
1920
}
20-
if (!params.url.toLowerCase().startsWith("wss://")) {
21-
return undefined;
22-
}
2321

2422
// Env overrides intentionally retain remote-mode pinning for private-cert deployments.
2523
// CLI targets and local fallback are distinct trust decisions and must not inherit that pin.
@@ -32,6 +30,9 @@ export async function resolveGatewayConnectionTlsFingerprint(params: {
3230
if (remoteTlsFingerprint) {
3331
return remoteTlsFingerprint;
3432
}
33+
if (!isWssUrl(params.url)) {
34+
return undefined;
35+
}
3536

3637
const usesConfiguredLocalGateway =
3738
params.urlSource === "local loopback" ||

0 commit comments

Comments
 (0)