diff --git a/spec/unit/http-api/fetch.spec.ts b/spec/unit/http-api/fetch.spec.ts index 236fbe1c3..2cb5f4dd3 100644 --- a/spec/unit/http-api/fetch.spec.ts +++ b/spec/unit/http-api/fetch.spec.ts @@ -300,7 +300,7 @@ describe("FetchHttpApi", () => { const fetchFn = jest.fn().mockReturnValue(deferred.promise); jest.spyOn(logger, "debug").mockImplementation(() => {}); const api = new FetchHttpApi(new TypedEventEmitter(), { baseUrl, prefix, fetchFn }); - const prom = api.requestOtherUrl(Method.Get, "https://server:8448/some/path#fragment?query=param"); + const prom = api.requestOtherUrl(Method.Get, "https://server:8448/some/path?query=param#fragment"); jest.advanceTimersByTime(1234); deferred.resolve({ ok: true, status: 200, text: () => Promise.resolve("RESPONSE") } as Response); await prom; @@ -310,12 +310,12 @@ describe("FetchHttpApi", () => { expect(logger.debug).toHaveBeenCalledTimes(2); expect(mocked(logger.debug).mock.calls[0]).toMatchInlineSnapshot(` [ - "FetchHttpApi: --> GET https://server:8448/some/path", + "FetchHttpApi: --> GET https://server:8448/some/path?query=xxx", ] `); expect(mocked(logger.debug).mock.calls[1]).toMatchInlineSnapshot(` [ - "FetchHttpApi: <-- GET https://server:8448/some/path [1234ms 200]", + "FetchHttpApi: <-- GET https://server:8448/some/path?query=xxx [1234ms 200]", ] `); }); diff --git a/src/http-api/fetch.ts b/src/http-api/fetch.ts index 9408c94ce..f59546bb0 100644 --- a/src/http-api/fetch.ts +++ b/src/http-api/fetch.ts @@ -224,7 +224,7 @@ export class FetchHttpApi { body?: Body, opts: Pick = {}, ): Promise> { - const urlForLogs = this.clearUrlParamsForLogs(url); + const urlForLogs = this.sanitizeUrlForLogs(url); logger.debug(`FetchHttpApi: --> ${method} ${urlForLogs}`); const headers = Object.assign({}, opts.headers || {}); @@ -299,7 +299,7 @@ export class FetchHttpApi { return res as ResponseType; } - private clearUrlParamsForLogs(url: URL | string): string { + private sanitizeUrlForLogs(url: URL | string): string { try { let asUrl: URL; if (typeof url === "string") { @@ -307,9 +307,15 @@ export class FetchHttpApi { } else { asUrl = url; } - // get just the path to remove any potential url param that could have - // some potential secrets - return asUrl.origin + asUrl.pathname; + // Remove the values of any URL params that could contain potential secrets + const sanitizedQs = new URLSearchParams(); + for (const key of asUrl.searchParams.keys()) { + sanitizedQs.append(key, "xxx"); + } + const sanitizedQsString = sanitizedQs.toString(); + const sanitizedQsUrlPiece = sanitizedQsString ? `?${sanitizedQsString}` : ""; + + return asUrl.origin + asUrl.pathname + sanitizedQsUrlPiece; } catch (error) { // defensive coding for malformed url return "??";