Skip to content

Commit 8997381

Browse files
authored
fix: remove credentials from stacktrace (#18)
1 parent a51a950 commit 8997381

File tree

4 files changed

+62
-4
lines changed

4 files changed

+62
-4
lines changed

clients/algoliasearch-client-javascript/utils/Transporter.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { RetryError } from './errors';
2222
import * as responseUtils from './Response';
2323
import { Requester } from './requester/Requester';
2424
import { HttpRequester } from './requester/HttpRequester';
25+
import { stackTraceWithoutCredentials, stackFrameWithoutCredentials } from './stackTrace';
2526

2627
export class Transporter {
2728
private hosts: Host[];
@@ -143,7 +144,7 @@ export class Transporter {
143144
*/
144145
const host = hosts.pop();
145146
if (host === undefined) {
146-
throw new RetryError(stackTrace);
147+
throw new RetryError(stackTraceWithoutCredentials(stackTrace));
147148
}
148149

149150
let responseTimeout = requestOptions.timeout;
@@ -181,12 +182,18 @@ export class Transporter {
181182
const response = await this.requester.send(payload, request);
182183

183184
if (responseUtils.isRetryable(response)) {
184-
pushToStackTrace(response);
185+
const stackFrame = pushToStackTrace(response);
185186

186187
// If response is a timeout, we increase the number of timeouts so we can increase the timeout later.
187188
if (response.isTimedOut) {
188189
timeoutsCount++;
189190
}
191+
/**
192+
* Failures are individually sent to the logger, allowing
193+
* the end user to debug / store stack frames even
194+
* when a retry error does not happen.
195+
*/
196+
console.log('Retryable failure', stackFrameWithoutCredentials(stackFrame));
190197

191198
/**
192199
* We also store the state of the host in failure cases. If the host, is
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { StackFrame } from './types';
2+
3+
export function stackTraceWithoutCredentials(stackTrace: StackFrame[]): StackFrame[] {
4+
return stackTrace.map((stackFrame) => stackFrameWithoutCredentials(stackFrame));
5+
}
6+
7+
export function stackFrameWithoutCredentials(stackFrame: StackFrame): StackFrame {
8+
const modifiedHeaders: Record<string, string> = stackFrame.request.headers['x-algolia-api-key']
9+
? { 'x-algolia-api-key': '*****' }
10+
: {};
11+
12+
return {
13+
...stackFrame,
14+
request: {
15+
...stackFrame.request,
16+
headers: {
17+
...stackFrame.request.headers,
18+
...modifiedHeaders,
19+
},
20+
},
21+
};
22+
}

clients/utils/javascript/Transporter.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { RetryError } from './errors';
2222
import * as responseUtils from './Response';
2323
import { Requester } from './requester/Requester';
2424
import { HttpRequester } from './requester/HttpRequester';
25+
import { stackTraceWithoutCredentials, stackFrameWithoutCredentials } from './stackTrace';
2526

2627
export class Transporter {
2728
private hosts: Host[];
@@ -143,7 +144,7 @@ export class Transporter {
143144
*/
144145
const host = hosts.pop();
145146
if (host === undefined) {
146-
throw new RetryError(stackTrace);
147+
throw new RetryError(stackTraceWithoutCredentials(stackTrace));
147148
}
148149

149150
let responseTimeout = requestOptions.timeout;
@@ -181,12 +182,18 @@ export class Transporter {
181182
const response = await this.requester.send(payload, request);
182183

183184
if (responseUtils.isRetryable(response)) {
184-
pushToStackTrace(response);
185+
const stackFrame = pushToStackTrace(response);
185186

186187
// If response is a timeout, we increase the number of timeouts so we can increase the timeout later.
187188
if (response.isTimedOut) {
188189
timeoutsCount++;
189190
}
191+
/**
192+
* Failures are individually send the logger, allowing
193+
* the end user to debug / store stack frames even
194+
* when a retry error does not happen.
195+
*/
196+
console.log('Retryable failure', stackFrameWithoutCredentials(stackFrame));
190197

191198
/**
192199
* We also store the state of the host in failure cases. If the host, is
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { StackFrame } from './types';
2+
3+
export function stackTraceWithoutCredentials(stackTrace: StackFrame[]): StackFrame[] {
4+
return stackTrace.map((stackFrame) => stackFrameWithoutCredentials(stackFrame));
5+
}
6+
7+
export function stackFrameWithoutCredentials(stackFrame: StackFrame): StackFrame {
8+
const modifiedHeaders: Record<string, string> = stackFrame.request.headers['x-algolia-api-key']
9+
? { 'x-algolia-api-key': '*****' }
10+
: {};
11+
12+
return {
13+
...stackFrame,
14+
request: {
15+
...stackFrame.request,
16+
headers: {
17+
...stackFrame.request.headers,
18+
...modifiedHeaders,
19+
},
20+
},
21+
};
22+
}

0 commit comments

Comments
 (0)