Repository navigation
Don't store the metadata of a new client whose client cookie wasn't stored - #42
Merged
Merged
Conversation
…tored A new client's metadata was stored even when the browser never got its client id: when a PreStoreCookieEvent listener vetoed the cookie, or when an exception ended the request without a response. No later request could recognize that client. ClientCookieJar now remembers the ids of each request's new clients (CookieBasedClientContext tells it) and whether it wrote the request's client cookie, in WeakMaps keyed by the request. StoreMetadataSubscriber skips store() for a new client whose cookie wasn't written. A returning client, and a client that a decorator of ClientContextInterface recognizes, are stored as before. Closes #18
The ids of a request's new clients are a fact about the request that StoreMetadataSubscriber reads, so they go in a request attribute, as Symfony does with _route and _stateless, instead of a WeakMap in ClientCookieJar, which doesn't create them. CookieBasedClientContext adds each id and reads them back with a static @internal isNewClient(), so the subscriber doesn't inject the context, which an app may decorate (ADR-0004).
Like Symfony's session listener stores the session, one listener, StoreClientSubscriber, now stores the client cookie and then the metadata of the current client on kernel.response, with priority -1000. It replaces StoreCookieSubscriber and StoreMetadataSubscriber. Whether the cookie was stored is a branch in that listener, so ClientCookieJar no longer tracks it, and a request that ends without a response stores nothing. CookieBasedClientContext lists the ids of the new clients it gives a request in the request attribute CookieBasedClientContext::NEW_CLIENT_IDS_ATTRIBUTE, which the subscriber reads directly, so the static @internal isNewClient() is gone.
The attribute's name moves from CookieBasedClientContext to ClientContextInterface, whose docblock asks every client context that makes up a client id for the main request to add it to the list. StoreClientSubscriber now depends only on the interface, an app that replaces the implementation sees the rule, and a decorator that makes up client ids of its own can opt in. The test app's MadeUpClientContext does, and a functional test pins that its client's metadata isn't stored when the client cookie is vetoed.
AbstractClientContext::newClient() adds a client's id to the main request's new client ids and returns the client, so only it writes the attribute and its format has one owner. A non-array value would make StoreClientSubscriber throw. It takes the client, defaulting to a new Client, so an app can make up its own client ids or use its own Client subclass. CookieBasedClientContext and the test app's MadeUpClientContext extend it, and the README shows decorators how to use it.
- Give the reason a new client's metadata isn't stored once, in the contract of ClientContextInterface::NEW_CLIENT_IDS_ATTRIBUTE. The constant now also says which client ids count as made up, and that newClient() is the attribute's only writer. - Drop the subscriber unit test that the functional tests already cover. - Rename the failed-request test to what it shows: a request without a response stores nothing. - README: define a new client like the glossary does, give a complete AbstractClientContext decorator, and say that a client id a later request can recognize isn't a new client's. - UPGRADE and CLAUDE.md: an earlier kernel.response listener that stops propagation skips the client cookie, the metadata and the cache fill for kernel.terminate. The request attribute rule moves to the conventions.
This was referenced Nov 30, 2026
Open
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.
Closes #18
Summary
The bug: a new client's metadata was stored even when the browser never got its client id.
abc.gclid = 123.PreStoreCookieEventlistener vetoes the cookie (no consent), so the browser never receivesabc.abc → {gclid: 123}.abcrow again.The same happened when an exception ended the request without a response: no cookie went out, but
kernel.finish_requeststill stored the metadata.The fix: the bundle stores the client the way Symfony's session listener stores the session, so the metadata goes out together with the cookie.
ClientContextInterface(e.g. a logged-in user)Design choices:
StoreCookieSubscriberandStoreMetadataSubscriber. "Was the cookie stored?" is anifin that listener, not state that two listeners share.ClientCookieJaris unchanged from 2.x._route. Its name is the constantClientContextInterface::NEW_CLIENT_IDS_ATTRIBUTE. The subscriber depends only on the interface and doesn't injectCookieBasedClientContext, which an app may decorate (ADR-0004).AbstractClientContext::newClient()is the only writer of the attribute.CookieBasedClientContextand the test app'sMadeUpClientContextextend it. It takes the client (a newClientby default), so apps can make up their own ids or use their ownClientsubclass. With one writer, nobody can set a non-array value, which would makeParameterBag::all()throw in the subscriber.Client, still counts. If a decorator calls the cookie-based context twice, the list has both ids.MetadataStoreInterfaceis unchanged.Docs:
PreStoreCookieEvent::$storedocblock.Evidence
src/with this PR's functional tests):OK (100 tests, 371 assertions)skip_cookie=1still has its metadata stored;client_id=X&skip_cookie=1without a cookie stores underX;Merge Danger
Door: two-way
A revert restores the old behavior. No schema or data change.
Blast Radius: behavioral
setono_client__metadatarows for them. Those rows were already unreachable.kernel.responseat -1000, not onkernel.finish_request. Metadata changed by a laterkernel.responselistener or akernel.finish_requestlistener isn't stored, and a request without a response stores nothing. In production an exception still gets an error response, so this only shows when there's no response at all. An earlierkernel.responselistener that stops propagation skips the cookie, the metadata, and the cache fill thatkernel.terminatelisteners rely on.StoreCookieSubscriberandStoreMetadataSubscriberare gone. An app that references them, e.g. to remove one, needsStoreClientSubscriber.