From 05e65efda6d13e760d4f7f87be7d6cebeba3cc64 Mon Sep 17 00:00:00 2001 From: Daniel Bankhead Date: Wed, 3 Apr 2024 11:33:57 -0700 Subject: [PATCH] fix: Error Redactor Case-Insensitive Matching (#613) --- src/common.ts | 12 ++++++------ test/test.getch.ts | 23 ++++++++++++++++------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/common.ts b/src/common.ts index 485a19c..7c35d2c 100644 --- a/src/common.ts +++ b/src/common.ts @@ -369,17 +369,17 @@ export function defaultErrorRedactor(data: { for (const key of Object.keys(headers)) { // any casing of `Authentication` - if (/^authentication$/.test(key)) { + if (/^authentication$/i.test(key)) { headers[key] = REDACT; } // any casing of `Authorization` - if (/^authorization$/.test(key)) { + if (/^authorization$/i.test(key)) { headers[key] = REDACT; } // anything containing secret, such as 'client secret' - if (/secret/.test(key)) { + if (/secret/i.test(key)) { headers[key] = REDACT; } } @@ -394,9 +394,9 @@ export function defaultErrorRedactor(data: { const text = obj[key]; if ( - /grant_type=/.test(text) || - /assertion=/.test(text) || - /secret/.test(text) + /grant_type=/i.test(text) || + /assertion=/i.test(text) || + /secret/i.test(text) ) { obj[key] = REDACT; } diff --git a/test/test.getch.ts b/test/test.getch.ts index 67e4df5..02b8d3c 100644 --- a/test/test.getch.ts +++ b/test/test.getch.ts @@ -26,7 +26,7 @@ import { GaxiosResponse, GaxiosPromise, } from '../src'; -import {GAXIOS_ERROR_SYMBOL} from '../src/common'; +import {GAXIOS_ERROR_SYMBOL, Headers} from '../src/common'; import {pkg} from '../src/util'; import qs from 'querystring'; import fs from 'fs'; @@ -772,8 +772,11 @@ describe('🎏 data handling', () => { const config: GaxiosOptions = { headers: { - authentication: 'My Auth', - authorization: 'My Auth', + Authentication: 'My Auth', + /** + * Ensure casing is properly handled + */ + AUTHORIZATION: 'My Auth', 'content-type': 'application/x-www-form-urlencoded', random: 'data', }, @@ -821,8 +824,8 @@ describe('🎏 data handling', () => { assert(e.config.headers); assert.deepStrictEqual(e.config.headers, { ...config.headers, // non-redactables should be present - authentication: REDACT, - authorization: REDACT, + Authentication: REDACT, + AUTHORIZATION: REDACT, }); // config redactions - data @@ -847,11 +850,17 @@ describe('🎏 data handling', () => { // response redactions assert(e.response); assert.deepStrictEqual(e.response.config, e.config); - assert.deepStrictEqual(e.response.headers, { + + const expectedHeaders: Headers = { ...responseHeaders, // non-redactables should be present authentication: REDACT, authorization: REDACT, - }); + }; + + delete expectedHeaders['AUTHORIZATION']; + delete expectedHeaders['Authentication']; + + assert.deepStrictEqual(e.response.headers, expectedHeaders); assert.deepStrictEqual(e.response.data, { ...response, // non-redactables should be present assertion: REDACT,