diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ef77990d1..e5ffa476af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,12 @@ and this project adheres to ## [Unreleased] +### Changed + +- @cosmjs/tendermint-rpc: Require protocol to be set in endpoint URLs (https://, + http://, wss:// or ws://). Otherwise an error is raised instead of falling + back to ws://. + ## [0.32.1] - 2023-12-04 ### Fixed diff --git a/packages/tendermint-rpc/src/rpcclients/httpbatchclient.ts b/packages/tendermint-rpc/src/rpcclients/httpbatchclient.ts index 4b8e4fe653..8d2267a02d 100644 --- a/packages/tendermint-rpc/src/rpcclients/httpbatchclient.ts +++ b/packages/tendermint-rpc/src/rpcclients/httpbatchclient.ts @@ -42,8 +42,10 @@ export class HttpBatchClient implements RpcClient { dispatchInterval: options.dispatchInterval ?? defaultHttpBatchClientOptions.dispatchInterval, }; if (typeof endpoint === "string") { - // accept host.name:port and assume http protocol - this.url = hasProtocol(endpoint) ? endpoint : "http://" + endpoint; + if (!hasProtocol(endpoint)) { + throw new Error("Endpoint URL is missing a protocol. Expected 'https://' or 'http://'."); + } + this.url = endpoint; } else { this.url = endpoint.url; this.headers = endpoint.headers; diff --git a/packages/tendermint-rpc/src/rpcclients/httpclient.ts b/packages/tendermint-rpc/src/rpcclients/httpclient.ts index a17e9699e3..b4107b321b 100644 --- a/packages/tendermint-rpc/src/rpcclients/httpclient.ts +++ b/packages/tendermint-rpc/src/rpcclients/httpclient.ts @@ -28,8 +28,10 @@ export class HttpClient implements RpcClient { public constructor(endpoint: string | HttpEndpoint) { if (typeof endpoint === "string") { - // accept host.name:port and assume http protocol - this.url = hasProtocol(endpoint) ? endpoint : "http://" + endpoint; + if (!hasProtocol(endpoint)) { + throw new Error("Endpoint URL is missing a protocol. Expected 'https://' or 'http://'."); + } + this.url = endpoint; } else { this.url = endpoint.url; this.headers = endpoint.headers; diff --git a/packages/tendermint-rpc/src/rpcclients/rpcclient.spec.ts b/packages/tendermint-rpc/src/rpcclients/rpcclient.spec.ts index a5fb0310bd..ce27f62751 100644 --- a/packages/tendermint-rpc/src/rpcclients/rpcclient.spec.ts +++ b/packages/tendermint-rpc/src/rpcclients/rpcclient.spec.ts @@ -1,7 +1,7 @@ import { createJsonRpcRequest } from "../jsonrpc"; import { defaultInstance } from "../testutil.spec"; import { HttpClient } from "./httpclient"; -import { instanceOfRpcStreamingClient } from "./rpcclient"; +import { hasProtocol, instanceOfRpcStreamingClient } from "./rpcclient"; import { WebsocketClient } from "./websocketclient"; function pendingWithoutTendermint(): void { @@ -41,3 +41,30 @@ describe("RpcClient", () => { wsClient.disconnect(); }); }); + +describe("hasProtocol", () => { + it("works", () => { + expect(hasProtocol("https://my.rpc.net")).toEqual(true); + expect(hasProtocol("https://my.rpc.net:443")).toEqual(true); + expect(hasProtocol("https://my.rpc.net:443/somewhere")).toEqual(true); + expect(hasProtocol("http://my.rpc.net")).toEqual(true); + expect(hasProtocol("http://my.rpc.net:443")).toEqual(true); + expect(hasProtocol("http://my.rpc.net:443/somewhere")).toEqual(true); + expect(hasProtocol("wss://my.rpc.net")).toEqual(true); + expect(hasProtocol("wss://my.rpc.net:443")).toEqual(true); + expect(hasProtocol("wss://my.rpc.net:443/somewhere")).toEqual(true); + expect(hasProtocol("ws://my.rpc.net")).toEqual(true); + expect(hasProtocol("ws://my.rpc.net:443")).toEqual(true); + expect(hasProtocol("ws://my.rpc.net:443/somewhere")).toEqual(true); + expect(hasProtocol("ws://my.rpc.net:443/somewhere")).toEqual(true); + expect(hasProtocol("file:///Users/joe/Library/CloudStorage/Notes.pdf")).toEqual(true); + + // Protocols without double slash is not what we want to see here + expect(hasProtocol("tel:+1-201-555-0123")).toEqual(false); + + // No protocols + expect(hasProtocol("")).toEqual(false); + expect(hasProtocol(" ")).toEqual(false); + expect(hasProtocol("/")).toEqual(false); + }); +}); diff --git a/packages/tendermint-rpc/src/rpcclients/websocketclient.ts b/packages/tendermint-rpc/src/rpcclients/websocketclient.ts index d5485b3536..52803d1b62 100644 --- a/packages/tendermint-rpc/src/rpcclients/websocketclient.ts +++ b/packages/tendermint-rpc/src/rpcclients/websocketclient.ts @@ -143,11 +143,13 @@ export class WebsocketClient implements RpcStreamingClient { private readonly subscriptionStreams = new Map>(); public constructor(baseUrl: string, onError: (err: any) => void = defaultErrorHandler) { - // accept host.name:port and assume ws protocol + if (!hasProtocol(baseUrl)) { + throw new Error("Base URL is missing a protocol. Expected 'ws://' or 'wss://'."); + } + // make sure we don't end up with ...//websocket const path = baseUrl.endsWith("/") ? "websocket" : "/websocket"; - const cleanBaseUrl = hasProtocol(baseUrl) ? baseUrl : "ws://" + baseUrl; - this.url = cleanBaseUrl + path; + this.url = baseUrl + path; this.socket = new ReconnectingSocket(this.url);