Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not throw on parsing client principal #160

Merged
merged 1 commit into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});
Loading