Skip to content

Commit ce2bcca

Browse files
committed
chore: throw error if user puts { or " at the start of username, password, url, or iam_apikey
1 parent 69c71c0 commit ce2bcca

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

lib/base_service.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,30 @@ function usesBasicForIam(obj: any): boolean {
8989
return obj.username === 'apikey' && !obj.password.startsWith('icp-');
9090
}
9191

92+
// returns true if the string has a { or " as the first character
93+
// these are common user-issues that we should handle before they get a network error
94+
function badFirstChar(value: string): boolean {
95+
return value.startsWith('{') || value.startsWith('"');
96+
}
97+
98+
// checks credentials for common user mistakes of copying { or " characters from the documentation
99+
function checkCredentials(obj: any) {
100+
let errorMessage = '';
101+
const credsToCheck = ['url', 'username', 'password', 'iam_apikey'];
102+
credsToCheck.forEach(cred => {
103+
if (obj[cred] && badFirstChar(obj[cred])) {
104+
errorMessage += `${cred} starts with a bad character. `;
105+
}
106+
});
107+
108+
if (errorMessage.length) {
109+
errorMessage += 'Revise these credentials - they should not start with a { or "';
110+
return errorMessage;
111+
} else {
112+
return null;
113+
}
114+
}
115+
92116
export class BaseService {
93117
static URL: string;
94118
name: string;
@@ -309,6 +333,11 @@ export class BaseService {
309333
}
310334
}
311335
}
336+
// check credentials for common user errors
337+
const credentialProblems = checkCredentials(_options);
338+
if (credentialProblems) {
339+
throw new Error(credentialProblems);
340+
}
312341
return _options;
313342
}
314343
/**

test/unit/baseService.test.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,4 +335,76 @@ describe('BaseService', function() {
335335
});
336336
expect(instance._options.rejectUnauthorized).toBe(true);
337337
});
338+
339+
describe('check credentials for common problems', function() {
340+
function assertConstructorThrows(params) {
341+
expect(() => {
342+
new TestService(params);
343+
}).toThrowError('Revise these credentials - they should not start with a { or "');
344+
}
345+
346+
it('should throw when username starts with {', function() {
347+
assertConstructorThrows({
348+
username: '{batman}',
349+
password: 'goodpass',
350+
});
351+
});
352+
353+
it('should throw when username starts with "', function() {
354+
assertConstructorThrows({
355+
username: '"<batman">',
356+
password: 'goodpass',
357+
});
358+
});
359+
360+
it('should throw when password starts with {', function() {
361+
assertConstructorThrows({
362+
username: 'batman',
363+
password: '{badpass}',
364+
});
365+
});
366+
367+
it('should throw when password starts with "', function() {
368+
assertConstructorThrows({
369+
username: 'batman',
370+
password: '"badpass"',
371+
});
372+
});
373+
374+
it('should throw when iam_apikey starts with {', function() {
375+
assertConstructorThrows({
376+
iam_apikey: '{abc123}',
377+
});
378+
});
379+
380+
it('should throw when iam_apikey starts with "', function() {
381+
assertConstructorThrows({
382+
iam_apikey: '"<abc123',
383+
});
384+
});
385+
386+
it('should throw when url starts with {', function() {
387+
assertConstructorThrows({
388+
username: 'batman',
389+
password: 'goodpass',
390+
url: '{watson-url}/some-api/v1/endpoint',
391+
});
392+
});
393+
394+
it('should throw when url starts with "', function() {
395+
assertConstructorThrows({
396+
username: 'batman',
397+
password: 'goodpass',
398+
url: '"watson-url.com/some-api/v1/endpoint',
399+
});
400+
});
401+
402+
it('should throw when mutiple creds are bad', function() {
403+
assertConstructorThrows({
404+
username: '{batman}',
405+
password: '"<badpass>"',
406+
url: '{watson-url}/some-api/v1/endpoint',
407+
});
408+
});
409+
});
338410
});

0 commit comments

Comments
 (0)