Skip to content
Open
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 @@ -29,6 +29,7 @@
import org.apache.hc.client5.http.cookie.CommonCookieAttributeHandler;
import org.apache.hc.client5.http.cookie.Cookie;
import org.apache.hc.client5.http.cookie.CookieOrigin;
import org.apache.hc.client5.http.cookie.CookieRestrictionViolationException;
import org.apache.hc.client5.http.cookie.MalformedCookieException;
import org.apache.hc.client5.http.cookie.SetCookie;
import org.apache.hc.core5.annotation.Contract;
Expand Down Expand Up @@ -61,6 +62,18 @@ public void parse(final SetCookie cookie, final String value)
cookie.setSecure(true);
}

@Override
public void validate(final Cookie cookie, final CookieOrigin origin)
throws MalformedCookieException {
Args.notNull(cookie, "Cookie");
Args.notNull(origin, "Cookie origin");
// RFC 6265bis, 5.7: only a secure origin may set a cookie with the Secure attribute
if (cookie.isSecure() && !origin.isSecure()) {
throw new CookieRestrictionViolationException(
"Secure cookie \"" + cookie.getName() + "\" may not be set by an insecure origin");
}
}

@Override
public boolean match(final Cookie cookie, final CookieOrigin origin) {
Args.notNull(cookie, "Cookie");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import org.apache.hc.client5.http.cookie.Cookie;
import org.apache.hc.client5.http.cookie.CookieOrigin;
import org.apache.hc.client5.http.cookie.CookieRestrictionViolationException;
import org.apache.hc.client5.http.cookie.CookieSpec;
import org.apache.hc.client5.http.cookie.CookieStore;
import org.apache.hc.client5.http.cookie.MalformedCookieException;
Expand Down Expand Up @@ -121,6 +123,10 @@ private void processCookies(
for (final Cookie cookie : cookies) {
try {
cookieSpec.validate(cookie, cookieOrigin);
if (!cookieOrigin.isSecure() && overlaysSecureCookie(cookie, cookieStore)) {
throw new CookieRestrictionViolationException(
"Insecure origin may not overwrite secure cookie \"" + cookie.getName() + "\"");
}
cookieStore.addCookie(cookie);

if (LOG.isDebugEnabled()) {
Expand All @@ -140,6 +146,42 @@ private void processCookies(
}
}

/**
* RFC 6265bis, 5.7: a cookie received from an insecure origin is ignored if the store
* holds a secure cookie with the same name whose domain domain-matches the new cookie's
* domain (or vice versa) and whose path is path-matched by the new cookie's path.
*/
private static boolean overlaysSecureCookie(final Cookie cookie, final CookieStore cookieStore) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Hsnmohd We usually do not implement RFC requirements piecemeal, especially if they are still in draft. Does RFC 6265 have this requirement?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. RFC 6265 lets an insecure origin both set a Secure cookie and replace one (5.3 step 11 matches on name, domain and path only), and it calls that out as a known limitation in 4.1.2.5 and 8.6 (Weak Integrity). Both checks here only exist in 6265bis. #869 already has the same two checks as part of the broader 6265bis work, so this one overlaps it; fine to close it in favor of that.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. RFC 6265 lets an insecure origin both set a Secure cookie and replace one (5.3 step 11 matches on name, domain and path only), and it calls that out as a known limitation in 4.1.2.5 and 8.6 (Weak Integrity). Both checks here only exist in 6265bis. #869 already has the same two checks as part of the broader 6265bis work, so this one overlaps it; fine to close it in favor of that.

@Hsnmohd Yes, please close this PR in favor of #869.

for (final Cookie existing : cookieStore.getCookies()) {
if (existing.isSecure()
&& existing.getName().equals(cookie.getName())
&& (domainMatch(existing.getDomain(), cookie.getDomain())
|| domainMatch(cookie.getDomain(), existing.getDomain()))
&& pathMatch(cookie.getPath(), existing.getPath())) {
return true;
}
}
return false;
}

private static boolean domainMatch(final String host, final String domain) {
if (host == null || domain == null) {
return false;
}
final String h = host.toLowerCase(Locale.ROOT);
final String d = domain.toLowerCase(Locale.ROOT);
return h.equals(d) || h.endsWith("." + d);
}

private static boolean pathMatch(final String path, final String cookiePath) {
final String p = path != null ? path : "/";
final String c = cookiePath != null ? cookiePath : "/";
if (p.equals(c)) {
return true;
}
return p.startsWith(c) && (c.endsWith("/") || p.charAt(c.length()) == '/');
}

private static String formatCookie(final Cookie cookie) {
final StringBuilder buf = new StringBuilder();
buf.append(cookie.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,24 @@ void testBasicSecureMatch() {
Assertions.assertTrue(h.match(cookie, origin2));
}

@Test
void testBasicSecureValidate() throws Exception {
final BasicClientCookie cookie = new BasicClientCookie("name", "value");
final CookieAttributeHandler h = BasicSecureHandler.INSTANCE;

final CookieOrigin insecureOrigin = new CookieOrigin("somehost", 80, "/stuff", false);
cookie.setSecure(false);
h.validate(cookie, insecureOrigin);
cookie.setSecure(true);
Assertions.assertThrows(MalformedCookieException.class, () -> h.validate(cookie, insecureOrigin));

final CookieOrigin secureOrigin = new CookieOrigin("somehost", 443, "/stuff", true);
cookie.setSecure(false);
h.validate(cookie, secureOrigin);
cookie.setSecure(true);
h.validate(cookie, secureOrigin);
}

@Test
void testBasicSecureInvalidInput() {
final CookieAttributeHandler h = new BasicSecureHandler();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,60 @@ void testNoCookieStore() throws Exception {
Assertions.assertEquals(0, cookies.size());
}

@Test
void testInsecureOriginCannotSetSecureCookie() throws Exception {
final HttpResponse response = new BasicHttpResponse(200, "OK");
response.addHeader("Set-Cookie", "name1=value1; Secure");

final HttpClientContext context = HttpClientContext.create();
context.setCookieOrigin(this.cookieOrigin);
context.setCookieSpec(this.cookieSpec);
context.setCookieStore(this.cookieStore);

final HttpResponseInterceptor interceptor = ResponseProcessCookies.INSTANCE;
interceptor.process(response, null, context);

Assertions.assertEquals(0, this.cookieStore.getCookies().size());
}

@Test
void testInsecureOriginCannotOverwriteSecureCookie() throws Exception {
final HttpResponseInterceptor interceptor = ResponseProcessCookies.INSTANCE;

final HttpResponse secureResponse = new BasicHttpResponse(200, "OK");
secureResponse.addHeader("Set-Cookie", "session=secure-value; Secure; Path=/");
final HttpClientContext secureContext = HttpClientContext.create();
secureContext.setCookieOrigin(new CookieOrigin("www.example.com", 443, "/", true));
secureContext.setCookieSpec(this.cookieSpec);
secureContext.setCookieStore(this.cookieStore);
interceptor.process(secureResponse, null, secureContext);

final HttpResponse insecureResponse = new BasicHttpResponse(200, "OK");
insecureResponse.addHeader("Set-Cookie", "session=insecure-value; Path=/");
insecureResponse.addHeader("Set-Cookie", "session=insecure-value; Domain=example.com; Path=/sub");
insecureResponse.addHeader("Set-Cookie", "other=insecure-value; Path=/");
final HttpClientContext insecureContext = HttpClientContext.create();
insecureContext.setCookieOrigin(new CookieOrigin("www.example.com", 80, "/", false));
insecureContext.setCookieSpec(this.cookieSpec);
insecureContext.setCookieStore(this.cookieStore);
interceptor.process(insecureResponse, null, insecureContext);

final List<Cookie> cookies = this.cookieStore.getCookies();
Assertions.assertEquals(2, cookies.size());
for (final Cookie cookie : cookies) {
if (cookie.getName().equals("session")) {
Assertions.assertEquals("secure-value", cookie.getValue());
Assertions.assertTrue(cookie.isSecure());
} else {
Assertions.assertEquals("other", cookie.getName());
}
}

final HttpResponse secureUpdate = new BasicHttpResponse(200, "OK");
secureUpdate.addHeader("Set-Cookie", "session=new-secure-value; Secure; Path=/");
interceptor.process(secureUpdate, null, secureContext);
Assertions.assertTrue(this.cookieStore.getCookies().stream().anyMatch(c ->
c.getName().equals("session") && c.getValue().equals("new-secure-value")));
}

}
Loading