You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2025/03/31 12:00:17 UTC

(httpcomponents-client) branch master updated: HTTPCLIENT-2365, regression: corrected handling of private domains by PublicSuffixMatcher

This is an automated email from the ASF dual-hosted git repository.

olegk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/httpcomponents-client.git


The following commit(s) were added to refs/heads/master by this push:
     new a06030afa HTTPCLIENT-2365, regression: corrected handling of private domains by PublicSuffixMatcher
a06030afa is described below

commit a06030afac030b83c2c9687b6314fbaeae323f96
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Sat Mar 29 15:51:50 2025 +0100

    HTTPCLIENT-2365, regression: corrected handling of private domains by PublicSuffixMatcher
---
 .../http/impl/cookie/PublicSuffixDomainFilter.java | 19 ++++-
 .../hc/client5/http/psl/PublicSuffixMatcher.java   | 67 ++++++++++++++--
 .../client5/http/ssl/DefaultHostnameVerifier.java  | 27 ++-----
 .../client5/http/psl/TestPublicSuffixMatcher.java  | 38 ++++++++--
 .../http/ssl/TestDefaultHostnameVerifier.java      | 88 ++++------------------
 .../src/test/resources/suffixlistmatcher.txt       | 11 ++-
 6 files changed, 138 insertions(+), 112 deletions(-)

diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/cookie/PublicSuffixDomainFilter.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/cookie/PublicSuffixDomainFilter.java
index 1ae0c25b0..bc2f79e6a 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/cookie/PublicSuffixDomainFilter.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/cookie/PublicSuffixDomainFilter.java
@@ -39,6 +39,8 @@
 import org.apache.hc.core5.annotation.Contract;
 import org.apache.hc.core5.annotation.ThreadingBehavior;
 import org.apache.hc.core5.util.Args;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Wraps a {@link org.apache.hc.client5.http.cookie.CookieAttributeHandler} and leverages
@@ -54,6 +56,8 @@
 @Contract(threading = ThreadingBehavior.STATELESS)
 public class PublicSuffixDomainFilter implements CommonCookieAttributeHandler {
 
+    private static final Logger LOG = LoggerFactory.getLogger(PublicSuffixDomainFilter.class);
+
     private final CommonCookieAttributeHandler handler;
     private final PublicSuffixMatcher publicSuffixMatcher;
     private final Map<String, Boolean> localDomainMap;
@@ -84,6 +88,17 @@ public PublicSuffixDomainFilter(
         this.localDomainMap = createLocalDomainMap();
     }
 
+    private boolean verifyHost(final String host) {
+        if (this.publicSuffixMatcher.verify(host)) {
+            return true;
+        } else {
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("Public Suffix List verification failed for host '{}'", host);
+            }
+            return false;
+        }
+    }
+
     /**
      * Never matches if the cookie's domain is from the blacklist.
      */
