Skip to content

Commit 0ff1bb1

Browse files
fix(javascript): mask API key in ApiError stack trace (generated)
algolia/api-clients-automation#7182 Co-authored-by: Brennan Blair <36816721+bblair31@users.noreply.github.com>
1 parent 4e5362f commit 0ff1bb1

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

‎packages/client-common/src/__tests__/transporter/correlationId.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,29 @@ describe('correlation ID on errors', () => {
8181
expect(error.message).toBe('Invalid API key');
8282
});
8383

84+
test('masks the API key in every stack frame of 4xx errors', async () => {
85+
let attempt = 0;
86+
const transporter = createTestTransporter(
87+
{
88+
send: async () => {
89+
attempt++;
90+
return attempt === 1
91+
? { status: 500, content: '', isTimedOut: false }
92+
: { status: 403, content: JSON.stringify({ message: 'Invalid API key', status: 403 }), isTimedOut: false };
93+
},
94+
},
95+
2,
96+
);
97+
98+
const error: ApiError = await transporter.request({ ...request, headers: { 'x-algolia-api-key': 'secret' } }).then(
99+
() => Promise.reject(new Error('should have thrown')),
100+
(err) => err,
101+
);
102+
103+
expect(error).toBeInstanceOf(ApiError);
104+
expect(error.stackTrace.map((frame) => frame.request.headers['x-algolia-api-key'])).toEqual(['*****', '*****']);
105+
});
106+
84107
test('reads the header case-insensitively from custom requesters', async () => {
85108
const transporter = createTestTransporter({
86109
send: async () => ({

‎packages/client-common/src/transporter/createTransporter.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ export function createTransporter({
270270
}
271271

272272
pushToStackTrace(response);
273-
throw deserializeFailure(response, stackTrace);
273+
throw deserializeFailure(response, stackTraceWithoutCredentials(stackTrace));
274274
};
275275

276276
/**

0 commit comments

Comments
 (0)