Skip to content

Protect the guest cookie and save the guest before the action changes it - #1070

Merged
KrzysztofPajak merged 2 commits into
developfrom
fix/guest-cookie-protection
Nov 19, 2026
Merged

KrzysztofPajak merged 2 commits into
developfrom
fix/guest-cookie-protection

Conversation

@KrzysztofPajak

@KrzysztofPajak KrzysztofPajak commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Issue

The guest cookie (.Grand.Customer) held the bare CustomerGuid. The same GUID is public: the shared wishlist URL (/wishlist/{guid}) and the "email a friend" wishlist message show it. Anyone with that link could put the GUID in their own cookie and become that guest, with the guest's cart, the addresses entered at checkout and the guest's orders. Registered accounts are not affected, because the guest cookie never resolves a registered, system, inactive or deleted customer.

Since the lazy guest change (#918), a cookie with any GUID that no customer has is also accepted and saved as a new guest. A client can choose its own guest GUID, and a bot sending a random GUID on every request still creates one customer per request.

Two smaller problems with the unsaved guest:

  • AffiliateAttribute called EnsurePersisted() after the action. On a first GET with ?affiliateid= it saved the unsaved guest together with whatever the action had changed on that object.
  • The Google and Facebook login callbacks are GET actions. When the redirect back from the provider comes without the guest cookie (for example with CookieSameSite: Strict), registration ran against an unsaved guest. The partial updates did nothing (no upsert), the ExternalAuthentication row pointed at a customer id that does not exist, and the sign-in cookie named that same missing id, so the registration was silently lost.

Solution

  • CookieAuthenticationService protects the guest cookie with Data Protection (purpose Grand.Web.Customer.Guest) and unprotects it on read. A bare GUID, a tampered value, or a value protected with another key reads as no cookie, so a new guest is issued. The public wishlist GUID is therefore no longer a credential, and only cookies the store issued can be saved as a returning guest.
  • AffiliateAttribute sets the affiliate (and saves the guest) before the action runs, as its doc comment already said.
  • GoogleLoginCallback and FacebookLoginCallback are marked [PersistGuestCustomer].
  • Transition, removed in 2.6: a guest cookie written before this change (a bare GUID) still finds its existing guest, and the response rewrites it protected. A bare GUID never creates a guest. Until 2.6 a bare GUID of an existing guest is still accepted, as it is today.

Breaking changes

Only the guest cookie (.Grand.Customer) changes. The sign-in cookie (.Grand.Authentication) is untouched: signed-in customers stay signed in, and no account or customer data changes.

None for guests: an old guest cookie keeps its guest, cart and wishlist, and is rewritten protected on the first visit. In 2.6 the bare-GUID read is removed; a guest who has not come back by then gets a new guest.

IGrandAuthenticationService has a new member GetLegacyCustomerGuid() with a default implementation, so other implementations still compile.

Data Protection keys must be shared between instances, which the sign-in cookie already requires (PersistKeysToRedis / Azure). If the keys are lost, guests get a new guest instead of an error.

Testing

  • dotnet test Grand.Web.Common.Tests: 766 passed. New tests: the cookie is not the bare GUID and reads back; a bare GUID is rejected; a value protected with another key is rejected; no cookie gives null. Transition: a bare GUID of an existing guest resolves it and rewrites the cookie; a bare GUID with no guest resolves nothing and is not rewritten; a protected cookie is not rewritten. The affiliate test now asserts persist → write → action.
  • Grand.Web, Authentication.Google and Authentication.Facebook build.
  • Manual check on Kestrel against a local Mongo store:
    • A first GET sets a protected .Grand.Customer value (CfDJ8…, not a GUID).
    • Returning with that cookie twice sets no new cookie, so the guest is resolved.
    • A bare GUID cookie gets a new cookie.
    • No errors in the log.

The guest cookie held the bare customer GUID, which the shared wishlist URL
also shows, and since the lazy guest change any GUID nobody has is accepted
and saved as a new guest. The cookie is now protected with Data Protection;
a bare or foreign value is ignored and a new guest is issued.

AffiliateAttribute saved an unsaved guest after the action, together with
whatever the action changed on it; the affiliate is now set before the
action. The Google and Facebook login callbacks save the guest first, so a
redirect back without the guest cookie still registers the account.
Copilot AI balanced review requested due to automatic review settings November 19, 2026 13:42

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

A guest cookie written before it was protected (a bare GUID) still finds its existing guest and is
rewritten protected. It never creates a guest. Transition only, removed in 2.6.
@KrzysztofPajak
KrzysztofPajak merged commit 6246f40 into develop Nov 19, 2026
8 of 9 checks passed
@KrzysztofPajak
KrzysztofPajak deleted the fix/guest-cookie-protection branch November 19, 2026 19:34
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.

2 participants