Skip to content

Commit

Permalink
Extend the regex pattern to allow top-level domains (TLDs) up to 63 c…
Browse files Browse the repository at this point in the history
…haracters to comply with RFC 1034 standard. (mongodb#1427)

JAVA-5490
  • Loading branch information
vbabanin authored Jun 28, 2024
1 parent 98934dd commit e4b8a83
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
public class DomainNameUtils {
private static final Pattern DOMAIN_PATTERN =
Pattern.compile("^(?=.{1,255}$)((([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,6}|localhost))$");
Pattern.compile("^(?=.{1,255}$)((([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,63}|localhost))$");

static boolean isDomainName(final String domainName) {
return DOMAIN_PATTERN.matcher(domainName).matches();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,25 @@ class DomainNameUtilsTest {
"123numbers.com",
"mixed-123domain.net",
"longdomainnameabcdefghijk.com",
"i-0123456789abcdef.ec2.internal",
"ip-10-24-34-0.ec2.internal",
"xn--frosch-6ya.com",
"xn--emoji-grinning-3s0b.org",
"xn--bcher-kva.ch",
"localhost",
"abcdefghijklmnopqrstuvwxyz0123456789-abcdefghijklmnopqrstuvwxyz.com",
"abcdefghijklmnopqrstuvwxyz0123456789-abcdefghijklmnopqrstuvwxyz.com", //63 characters label name.
"a.abcdefghijklmnopqrstuvwxyzabcdefghjklabcdefghijklmnopqrstuvwxyz", //63 characters TLD.
"xn--weihnachten-uzb.org",
"sub.domain.com.sub.domain.com.sub.domain.com.sub.domain.com.sub.domain.com.sub.domain.com.sub.domain."
+ "com.sub.domain.com.sub.domain.com.sub.domain.com.sub.domain.com.sub.domain.com.sub.domain.com.sub.domain.com.sub.domain."
+ "com.domain.com.sub.domain.subb.com" //255 characters
})
void shouldReturnTrueWithValidHostName(final String hostname) {
Assertions.assertTrue(isDomainName(hostname));
Assertions.assertTrue(isDomainName(hostname), hostname + " is not a valid domain name");
}

@ParameterizedTest
@ValueSource(strings = {
"xn--tst-0qa.example",
"xn--frosch-6ya.w23",
"-special_chars_$$.net",
"special_chars_$$.net",
Expand All @@ -60,7 +62,8 @@ void shouldReturnTrueWithValidHostName(final String hostname) {
"notlocalhost",
"домен.com", //NON-ASCII
"ẞẞ.com", //NON-ASCII
"abcdefghijklmnopqrstuvwxyz0123456789-abcdefghijklmnopqrstuvwxyzl.com",
"abcdefghijklmnopqrstuvwxyz0123456789-abcdefghijklmnopqrstuvwxyzl.com", //64 characters label name.
"a.abcdefghijklmnopqrstuvwxyzabcdefghjklabcdefghijklmnopqrstuvwxyza", //64 characters TLD.
"this-domain-is-really-long-because-it-just-keeps-going-and-going-and-its-still-not-done-yet-because-theres-more.net",
"verylongsubdomainnamethatisreallylongandmaycausetroubleforparsing.example",
"sub.domain.com.sub.domain.com.sub.domain.com.sub.domain.com.sub.domain.com.sub.domain.com.sub.domain."
Expand Down

0 comments on commit e4b8a83

Please sign in to comment.