Skip to content

Keep marketing routes on the app host when signed in - #211

Merged
neuromechanist merged 2 commits into
mainfrom
210-signed-in-users-lose-their-session-when-following-a-marketing-link
Jul 29, 2026
Merged

neuromechanist merged 2 commits into
mainfrom
210-signed-in-users-lose-their-session-when-following-a-marketing-link

Conversation

@neuromechanist

Copy link
Copy Markdown
Contributor

Closes #210.

Problem

Sign in at app.nemar.org, click Discover, and you are signed out. Reported from real use.

Three correct decisions combined into a wrong outcome:

  • /discover is classified as a marketing route, so getCrossHostRedirect 301s it off the app host.
  • The session cookie is scoped to app.nemar.org and does not follow.
  • The marketing host deliberately skips /auth/me so its responses stay anonymous and edge-cacheable.

The nav made it unavoidable rather than incidental: on the app host the primary nav collapsed to a single absolute link, marketingUrl("/discover"). It left the app host by construction, so the only nav link a signed-in user had was the one that signed them out.

This is distinct from the apex-cutover regression (#190), where MARKETING_BASE_URL pointed at the legacy origin and ejected users onto the old HUBzero site. That was a bug in the destination. This is what remained once the destination was correct: the hop itself drops the session.

Change

Marketing-bound redirects are suppressed when the request carries a session, so the app host renders the marketing route itself.

The suppression is deliberately one-directional, and that asymmetry is the load-bearing part:

  • app -> marketing is suppressed with a session. The app host is already uncached and already personalized, so serving a marketing route there costs nothing and changes no caching assumption.
  • marketing -> app is never suppressed, cookie or not. The marketing host's responses are shared edge-cache entries; letting a cookie change what it serves would vary a shared entry per-user and hand one visitor's page to the next. Authenticated routes leave that host unconditionally.

Both directions are pinned by tests.

Cookie presence, not a resolved session

The redirect decision runs before applySession, and resolving costs an /auth/me round-trip that a redirect would discard. So the check is cookie presence.

A stale or forged cookie therefore buys nothing beyond a marketing page rendered on the app host instead of redirected: the page still resolves the session downstream and renders signed-out. Worst case is a slower anonymous render on the wrong host, not disclosure.

Two consequences that would otherwise be silent

  • Edge cache. The skip now keys on cookie presence rather than on a resolved session. Without this, a cookie that fails to resolve (expired, rotated secret) would produce a cacheable render of a page only cookie-bearing requests can reach, and put it in the shared cache.
  • Canonical. Base.astro derived its origin from the serving host. That was right only because a marketing route could never be served by the app host — which is exactly what this PR changes. Left alone it would publish app.nemar.org/discover as canonical, competing with the marketing host for identical content. New canonicalOriginFor(pathname, hostname) resolves by route: app routes to the app host, everything else to MARKETING_BASE_URL. Single-host mode (localhost, *.pages.dev) is unchanged.

Nav

On the app host the nav now depends on whether the user can be kept there:

  • Signed in — the full marketing nav with relative hrefs, so links resolve in place and the session survives.
  • Signed out (e.g. sitting on /login) — unchanged single absolute link, since the redirect still fires and relative hrefs would only add a pointless hop.

Tests

bun run test 1006 passing, up from 976. typecheck 0 errors, lint clean.

One existing middleware test flipped, correctly: it asserted an app-host /discover 301 using a harness that always supplies a cookie, which is now precisely the case that must not redirect. Split into a cookie-less case (still 301s) and a cookie-bearing case (200), and added an anonCtx helper since the old harness could no longer express both.

Not verified

Dev-server and browser tooling were hanging in this environment, so this is covered by unit tests and reading rather than a live click-through. Worth exercising on a preview: sign in, click each nav link, confirm the user menu persists and the URL stays on the app host.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jul 29, 2026 •

Copy link
Copy Markdown

@neuromechanist

Copy link
Copy Markdown
Contributor Author

Good catch on finding 3 — that one was mine, and the mechanism is worth naming: dataset/[id].astro grew its own copy of the host-derived origin in #205 (the JSON-LD PR), after I had already read Base.astro. So when I replaced the logic in Base.astro I replaced what I thought was the only copy. The comment on that line even asserted the two were the same logic, which my PR quietly made false.

Confirmed before fixing: /dataset/<id> is marketing-classified (DATASET_COLLABORATORS_RE only matches the /collaborators sub-route), so with the redirect suppressed a signed-in user renders it on the app host and gets canonical=nemar.org/... next to a JSON-LD url of app.nemar.org/... in the same <head>.

Fixed in dataset/[id].astro: jsonLdOrigin now calls canonicalOriginFor(url.pathname, url.hostname), and the unused APP_HOST/MARKETING_BASE_URL/hostMode imports are dropped. Rewrote the comment to say why it shares the helper rather than claiming equivalence with a copy that can drift.

Also added a test pinning the exact case, since "canonical is right but the structured data beside it is wrong" is invisible in a preview by construction (single-host mode collapses both to the same origin) and would have shipped again otherwise:

canonicalOriginFor("/dataset/nm000103", APP_HOST)               -> MARKETING_BASE_URL
canonicalOriginFor("/dataset/nm000103/collaborators", APP_HOST) -> https://app.nemar.org

1007 tests passing, typecheck 0 errors, lint clean.

On the verification gap

Agreed, and thanks for pushing further than I did rather than just restating it. Vite's DNS-rebinding protection rejecting a spoofed Host is a better answer than "it hung" — and you were right not to add server.allowedHosts to astro.config.mjs unilaterally to work around it.

So the redirect suppression itself remains unverified end-to-end by either of us: not on the preview (single-host mode, redirects never fire), not locally (Host spoofing blocked). It is covered by unit tests on both directions plus your executed cache-poisoning test. I will verify the real flow on app.nemar.org immediately after merge, where the two-host logic actually runs. The failure mode if something is wrong is that signed-in users keep getting bounced to the marketing host — which is exactly today's behaviour, so a regression would be a no-op rather than a new outage.

Particularly appreciated the mutation test on the cache skip: demonstrating that the pre-PR line really would have written the unresolved-cookie render into the shared cache is the difference between "this looks right" and "this is right".

@neuromechanist
neuromechanist merged commit b87acee into main Jul 29, 2026
5 checks passed
@neuromechanist
neuromechanist deleted the 210-signed-in-users-lose-their-session-when-following-a-marketing-link branch July 29, 2026 21:45
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.

Signed-in users lose their session when following a marketing link

1 participant