mirror of
https://github.com/caprover/caprover
synced 2025-10-30 01:57:03 +00:00
fix: add app's subdomain & add test case
This commit is contained in:
@@ -274,39 +274,13 @@ class ServiceManager {
|
||||
return Promise.resolve()
|
||||
.then(function () {
|
||||
const rootDomain = self.dataStore.getRootDomain()
|
||||
const dotRootDomain = `.${rootDomain}`
|
||||
const appDomain = appName + dotRootDomain
|
||||
|
||||
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 ===
|
||||
appDomain.length
|
||||
) {
|
||||
throw ApiStatusCodes.createError(
|
||||
ApiStatusCodes.STATUS_ERROR_BAD_NAME,
|
||||
'Domain name is not accepted. Custom domain cannot be subdomain of root domain.'
|
||||
error
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -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.'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user