Repository navigation
Match cookie domains without building a string - #2422
Open
pavel-ptashyts wants to merge 1 commit into
Open
pavel-ptashyts wants to merge 1 commit into
pavel-ptashyts wants to merge 1 commit into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Interceptors.exitAfterInterceptstores everySet-Cookieof every response on the event loop. For a response that did not arrive over TLS,ThreadSafeCookieStore.addrunsshadowsSecureCookie(rfc6265bis section 5.7 step 16) before storing the cookie. That check walks every domain bucket in the jar and callsdomainsMatchtwice per bucket, and each call built'.' + cookieDomainfor itsendsWithtest. So one plaintextSet-Cookiecost 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 overhttp.Modification
domainsMatchcompares the dotted suffix in place (isDotSuffix: a length check, onecharAt, thenregionMatches) instead of building the string.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
domainsMatchchanges. It is also the Domain attribute check inadd(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, andisSecure()is read from the caller's mutableCookieat 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. IteratingkeySet()instead ofentrySet()was measured as well and gave nothing beyond this change, so it is not included.Behaviour preserved
The existing
ThreadSafeCookieStoretests (SecureCookieSchemeTest,SecureCookieEvictionTest,PublicSuffixCookieTest,ThreadSafeCookieStoreGetTest,ThreadSafeCookieStoreExpiryTest,CookieStoreTest) pass unchanged. New inSecureCookieSchemeTest, 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 plaintextDomain=example.comcookie, which would cover the sibling, is refused.theOverlayCheckIgnoresBareSuffixes:notexample.com,www.notexample.com,example.coandxample.comdo not matchexample.com.anIpAddressOnlyOverlaysItself: a Secure cookie on198.51.100.1blocks a plaintext one from the same address only, not from100.1or51.100.1.aTrailingDotDoesNotEscapeTheOverlayCheck:example.com.hosts andDomain=example.com./Domain=.example.com.aLoopbackSecureCookieIsProtectedFromANonLoopbackSubdomain: a Secure cookie thathttp://localhostset (not over TLS) still blocks a plaintext cookie fromwww.localhost../mvnw clean verify -Dgpg.skip=trueon Corretto JDK 11.0.33 (Windows): BUILD SUCCESS, 2490 tests, 0 failures, 0 errors, 47 skipped, Revapi clean. (-Dgpg.skip=trueonly because there is no signing key on this machine.)Benchmark
New
CookieStorePlaintextAddBenchmark:addof 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.mainand this branch run the same benchmark class; formain, itsThreadSafeCookieStore.classcomes first on the classpath. Other builds ran on the host, so times are somewhat noisy; B/op is exact.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