@@ -97,13 +112,13 @@ public boolean match(final Cookie cookie, final CookieOrigin origin) {
         if (i >= 0) {
             final String domain = host.substring(i);
             if (!this.localDomainMap.containsKey(domain)) {
-                if (this.publicSuffixMatcher.matches(host)) {
+                if (!verifyHost(host)) {
                     return false;
                 }
             }
         } else {
             if (!host.equalsIgnoreCase(origin.getHost())) {
-                if (this.publicSuffixMatcher.matches(host)) {
+                if (!verifyHost(host)) {
                     return false;
                 }
             }
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/psl/PublicSuffixMatcher.java b/httpclient5/src/main/java/org/apache/hc/client5/http/psl/PublicSuffixMatcher.java
index a78f29826..1a39fa808 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/psl/PublicSuffixMatcher.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/psl/PublicSuffixMatcher.java
@@ -137,6 +137,24 @@ public String getDomainRoot(final String domain, final DomainType expectedType)
         if (domain.startsWith(".")) {
             return null;
         }
+        final DomainRootInfo match = resolveDomainRoot(domain, expectedType);
+        return match != null ? match.root : null;
+    }
+
+    static final class DomainRootInfo {
+
+        final String root;
+        final String matchingKey;
+        final DomainType domainType;
+
+        DomainRootInfo(final String root, final String matchingKey, final DomainType domainType) {
+            this.root = root;
+            this.matchingKey = matchingKey;
+            this.domainType = domainType;
+        }
+    }
+
+    DomainRootInfo resolveDomainRoot(final String domain, final DomainType expectedType) {
         String segment = DnsUtils.normalize(domain);
         String result = null;
         while (segment != null) {
@@ -144,14 +162,11 @@ public String getDomainRoot(final String domain, final DomainType expectedType)
             final String key = IDN.toUnicode(segment);
             final DomainType exceptionRule = findEntry(exceptions, key);
             if (match(exceptionRule, expectedType)) {
-                return segment;
+                return new DomainRootInfo(segment, key, exceptionRule);
             }
             final DomainType domainRule = findEntry(rules, key);
             if (match(domainRule, expectedType)) {
-                // Prior to version 5.4 the result for "private" rules was different. However, the
-                // PSL algorithm doesn't have any rules changing the result based on "domain type"
-                // see https://github.com/publicsuffix/list/wiki/Format#formal-algorithm
-                return result;
+                return new DomainRootInfo(result, key, domainRule);
             }
 
             final int nextdot = segment.indexOf('.');
@@ -161,7 +176,7 @@ public String getDomainRoot(final String domain, final DomainType expectedType)
             final String wildcardKey = (nextSegment == null) ? "*" : "*." + IDN.toUnicode(nextSegment);
             final DomainType wildcardDomainRule = findEntry(rules, wildcardKey);
             if (match(wildcardDomainRule, expectedType)) {
-                return result;
+                return new DomainRootInfo(result, wildcardKey, wildcardDomainRule);
             }
 
             // If we're out of segments, and we're not looking for a specific type of entry,
@@ -169,7 +184,7 @@ public String getDomainRoot(final String domain, final DomainType expectedType)
             // This wildcard rule means any final segment in a domain is a public suffix,
             // so the current `result` is the desired public suffix plus 1
             if (nextSegment == null && (expectedType == null || expectedType == DomainType.UNKNOWN)) {
-                return result;
+                return new DomainRootInfo(result, null, null);
             }
 
             result = segment;
@@ -178,7 +193,7 @@ public String getDomainRoot(final String domain, final DomainType expectedType)
 
         // If no expectations then this result is good.
         if (expectedType == null || expectedType == DomainType.UNKNOWN) {
-            return result;
+            return new DomainRootInfo(result, null, null);
         }
 
         // If we did have expectations apparently there was no match
@@ -210,4 +225,40 @@ public boolean matches(final String domain, final DomainType expectedType) {
         return domainRoot == null;
     }
 
+    /**
+     * Verifies if the given domain does not represent a public domain root and is
+     * allowed to set cookies, have an identity represented by a certificate, etc.
+     * <p>
+     * This method tolerates a leading dot in the domain name, for example '.example.com'
+     * which will be automatically stripped.
+     * </p>
+     *
+     * @since 5.5
+     */
+     public boolean verify(final String domain) {
+         if (domain == null) {
+             return false;
+         }
+         return verifyStrict(domain.startsWith(".") ? domain.substring(1) : domain);
+     }
+
+    /**
+     * Verifies if the given domain does not represent a public domain root and is
+     * allowed to set cookies, have an identity represented by a certificate, etc.
+     *
+     * @since 5.5
+     */
+    public boolean verifyStrict(final String domain) {
+        Args.notNull(domain, "Domain");
+        if (domain.startsWith(".")) {
+            return false;
+        }
+        final DomainRootInfo domainRootInfo = resolveDomainRoot(domain, null);
+        if (domainRootInfo == null) {
+            return false;
+        }
+        return domainRootInfo.root != null ||
+                domainRootInfo.domainType == DomainType.PRIVATE && domainRootInfo.matchingKey != null;
+    }
+
 }
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/ssl/DefaultHostnameVerifier.java b/httpclient5/src/main/java/org/apache/hc/client5/http/ssl/DefaultHostnameVerifier.java
index e7f3d3785..3b0f0b3b3 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/ssl/DefaultHostnameVerifier.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/ssl/DefaultHostnameVerifier.java
@@ -44,7 +44,6 @@
 import javax.net.ssl.SSLSession;
 import javax.security.auth.x500.X500Principal;
 
-import org.apache.hc.client5.http.psl.DomainType;
 import org.apache.hc.client5.http.psl.PublicSuffixMatcher;
 import org.apache.hc.client5.http.utils.DnsUtils;
 import org.apache.hc.core5.annotation.Contract;
@@ -227,7 +226,6 @@ static boolean matchDomainRoot(final String host, final String domainRoot) {
 
     private static boolean matchIdentity(final String host, final String identity,
                                          final PublicSuffixMatcher publicSuffixMatcher,
-                                         final DomainType domainType,
                                          final boolean strict) {
 
         final String normalizedIdentity;
@@ -240,7 +238,10 @@ private static boolean matchIdentity(final String host, final String identity,
 
         // Public suffix check on the Unicode identity
         if (publicSuffixMatcher != null && host.contains(".")) {
-            if (publicSuffixMatcher.getDomainRoot(normalizedIdentity, domainType) == null) {
+            if (!publicSuffixMatcher.verifyStrict(normalizedIdentity)) {
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("Public Suffix List verification failed for identity '{}'", identity);
+                }
                 return false;
             }
         }
@@ -278,32 +279,20 @@ private static boolean matchIdentity(final String host, final String identity,
 
     static boolean matchIdentity(final String host, final String identity,
                                  final PublicSuffixMatcher publicSuffixMatcher) {
-        return matchIdentity(host, identity, publicSuffixMatcher, null, false);
+        return matchIdentity(host, identity, publicSuffixMatcher, false);
     }
 
     static boolean matchIdentity(final String host, final String identity) {
-        return matchIdentity(host, identity, null, null, false);
+        return matchIdentity(host, identity, null, false);
     }
 
     static boolean matchIdentityStrict(final String host, final String identity,
                                        final PublicSuffixMatcher publicSuffixMatcher) {
-        return matchIdentity(host, identity, publicSuffixMatcher, null, true);
+        return matchIdentity(host, identity, publicSuffixMatcher, true);
     }
 
     static boolean matchIdentityStrict(final String host, final String identity) {
-        return matchIdentity(host, identity, null, null, true);
-    }
-
-    static boolean matchIdentity(final String host, final String identity,
-                                 final PublicSuffixMatcher publicSuffixMatcher,
-                                 final DomainType domainType) {
-        return matchIdentity(host, identity, publicSuffixMatcher, domainType, false);
-    }
-
-    static boolean matchIdentityStrict(final String host, final String identity,
-                                       final PublicSuffixMatcher publicSuffixMatcher,
-                                       final DomainType domainType) {
-        return matchIdentity(host, identity, publicSuffixMatcher, domainType, true);
+        return matchIdentity(host, identity, null, true);
     }
 
     static String extractCN(final String subjectPrincipal) throws SSLException {
diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/psl/TestPublicSuffixMatcher.java b/httpclient5/src/test/java/org/apache/hc/client5/http/psl/TestPublicSuffixMatcher.java
index b2709d996..2dce2cbdd 100644
--- a/httpclient5/src/test/java/org/apache/hc/client5/http/psl/TestPublicSuffixMatcher.java
+++ b/httpclient5/src/test/java/org/apache/hc/client5/http/psl/TestPublicSuffixMatcher.java
@@ -43,12 +43,6 @@ class TestPublicSuffixMatcher {
     private PublicSuffixMatcher matcher;
     private PublicSuffixMatcher pslMatcher;
 
-    /**
-     * Create a matcher using the public suffix list file provided by publicsuffix.org (Mozilla).
-     *
-     * This test uses a copy of https://publicsuffix.org/list/effective_tld_names.dat in
-     * src/main/resources/org/publicsuffix/list/effective_tld_names.dat
-     */
     @BeforeEach
     void setUp() throws Exception {
         final ClassLoader classLoader = getClass().getClassLoader();
@@ -58,6 +52,7 @@ void setUp() throws Exception {
             final List<PublicSuffixList> lists = PublicSuffixListParser.INSTANCE.parseByType(new InputStreamReader(in, StandardCharsets.UTF_8));
             matcher = new PublicSuffixMatcher(lists);
         }
+        // Create a matcher using the public suffix list file provided by publicsuffix.org (Mozilla).
         pslMatcher = PublicSuffixMatcherLoader.getDefault();
     }
 
@@ -87,6 +82,8 @@ void testGetDomainRootAnyType() {
         Assertions.assertEquals("example.xx", matcher.getDomainRoot("www.blah.blah.example.XX"));
         Assertions.assertEquals(null, matcher.getDomainRoot("appspot.com"));
         Assertions.assertEquals("example.appspot.com", matcher.getDomainRoot("example.appspot.com"));
+        Assertions.assertEquals(null, matcher.getDomainRoot("s3.amazonaws.com"));
+        Assertions.assertEquals(null, matcher.getDomainRoot("blah.s3.amazonaws.com"));
         // Too short
         Assertions.assertNull(matcher.getDomainRoot("jp"));
         Assertions.assertNull(matcher.getDomainRoot("ac.jp"));
@@ -122,6 +119,8 @@ void testGetDomainRootOnlyPRIVATE() {
         Assertions.assertNull(matcher.getDomainRoot("garbage.garbage", DomainType.PRIVATE));
         Assertions.assertNull(matcher.getDomainRoot("*.garbage.garbage", DomainType.PRIVATE));
         Assertions.assertNull(matcher.getDomainRoot("*.garbage.garbage.garbage", DomainType.PRIVATE));
+        Assertions.assertNull(matcher.getDomainRoot("s3.amazonaws.com"));
+        Assertions.assertNull(matcher.getDomainRoot("blah.s3.amazonaws.com"));
     }
 
     @Test
@@ -146,6 +145,33 @@ void testGetDomainRootOnlyICANN() {
         Assertions.assertNull(matcher.getDomainRoot("*.garbage.garbage.garbage", DomainType.ICANN));
     }
 
+    @Test
+    void testMaySetCookies() {
+        Assertions.assertTrue(matcher.verify("foo.com"));
+
+        Assertions.assertFalse(matcher.verify("bar.foo.com"));
+        Assertions.assertTrue(matcher.verify("example.bar.foo.com"));
+
+        Assertions.assertTrue(matcher.verify("foo.bar.jp"));
+        Assertions.assertFalse(matcher.verify("bar.jp"));
+
+        Assertions.assertTrue(matcher.verify("foo.bar.hokkaido.jp"));
+        Assertions.assertFalse(matcher.verify("bar.hokkaido.jp"));
+
+        Assertions.assertTrue(matcher.verify("foo.bar.tokyo.jp"));
+        Assertions.assertFalse(matcher.verify("bar.tokyo.jp"));
+
+        Assertions.assertTrue(matcher.verify("pref.hokkaido.jp")); // exception from a wildcard rule
+        Assertions.assertTrue(matcher.verify("metro.tokyo.jp")); // exception from a wildcard rule
+    }
+
+    @Test
+    void testVerifyPrivate() {
+        Assertions.assertTrue(matcher.verify("s3.amazonaws.com"));
+        Assertions.assertTrue(matcher.verify("blah.s3.amazonaws.com"));
+        Assertions.assertTrue(matcher.verify("blah.xxx.uk"));
+    }
+
     @Test
     void testMatch() {
         Assertions.assertTrue(matcher.matches(".jp"));
diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/ssl/TestDefaultHostnameVerifier.java b/httpclient5/src/test/java/org/apache/hc/client5/http/ssl/TestDefaultHostnameVerifier.java
index 38da6e2c7..f4b8f83ad 100644
--- a/httpclient5/src/test/java/org/apache/hc/client5/http/ssl/TestDefaultHostnameVerifier.java
+++ b/httpclient5/src/test/java/org/apache/hc/client5/http/ssl/TestDefaultHostnameVerifier.java
@@ -56,7 +56,6 @@
 
 import javax.net.ssl.SSLException;
 
-import org.apache.hc.client5.http.psl.DomainType;
 import org.apache.hc.client5.http.psl.PublicSuffixList;
 import org.apache.hc.client5.http.psl.PublicSuffixListParser;
 import org.apache.hc.client5.http.psl.PublicSuffixMatcher;
@@ -289,11 +288,11 @@ void testIdentityMatching() {
         Assertions.assertTrue(DefaultHostnameVerifier.matchIdentity("a.b.xxx.uk", "*.b.xxx.uk", publicSuffixMatcher));
         Assertions.assertTrue(DefaultHostnameVerifier.matchIdentityStrict("a.b.xxx.uk", "*.b.xxx.uk", publicSuffixMatcher));
 
-        Assertions.assertFalse(DefaultHostnameVerifier.matchIdentity("b.xxx.uk", "b.xxx.uk", publicSuffixMatcher));
-        Assertions.assertFalse(DefaultHostnameVerifier.matchIdentityStrict("b.xxx.uk", "b.xxx.uk", publicSuffixMatcher));
+        Assertions.assertTrue(DefaultHostnameVerifier.matchIdentity("b.xxx.uk", "b.xxx.uk", publicSuffixMatcher));
+        Assertions.assertTrue(DefaultHostnameVerifier.matchIdentityStrict("b.xxx.uk", "b.xxx.uk", publicSuffixMatcher));
 
-        Assertions.assertFalse(DefaultHostnameVerifier.matchIdentity("b.xxx.uk", "*.xxx.uk", publicSuffixMatcher));
-        Assertions.assertFalse(DefaultHostnameVerifier.matchIdentityStrict("b.xxx.uk", "*.xxx.uk", publicSuffixMatcher));
+        Assertions.assertTrue(DefaultHostnameVerifier.matchIdentity("b.xxx.uk", "*.xxx.uk", publicSuffixMatcher));
+        Assertions.assertTrue(DefaultHostnameVerifier.matchIdentityStrict("b.xxx.uk", "*.xxx.uk", publicSuffixMatcher));
     }
 
     @Test
@@ -312,7 +311,7 @@ void testHTTPCLIENT_1255() {
     }
 
     @Test
-    void testHTTPCLIENT_1997_ANY() { // Only True on all domains
+    void testHTTPCLIENT_1997() {
         String domain;
         // Unknown
         domain = "dev.b.cloud.a";
@@ -336,65 +335,7 @@ void testHTTPCLIENT_1997_ANY() { // Only True on all domains
         Assertions.assertTrue(DefaultHostnameVerifier.matchIdentityStrict("service.apps." + domain, "*.apps." + domain, publicSuffixMatcher));
     }
 
-    @Test
-    void testHTTPCLIENT_1997_ICANN() { // Only True on ICANN domains
-        String domain;
-        // Unknown
-        domain = "dev.b.cloud.a";
-        Assertions.assertFalse(DefaultHostnameVerifier.matchIdentity("service.apps." + domain, "*.apps." + domain, publicSuffixMatcher, DomainType.ICANN));
-        Assertions.assertFalse(DefaultHostnameVerifier.matchIdentityStrict("service.apps." + domain, "*.apps." + domain, publicSuffixMatcher, DomainType.ICANN));
-
-        // ICANN
-        domain = "dev.b.cloud.com";
-        Assertions.assertTrue(DefaultHostnameVerifier.matchIdentity("service.apps." + domain, "*.apps." + domain, publicSuffixMatcher, DomainType.ICANN));
-        Assertions.assertTrue(DefaultHostnameVerifier.matchIdentityStrict("service.apps." + domain, "*.apps." + domain, publicSuffixMatcher, DomainType.ICANN));
-
-        // PRIVATE
-        domain = "dev.b.cloud.lan";
-        Assertions.assertFalse(DefaultHostnameVerifier.matchIdentity("service.apps." + domain, "*.apps." + domain, publicSuffixMatcher, DomainType.ICANN));
-        Assertions.assertFalse(DefaultHostnameVerifier.matchIdentityStrict("service.apps." + domain, "*.apps." + domain, publicSuffixMatcher, DomainType.ICANN));
-    }
-
-    @Test
-    void testHTTPCLIENT_1997_PRIVATE() { // Only True on PRIVATE domains
-        String domain;
-        // Unknown
-        domain = "dev.b.cloud.a";
-        Assertions.assertFalse(DefaultHostnameVerifier.matchIdentity("service.apps." + domain, "*.apps." + domain, publicSuffixMatcher, DomainType.PRIVATE));
-        Assertions.assertFalse(DefaultHostnameVerifier.matchIdentityStrict("service.apps." + domain, "*.apps." + domain, publicSuffixMatcher, DomainType.PRIVATE));
-
-        // ICANN
-        domain = "dev.b.cloud.com";
-        Assertions.assertFalse(DefaultHostnameVerifier.matchIdentity("service.apps." + domain, "*.apps." + domain, publicSuffixMatcher, DomainType.PRIVATE));
-        Assertions.assertFalse(DefaultHostnameVerifier.matchIdentityStrict("service.apps." + domain, "*.apps." + domain, publicSuffixMatcher, DomainType.PRIVATE));
-
-        // PRIVATE
-        domain = "dev.b.cloud.lan";
-        Assertions.assertTrue(DefaultHostnameVerifier.matchIdentity("service.apps." + domain, "*.apps." + domain, publicSuffixMatcher, DomainType.PRIVATE));
-        Assertions.assertTrue(DefaultHostnameVerifier.matchIdentityStrict("service.apps." + domain, "*.apps." + domain, publicSuffixMatcher, DomainType.PRIVATE));
-    }
-
-    @Test
-    void testHTTPCLIENT_1997_UNKNOWN() { // Only True on all domains (same as ANY)
-        String domain;
-        // Unknown
-        domain = "dev.b.cloud.a";
-        Assertions.assertTrue(DefaultHostnameVerifier.matchIdentity("service.apps." + domain, "*.apps." + domain, publicSuffixMatcher, DomainType.UNKNOWN));
-        Assertions.assertTrue(DefaultHostnameVerifier.matchIdentityStrict("service.apps." + domain, "*.apps." + domain, publicSuffixMatcher, DomainType.UNKNOWN));
-
-        // ICANN
-        domain = "dev.b.cloud.com";
-        Assertions.assertTrue(DefaultHostnameVerifier.matchIdentity("service.apps." + domain, "*.apps." + domain, publicSuffixMatcher, DomainType.UNKNOWN));
-        Assertions.assertTrue(DefaultHostnameVerifier.matchIdentityStrict("service.apps." + domain, "*.apps." + domain, publicSuffixMatcher, DomainType.UNKNOWN));
-
-        // PRIVATE
-        domain = "dev.b.cloud.lan";
-        Assertions.assertTrue(DefaultHostnameVerifier.matchIdentity("service.apps." + domain, "*.apps." + domain, publicSuffixMatcher, DomainType.UNKNOWN));
-        Assertions.assertTrue(DefaultHostnameVerifier.matchIdentityStrict("service.apps." + domain, "*.apps." + domain, publicSuffixMatcher, DomainType.UNKNOWN));
-    }
-
-    // Check compressed IPv6 hostname matching
-    @Test
+    @Test // Check compressed IPv6 hostname matching
     void testHTTPCLIENT_1316() throws Exception {
         final String host1 = "2001:0db8:aaaa:bbbb:cccc:0:0:0001";
         DefaultHostnameVerifier.matchIPv6Address(host1, Collections.singletonList(SubjectName.IP("2001:0db8:aaaa:bbbb:cccc:0:0:0001")));
@@ -477,15 +418,14 @@ void testMatchDNSName() throws Exception {
                 Collections.singletonList(SubjectName.DNS("*.ec2.compute-1.amazonaws.com")),
                 publicSuffixMatcher);
         Assertions.assertThrows(SSLException.class, () ->
-                DefaultHostnameVerifier.matchDNSName(
-                        "ec2.compute-1.amazonaws.com",
-                        Collections.singletonList(SubjectName.DNS("ec2.compute-1.amazonaws.com")),
-                        publicSuffixMatcher));
-        Assertions.assertThrows(SSLException.class, () ->
-                DefaultHostnameVerifier.matchDNSName(
-                        "ec2.compute-1.amazonaws.com",
-                        Collections.singletonList(SubjectName.DNS("*.compute-1.amazonaws.com")),
-                        publicSuffixMatcher));
+            DefaultHostnameVerifier.matchDNSName(
+                    "compute-1.amazonaws.com",
+                    Collections.singletonList(SubjectName.DNS("*.compute-1.amazonaws.com")),
+                    publicSuffixMatcher));
+        DefaultHostnameVerifier.matchDNSName(
+                "ec2.compute-1.amazonaws.com",
+                Collections.singletonList(SubjectName.DNS("*.compute-1.amazonaws.com")),
+                publicSuffixMatcher);
     }
 
     @Test
diff --git a/httpclient5/src/test/resources/suffixlistmatcher.txt b/httpclient5/src/test/resources/suffixlistmatcher.txt
index decd10794..3dcbbf299 100644
--- a/httpclient5/src/test/resources/suffixlistmatcher.txt
+++ b/httpclient5/src/test/resources/suffixlistmatcher.txt
@@ -27,6 +27,8 @@
 xx
 lan
 appspot.com
+s3.amazonaws.com
+*.s3.amazonaws.com
 s3.eu-central-1.amazonaws.com
 *.compute.amazonaws.com
 *.compute-1.amazonaws.com
@@ -38,15 +40,18 @@ us-east-1.amazonaws.com
 
 // ===BEGIN ICANN DOMAINS===
 
-jp
+com
+*.foo.com
 *.jp
+*.hokkaido.jp
 *.tokyo.jp
+!pref.hokkaido.jp
 !metro.tokyo.jp
+// Hosts in .hokkaido.jp can't set cookies below level 4...
+// ...except hosts in pref.hokkaido.jp, which can set cookies at level 3.
 
-com
 co.jp
 gov.uk
-*.foo.com
 
 // unicode
 no