From d4bf46b400d2e098ef2a438c87579dd3a48e77e9 Mon Sep 17 00:00:00 2001 From: Geoff Rich <4992896+geoffrich@users.noreply.github.com> Date: Sun, 19 Nov 2023 16:00:27 -0800 Subject: [PATCH] fix: do not throw on parsing client principal --- files/headers.js | 13 +++++++++---- test/headers.test.js | 8 ++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/files/headers.js b/files/headers.js index df2a7f9..b606ab6 100644 --- a/files/headers.js +++ b/files/headers.js @@ -54,9 +54,14 @@ export function getClientPrincipalFromHeaders(headers) { return undefined; } - const encoded = Buffer.from(header, 'base64'); - const decoded = encoded.toString('ascii'); - const clientPrincipal = JSON.parse(decoded); + try { + const encoded = Buffer.from(header, 'base64'); + const decoded = encoded.toString('ascii'); + const clientPrincipal = JSON.parse(decoded); - return clientPrincipal; + return clientPrincipal; + } catch (e) { + console.log('Unable to parse client principal:', e); + return undefined; + } } diff --git a/test/headers.test.js b/test/headers.test.js index e63c5f0..37145bc 100644 --- a/test/headers.test.js +++ b/test/headers.test.js @@ -118,4 +118,12 @@ describe('client principal parsing', () => { test('returns undefined when there is no client principal', () => { expect(getClientPrincipalFromHeaders(new Headers())).toBeUndefined(); }); + + test('returns undefined if unable to parse', () => { + const headers = new Headers({ + 'x-ms-client-principal': 'boom' + }); + + expect(getClientPrincipalFromHeaders(headers)).toBeUndefined(); + }); });