From 03246e03e604eea50453943c5e671383deece02b Mon Sep 17 00:00:00 2001 From: Pierre Millot Date: Fri, 26 Nov 2021 16:38:00 +0100 Subject: [PATCH 1/2] fix: remove credentials from stacktrace --- .../utils/Transporter.ts | 11 ++++++++-- .../utils/stackTrace.ts | 22 +++++++++++++++++++ clients/utils/javascript/Transporter.ts | 11 ++++++++-- clients/utils/javascript/stackTrace.ts | 22 +++++++++++++++++++ 4 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 clients/algoliasearch-client-javascript/utils/stackTrace.ts create mode 100644 clients/utils/javascript/stackTrace.ts diff --git a/clients/algoliasearch-client-javascript/utils/Transporter.ts b/clients/algoliasearch-client-javascript/utils/Transporter.ts index f7fccad3ff5..0d3529d732f 100644 --- a/clients/algoliasearch-client-javascript/utils/Transporter.ts +++ b/clients/algoliasearch-client-javascript/utils/Transporter.ts @@ -22,6 +22,7 @@ import { RetryError } from './errors'; import * as responseUtils from './Response'; import { Requester } from './requester/Requester'; import { HttpRequester } from './requester/HttpRequester'; +import { stackTraceWithoutCredentials, stackFrameWithoutCredentials } from './stackTrace'; export class Transporter { private hosts: Host[]; @@ -143,7 +144,7 @@ export class Transporter { */ const host = hosts.pop(); if (host === undefined) { - throw new RetryError(stackTrace); + throw new RetryError(stackTraceWithoutCredentials(stackTrace)); } let responseTimeout = requestOptions.timeout; @@ -181,12 +182,18 @@ export class Transporter { const response = await this.requester.send(payload, request); if (responseUtils.isRetryable(response)) { - pushToStackTrace(response); + const stackFrame = pushToStackTrace(response); // If response is a timeout, we increase the number of timeouts so we can increase the timeout later. if (response.isTimedOut) { timeoutsCount++; } + /** + * Failures are individually send the logger, allowing + * the end user to debug / store stack frames even + * when a retry error does not happen. + */ + console.log('Retryable failure', stackFrameWithoutCredentials(stackFrame)); /** * We also store the state of the host in failure cases. If the host, is diff --git a/clients/algoliasearch-client-javascript/utils/stackTrace.ts b/clients/algoliasearch-client-javascript/utils/stackTrace.ts new file mode 100644 index 00000000000..bb4f12199fd --- /dev/null +++ b/clients/algoliasearch-client-javascript/utils/stackTrace.ts @@ -0,0 +1,22 @@ +import { StackFrame } from './types'; + +export function stackTraceWithoutCredentials(stackTrace: StackFrame[]): StackFrame[] { + return stackTrace.map((stackFrame) => stackFrameWithoutCredentials(stackFrame)); +} + +export function stackFrameWithoutCredentials(stackFrame: StackFrame): StackFrame { + const modifiedHeaders: Record = stackFrame.request.headers['x-algolia-api-key'] + ? { 'x-algolia-api-key': '*****' } + : {}; + + return { + ...stackFrame, + request: { + ...stackFrame.request, + headers: { + ...stackFrame.request.headers, + ...modifiedHeaders, + }, + }, + }; +} diff --git a/clients/utils/javascript/Transporter.ts b/clients/utils/javascript/Transporter.ts index f7fccad3ff5..0d3529d732f 100644 --- a/clients/utils/javascript/Transporter.ts +++ b/clients/utils/javascript/Transporter.ts @@ -22,6 +22,7 @@ import { RetryError } from './errors'; import * as responseUtils from './Response'; import { Requester } from './requester/Requester'; import { HttpRequester } from './requester/HttpRequester'; +import { stackTraceWithoutCredentials, stackFrameWithoutCredentials } from './stackTrace'; export class Transporter { private hosts: Host[]; @@ -143,7 +144,7 @@ export class Transporter { */ const host = hosts.pop(); if (host === undefined) { - throw new RetryError(stackTrace); + throw new RetryError(stackTraceWithoutCredentials(stackTrace)); } let responseTimeout = requestOptions.timeout; @@ -181,12 +182,18 @@ export class Transporter { const response = await this.requester.send(payload, request); if (responseUtils.isRetryable(response)) { - pushToStackTrace(response); + const stackFrame = pushToStackTrace(response); // If response is a timeout, we increase the number of timeouts so we can increase the timeout later. if (response.isTimedOut) { timeoutsCount++; } + /** + * Failures are individually send the logger, allowing + * the end user to debug / store stack frames even + * when a retry error does not happen. + */ + console.log('Retryable failure', stackFrameWithoutCredentials(stackFrame)); /** * We also store the state of the host in failure cases. If the host, is diff --git a/clients/utils/javascript/stackTrace.ts b/clients/utils/javascript/stackTrace.ts new file mode 100644 index 00000000000..bb4f12199fd --- /dev/null +++ b/clients/utils/javascript/stackTrace.ts @@ -0,0 +1,22 @@ +import { StackFrame } from './types'; + +export function stackTraceWithoutCredentials(stackTrace: StackFrame[]): StackFrame[] { + return stackTrace.map((stackFrame) => stackFrameWithoutCredentials(stackFrame)); +} + +export function stackFrameWithoutCredentials(stackFrame: StackFrame): StackFrame { + const modifiedHeaders: Record = stackFrame.request.headers['x-algolia-api-key'] + ? { 'x-algolia-api-key': '*****' } + : {}; + + return { + ...stackFrame, + request: { + ...stackFrame.request, + headers: { + ...stackFrame.request.headers, + ...modifiedHeaders, + }, + }, + }; +} From cc482be8e1cf038240666b8be70b0b4765310cc4 Mon Sep 17 00:00:00 2001 From: Pierre Millot Date: Mon, 29 Nov 2021 14:01:24 +0100 Subject: [PATCH 2/2] Update clients/algoliasearch-client-javascript/utils/Transporter.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Clément Vannicatte <20689156+shortcuts@users.noreply.github.com> --- clients/algoliasearch-client-javascript/utils/Transporter.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clients/algoliasearch-client-javascript/utils/Transporter.ts b/clients/algoliasearch-client-javascript/utils/Transporter.ts index 0d3529d732f..e600dc6f996 100644 --- a/clients/algoliasearch-client-javascript/utils/Transporter.ts +++ b/clients/algoliasearch-client-javascript/utils/Transporter.ts @@ -189,7 +189,7 @@ export class Transporter { timeoutsCount++; } /** - * Failures are individually send the logger, allowing + * Failures are individually sent to the logger, allowing * the end user to debug / store stack frames even * when a retry error does not happen. */