Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log query parameters on HTTP requests #3591

Merged
merged 2 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions spec/unit/http-api/fetch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ describe("FetchHttpApi", () => {
const fetchFn = jest.fn().mockReturnValue(deferred.promise);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

happy to merge when you're ready (ping me).

@andybalaam Feel free to merge whenever 🙂

jest.spyOn(logger, "debug").mockImplementation(() => {});
const api = new FetchHttpApi(new TypedEventEmitter<any, any>(), { 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;
Expand All @@ -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]",
]
`);
});
Expand Down
16 changes: 11 additions & 5 deletions src/http-api/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ export class FetchHttpApi<O extends IHttpOpts> {
body?: Body,
opts: Pick<IRequestOpts, "headers" | "json" | "localTimeoutMs" | "keepAlive" | "abortSignal"> = {},
): Promise<ResponseType<T, O>> {
const urlForLogs = this.clearUrlParamsForLogs(url);
const urlForLogs = this.sanitizeUrlForLogs(url);
logger.debug(`FetchHttpApi: --> ${method} ${urlForLogs}`);

const headers = Object.assign({}, opts.headers || {});
Expand Down Expand Up @@ -301,17 +301,23 @@ export class FetchHttpApi<O extends IHttpOpts> {
return res as ResponseType<T, O>;
}

private clearUrlParamsForLogs(url: URL | string): string {
private sanitizeUrlForLogs(url: URL | string): string {
try {
let asUrl: URL;
if (typeof url === "string") {
asUrl = new URL(url);
} 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 "??";
Expand Down