Skip to content

Commit

Permalink
fix: do not throw on parsing client principal (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffrich authored Nov 20, 2023
1 parent 1dc3918 commit 0fe3eaa
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
13 changes: 9 additions & 4 deletions files/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
8 changes: 8 additions & 0 deletions test/headers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});

0 comments on commit 0fe3eaa

Please sign in to comment.