Skip to content

Commit e5b9101

Browse files
kshyun28alcuadrado
authored andcommitted
Add empty string URL validation in HttpProvider
1 parent 5604169 commit e5b9101

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

packages/hardhat-core/src/internal/core/errors-list.ts

+11
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,17 @@ Please double check your transactions' parameters.`,
475475
Please check that you are sending an \`address\` parameter.`,
476476
shouldBeReported: false,
477477
},
478+
EMPTY_URL: {
479+
number: 117,
480+
message:
481+
"Empty string `%value%` for network or forking URL - Expected a non-empty string.",
482+
title:
483+
"Empty string `%value%` for network or forking URL - Expected a non-empty string.",
484+
description: `You are trying to connect to a network with an empty network or forking URL.
485+
486+
Please check that you are sending a non-empty string for network or forking \`URL\` parameter.`,
487+
shouldBeReported: false,
488+
},
478489
},
479490
TASK_DEFINITIONS: {
480491
PARAM_AFTER_VARIADIC: {

packages/hardhat-core/src/internal/core/providers/http.ts

+6
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ export class HttpProvider extends EventEmitter implements EIP1193Provider {
5151

5252
const { Pool, ProxyAgent } = require("undici") as typeof Undici;
5353

54+
if (this._url.trim().length === 0) {
55+
throw new HardhatError(ERRORS.NETWORK.EMPTY_URL, {
56+
value: this._url,
57+
});
58+
}
59+
5460
const url = new URL(this._url);
5561
this._path = url.pathname;
5662
this._authHeader =

packages/hardhat-core/test/internal/core/providers/http.ts

+16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { assert } from "chai";
22
import { MockAgent, MockPool } from "undici";
33

44
import { HttpProvider } from "../../../../src/internal/core/providers/http";
5+
import { ERRORS } from "../../../../src/internal/core/errors-list";
56
import { SuccessfulJsonRpcResponse } from "../../../../src/internal/util/jsonrpc";
7+
import { expectHardhatError } from "../../../helpers/errors";
68

79
const TOO_MANY_REQUEST_STATUS = 429;
810

@@ -27,6 +29,20 @@ describe("HttpProvider", function () {
2729
result: "whatever",
2830
};
2931

32+
describe("constructor()", function () {
33+
it("should throw an error if network or forking URL is an empty string", async function () {
34+
expectHardhatError(() => {
35+
const emptyURL = "";
36+
new HttpProvider(emptyURL, networkName, {}, 20000);
37+
}, ERRORS.NETWORK.EMPTY_URL);
38+
39+
expectHardhatError(() => {
40+
const emptyURLwithWhitespace = " ";
41+
new HttpProvider(emptyURLwithWhitespace, networkName, {}, 20000);
42+
}, ERRORS.NETWORK.EMPTY_URL);
43+
});
44+
});
45+
3046
describe("request()", function () {
3147
it("should call mock pool's request()", async function () {
3248
const mockPool = makeMockPool(url);

0 commit comments

Comments
 (0)