Repository navigation
fix: bypass refresh-token flow on localhost in 403 page and useRefreshToken hook - #3326
Conversation
|
Warning Rate limit exceeded
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 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 configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (7)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
|
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.
31663ee to
0cbd7ba
Compare
@faststore/api
@faststore/cli
@faststore/components
@faststore/core
@faststore/graphql-utils
@faststore/lighthouse
@faststore/sdk
@faststore/ui
commit: |
Backport of #3325 (targeting
dev) to the3.xrelease line.What
When
experimental.refreshTokenis enabled indiscovery.config, developers could not test logged-in flows on localhost by manually injecting theVtexIdclientAutCookie_<account>cookie. Any SSR query in/pvt/account/*that returned 401/403 redirected to/pvt/account/403, which then fired theuseRefreshTokenhook. That hook POSTs cross-origin to${storeUrl}/api/vtexid/refreshtoken/webstore— and fromlocalhostthevid_rtcookie is not sent, so the request always fails andlogoutAndClearSessionwipes 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 theexperimental.refreshTokenflag. Part of the bypass was already in place forValidateSessionin the SDK and/api/graphql; this PR extends it to the only two remaining paths:useRefreshToken(client hook called from/pvt/account/403.tsx)needsRefreshTokendecision in/pvt/account/403.tsxgetServerSidePropsHow
isLocalHostutil (packages/core/src/utils/isLocalHost.ts) exposing:isLocalHost(hostname): pure helper,trueforlocalhost/127.0.0.1(case-insensitive). Expects a normalized hostname (no port) — callers strip the port withhost?.split(':')[0].isLocalHostBrowser(): client-side wrapper that readswindow.location.hostname.src/sdk/session/index.ts— drops the localisLocalEnvironmenthelper.src/pages/api/graphql.ts— usesisLocalHost(request.headers.host?.split(':')[0]).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—getServerSidePropsnow also requires!isLocalto setneedsRefreshToken: true, providing defense in depth from SSR.Diff vs the
devPRThe only adaptations needed for the 3.x port:
src/utils/getRequestHostname.ts, so the API/SSR call sites strip the port inline withhost?.split(':')[0](theisLocalHosthelper already lowercases the input).dev): noimport { 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): stubswindow.location.hostnameto validateisLocalHostBrowserforlocalhost,127.0.0.1and a production hostname.test/utils/validateSessionRefreshToken.test.tscontinues to pass.Test plan
experimental.refreshToken: true, runpnpm devand openlocalhost:3000with a validVtexIdclientAutCookie_<account>cookie injected via DevTools./pvt/account/profile: page should load with the user data (cookie is honored, no redirect to/403)./pvt/account/403(static view) and the injected cookie remains in the browser (nologoutAndClearSessionround-trip).experimental.refreshToken: false: behavior on localhost must be identical (the flag is now a no-op on localhost).Related
dev: fix: bypass refresh-token flow on localhost in 403 page and useRefreshToken hook #3325Made with Cursor