Skip to content

Commit

Permalink
Improve security config validation (#1154)
Browse files Browse the repository at this point in the history
Initialize username and password with null to avoid them being `undefined`.
With `undefined` value it still works due to `CodecUtil.encodeNullable` logic
but it may be confusing.

Also, if `token` is not provided in token credentials throw instead of ignoring.
  • Loading branch information
srknzl authored Dec 29, 2021
1 parent baa3f35 commit 875c528
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/config/ConfigBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ export class ConfigBuilder {
}

private handleUsernamePasswordCredentials(jsonObject: any): void {
let username: string;
let password: string;
let username: string | null = null;
let password: string | null = null;
for (const key in jsonObject) {
const value = jsonObject[key];
if (key === 'username') {
Expand Down Expand Up @@ -168,7 +168,7 @@ export class ConfigBuilder {
}

if (token == null) {
return;
throw new RangeError('\'token\' option must be provided in token credentials.');
}

this.effectiveConfig.security.token = new TokenCredentialsImpl(token, encoding);
Expand Down
8 changes: 8 additions & 0 deletions test/unit/config/ConfigBuilderTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,14 @@ describe('ConfigBuilderValidationTest', function () {
}
}
},
// token field is mandatory
{
'security': {
'token': {
'encoding': TokenEncoding.ASCII
}
}
},
{
'security': {
'token': {
Expand Down

0 comments on commit 875c528

Please sign in to comment.