Skip to content

Commit 0fe3eaa

Browse files
authored
fix: do not throw on parsing client principal (#160)
1 parent 1dc3918 commit 0fe3eaa

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

files/headers.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,14 @@ export function getClientPrincipalFromHeaders(headers) {
5454
return undefined;
5555
}
5656

57-
const encoded = Buffer.from(header, 'base64');
58-
const decoded = encoded.toString('ascii');
59-
const clientPrincipal = JSON.parse(decoded);
57+
try {
58+
const encoded = Buffer.from(header, 'base64');
59+
const decoded = encoded.toString('ascii');
60+
const clientPrincipal = JSON.parse(decoded);
6061

61-
return clientPrincipal;
62+
return clientPrincipal;
63+
} catch (e) {
64+
console.log('Unable to parse client principal:', e);
65+
return undefined;
66+
}
6267
}

test/headers.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,12 @@ describe('client principal parsing', () => {
118118
test('returns undefined when there is no client principal', () => {
119119
expect(getClientPrincipalFromHeaders(new Headers())).toBeUndefined();
120120
});
121+
122+
test('returns undefined if unable to parse', () => {
123+
const headers = new Headers({
124+
'x-ms-client-principal': 'boom'
125+
});
126+
127+
expect(getClientPrincipalFromHeaders(headers)).toBeUndefined();
128+
});
121129
});

0 commit comments

Comments
 (0)