Skip to content

Don't store the metadata of a new client whose client cookie wasn't stored - #42

Merged
loevgaard merged 6 commits into
2.xfrom
18-new-client-metadata-without-cookie
Nov 30, 2026
Merged

loevgaard merged 6 commits into
2.xfrom
18-new-client-metadata-without-cookie

Conversation

@loevgaard

@loevgaard loevgaard commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Closes #18

Summary

The bug: a new client's metadata was stored even when the browser never got its client id.

  1. A visitor arrives without a client cookie, so the bundle makes up a client id, say abc.
  2. The app sets metadata on that client, e.g. gclid = 123.
  3. A PreStoreCookieEvent listener vetoes the cookie (no consent), so the browser never receives abc.
  4. At the end of the request, the bundle still writes the row abc → {gclid: 123}.
  5. The next request has no cookie either, so it gets a new id, and nothing ever reads the abc row again.

The same happened when an exception ended the request without a response: no cookie went out, but kernel.finish_request still 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.

StoreClientSubscriber::store()          kernel.response, priority -1000 (like the session listener)
  client = current client
  dispatch PreStoreCookieEvent
  if the cookie may be stored
    write the client cookie
  else if the client is a new client     its id is in ClientContextInterface::NEW_CLIENT_IDS_ATTRIBUTE
    return                               no later request could recognize it
  store the client's metadata

CookieBasedClientContext::getClient()
  no valid client cookie
    $this->newClient($request)          AbstractClientContext: adds the id to the request attribute
Current client Client cookie stored? Metadata stored?
Made up during this request yes yes
Made up during this request no (veto) no
Recognized by the cookie it already has no (veto) yes: the next request recognizes it by that cookie
Recognized by a decorator of ClientContextInterface (e.g. a logged-in user) no (veto) yes: the next request recognizes it the same way
Any no response (an exception) no: nothing is stored, as with the session

Design choices:

  • One listener replaces StoreCookieSubscriber and StoreMetadataSubscriber. "Was the cookie stored?" is an if in that listener, not state that two listeners share. ClientCookieJar is unchanged from 2.x.
  • The new client ids are a request attribute, which Symfony uses for facts that listeners share, e.g. _route. Its name is the constant ClientContextInterface::NEW_CLIENT_IDS_ATTRIBUTE. The subscriber depends only on the interface and doesn't inject CookieBasedClientContext, which an app may decorate (ADR-0004).
  • The interface states the rule: a client context that makes up a client id for the main request should add it to the list. An app that replaces the implementation sees it, and a decorator that makes up client ids of its own can opt in (README, "Extending the bundle"). A decorator that doesn't is stored as before.
  • AbstractClientContext::newClient() is the only writer of the attribute. CookieBasedClientContext and the test app's MadeUpClientContext extend it. It takes the client (a new Client by default), so apps can make up their own ids or use their own Client subclass. With one writer, nobody can set a non-array value, which would make ParameterBag::all() throw in the subscriber.
  • Matched by client id, so a decorator that passes the inner client through, or rebuilds it as an app subclass of Client, still counts. If a decorator calls the cookie-based context twice, the list has both ids.
  • MetadataStoreInterface is unchanged.

Docs:

  • GLOSSARY: New client is a client whose id a client context made up, and its metadata is stored only with its client cookie.
  • README: the consent section, "How it works" and "Extending the bundle".
  • UPGRADE-2.0: service ids, the replaced subscribers, the new timing, and the veto rule.
  • The PreStoreCookieEvent::$store docblock.
  • CLAUDE.md.

Evidence

  • Before (2.x's src/ with this PR's functional tests):
    it_does_not_store_the_metadata_of_a_new_visitor_whose_client_cookie_was_vetoed
      GET /metadata/set?key=foo&value=bar&skip_cookie=1  → no cookie, but 1 stored row (expected 0)
    it_does_not_store_metadata_when_the_request_ends_without_a_response
      GET /metadata/set-and-fail?key=foo&value=bar       → throws, but 1 stored row (expected 0)
    Tests: 47, Failures: 2
    
    After: OK (100 tests, 371 assertions)
  • The rows that must keep working are pinned too:
    • a returning visitor with skip_cookie=1 still has its metadata stored;
    • client_id=X&skip_cookie=1 without a cookie stores under X;
    • a decorator that makes up its own client id and adds it gets no row when the cookie is vetoed;
    • the existing tests are unchanged, including new visitors, and the no-query and no-entity-manager tests.
  • Checks:
    • ECS and PHPStan pass.
    • Infection killed all 101 mutants (100% MSI).
    • The tests ran locally on SQLite only; there's no new SQL.

Merge Danger

Door: two-way

A revert restores the old behavior. No schema or data change.

Blast Radius: behavioral

  • Consent: apps that veto the cookie for new visitors stop getting setono_client__metadata rows for them. Those rows were already unreachable.
  • Timing: the metadata is now stored on kernel.response at -1000, not on kernel.finish_request. Metadata changed by a later kernel.response listener or a kernel.finish_request listener 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 earlier kernel.response listener that stops propagation skips the cookie, the metadata, and the cache fill that kernel.terminate listeners rely on.
  • Cookie priority: the client cookie is now written at -1000 instead of 0, so the profiler's response panel no longer lists it, as with the session cookie.
  • Service ids: StoreCookieSubscriber and StoreMetadataSubscriber are gone. An app that references them, e.g. to remove one, needs StoreClientSubscriber.

…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.
@loevgaard
loevgaard merged commit 8757ac1 into 2.x Nov 30, 2026
23 checks passed
@loevgaard
loevgaard deleted the 18-new-client-metadata-without-cookie branch November 30, 2026 14:26
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.

Metadata is stored for a new visitor whose cookie was vetoed

1 participant