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 2015/10/03 15:07:21 UTC

svn commit: r1706576 - in /httpcomponents/httpclient/branches/4.5.x/httpclient/src: main/java/org/apache/http/impl/cookie/PublicSuffixDomainFilter.java test/java/org/apache/http/impl/cookie/TestPublicSuffixListParser.java

Author: olegk
Date: Sat Oct  3 13:07:21 2015
New Revision: 1706576

URL: http://svn.apache.org/viewvc?rev=1706576&view=rev
Log:
HTTPCLIENT-1685: PublicSuffixDomainFilter to ignore local hosts and local domains

Modified:
    httpcomponents/httpclient/branches/4.5.x/httpclient/src/main/java/org/apache/http/impl/cookie/PublicSuffixDomainFilter.java
    httpcomponents/httpclient/branches/4.5.x/httpclient/src/test/java/org/apache/http/impl/cookie/TestPublicSuffixListParser.java

Modified: httpcomponents/httpclient/branches/4.5.x/httpclient/src/main/java/org/apache/http/impl/cookie/PublicSuffixDomainFilter.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.5.x/httpclient/src/main/java/org/apache/http/impl/cookie/PublicSuffixDomainFilter.java?rev=1706576&r1=1706575&r2=1706576&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.5.x/httpclient/src/main/java/org/apache/http/impl/cookie/PublicSuffixDomainFilter.java (original)
+++ httpcomponents/httpclient/branches/4.5.x/httpclient/src/main/java/org/apache/http/impl/cookie/PublicSuffixDomainFilter.java Sat Oct  3 13:07:21 2015
@@ -26,6 +26,9 @@
  */
 package org.apache.http.impl.cookie;
 
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
 import org.apache.http.annotation.Immutable;
 import org.apache.http.conn.util.PublicSuffixList;
 import org.apache.http.conn.util.PublicSuffixMatcher;
@@ -52,11 +55,23 @@ public class PublicSuffixDomainFilter im
 
     private final CommonCookieAttributeHandler handler;
     private final PublicSuffixMatcher publicSuffixMatcher;
+    private final Map<String, Boolean> localDomainMap;
+
+    private static Map<String, Boolean> createLocalDomainMap() {
+        final ConcurrentHashMap<String, Boolean> map = new ConcurrentHashMap<String, Boolean>();
+        map.put(".localhost.", Boolean.TRUE);  // RFC 6761
+        map.put(".test.", Boolean.TRUE);       // RFC 6761
+        map.put(".local.", Boolean.TRUE);      // RFC 6762
+        map.put(".local", Boolean.TRUE);
+        map.put(".localdomain", Boolean.TRUE);
+        return map;
+    }
 
     public PublicSuffixDomainFilter(
             final CommonCookieAttributeHandler handler, final PublicSuffixMatcher publicSuffixMatcher) {
         this.handler = Args.notNull(handler, "Cookie handler");
         this.publicSuffixMatcher = Args.notNull(publicSuffixMatcher, "Public suffix matcher");
+        this.localDomainMap = createLocalDomainMap();
     }
 
     public PublicSuffixDomainFilter(
@@ -65,6 +80,7 @@ public class PublicSuffixDomainFilter im
         Args.notNull(suffixList, "Public suffix list");
         this.handler = handler;
         this.publicSuffixMatcher = new PublicSuffixMatcher(suffixList.getRules(), suffixList.getExceptions());
+        this.localDomainMap = createLocalDomainMap();
     }
 
     /**
@@ -72,12 +88,17 @@ public class PublicSuffixDomainFilter im
      */
     @Override
     public boolean match(final Cookie cookie, final CookieOrigin origin) {
-        final String domain = cookie.getDomain();
-        if (!domain.equalsIgnoreCase("localhost") && publicSuffixMatcher.matches(domain)) {
-            return false;
-        } else {
-            return handler.match(cookie, origin);
+        final String host = cookie.getDomain();
+        final int i = host.indexOf('.');
+        if (i >= 0) {
+            final String domain = host.substring(i);
+            if (!this.localDomainMap.containsKey(domain)) {
+                if (this.publicSuffixMatcher.matches(host)) {
+                    return false;
+                }
+            }
         }
+        return handler.match(cookie, origin);
     }
 
     @Override

Modified: httpcomponents/httpclient/branches/4.5.x/httpclient/src/test/java/org/apache/http/impl/cookie/TestPublicSuffixListParser.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.5.x/httpclient/src/test/java/org/apache/http/impl/cookie/TestPublicSuffixListParser.java?rev=1706576&r1=1706575&r2=1706576&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.5.x/httpclient/src/test/java/org/apache/http/impl/cookie/TestPublicSuffixListParser.java (original)
+++ httpcomponents/httpclient/branches/4.5.x/httpclient/src/test/java/org/apache/http/impl/cookie/TestPublicSuffixListParser.java Sat Oct  3 13:07:21 2015
@@ -79,6 +79,32 @@ public class TestPublicSuffixListParser
     }
 
     @Test
+    public void testParseLocal() throws Exception {
+        final BasicClientCookie cookie = new BasicClientCookie("name", "value");
+
+        cookie.setDomain("localhost");
+        Assert.assertTrue(filter.match(cookie, new CookieOrigin("localhost", 80, "/stuff", false)));
+
+        cookie.setDomain("somehost");
+        Assert.assertTrue(filter.match(cookie, new CookieOrigin("somehost", 80, "/stuff", false)));
+
+        cookie.setDomain(".localdomain");
+        Assert.assertTrue(filter.match(cookie, new CookieOrigin("somehost.localdomain", 80, "/stuff", false)));
+
+        cookie.setDomain(".local.");
+        Assert.assertTrue(filter.match(cookie, new CookieOrigin("somehost.local.", 80, "/stuff", false)));
+
+        cookie.setDomain(".localhost.");
+        Assert.assertTrue(filter.match(cookie, new CookieOrigin("somehost.localhost.", 80, "/stuff", false)));
+
+        cookie.setDomain(".local");
+        Assert.assertTrue(filter.match(cookie, new CookieOrigin("somehost.local", 80, "/stuff", false)));
+
+        cookie.setDomain(".blah");
+        Assert.assertFalse(filter.match(cookie, new CookieOrigin("somehost.blah", 80, "/stuff", false)));
+    }
+
+    @Test
     public void testUnicode() throws Exception {
         final BasicClientCookie cookie = new BasicClientCookie("name", "value");