Skip to content

Match cookie domains without building a string - #2422

Open
pavel-ptashyts wants to merge 1 commit into
AsyncHttpClient:mainfrom
maygemdev:pavel-ptashyts/perf-set-cookie-scan
Open

pavel-ptashyts wants to merge 1 commit into
AsyncHttpClient:mainfrom
maygemdev:pavel-ptashyts/perf-set-cookie-scan

Conversation

@pavel-ptashyts

Copy link
Copy Markdown
Contributor

Motivation

Interceptors.exitAfterIntercept stores every Set-Cookie of every response on the event loop. For a response that did not arrive over TLS, ThreadSafeCookieStore.add runs shadowsSecureCookie (rfc6265bis section 5.7 step 16) before storing the cookie. That check walks every domain bucket in the jar and calls domainsMatch twice per bucket, and each call built '.' + cookieDomain for its endsWith test. So one plaintext Set-Cookie cost two string allocations per domain in the jar, about 79 bytes per domain. It matters for a jar holding many domains (a crawler, a multi-tenant client) that receives cookies over http.

Modification

  • domainsMatch compares the dotted suffix in place (isDotSuffix: a length check, one charAt, then regionMatches) instead of building the string.
  • The suffix test now runs before isIpAddressLiteral, so an unrelated bucket is rejected without scanning its host's last label. Both tests are pure, so the result is the same for every input.

Only domainsMatch changes. It is also the Domain attribute check in add (RFC 6265 section 5.3 step 6), which keeps the same result. get, the request-domain helpers and HTTP/2 paths are untouched. No public API change.

What I did not do

The scan itself stays. Parent domains of the new cookie could be found with map lookups, but stored subdomains of it cannot be enumerated without a scan or an index, so the walk over the jar is still needed. A secondary index (by cookie name, say) would have to stay exact under concurrent add, remove, eviction and clear, and isSecure() is read from the caller's mutable Cookie at check time. I did not find a way to show that an index never misses a Secure cookie under those conditions, and a miss would let a plaintext response overlay one. Iterating keySet() instead of entrySet() was measured as well and gave nothing beyond this change, so it is not included.

Behaviour preserved

The existing ThreadSafeCookieStore tests (SecureCookieSchemeTest, SecureCookieEvictionTest, PublicSuffixCookieTest, ThreadSafeCookieStoreGetTest, ThreadSafeCookieStoreExpiryTest, CookieStoreTest) pass unchanged. New in SecureCookieSchemeTest, pinning the domain test of the overlay check:

  • theOverlayCheckFindsParentAndChildDomainsAmongUnrelatedOnes: a Secure parent Domain cookie and a Secure grandchild host cookie are both found among 200 unrelated domains.
  • theOverlayCheckIgnoresASiblingHost: a plaintext host cookie on a sibling is stored, and a plaintext Domain=example.com cookie, which would cover the sibling, is refused.
  • theOverlayCheckIgnoresBareSuffixes: notexample.com, www.notexample.com, example.co and xample.com do not match example.com.
  • anIpAddressOnlyOverlaysItself: a Secure cookie on 198.51.100.1 blocks a plaintext one from the same address only, not from 100.1 or 51.100.1.
  • aTrailingDotDoesNotEscapeTheOverlayCheck: example.com. hosts and Domain=example.com. / Domain=.example.com.
  • aLoopbackSecureCookieIsProtectedFromANonLoopbackSubdomain: a Secure cookie that http://localhost set (not over TLS) still blocks a plaintext cookie from www.localhost.

./mvnw clean verify -Dgpg.skip=true on Corretto JDK 11.0.33 (Windows): BUILD SUCCESS, 2490 tests, 0 failures, 0 errors, 47 skipped, Revapi clean. (-Dgpg.skip=true only because there is no signing key on this machine.)

Benchmark

New CookieStorePlaintextAddBenchmark: add of a plaintext cookie that matches none of the stored domains, to a jar of 1, 100 and 10000 domains (one Secure cookie each). JMH 1.37, JDK 11.0.33, Windows, 1 fork, 5 x 1 s warmup, 5 x 1 s measurement, -prof gc. main and this branch run the same benchmark class; for main, its ThreadSafeCookieStore.class comes first on the classpath. Other builds ran on the host, so times are somewhat noisy; B/op is exact.

domains main ns/op branch ns/op main B/op branch B/op
1 122.5 +- 1.1 92.1 +- 2.5 272 200
100 3414 +- 35 1111 +- 9 7400 200
10000 464976 +- 6457 178663 +- 5227 792227 225

The allocation no longer depends on the jar's size; the remaining 200 bytes are the stored cookie itself. Time is about 2.6 to 3 times lower, but it still grows linearly with the number of domains, because the walk over the jar remains (see above).

Claude Code on behalf of Pavel Ptashyts

🤖 Generated with Claude Code

A Set-Cookie received over plaintext is checked against every domain
bucket in the jar before it is stored, so that it cannot overlay a
Secure cookie (rfc6265bis section 5.7 step 16). Each bucket ran
domainsMatch twice, and every call built '.' + cookieDomain for its
suffix test: two strings per domain on the event loop, about 79 bytes
per domain in the jar.

Compare the suffix in place with regionMatches instead, and test it
before the IP literal check so that an unrelated bucket is rejected
with a length check and one character. The result is the same for
every input, so the overlay check and the Domain attribute check keep
their exact semantics, including IP literals and loopback provenance.

Tests pin the overlay check for parent, child, sibling and bare-suffix
domains, IP literals, trailing-dot hosts and a non-loopback subdomain
of localhost. A JMH benchmark measures a plaintext add against jars of
1, 100 and 10000 domains.

Claude Code on behalf of Pavel Ptashyts

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant