Skip to content

fix: keep the order access token out of payments and stored emails readable by admins - #18

Merged
bvdaakster merged 1 commit into
mainfrom
fix/order-token-leaks
Nov 28, 2026
Merged

bvdaakster merged 1 commit into
mainfrom
fix/order-token-leaks

Conversation

@bvdaakster

Copy link
Copy Markdown
Contributor

Why

orders.accessToken (from @xtr-dev/payload-ecommerce 0.3.0) opens an order page without signing in. The plugin hides the field from REST, GraphQL and the admin panel for everyone, admins included. The site still copied the full order link, token included, into two places an admin can read:

  1. payments.redirectUrl, the URL the shopper returns to after paying.
  2. The stored order emails: the emails collection (html, text and variables.orderToken) and the demo outbox (mail-outbox, shown on /mailbox).

Workflow run snapshots were already stripped and are untouched.

What changed

Payment return

  • redirectUrl is now /shop/orders/return?t={returnToken}: billing's own per-payment return token, the mechanism bookings already use.
  • A new route handler, src/app/(frontend)/shop/orders/return/route.ts, looks the payment up by that token on the server, sets an order grant cookie and redirects to the plain /shop/orders/<number>.
  • The grant (src/lib/orderLinks.ts) is <expiry>.<HMAC-SHA256> over the order number, signed with the Payload secret. The cookie is httpOnly, SameSite=Lax, scoped to that order page's path, valid for one hour, and Secure when the site origin is https.
  • placeOrderAction sets the same cookie for the browser that placed the order, so an order that costs nothing (or a payment page left without paying) still opens for a guest.
  • A return link stops granting access 24 hours after the payment was started. After that a member signs in and a guest uses the link in their email.
  • /checkout/return forwards an order payment's token to the new route.

Why a signed cookie and not the token. I weighed two ways to let the shopper in after the return:

Approach Problem
Redirect to /shop/orders/N?t=<accessToken> from the return route Anyone who can read payments.returnToken (admins can) could follow the return link and read the access token from the address bar. The token would also land in browser history and proxy logs on every checkout.
Cookie holding the access token Same hole: follow the return link, read the cookie in devtools.
Cookie holding a signed, expiring grant (chosen) Nothing derived from the return token contains the access token. The grant is useless for any other order and after an hour. Nothing is stored server-side.

Emails

  • sendOrderConfirmation / sendOrderShipped render the template with a placeholder as orderToken, so the stored body, plain-text part and variables read …?t=sent-only-to-the-customer.
  • The mailing plugin's beforeSend hook (withOrderLinkTokens) puts the real token in as the message is handed to the transport. It runs on every attempt, so a retried email gets a working link.
  • The hook refuses to send (the plugin records the error and retries) when the order cannot be read, and when any recipient in to, cc or bcc is not the order's customerEmail. Without the second check an admin could create an emails document addressed to themselves, linked to someone's order, and have the token mailed to them.
  • The capturing dev transport redacts the token before it stores a message in mail-outbox.
  • /mailbox restores the working link only for the signed-in member who placed the order and received the message (getMailboxMessage in src/lib/mailbox.ts). Admins, and a member who merely shares an address with a guest order, see the placeholder. Guests cannot open the mailbox today (it needs an account), and that is unchanged.

I chose send-time rendering over field-level read access or redacting after the send because it is the only option where the token is never written to the emails collection at all, and it keeps retries working without special cases.

What an admin still sees

  • On a payment: redirectUrl with the payment's returnToken. Following it gives a one-hour grant for an order page the admin can already open as admin; it reveals no token.
  • On an order email and in the outbox and mailbox: the whole message, with the order link ending in ?t=sent-only-to-the-customer.

Tests

  • New tests/int/order-token.int.spec.ts (12 tests): grant signing and expiry, placeholder fill and redaction, the payment through Local API with overrideAccess: false, REST and GraphQL, the guest and member return flows, stored versus sent email for the confirmation and shipped emails, the mailbox data per viewer, a retried failed send, an admin-crafted email, and a sweep of every collection an admin can read (Local API and REST) plus stored job payloads for any token of the orders placed in the file.
  • tests/int/ecommerce.int.spec.ts updated for the new return URL and stored link.
  • tests/e2e/ecommerce.e2e.spec.ts: guest returns to the tokenless order page with the grant cookie, a stranger gets a 404, an admin finds only the return link and the placeholder over REST, the return link opens the order in another browser, the member opens the working link from their mailbox, and the admin sees the placeholder in the same message.

Locally: lint, typecheck, 135 integration tests and the shop e2e spec (10 tests) pass. The e2e run was before the last two hardening changes (recipient check in beforeSend, mailbox restore limited to the ordering member); CI covers those.

Not covered here

  • Existing rows: payments and emails stored before this change keep the old link. The demo database is reseeded, so no migration is included.
  • An admin can still change an order's customerEmail and trigger a new shipped email to that address. Closing that needs a rule about who may edit the address, which is a product decision.
  • The mailing plugin has no supported way to render a variable only at send time; the hook relies on beforeSend receiving the email with its order populated. Filed in the work tracker for payload-mailing.

🤖 Generated with Claude Code

https://claude.ai/code/session_0122PV8TJ7ELGdAX7H2hdrRU

…adable by admins

The ecommerce plugin hides an order's access token from the API and the admin
panel for everyone, but the site copied it into two places admins read: the
payment's redirectUrl and the stored order emails (emails and mail-outbox).

Payment return: the redirectUrl is now /shop/orders/return?t={returnToken},
billing's own return token. A route handler looks the payment up by it and
gives the browser a one-hour, httpOnly grant cookie for that one order (an
expiry plus an HMAC over the order number, no token inside), then redirects
to the plain order page. Checkout sets the same cookie for the browser that
placed the order.

Emails: order emails are rendered with a placeholder for the token, so the
emails document never holds it. The mailing plugin's beforeSend hook fills
the real token in on every send attempt, only when all recipients are the
order's customer, and fails the send when it cannot. The capturing transport
redacts the token before storing to mail-outbox; /mailbox restores the link
only for the member who placed the order.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0122PV8TJ7ELGdAX7H2hdrRU
@claude

claude Bot commented Nov 28, 2026

Copy link
Copy Markdown

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

@bvdaakster
bvdaakster merged commit 2b88872 into main Nov 28, 2026
3 checks passed
@bvdaakster
bvdaakster deleted the fix/order-token-leaks branch November 28, 2026 20:36
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