Bug fix: fixed handling of private domains by PublicSuffixMatcher

This commit is contained in:
Oleg Kalnichevski 2020-01-25 15:24:19 +01:00
parent 95dbbf099b
commit 3575cff3dc
3 changed files with 28 additions and 24 deletions

View File

@ -99,23 +99,15 @@ public final class PublicSuffixMatcher {
}
}
private static boolean hasEntry(final Map<String, DomainType> map, final String rule, final DomainType expectedType) {
private static DomainType findEntry(final Map<String, DomainType> map, final String rule) {
if (map == null) {
return false;
return null;
}
final DomainType domainType = map.get(rule);
if (domainType == null) {
return false;
}
return expectedType == null || domainType.equals(expectedType);
return map.get(rule);
}
private boolean hasRule(final String rule, final DomainType expectedType) {
return hasEntry(this.rules, rule, expectedType);
}
private boolean hasException(final String exception, final DomainType expectedType) {
return hasEntry(this.exceptions, exception, expectedType);
private static boolean match(final DomainType domainType, final DomainType expectedType) {
return domainType != null && (expectedType == null || domainType.equals(expectedType));
}
/**
@ -152,10 +144,15 @@ public final class PublicSuffixMatcher {
while (segment != null) {
// An exception rule takes priority over any other matching rule.
final String key = IDN.toUnicode(segment);
if (hasException(key, expectedType)) {
final DomainType exceptionRule = findEntry(exceptions, key);
if (match(exceptionRule, expectedType)) {
return segment;
}
if (hasRule(key, expectedType)) {
final DomainType domainRule = findEntry(rules, key);
if (match(domainRule, expectedType)) {
if (domainRule == DomainType.PRIVATE) {
return segment;
}
return result;
}
@ -163,7 +160,11 @@ public final class PublicSuffixMatcher {
final String nextSegment = nextdot != -1 ? segment.substring(nextdot + 1) : null;
if (nextSegment != null) {
if (hasRule("*." + IDN.toUnicode(nextSegment), expectedType)) {
final DomainType wildcardDomainRule = findEntry(rules, "*." + IDN.toUnicode(nextSegment));
if (match(wildcardDomainRule, expectedType)) {
if (wildcardDomainRule == DomainType.PRIVATE) {
return segment;
}
return result;
}
}

View File

@ -55,11 +55,11 @@ public class TestPublicSuffixMatcher {
@Test
public void testGetDomainRootAnyType() {
// Private
Assert.assertEquals("example.xx", matcher.getDomainRoot("example.XX"));
Assert.assertEquals("example.xx", matcher.getDomainRoot("www.example.XX"));
Assert.assertEquals("example.xx", matcher.getDomainRoot("www.blah.blah.example.XX"));
Assert.assertEquals("xx", matcher.getDomainRoot("example.XX"));
Assert.assertEquals("xx", matcher.getDomainRoot("www.example.XX"));
Assert.assertEquals("xx", matcher.getDomainRoot("www.blah.blah.example.XX"));
Assert.assertEquals("appspot.com", matcher.getDomainRoot("example.appspot.com"));
// Too short
Assert.assertEquals(null, matcher.getDomainRoot("xx"));
Assert.assertEquals(null, matcher.getDomainRoot("jp"));
Assert.assertEquals(null, matcher.getDomainRoot("ac.jp"));
Assert.assertEquals(null, matcher.getDomainRoot("any.tokyo.jp"));
@ -77,11 +77,11 @@ public class TestPublicSuffixMatcher {
@Test
public void testGetDomainRootOnlyPRIVATE() {
// Private
Assert.assertEquals("example.xx", matcher.getDomainRoot("example.XX", DomainType.PRIVATE));
Assert.assertEquals("example.xx", matcher.getDomainRoot("www.example.XX", DomainType.PRIVATE));
Assert.assertEquals("example.xx", matcher.getDomainRoot("www.blah.blah.example.XX", DomainType.PRIVATE));
Assert.assertEquals("xx", matcher.getDomainRoot("example.XX", DomainType.PRIVATE));
Assert.assertEquals("xx", matcher.getDomainRoot("www.example.XX", DomainType.PRIVATE));
Assert.assertEquals("xx", matcher.getDomainRoot("www.blah.blah.example.XX", DomainType.PRIVATE));
Assert.assertEquals("appspot.com", matcher.getDomainRoot("example.appspot.com"));
// Too short
Assert.assertEquals(null, matcher.getDomainRoot("xx", DomainType.PRIVATE));
Assert.assertEquals(null, matcher.getDomainRoot("jp", DomainType.PRIVATE));
Assert.assertEquals(null, matcher.getDomainRoot("ac.jp", DomainType.PRIVATE));
Assert.assertEquals(null, matcher.getDomainRoot("any.tokyo.jp", DomainType.PRIVATE));
@ -126,6 +126,8 @@ public class TestPublicSuffixMatcher {
Assert.assertTrue(matcher.matches(".any.tokyo.jp"));
// exception
Assert.assertFalse(matcher.matches(".metro.tokyo.jp"));
Assert.assertFalse(matcher.matches(".xx"));
Assert.assertFalse(matcher.matches(".appspot.com"));
}
@Test

View File

@ -26,6 +26,7 @@
// ===BEGIN PRIVATE DOMAINS===
xx
lan
appspot.com
// ===END PRIVATE DOMAINS===
// ===BEGIN ICANN DOMAINS===