Skip to content

Commit

Permalink
Allow for domain name trailing dot in PublicSuffixDatabase (square#6111)
Browse files Browse the repository at this point in the history
  • Loading branch information
yschimke authored and squarejesse committed Jun 11, 2020
1 parent b086b8d commit 3a5f362
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ class PublicSuffixDatabase {
fun getEffectiveTldPlusOne(domain: String): String? {
// We use UTF-8 in the list so we need to convert to Unicode.
val unicodeDomain = IDN.toUnicode(domain)
val domainLabels = unicodeDomain.split('.')
val domainLabels = splitDomain(unicodeDomain)

val rule = findMatchingRule(domainLabels)
if (domainLabels.size == rule.size && rule[0][0] != EXCEPTION_MARKER) {
return null // The domain is a public suffix.
Expand All @@ -81,7 +82,18 @@ class PublicSuffixDatabase {
domainLabels.size - (rule.size + 1)
}

return domain.split('.').asSequence().drop(firstLabelOffset).joinToString(".")
return splitDomain(domain).asSequence().drop(firstLabelOffset).joinToString(".")
}

private fun splitDomain(domain: String): List<String> {
val domainLabels = domain.split('.')

if (domainLabels.last() == "") {
// allow for domain name trailing dot
return domainLabels.dropLast(1)
}

return domainLabels
}

private fun findMatchingRule(domainLabels: List<String>): List<String> {
Expand Down
15 changes: 15 additions & 0 deletions okhttp/src/test/java/okhttp3/HttpUrlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1756,6 +1756,21 @@ HttpUrl parse(String url) {
assertThat(parse("https://xn--4pvxs.jp").topPrivateDomain()).isNull();
assertThat(parse("https://localhost").topPrivateDomain()).isNull();
assertThat(parse("https://127.0.0.1").topPrivateDomain()).isNull();

// https://github.com/square/okhttp/issues/6109
assertThat(parse("http://a./").topPrivateDomain()).isNull();
assertThat(parse("http://./").topPrivateDomain()).isNull();

assertThat(parse("http://squareup.com./").topPrivateDomain()).isEqualTo("squareup.com");
}

@Test
public void unparseableTopPrivateDomain() {
assertInvalid("http://a../", "Invalid URL host: \"a..\"");
assertInvalid("http://..a/", "Invalid URL host: \"..a\"");
assertInvalid("http://a..b/", "Invalid URL host: \"a..b\"");
assertInvalid("http://.a/", "Invalid URL host: \".a\"");
assertInvalid("http://../", "Invalid URL host: \"..\"");
}

private void assertInvalid(String string, String exceptionMessage) {
Expand Down

0 comments on commit 3a5f362

Please sign in to comment.