Repository navigation
fix: forward auth token only via account-scoped cookie - #3294
Conversation
WalkthroughVTEX auth handling now derives the validation token from the merged cookie set using ChangesAuth Cookie Token Handling
Sequence DiagramsequenceDiagram
participant Client
participant API as FastStore API (ctx)
participant Store as Cookie Store (getUpdatedCookie)
participant Commerce as VTEX Commerce (vtexid.validate)
Client->>API: HTTP request with cookie header
API->>Store: getUpdatedCookie(ctx)
Store-->>API: updated cookie set (maybe null)
API->>API: getAuthCookie(mergedCookies, account)
API->>Commerce: POST /api/vtexid/credential/validate { token }
Commerce-->>API: validation response
API-->>Client: response
Estimated Code Review Effort🎯 3 (Moderate) | ⏱️ ~25 minutes Suggested Labels
Suggested Reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Tip 💬 Introducing Slack Agent: The best way for teams to turn conversations into code.Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.
Built for teams:
One agent for your entire SDLC. Right inside Slack. 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. |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/api/src/platforms/vtex/clients/commerce/index.ts`:
- Around line 740-748: The token is being extracted from the original request
cookies (ctx.headers?.cookie) but headers are created with withAutCookie(...)
and may contain an updated auth cookie; update the call to getAuthCookie to read
the merged cookie string from the headers created by withAutCookie (the same
HeadersInit used in the fetch) instead of ctx.headers?.cookie so
vtexid.validate() sends the current token; specifically, in the block where
headers is assigned and token is computed, replace using ctx.headers?.cookie
with the cookie value from headers (e.g., headers['cookie'] or
headers.get('cookie') depending on the HeadersInit shape) before passing it to
getAuthCookie.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: b633c73f-79f5-4da0-8093-a5b4012d4d94
📒 Files selected for processing (3)
packages/api/src/platforms/vtex/clients/commerce/index.tspackages/api/src/platforms/vtex/utils/cookies.tspackages/api/test/integration/vtex.cookies.test.ts
@faststore/api
@faststore/cli
@faststore/components
@faststore/core
@faststore/graphql-utils
@faststore/lighthouse
@faststore/sdk
@faststore/ui
commit: |
439acaa to
c699cfa
Compare
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/api/test/integration/vtex.cookies.test.ts (1)
115-118: ⚡ Quick winAvoid asserting the concrete
HeadersInitshape in these tests.The
as Record<string, string>casts couple the suite to today’s implementation detail. Wrapping the result innew Headers(...)and asserting with.get()/.has()keeps the tests valid even ifgetWithAutCookielater returns aHeadersinstance instead of a plain object.Example adjustment
- const headers = withAutCookie(FORWARDED_HOST, ACCOUNT) as Record< - string, - string - > + const headers = new Headers(withAutCookie(FORWARDED_HOST, ACCOUNT)) - expect(headers).toEqual({ - 'content-type': 'application/json', - 'X-FORWARDED-HOST': FORWARDED_HOST, - cookie, - }) + expect(headers.get('content-type')).toBe('application/json') + expect(headers.get('X-FORWARDED-HOST')).toBe(FORWARDED_HOST) + expect(headers.get('cookie')).toBe(cookie) - expect(headers).not.toHaveProperty('VtexIdclientAutCookie') + expect(headers.has('VtexIdclientAutCookie')).toBe(false)As per coding guidelines, "Ensure type safety and avoid type assertions when possible".
Also applies to: 136-139, 151-154
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/api/test/integration/vtex.cookies.test.ts` around lines 115 - 118, The tests currently cast the result of withAutCookie(FORWARDED_HOST, ACCOUNT) to Record<string,string>, coupling tests to a concrete HeadersInit shape; instead, construct a Headers object from the result (new Headers(...)) and assert using headers.get(name) / headers.has(name) so the tests work whether withAutCookie returns a plain object or a Headers instance—apply this change for the occurrences around the withAutCookie call and the similar casts referenced (the other usages in the same file).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@packages/api/test/integration/vtex.cookies.test.ts`:
- Around line 115-118: The tests currently cast the result of
withAutCookie(FORWARDED_HOST, ACCOUNT) to Record<string,string>, coupling tests
to a concrete HeadersInit shape; instead, construct a Headers object from the
result (new Headers(...)) and assert using headers.get(name) / headers.has(name)
so the tests work whether withAutCookie returns a plain object or a Headers
instance—apply this change for the occurrences around the withAutCookie call and
the similar casts referenced (the other usages in the same file).
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: d4519e5c-2da3-430a-8104-9f3fcd3d0118
📒 Files selected for processing (3)
packages/api/src/platforms/vtex/clients/commerce/index.tspackages/api/src/platforms/vtex/utils/cookies.tspackages/api/test/integration/vtex.cookies.test.ts
| * Headers for authenticated VtexID requests. The auth token is carried by | ||
| * the forwarded `cookie` header — not duplicated as a separate header. | ||
| */ | ||
| export const getWithAutCookie = (ctx: ContextForCookies) => { |
There was a problem hiding this comment.
Does this function make sense yet? Looks like it does the same as getWithCookie now
What's the purpose of this pull request?
Logged-in shoppers were unable to access the FastStore My Account area: any navigation to
/pvt/account/*resulted in an immediate redirect to/pvt/account/403?from=..., where the page entered an infinite refresh-token loop.The root cause was on the API side. Every
@auth-protected GraphQL query relies onvtexid.validate, and the request that backs that call was forwarding the auth token in two ways at once: once through the standard account-scoped cookie and again under a duplicated, top-level header. The duplicated header was being rejected by the platform with401, even though the user's session was perfectly valid.How it works?
This PR removes the duplication. The auth token is now forwarded exclusively through the account-scoped cookie (
VtexIdclientAutCookie_{account}) — exactly like every other authenticated path in the codebase already does (seegetAuthCookie,getJWTAutCookie,getIsRepresentative).How to test it?
Pre-conditions: a logged-in shopper on a store that has FastStore My Account enabled (
experimental.enableFaststoreMyAccount = true).Navigate to
/pvt/account/profile— should render the profile page directly, without redirecting to/pvt/account/403.Run the package tests:
pnpm --filter @faststore/api testStarters Deploy Preview
Before
Gravacao.de.Tela.2026-05-05.as.19.26.38.mov
References
@faststore/apipackages/core/src/pages/pvt/account/*@authdirective (packages/api/src/directives/auth.ts) →validateUserAuthentication→vtexid.validateSummary by CodeRabbit
Bug Fixes
Tests