Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -52,11 +55,23 @@ public class PublicSuffixDomainFilter implements CommonCookieAttributeHandler {

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(
Expand All @@ -65,19 +80,31 @@ public PublicSuffixDomainFilter(
Args.notNull(suffixList, "Public suffix list");
this.handler = handler;
this.publicSuffixMatcher = new PublicSuffixMatcher(suffixList.getRules(), suffixList.getExceptions());
this.localDomainMap = createLocalDomainMap();
}

/**
* Never matches if the cookie's domain is from the blacklist.
*/
@Override
public boolean match(final Cookie cookie, final CookieOrigin origin) {
final String domain = cookie.getDomain();
if (!domain.equalsIgnoreCase("localhost") && publicSuffixMatcher.matches(domain)) {
return false;
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;
}
}
} else {
return handler.match(cookie, origin);
if (!host.equalsIgnoreCase(origin.getHost())) {
if (this.publicSuffixMatcher.matches(host)) {
return false;
}
}
}
return handler.match(cookie, origin);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,32 @@ public void testParse() throws Exception {
Assert.assertTrue(filter.match(cookie, new CookieOrigin("apache.metro.tokyo.jp", 80, "/stuff", false)));
}

@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");
Expand Down