diff --git a/client/src/jmh/java/org/asynchttpclient/bench/CookieStorePlaintextAddBenchmark.java b/client/src/jmh/java/org/asynchttpclient/bench/CookieStorePlaintextAddBenchmark.java new file mode 100644 index 000000000..2e687731e --- /dev/null +++ b/client/src/jmh/java/org/asynchttpclient/bench/CookieStorePlaintextAddBenchmark.java @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2026 AsyncHttpClient Project. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.asynchttpclient.bench; + +import io.netty.handler.codec.http.cookie.DefaultCookie; +import org.asynchttpclient.cookie.ThreadSafeCookieStore; +import org.asynchttpclient.uri.Uri; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Level; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Param; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; + +import java.util.concurrent.TimeUnit; + +/** + * {@code ThreadSafeCookieStore.add(Uri, Cookie)} for a Set-Cookie + * received over plaintext. Before storing it, the store checks every domain bucket in the jar for a + * Secure cookie the new one would overlay, so the cost grows with the number of domains. The new + * cookie's domain is related to none of them. + */ +@State(Scope.Thread) +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.NANOSECONDS) +public class CookieStorePlaintextAddBenchmark { + + private ThreadSafeCookieStore store; + private Uri requestUri; + private DefaultCookie cookie; + + @Param({"1", "100", "10000"}) + public int domains; + + @Setup(Level.Trial) + public void setup() { + store = new ThreadSafeCookieStore(); + for (int i = 0; i < domains; i++) { + DefaultCookie stored = new DefaultCookie("sid", "v" + i); + stored.setPath("/"); + stored.setSecure(true); + store.add(Uri.create("https://www.site" + i + ".com/"), stored); + } + requestUri = Uri.create("http://www.unrelated.org/login"); + cookie = new DefaultCookie("sid", "value"); + cookie.setPath("/"); + } + + @Benchmark + public ThreadSafeCookieStore addPlaintext() { + store.add(requestUri, cookie); + return store; + } +} diff --git a/client/src/main/java/org/asynchttpclient/cookie/ThreadSafeCookieStore.java b/client/src/main/java/org/asynchttpclient/cookie/ThreadSafeCookieStore.java index 9cb5a82e2..b6c0fd57c 100644 --- a/client/src/main/java/org/asynchttpclient/cookie/ThreadSafeCookieStore.java +++ b/client/src/main/java/org/asynchttpclient/cookie/ThreadSafeCookieStore.java @@ -218,7 +218,15 @@ private static boolean domainsMatch(String cookieDomain, String requestDomain) { } // RFC 6265 section 5.1.3: the suffix branch only applies to a host name, not an IP address. Otherwise // Domain=1 from 198.51.100.1 reaches every address ending in .1. - return !isIpAddressLiteral(requestDomain) && requestDomain.endsWith('.' + cookieDomain); + return isDotSuffix(cookieDomain, requestDomain) && !isIpAddressLiteral(requestDomain); + } + + // requestDomain.endsWith('.' + cookieDomain) without building that string: shadowsSecureCookie runs this + // for every domain in the jar. + private static boolean isDotSuffix(String cookieDomain, String requestDomain) { + int dot = requestDomain.length() - cookieDomain.length() - 1; + return dot >= 0 && requestDomain.charAt(dot) == '.' + && requestDomain.regionMatches(dot + 1, cookieDomain, 0, cookieDomain.length()); } /** diff --git a/client/src/test/java/org/asynchttpclient/cookie/SecureCookieSchemeTest.java b/client/src/test/java/org/asynchttpclient/cookie/SecureCookieSchemeTest.java index 98e3a8d17..a053aa9b0 100644 --- a/client/src/test/java/org/asynchttpclient/cookie/SecureCookieSchemeTest.java +++ b/client/src/test/java/org/asynchttpclient/cookie/SecureCookieSchemeTest.java @@ -102,6 +102,88 @@ public void theOverlayCheckMatchesDomainsInBothDirections() { "a plaintext host cookie under a Secure Domain cookie: " + hostUnderParent.getAll()); } + @Test + public void theOverlayCheckFindsParentAndChildDomainsAmongUnrelatedOnes() { + ThreadSafeCookieStore parent = storeWithUnrelatedDomains(); + set(parent, "https://example.com/", "SID=good; Secure; Domain=example.com; Path=/"); + set(parent, "http://a.b.example.com/", "SID=evil; Path=/"); + assertEquals(201, parent.getAll().size(), "a plaintext cookie under a Secure parent's domain"); + + ThreadSafeCookieStore child = storeWithUnrelatedDomains(); + set(child, "https://a.b.example.com/", "SID=good; Secure; Path=/"); + set(child, "http://www.example.com/", "SID=evil; Domain=example.com; Path=/"); + assertEquals(201, child.getAll().size(), "a plaintext Domain cookie over a Secure grandchild's host"); + } + + /** A sibling host does not domain-match, but a Domain cookie for the parent covers the sibling too. */ + @Test + public void theOverlayCheckIgnoresASiblingHost() { + ThreadSafeCookieStore store = new ThreadSafeCookieStore(); + set(store, "https://a.example.com/", "SID=good; Secure; Path=/"); + set(store, "http://b.example.com/", "SID=other; Path=/"); + assertEquals(2, store.getAll().size(), store.getAll().toString()); + + set(store, "http://b.example.com/", "SID=evil; Domain=example.com; Path=/"); + assertEquals(2, store.getAll().size(), store.getAll().toString()); + } + + /** Names that only end in the same characters do not domain-match. */ + @Test + public void theOverlayCheckIgnoresBareSuffixes() { + for (String secure : new String[]{"https://notexample.com/", "https://www.notexample.com/", + "https://example.co/", "https://xample.com/"}) { + ThreadSafeCookieStore store = new ThreadSafeCookieStore(); + set(store, secure, "SID=good; Secure; Path=/"); + set(store, "http://b.example.com/", "SID=other; Domain=example.com; Path=/"); + set(store, "http://www.example.com/", "SID=other; Path=/"); + assertEquals(3, store.getAll().size(), secure + ": " + store.getAll()); + } + } + + /** RFC 6265 section 5.1.3: an IP address domain-matches only itself. */ + @Test + public void anIpAddressOnlyOverlaysItself() { + ThreadSafeCookieStore store = new ThreadSafeCookieStore(); + set(store, "https://198.51.100.1/", "SID=good; Secure; Path=/"); + set(store, "http://100.1/", "SID=other; Path=/"); + set(store, "http://51.100.1/", "SID=other; Path=/"); + assertEquals(3, store.getAll().size(), "an address ending in another one: " + store.getAll()); + + set(store, "http://198.51.100.1/", "SID=evil; Path=/"); + assertEquals(3, store.getAll().size(), "the same address: " + store.getAll()); + assertEquals("good", sent(store, "https://198.51.100.1/", "SID")); + } + + @Test + public void aTrailingDotDoesNotEscapeTheOverlayCheck() { + ThreadSafeCookieStore store = new ThreadSafeCookieStore(); + set(store, "https://example.com./", "SID=good; Secure; Path=/"); + set(store, "http://example.com/", "SID=evil; Path=/"); + set(store, "http://www.example.com./", "SID=evil; Domain=example.com.; Path=/"); + set(store, "http://www.example.com./", "SID=evil; Domain=.example.com; Path=/"); + assertEquals(1, store.getAll().size(), store.getAll().toString()); + assertEquals("good", sent(store, "https://example.com/", "SID")); + } + + /** A Secure cookie a loopback server set over plaintext is still protected from a non-loopback subdomain. */ + @Test + public void aLoopbackSecureCookieIsProtectedFromANonLoopbackSubdomain() { + ThreadSafeCookieStore store = new ThreadSafeCookieStore(); + set(store, "http://localhost:8080/", "dev=good; Secure; Path=/"); + set(store, "http://www.localhost/", "dev=evil; Path=/"); + assertEquals(1, store.getAll().size(), store.getAll().toString()); + assertEquals("good", sent(store, "http://localhost:8080/", "dev")); + } + + private static ThreadSafeCookieStore storeWithUnrelatedDomains() { + ThreadSafeCookieStore store = new ThreadSafeCookieStore(); + for (int i = 0; i < 100; i++) { + set(store, "https://www.site" + i + ".com/", "SID=" + i + "; Secure; Path=/"); + set(store, "https://example" + i + ".com/", "SID=" + i + "; Secure; Path=/"); + } + return store; + } + @Test public void thePathOverlayRuleIsOneWay() { ThreadSafeCookieStore store = new ThreadSafeCookieStore();