Skip to content

Commit

Permalink
Add empty string URL validation in HttpProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
kshyun28 authored and alcuadrado committed May 17, 2024
1 parent 5604169 commit e5b9101
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/hardhat-core/src/internal/core/errors-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,17 @@ Please double check your transactions' parameters.`,
Please check that you are sending an \`address\` parameter.`,
shouldBeReported: false,
},
EMPTY_URL: {
number: 117,
message:
"Empty string `%value%` for network or forking URL - Expected a non-empty string.",
title:
"Empty string `%value%` for network or forking URL - Expected a non-empty string.",
description: `You are trying to connect to a network with an empty network or forking URL.
Please check that you are sending a non-empty string for network or forking \`URL\` parameter.`,
shouldBeReported: false,
},
},
TASK_DEFINITIONS: {
PARAM_AFTER_VARIADIC: {
Expand Down
6 changes: 6 additions & 0 deletions packages/hardhat-core/src/internal/core/providers/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ export class HttpProvider extends EventEmitter implements EIP1193Provider {

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

if (this._url.trim().length === 0) {
throw new HardhatError(ERRORS.NETWORK.EMPTY_URL, {
value: this._url,
});
}

const url = new URL(this._url);
this._path = url.pathname;
this._authHeader =
Expand Down
16 changes: 16 additions & 0 deletions packages/hardhat-core/test/internal/core/providers/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { assert } from "chai";
import { MockAgent, MockPool } from "undici";

import { HttpProvider } from "../../../../src/internal/core/providers/http";
import { ERRORS } from "../../../../src/internal/core/errors-list";
import { SuccessfulJsonRpcResponse } from "../../../../src/internal/util/jsonrpc";
import { expectHardhatError } from "../../../helpers/errors";

const TOO_MANY_REQUEST_STATUS = 429;

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

describe("constructor()", function () {
it("should throw an error if network or forking URL is an empty string", async function () {
expectHardhatError(() => {
const emptyURL = "";
new HttpProvider(emptyURL, networkName, {}, 20000);
}, ERRORS.NETWORK.EMPTY_URL);

expectHardhatError(() => {
const emptyURLwithWhitespace = " ";
new HttpProvider(emptyURLwithWhitespace, networkName, {}, 20000);
}, ERRORS.NETWORK.EMPTY_URL);
});
});

describe("request()", function () {
it("should call mock pool's request()", async function () {
const mockPool = makeMockPool(url);
Expand Down

0 comments on commit e5b9101

Please sign in to comment.