Skip to content

Send no Mercure cookie on a host outside the hub's domain - #616

Merged
silverbackdan merged 3 commits into
mainfrom
fix/mercure-cookie-foreign-host
Nov 25, 2026
Merged

silverbackdan merged 3 commits into
mainfrom
fix/mercure-cookie-foreign-host

Conversation

@silverbackdan

Copy link
Copy Markdown
Collaborator

symfony/mercure's Authorization throws RuntimeException('Unable to create authorization cookie for a hub on the different second-level domain …') from both createCookie() and createClearCookie() when the request host and the hub's public_url share no registrable domain. Once the template anchors its Caddy internal-host matcher, php sees such hosts (a production alias, a tunnel), and login, JWT refresh, logout and a failed /me all 500'd.

Now the bundle sends no Mercure cookie on such a host, logs a warning, and lets the request carry on. Login works there and only real-time is unavailable.

  • MercureAuthorization::getAuthorizationCookie() and getClearAuthorizationCookie() return ?Cookie, and both go through one private helper. JWTEventListener (login, refresh), LogoutListener, JWTClearTokenListener and AccessDeniedListener skip a null.
  • The catch is symfony/mercure's RuntimeException only, and it is rethrown unless Utility\MercureCookieDomain::isOutside() confirms the host is foreign. That narrowing is needed because the token factory, which runs before the domain check, can throw the same class for another reason (WebTokenFactory's JWKS fetch). isOutside() mirrors the library's private getCookieDomain() rule, and MercureCookieDomainTest checks every case against the real Authorization, so the two can't drift without a failing test. InvalidArgumentException (JWT configuration, __Secure- over plain HTTP) always propagates.
  • MercureAuthorization takes the HubRegistry (for the hub's public URL) and an optional logger (NULL_ON_INVALID_REFERENCE).

Tests:

  • Functional\User\ForeignHostMercureCookieTest covers login, JWT refresh, GET and POST logout, a failed /me with an expired session, a failed /me carrying only a Mercure cookie, and an invalid JWT, all on https://example.org. Each checks the status, that the JWT cookie is set or cleared, that no Mercure cookie is sent, and that a warning is logged.
  • Unchanged behaviour on the normal host is already pinned by Functional\User\SecurityTest (test_a_successful_login, test_expired_jwt_tokens_should_be_refreshed, the logout tests, the failed /me tests).
  • Unit tests cover MercureAuthorization, every listener (a new LogoutListenerTest), MercureCookieDomain and the DI wiring.

Docs: components-web-app/docs#345

🤖 Generated with Claude Code

symfony/mercure refuses to build the subscriber cookie, and its clear
cookie, when the request host shares no registrable domain with the
hub's public URL. Login, JWT refresh, logout and a failed /me then
500'd. The bundle now logs a warning and sends no Mercure cookie, so
the request carries on without real-time.
@codecov

codecov Bot commented Nov 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.25%. Comparing base (22f3fab) to head (4a82011).

Additional details and impacted files
@@            Coverage Diff            @@
##               main     #616   +/-   ##
=========================================
  Coverage     97.24%   97.25%           
- Complexity     5587     5604   +17     
=========================================
  Files           460      461    +1     
  Lines         14335    14364   +29     
=========================================
+ Hits          13940    13969   +29     
  Misses          395      395           
Flag Coverage Δ
phpunit 97.25% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@silverbackdan
silverbackdan merged commit 776206a into main Nov 25, 2026
14 of 15 checks passed
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