Skip to content

Commit ab4c1cb

Browse files
authored
Merge pull request #814 from watson-developer-cloud/handle-bad-chars-in-urls-creds
chore: throw error if user makes common mistake
2 parents 9360470 + 994169a commit ab4c1cb

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-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 curly bracket or quote as the first or last character
93+
// these are common user-issues that we should handle before they get a network error
94+
function badCharAtAnEnd(value: string): boolean {
95+
return value.startsWith('{') || value.startsWith('"') || value.endsWith('}') || value.endsWith('"');
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] && badCharAtAnEnd(obj[cred])) {
104+
errorMessage += `The ${cred} shouldn't start or end with curly brackets or quotes. Be sure to remove any {, }, or "`;
105+
}
106+
});
107+
108+
if (errorMessage.length) {
109+
errorMessage += 'Revise these credentials - they should not start or end with curly brackets or quotes.';
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: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,4 +335,86 @@ 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(
344+
'Revise these credentials - they should not start or end with curly brackets or quotes.'
345+
);
346+
}
347+
348+
it('should throw when username starts with {', function() {
349+
assertConstructorThrows({
350+
username: '{batman}',
351+
password: 'goodpass',
352+
});
353+
});
354+
355+
it('should throw when username starts with "', function() {
356+
assertConstructorThrows({
357+
username: '"<batman">',
358+
password: 'goodpass',
359+
});
360+
});
361+
362+
it('should throw when password starts with {', function() {
363+
assertConstructorThrows({
364+
username: 'batman',
365+
password: '{badpass}',
366+
});
367+
});
368+
369+
it('should throw when password starts with "', function() {
370+
assertConstructorThrows({
371+
username: 'batman',
372+
password: '"badpass"',
373+
});
374+
});
375+
376+
it('should throw when iam_apikey starts with {', function() {
377+
assertConstructorThrows({
378+
iam_apikey: '{abc123}',
379+
});
380+
});
381+
382+
it('should throw when iam_apikey starts with "', function() {
383+
assertConstructorThrows({
384+
iam_apikey: '"<abc123',
385+
});
386+
});
387+
388+
it('should throw when url starts with {', function() {
389+
assertConstructorThrows({
390+
username: 'batman',
391+
password: 'goodpass',
392+
url: '{watson-url}/some-api/v1/endpoint',
393+
});
394+
});
395+
396+
it('should throw when url ends with }', function() {
397+
assertConstructorThrows({
398+
username: 'batman',
399+
password: 'goodpass',
400+
url: 'watson-url.com/some-api/v1/endpoint}',
401+
});
402+
});
403+
404+
it('should throw when url starts with "', function() {
405+
assertConstructorThrows({
406+
username: 'batman',
407+
password: 'goodpass',
408+
url: '"watson-url.com/some-api/v1/endpoint',
409+
});
410+
});
411+
412+
it('should throw when mutiple creds are bad', function() {
413+
assertConstructorThrows({
414+
username: '{batman}',
415+
password: '"<badpass>"',
416+
url: '{watson-url}/some-api/v1/endpoint',
417+
});
418+
});
419+
});
338420
});

0 commit comments

Comments
 (0)