Skip to content

Commit b0ccb04

Browse files
committed
Require protocol to be set in endpoint URLs
1 parent 8ab4b7b commit b0ccb04

File tree

12 files changed

+41
-48
lines changed

12 files changed

+41
-48
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ and this project adheres to
1313

1414
[#1522]: https://github.com/cosmos/cosmjs/pull/1522
1515

16+
### Changed
17+
18+
- @cosmjs/tendermint-rpc: Require protocol to be set in endpoint URLs (https://,
19+
http://, wss:// or ws://). Otherwise an error is raised instead of falling
20+
back to ws://. ([#1527])
21+
22+
[#1527]: https://github.com/cosmos/cosmjs/pull/1527
23+
1624
## [0.32.1] - 2023-12-04
1725

1826
### Fixed

packages/tendermint-rpc/src/comet38/comet38client.spec.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -885,14 +885,6 @@ describe("Comet38Client", () => {
885885
it("can connect to a given url", async () => {
886886
pendingWithoutTendermint();
887887

888-
// default connection
889-
{
890-
const client = await Comet38Client.connect(url);
891-
const info = await client.abciInfo();
892-
expect(info).toBeTruthy();
893-
client.disconnect();
894-
}
895-
896888
// http connection
897889
{
898890
const client = await Comet38Client.connect("http://" + url);
@@ -911,13 +903,13 @@ describe("Comet38Client", () => {
911903
});
912904

913905
describe("With HttpClient", () => {
914-
defaultTestSuite(() => new HttpClient(url), expected);
906+
defaultTestSuite(() => new HttpClient("http://" + url), expected);
915907
});
916908

917909
describe("With WebsocketClient", () => {
918910
// don't print out WebSocket errors if marked pending
919911
const onError = process.env.TENDERMINT_ENABLED ? console.error : () => 0;
920-
const factory = (): WebsocketClient => new WebsocketClient(url, onError);
912+
const factory = (): WebsocketClient => new WebsocketClient("ws://" + url, onError);
921913
defaultTestSuite(factory, expected);
922914
websocketTestSuite(factory, expected);
923915
});

packages/tendermint-rpc/src/rpcclients/httpbatchclient.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ function pendingWithoutTendermint(): void {
99
}
1010
}
1111

12-
const tendermintUrl = defaultInstance.url;
13-
1412
describe("HttpBatchClient", () => {
13+
const tendermintUrl = "http://" + defaultInstance.url;
14+
1515
it("can make a simple call", async () => {
1616
pendingWithoutTendermint();
1717
const client = new HttpBatchClient(tendermintUrl);

packages/tendermint-rpc/src/rpcclients/httpbatchclient.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ export class HttpBatchClient implements RpcClient {
4242
dispatchInterval: options.dispatchInterval ?? defaultHttpBatchClientOptions.dispatchInterval,
4343
};
4444
if (typeof endpoint === "string") {
45-
// accept host.name:port and assume http protocol
46-
this.url = hasProtocol(endpoint) ? endpoint : "http://" + endpoint;
45+
if (!hasProtocol(endpoint)) {
46+
throw new Error("Endpoint URL is missing a protocol. Expected 'https://' or 'http://'.");
47+
}
48+
this.url = endpoint;
4749
} else {
4850
this.url = endpoint.url;
4951
this.headers = endpoint.headers;

packages/tendermint-rpc/src/rpcclients/httpclient.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ function pendingWithoutTendermint(): void {
88
}
99
}
1010

11-
const tendermintUrl = defaultInstance.url;
12-
1311
describe("HttpClient", () => {
12+
const tendermintUrl = "http://" + defaultInstance.url;
13+
1414
it("can make a simple call", async () => {
1515
pendingWithoutTendermint();
1616
const client = new HttpClient(tendermintUrl);

packages/tendermint-rpc/src/rpcclients/httpclient.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ export class HttpClient implements RpcClient {
2828

2929
public constructor(endpoint: string | HttpEndpoint) {
3030
if (typeof endpoint === "string") {
31-
// accept host.name:port and assume http protocol
32-
this.url = hasProtocol(endpoint) ? endpoint : "http://" + endpoint;
31+
if (!hasProtocol(endpoint)) {
32+
throw new Error("Endpoint URL is missing a protocol. Expected 'https://' or 'http://'.");
33+
}
34+
this.url = endpoint;
3335
} else {
3436
this.url = endpoint.url;
3537
this.headers = endpoint.headers;

packages/tendermint-rpc/src/rpcclients/rpcclient.spec.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createJsonRpcRequest } from "../jsonrpc";
22
import { defaultInstance } from "../testutil.spec";
33
import { HttpClient } from "./httpclient";
4-
import { instanceOfRpcStreamingClient } from "./rpcclient";
4+
import { hasProtocol, instanceOfRpcStreamingClient } from "./rpcclient";
55
import { WebsocketClient } from "./websocketclient";
66

77
function pendingWithoutTendermint(): void {
@@ -11,13 +11,14 @@ function pendingWithoutTendermint(): void {
1111
}
1212

1313
describe("RpcClient", () => {
14-
const tendermintUrl = defaultInstance.url;
14+
const httpUrl = "http://" + defaultInstance.url;
15+
const wsUrl = "ws://" + defaultInstance.url;
1516

1617
it("has working instanceOfRpcStreamingClient()", async () => {
1718
pendingWithoutTendermint();
1819

19-
const httpClient = new HttpClient(tendermintUrl);
20-
const wsClient = new WebsocketClient(tendermintUrl);
20+
const httpClient = new HttpClient(httpUrl);
21+
const wsClient = new WebsocketClient(wsUrl);
2122

2223
expect(instanceOfRpcStreamingClient(httpClient)).toEqual(false);
2324
expect(instanceOfRpcStreamingClient(wsClient)).toEqual(true);
@@ -32,11 +33,11 @@ describe("RpcClient", () => {
3233

3334
const statusRequest = createJsonRpcRequest("status");
3435

35-
const httpClient = new HttpClient(tendermintUrl + "/");
36+
const httpClient = new HttpClient(httpUrl + "/");
3637
expect(await httpClient.execute(statusRequest)).toBeDefined();
3738
httpClient.disconnect();
3839

39-
const wsClient = new WebsocketClient(tendermintUrl + "/");
40+
const wsClient = new WebsocketClient(wsUrl + "/");
4041
expect(await wsClient.execute(statusRequest)).toBeDefined();
4142
wsClient.disconnect();
4243
});

packages/tendermint-rpc/src/rpcclients/websocketclient.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ function pendingWithoutTendermint(): void {
1414
}
1515

1616
describe("WebsocketClient", () => {
17-
const { blockTime, url: tendermintUrl } = defaultInstance;
17+
const { blockTime, url } = defaultInstance;
18+
const tendermintUrl = "ws://" + url;
1819

1920
it("can make a simple call", async () => {
2021
pendingWithoutTendermint();

packages/tendermint-rpc/src/rpcclients/websocketclient.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,13 @@ export class WebsocketClient implements RpcStreamingClient {
143143
private readonly subscriptionStreams = new Map<string, Stream<SubscriptionEvent>>();
144144

145145
public constructor(baseUrl: string, onError: (err: any) => void = defaultErrorHandler) {
146-
// accept host.name:port and assume ws protocol
146+
if (!hasProtocol(baseUrl)) {
147+
throw new Error("Base URL is missing a protocol. Expected 'ws://' or 'wss://'.");
148+
}
149+
147150
// make sure we don't end up with ...//websocket
148151
const path = baseUrl.endsWith("/") ? "websocket" : "/websocket";
149-
const cleanBaseUrl = hasProtocol(baseUrl) ? baseUrl : "ws://" + baseUrl;
150-
this.url = cleanBaseUrl + path;
152+
this.url = baseUrl + path;
151153

152154
this.socket = new ReconnectingSocket(this.url);
153155

packages/tendermint-rpc/src/tendermint34/tendermint34client.spec.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -876,14 +876,6 @@ describe("Tendermint34Client", () => {
876876
it("can connect to a given url", async () => {
877877
pendingWithoutTendermint();
878878

879-
// default connection
880-
{
881-
const client = await Tendermint34Client.connect(url);
882-
const info = await client.abciInfo();
883-
expect(info).toBeTruthy();
884-
client.disconnect();
885-
}
886-
887879
// http connection
888880
{
889881
const client = await Tendermint34Client.connect("http://" + url);
@@ -902,13 +894,13 @@ describe("Tendermint34Client", () => {
902894
});
903895

904896
describe("With HttpClient", () => {
905-
defaultTestSuite(() => new HttpClient(url), expected);
897+
defaultTestSuite(() => new HttpClient("http://" + url), expected);
906898
});
907899

908900
describe("With WebsocketClient", () => {
909901
// don't print out WebSocket errors if marked pending
910902
const onError = process.env.TENDERMINT_ENABLED ? console.error : () => 0;
911-
const factory = (): WebsocketClient => new WebsocketClient(url, onError);
903+
const factory = (): WebsocketClient => new WebsocketClient("ws://" + url, onError);
912904
defaultTestSuite(factory, expected);
913905
websocketTestSuite(factory, expected);
914906
});

0 commit comments

Comments
 (0)