1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-18 09:13:33 +00:00

SG-680 - Domain Name Custom Reactive forms validator refactor - swapped to regex to support proper domain format (which now enforces the requirement of a .com or similar)

This commit is contained in:
Jared Snider
2022-12-09 16:49:57 -05:00
parent cbd6e96aa4
commit a2aac79ce2

View File

@@ -8,16 +8,26 @@ export function domainNameValidator(errorMessage: string): ValidatorFn {
return null;
}
const forbiddenPatterns = [/^https:\/\//, /^http:\/\//, /^www\./];
for (const pattern of forbiddenPatterns) {
if (pattern.test(control.value)) {
return {
invalidDomainName: {
message: errorMessage,
},
};
}
// Domain labels (sections) are only allowed to be 63 chars in length max
// 1st and last chars cannot be hyphens per RFC 3696 (https://www.rfc-editor.org/rfc/rfc3696#section-2)
// /^[a-zA-Z0-9] # The domain name must start with a letter or a number
// [a-zA-Z0-9-]{1,61} # The domain name can have one to 61 characters that are letters, numbers, or hyphens
// [a-zA-Z0-9] # The domain name must end with a letter or a number
// \.[a-zA-Z]{2,}$/ # The domain name must have a period followed by at least two letters (the domain extension)
const domainNameRegex = /^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\.[a-zA-Z]{2,}$/;
const invalid = !domainNameRegex.test(control.value);
if (invalid) {
return {
invalidDomainName: {
message: errorMessage,
},
};
}
return null;
};
}