Skip to content

fix: bypass refresh-token flow on localhost in 403 page and useRefreshToken hook - #3326

Merged
lemagnetic merged 1 commit into
3.xfrom
fix/refresh-token-localhost-bypass-3x
May 19, 2026
Merged

lemagnetic merged 1 commit into
3.xfrom
fix/refresh-token-localhost-bypass-3x

Conversation

@eduardoformiga

Copy link
Copy Markdown
Member

Backport of #3325 (targeting dev) to the 3.x release line.

What

When experimental.refreshToken is enabled in discovery.config, developers could not test logged-in flows on localhost by manually injecting the VtexIdclientAutCookie_<account> cookie. Any SSR query in /pvt/account/* that returned 401/403 redirected to /pvt/account/403, which then fired the useRefreshToken hook. That hook POSTs cross-origin to ${storeUrl}/api/vtexid/refreshtoken/webstore — and from localhost the vid_rt cookie is not sent, so the request always fails and logoutAndClearSession wipes the manually injected auth cookie.

Why

The expected behavior is that on localhost the refresh-token flow should be a no-op so developers can drive logged-in flows with an injected VtexIdclientAutCookie_<account> cookie, regardless of the experimental.refreshToken flag. Part of the bypass was already in place for ValidateSession in the SDK and /api/graphql; this PR extends it to the only two remaining paths:

  • useRefreshToken (client hook called from /pvt/account/403.tsx)
  • The needsRefreshToken decision in /pvt/account/403.tsx getServerSideProps

How

  • Adds a shared isLocalHost util (packages/core/src/utils/isLocalHost.ts) exposing:
    • isLocalHost(hostname): pure helper, true for localhost/127.0.0.1 (case-insensitive). Expects a normalized hostname (no port) — callers strip the port with host?.split(':')[0].
    • isLocalHostBrowser(): client-side wrapper that reads window.location.hostname.
  • Refactors existing inline copies to use the shared util (no behavior change):
    • src/sdk/session/index.ts — drops the local isLocalEnvironment helper.
    • src/pages/api/graphql.ts — uses isLocalHost(request.headers.host?.split(':')[0]).
  • Closes the remaining gaps (real bug fix):
    • src/sdk/account/useRefreshToken.ts — bails out and shows the static 403 view on localhost instead of attempting the cross-origin refresh that would wipe the injected cookie.
    • src/pages/pvt/account/403.tsx — getServerSideProps now also requires !isLocal to set needsRefreshToken: true, providing defense in depth from SSR.

Diff vs the dev PR

The only adaptations needed for the 3.x port:

  • 3.x has no standalone src/utils/getRequestHostname.ts, so the API/SSR call sites strip the port inline with host?.split(':')[0] (the isLocalHost helper already lowercases the input).
  • Tests use Jest (instead of Vitest on dev): no import { describe, it, expect } from 'vitest', and the browser test uses the /** @jest-environment jsdom */ pragma.

Tests

  • test/utils/isLocalHost.test.ts (node, 7 cases): localhost, 127.0.0.1, case-insensitive, production hostnames, IPv6 loopback (out of scope), input still carrying a port (contract: caller must normalize), null/undefined/''.
  • test/utils/isLocalHost.browser.test.ts (jsdom, 3 cases): stubs window.location.hostname to validate isLocalHostBrowser for localhost, 127.0.0.1 and a production hostname.
  • Existing test/utils/validateSessionRefreshToken.test.ts continues to pass.

Test plan

  • In a store with experimental.refreshToken: true, run pnpm dev and open localhost:3000 with a valid VtexIdclientAutCookie_<account> cookie injected via DevTools.
  • Navigate to /pvt/account/profile: page should load with the user data (cookie is honored, no redirect to /403).
  • Manually expire/tamper with the cookie to force a 401 on the SSR profile query: the user lands on /pvt/account/403 (static view) and the injected cookie remains in the browser (no logoutAndClearSession round-trip).
  • Repeat with experimental.refreshToken: false: behavior on localhost must be identical (the flag is now a no-op on localhost).
  • On a non-local hostname (e.g. CodeSandbox preview): the existing refresh-token flow must still trigger as before.

Related

Made with Cursor

@eduardoformiga
eduardoformiga requested a review from a team as a code owner May 14, 2026 19:07
@eduardoformiga
eduardoformiga requested review from renatamottam and renatomaurovtex and removed request for a team May 14, 2026 19:07
@coderabbitai

coderabbitai Bot commented May 14, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Rate limit exceeded

@eduardoformiga has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 36 minutes and 14 seconds before requesting another review.

You’ve run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: fcb7e1f3-2606-4ec6-87e2-f902bd49db2e

📥 Commits

Reviewing files that changed from the base of the PR and between baaf13c and 0cbd7ba.

📒 Files selected for processing (7)
  • packages/core/src/pages/api/graphql.ts
  • packages/core/src/pages/pvt/account/403.tsx
  • packages/core/src/sdk/account/useRefreshToken.ts
  • packages/core/src/sdk/session/index.ts
  • packages/core/src/utils/isLocalHost.ts
  • packages/core/test/utils/isLocalHost.browser.test.ts
  • packages/core/test/utils/isLocalHost.test.ts
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/refresh-token-localhost-bypass-3x

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codesandbox-ci

codesandbox-ci Bot commented May 14, 2026 •

Copy link
Copy Markdown

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

…hToken hook

When `experimental.refreshToken` is enabled, manually injecting a
`VtexIdclientAutCookie_<account>` on localhost was unusable: any SSR query
in `/pvt/account/*` that returned 401/403 redirected to `/pvt/account/403`,
which then fired `useRefreshToken`. That hook POSTs cross-origin to the
production refresh endpoint without the `vid_rt` cookie, so the request
always failed and `logoutAndClearSession` wiped the injected auth cookie.

Centralizes the `localhost`/`127.0.0.1` check in a new `isLocalHost` util
(consolidating the inline copies in `sdk/session` and `api/graphql`) and
extends the bypass to both `useRefreshToken` and the `403.tsx` SSR
`needsRefreshToken` decision so developers can drive logged-in flows from
localhost regardless of the flag.

Backport of the fix targeting `dev` (PR #3325) to the `3.x` release line.
@eduardoformiga
eduardoformiga force-pushed the fix/refresh-token-localhost-bypass-3x branch from 31663ee to 0cbd7ba Compare May 14, 2026 19:14
@pkg-pr-new

pkg-pr-new Bot commented May 14, 2026

Copy link
Copy Markdown

Open in StackBlitz

@faststore/api

yarn add https://pkg.pr.new/vtex/faststore/@faststore/[email protected]

@faststore/cli

yarn add https://pkg.pr.new/vtex/faststore/@faststore/[email protected]

@faststore/components

yarn add https://pkg.pr.new/vtex/faststore/@faststore/[email protected]

@faststore/core

yarn add https://pkg.pr.new/vtex/faststore/@faststore/[email protected]

@faststore/graphql-utils

yarn add https://pkg.pr.new/vtex/faststore/@faststore/[email protected]

@faststore/lighthouse

yarn add https://pkg.pr.new/vtex/faststore/@faststore/[email protected]

@faststore/sdk

yarn add https://pkg.pr.new/vtex/faststore/@faststore/[email protected]

@faststore/ui

yarn add https://pkg.pr.new/vtex/faststore/@faststore/[email protected]

commit: 0cbd7ba

@lemagnetic
lemagnetic merged commit 5943129 into 3.x May 19, 2026
4 checks passed
@lemagnetic
lemagnetic deleted the fix/refresh-token-localhost-bypass-3x branch May 19, 2026 17:39
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.

3 participants