From b0ccb04dacb600423832c5413f9237fce8aaecf3 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Tue, 19 Dec 2023 13:54:16 +0100 Subject: [PATCH] Require protocol to be set in endpoint URLs --- CHANGELOG.md | 8 ++++++++ .../src/comet38/comet38client.spec.ts | 12 ++---------- .../src/rpcclients/httpbatchclient.spec.ts | 4 ++-- .../src/rpcclients/httpbatchclient.ts | 6 ++++-- .../src/rpcclients/httpclient.spec.ts | 4 ++-- .../tendermint-rpc/src/rpcclients/httpclient.ts | 6 ++++-- .../tendermint-rpc/src/rpcclients/rpcclient.spec.ts | 13 +++++++------ .../src/rpcclients/websocketclient.spec.ts | 3 ++- .../src/rpcclients/websocketclient.ts | 8 +++++--- .../src/tendermint34/tendermint34client.spec.ts | 12 ++---------- .../src/tendermint37/tendermint37client.spec.ts | 12 ++---------- packages/tendermint-rpc/src/testutil.spec.ts | 1 + 12 files changed, 41 insertions(+), 48 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 66bef37269..f610ce72b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,14 @@ and this project adheres to [#1522]: https://github.com/cosmos/cosmjs/pull/1522 +### 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://. ([#1527]) + +[#1527]: https://github.com/cosmos/cosmjs/pull/1527 + ## [0.32.1] - 2023-12-04 ### Fixed diff --git a/packages/tendermint-rpc/src/comet38/comet38client.spec.ts b/packages/tendermint-rpc/src/comet38/comet38client.spec.ts index c949036a4b..3d411432dd 100644 --- a/packages/tendermint-rpc/src/comet38/comet38client.spec.ts +++ b/packages/tendermint-rpc/src/comet38/comet38client.spec.ts @@ -885,14 +885,6 @@ describe("Comet38Client", () => { it("can connect to a given url", async () => { pendingWithoutTendermint(); - // default connection - { - const client = await Comet38Client.connect(url); - const info = await client.abciInfo(); - expect(info).toBeTruthy(); - client.disconnect(); - } - // http connection { const client = await Comet38Client.connect("http://" + url); @@ -911,13 +903,13 @@ describe("Comet38Client", () => { }); describe("With HttpClient", () => { - defaultTestSuite(() => new HttpClient(url), expected); + defaultTestSuite(() => new HttpClient("http://" + url), expected); }); describe("With WebsocketClient", () => { // don't print out WebSocket errors if marked pending const onError = process.env.TENDERMINT_ENABLED ? console.error : () => 0; - const factory = (): WebsocketClient => new WebsocketClient(url, onError); + const factory = (): WebsocketClient => new WebsocketClient("ws://" + url, onError); defaultTestSuite(factory, expected); websocketTestSuite(factory, expected); }); diff --git a/packages/tendermint-rpc/src/rpcclients/httpbatchclient.spec.ts b/packages/tendermint-rpc/src/rpcclients/httpbatchclient.spec.ts index b05c3e56de..e25cf478ef 100644 --- a/packages/tendermint-rpc/src/rpcclients/httpbatchclient.spec.ts +++ b/packages/tendermint-rpc/src/rpcclients/httpbatchclient.spec.ts @@ -9,9 +9,9 @@ function pendingWithoutTendermint(): void { } } -const tendermintUrl = defaultInstance.url; - describe("HttpBatchClient", () => { + const tendermintUrl = "http://" + defaultInstance.url; + it("can make a simple call", async () => { pendingWithoutTendermint(); const client = new HttpBatchClient(tendermintUrl); 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.spec.ts b/packages/tendermint-rpc/src/rpcclients/httpclient.spec.ts index 11cf707e85..8026d2e2c0 100644 --- a/packages/tendermint-rpc/src/rpcclients/httpclient.spec.ts +++ b/packages/tendermint-rpc/src/rpcclients/httpclient.spec.ts @@ -8,9 +8,9 @@ function pendingWithoutTendermint(): void { } } -const tendermintUrl = defaultInstance.url; - describe("HttpClient", () => { + const tendermintUrl = "http://" + defaultInstance.url; + it("can make a simple call", async () => { pendingWithoutTendermint(); const client = new HttpClient(tendermintUrl); 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 9097dec2c1..8720ffd256 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 { @@ -11,13 +11,14 @@ function pendingWithoutTendermint(): void { } describe("RpcClient", () => { - const tendermintUrl = defaultInstance.url; + const httpUrl = "http://" + defaultInstance.url; + const wsUrl = "ws://" + defaultInstance.url; it("has working instanceOfRpcStreamingClient()", async () => { pendingWithoutTendermint(); - const httpClient = new HttpClient(tendermintUrl); - const wsClient = new WebsocketClient(tendermintUrl); + const httpClient = new HttpClient(httpUrl); + const wsClient = new WebsocketClient(wsUrl); expect(instanceOfRpcStreamingClient(httpClient)).toEqual(false); expect(instanceOfRpcStreamingClient(wsClient)).toEqual(true); @@ -32,11 +33,11 @@ describe("RpcClient", () => { const statusRequest = createJsonRpcRequest("status"); - const httpClient = new HttpClient(tendermintUrl + "/"); + const httpClient = new HttpClient(httpUrl + "/"); expect(await httpClient.execute(statusRequest)).toBeDefined(); httpClient.disconnect(); - const wsClient = new WebsocketClient(tendermintUrl + "/"); + const wsClient = new WebsocketClient(wsUrl + "/"); expect(await wsClient.execute(statusRequest)).toBeDefined(); wsClient.disconnect(); }); diff --git a/packages/tendermint-rpc/src/rpcclients/websocketclient.spec.ts b/packages/tendermint-rpc/src/rpcclients/websocketclient.spec.ts index 8c88a450ee..9af778455f 100644 --- a/packages/tendermint-rpc/src/rpcclients/websocketclient.spec.ts +++ b/packages/tendermint-rpc/src/rpcclients/websocketclient.spec.ts @@ -14,7 +14,8 @@ function pendingWithoutTendermint(): void { } describe("WebsocketClient", () => { - const { blockTime, url: tendermintUrl } = defaultInstance; + const { blockTime, url } = defaultInstance; + const tendermintUrl = "ws://" + url; it("can make a simple call", async () => { pendingWithoutTendermint(); 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); diff --git a/packages/tendermint-rpc/src/tendermint34/tendermint34client.spec.ts b/packages/tendermint-rpc/src/tendermint34/tendermint34client.spec.ts index eca45de830..f8f2140e87 100644 --- a/packages/tendermint-rpc/src/tendermint34/tendermint34client.spec.ts +++ b/packages/tendermint-rpc/src/tendermint34/tendermint34client.spec.ts @@ -876,14 +876,6 @@ describe("Tendermint34Client", () => { it("can connect to a given url", async () => { pendingWithoutTendermint(); - // default connection - { - const client = await Tendermint34Client.connect(url); - const info = await client.abciInfo(); - expect(info).toBeTruthy(); - client.disconnect(); - } - // http connection { const client = await Tendermint34Client.connect("http://" + url); @@ -902,13 +894,13 @@ describe("Tendermint34Client", () => { }); describe("With HttpClient", () => { - defaultTestSuite(() => new HttpClient(url), expected); + defaultTestSuite(() => new HttpClient("http://" + url), expected); }); describe("With WebsocketClient", () => { // don't print out WebSocket errors if marked pending const onError = process.env.TENDERMINT_ENABLED ? console.error : () => 0; - const factory = (): WebsocketClient => new WebsocketClient(url, onError); + const factory = (): WebsocketClient => new WebsocketClient("ws://" + url, onError); defaultTestSuite(factory, expected); websocketTestSuite(factory, expected); }); diff --git a/packages/tendermint-rpc/src/tendermint37/tendermint37client.spec.ts b/packages/tendermint-rpc/src/tendermint37/tendermint37client.spec.ts index 36732b31e7..b90653d9a3 100644 --- a/packages/tendermint-rpc/src/tendermint37/tendermint37client.spec.ts +++ b/packages/tendermint-rpc/src/tendermint37/tendermint37client.spec.ts @@ -885,14 +885,6 @@ describe("Tendermint37Client", () => { it("can connect to a given url", async () => { pendingWithoutTendermint(); - // default connection - { - const client = await Tendermint37Client.connect(url); - const info = await client.abciInfo(); - expect(info).toBeTruthy(); - client.disconnect(); - } - // http connection { const client = await Tendermint37Client.connect("http://" + url); @@ -911,13 +903,13 @@ describe("Tendermint37Client", () => { }); describe("With HttpClient", () => { - defaultTestSuite(() => new HttpClient(url), expected); + defaultTestSuite(() => new HttpClient("http://" + url), expected); }); describe("With WebsocketClient", () => { // don't print out WebSocket errors if marked pending const onError = process.env.TENDERMINT_ENABLED ? console.error : () => 0; - const factory = (): WebsocketClient => new WebsocketClient(url, onError); + const factory = (): WebsocketClient => new WebsocketClient("ws://" + url, onError); defaultTestSuite(factory, expected); websocketTestSuite(factory, expected); }); diff --git a/packages/tendermint-rpc/src/testutil.spec.ts b/packages/tendermint-rpc/src/testutil.spec.ts index e47bead89b..735805e4fa 100644 --- a/packages/tendermint-rpc/src/testutil.spec.ts +++ b/packages/tendermint-rpc/src/testutil.spec.ts @@ -15,6 +15,7 @@ export interface ExpectedValues { } export interface TendermintInstance { + /** URL without protocol. Protocol will be added in tests. */ readonly url: string; readonly version: string; /** rough block time in ms */