diff --git a/src/user/ServiceManager.ts b/src/user/ServiceManager.ts index 704ac94..0d983c7 100644 --- a/src/user/ServiceManager.ts +++ b/src/user/ServiceManager.ts @@ -274,38 +274,13 @@ class ServiceManager { return Promise.resolve() .then(function () { const rootDomain = self.dataStore.getRootDomain() - const dotRootDomain = `.${rootDomain}` - if (!customDomain || !/^[a-z0-9\-\.]+$/.test(customDomain)) { + try { + Utils.checkCustomDomain(customDomain, appName, rootDomain) + } catch (error) { throw ApiStatusCodes.createError( ApiStatusCodes.STATUS_ERROR_BAD_NAME, - 'Domain name is not accepted. Please use alphanumerical domains such as myapp.google123.ca' - ) - } - - if (customDomain.length > 80) { - throw ApiStatusCodes.createError( - ApiStatusCodes.STATUS_ERROR_BAD_NAME, - 'Domain name is not accepted. Please use alphanumerical domains less than 80 characters in length.' - ) - } - - if (customDomain.indexOf('..') >= 0) { - throw ApiStatusCodes.createError( - ApiStatusCodes.STATUS_ERROR_BAD_NAME, - 'Domain name is not accepted. You cannot have two consecutive periods ".." inside a domain name. Please use alphanumerical domains such as myapp.google123.ca' - ) - } - - if ( - customDomain.indexOf(dotRootDomain) >= 0 && - customDomain.indexOf(dotRootDomain) + - dotRootDomain.length === - customDomain.length - ) { - throw ApiStatusCodes.createError( - ApiStatusCodes.STATUS_ERROR_BAD_NAME, - 'Domain name is not accepted. Custom domain cannot be subdomain of root domain.' + error ) } }) diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index 381bbd7..7a23735 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -120,4 +120,34 @@ export default class Utils { return Promise.resolve() } + + static checkCustomDomain( + customDomain: string, + appName: string, + rootDomain: string + ) { + const dotRootDomain = `.${rootDomain}` + const dotAppDomain = `.${appName}${dotRootDomain}` + + if (!customDomain || !/^[a-z0-9\-\.]+$/.test(customDomain)) { + throw 'Domain name is not accepted. Please use alphanumerical domains such as myapp.google123.ca' + } + + if (customDomain.length > 80) { + throw 'Domain name is not accepted. Please use alphanumerical domains less than 80 characters in length.' + } + + if (customDomain.indexOf('..') >= 0) { + throw 'Domain name is not accepted. You cannot have two consecutive periods ".." inside a domain name. Please use alphanumerical domains such as myapp.google123.ca' + } + + if ( + customDomain.indexOf(dotAppDomain) === -1 && + customDomain.indexOf(dotRootDomain) >= 0 && + customDomain.indexOf(dotRootDomain) + dotRootDomain.length === + customDomain.length + ) { + throw 'Domain name is not accepted. Custom domain cannot be subdomain of root domain.' + } + } } diff --git a/tests/utils.test.ts b/tests/utils.test.ts index 505dcb6..6b0777b 100644 --- a/tests/utils.test.ts +++ b/tests/utils.test.ts @@ -83,3 +83,44 @@ test('Testing filter in place - remove 2nd', () => { expect(originalArray[0].val1).toBe('e-1-1') expect(originalArray[0].val2).toBe('e-2-1') }) + +test('Testing code to check custom domain', () => { + const rootDomain = 'google123.ca' + const appName = 'app1' + + expect(() => Utils.checkCustomDomain('', appName, rootDomain)).toThrow( + 'Domain name is not accepted. Please use alphanumerical domains such as myapp.google123.ca' + ) + + expect(() => + Utils.checkCustomDomain('x'.repeat(81), appName, rootDomain) + ).toThrow( + 'Domain name is not accepted. Please use alphanumerical domains less than 80 characters in length.' + ) + + expect(() => + Utils.checkCustomDomain('..google321.ca', appName, rootDomain) + ).toThrow( + 'Domain name is not accepted. You cannot have two consecutive periods ".." inside a domain name. Please use alphanumerical domains such as myapp.google123.ca' + ) + + expect(() => + Utils.checkCustomDomain(`app2.${rootDomain}`, appName, rootDomain) + ).toThrow( + 'Domain name is not accepted. Custom domain cannot be subdomain of root domain.' + ) + + expect(() => + Utils.checkCustomDomain( + `api.${appName}.${rootDomain}`, + appName, + rootDomain + ) + ).not.toThrow( + 'Domain name is not accepted. Custom domain cannot be subdomain of root domain.' + ) + + expect(() => + Utils.checkCustomDomain(`caprover123.ca`, appName, rootDomain) + ).not.toThrow() +})