Skip to content

Commit

Permalink
fix: do not URI-encode Basic Auth header contents
Browse files Browse the repository at this point in the history
  • Loading branch information
jshufro committed Dec 5, 2023
1 parent 12bd867 commit 814df3d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/api/src/utils/client/httpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ export class HttpClient implements IHttpClient {
// Extract the username and password, if any
if (url.username !== "" || url.password != "") {
urlOpts.extraHeaders = {
// eslint-disable-next-line @typescript-eslint/naming-convention
Authorization: `Basic ${toBase64(`${url.username}:${url.password}`)}`,
// eslint-disable-next-line @typescript-eslint/naming-convention
Authorization: `Basic ${toBase64(decodeURIComponent(`${url.username}:${url.password}`))}`,
...urlOpts.extraHeaders,
};
url.username = "";
Expand Down
15 changes: 15 additions & 0 deletions packages/api/test/unit/client/httpClientOptions.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import {expect} from "chai";
import {toBase64} from "@lodestar/utils";
import {HttpClient} from "../../../src/index.js";

describe("HTTPClient options", () => {
const baseUrl1 = "http://url-1/";
const baseUrl2 = "http://url-2/";
const baseUrl1BasicAuth = "http://username:password@url-1/";
const baseUrl2BasicAuth = "http://username:password@url-2/";
const baseUrl2BasicAuthEncoded = "http://username=:password@url-2/";
const bearerToken1 = "token-1";

Check failure

Code scanning / CodeQL

Hard-coded credentials Critical test

The hard-coded value "token-1" is used as
authorization header
.
The hard-coded value "token-1" is used as
authorization header
.
The hard-coded value "token-1" is used as
authorization header
.
The hard-coded value "token-1" is used as
authorization header
.
The hard-coded value "token-1" is used as
authorization header
.
The hard-coded value "token-1" is used as
authorization header
.
The hard-coded value "token-1" is used as
authorization header
.
const bearerToken2 = "token-2";

Check failure

Code scanning / CodeQL

Hard-coded credentials Critical test

The hard-coded value "token-2" is used as
authorization header
.
The hard-coded value "token-2" is used as
authorization header
.
The hard-coded value "token-2" is used as
authorization header
.
The hard-coded value "token-2" is used as
authorization header
.

Expand Down Expand Up @@ -156,6 +158,19 @@ describe("HTTPClient options", () => {
]);
});

it("Basic Auth should not be URI-encoded", () => {
const httpClient = new HttpClient({
urls: [baseUrl2BasicAuthEncoded],
});
expect(httpClient["urlsOpts"]).deep.equals([
{
baseUrl: baseUrl2,
// eslint-disable-next-line @typescript-eslint/naming-convention
extraHeaders: {Authorization: `Basic ${toBase64("username=:password")}`},
},
]);
});

it("Throw if empty baseUrl", () => {
expect(() => new HttpClient({baseUrl: ""})).to.throw(Error);
});
Expand Down

0 comments on commit 814df3d

Please sign in to comment.