Skip to content

Fix: Define cookie priority for order placed loader#1567

Open
vitoUwu wants to merge 1 commit intodeco-cx:mainfrom
vitoUwu:fix/order-placed-loader-cookie
Open

Fix: Define cookie priority for order placed loader#1567
vitoUwu wants to merge 1 commit intodeco-cx:mainfrom
vitoUwu:fix/order-placed-loader-cookie

Conversation

@vitoUwu
Copy link
Copy Markdown
Contributor

@vitoUwu vitoUwu commented Apr 1, 2026

Problem

After an order is placed, VTEX sets two short-lived cookies (CheckoutDataAccess and Vtex_CHKO_Auth) specifically to grant access to the order-placed page. However, the loader was sending those together with VtexIdclientAutCookie_*, which can still exist in the browser even after it has expired. Having an expired auth cookie in the request was causing it to fail.

Fix

The loader now prioritizes checkout cookies when they're present, using only them for the request. The auth cookie is only used as a fallback when the checkout cookies are absent.


Summary by cubic

Prioritize checkout cookies in the order-placed loader to prevent failures caused by stale auth cookies. When available, the loader now sends only CheckoutDataAccess and Vtex_CHKO_Auth, falling back to VtexIdclientAutCookie_* only if needed.

  • Bug Fixes
    • Added resolveOrderCookie to prefer checkout cookies; use auth cookie only as a fallback.
    • Prevents failed requests to the order-placed endpoint when an expired auth cookie is still present.

Written for commit 9d11d70. Summary will update on new commits.

Summary by CodeRabbit

  • Bug Fixes
    • Improved order placement by optimizing authentication cookie selection during checkout operations.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 1, 2026

Tagging Options

Should a new tag be published when this PR is merged?

  • 👍 for Patch 0.143.1 update
  • 🎉 for Minor 0.144.0 update
  • 🚀 for Major 1.0.0 update

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 1, 2026

📝 Walkthrough

Walkthrough

A helper function resolveOrderCookie() was added to the VTEX order loader that selects between checkout cookies (CHECKOUT_DATA_ACCESS_COOKIE, VTEX_CHKO_AUTH) or auth cookies (VTEX_ID_CLIENT_COOKIE*), preferring checkout cookies when available. The loader now uses this function instead of including both cookie types simultaneously.

Changes

Cohort / File(s) Summary
Cookie Resolution Logic
vtex/loaders/orders/orderplaced.ts
Added resolveOrderCookie() helper that implements conditional cookie selection (prioritizes VTEX checkout cookies over auth cookies) and refactored loader to use this new logic instead of combining both cookie types.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

  • Deduplicate vtex cookie #1480: Modifies VTEX checkout cookie handling and deduplication, directly related to the cookie selection strategy introduced in this PR.

Poem

🐰 A cookie fork, oh what a hop!
Checkout takes the lead, auth takes a stop,
Never both together, just the right one's choice,
The loader now dances with newfound voice! 🍪✨

🚥 Pre-merge checks | ✅ 2
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and concisely describes the main change: fixing cookie priority logic in the order-placed loader to handle checkout vs auth cookies appropriately.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@vtex/loaders/orders/orderplaced.ts`:
- Around line 42-43: The resolveOrderCookie function currently returns an empty
string when no eligible cookies are found, causing an empty "cookie" header to
be forwarded; change resolveOrderCookie to return undefined instead of "" when
no cookies are present, and update the caller that forwards the header (the code
that sets the "cookie" request/header using the return of resolveOrderCookie) to
only add the "cookie" header when the value is !== undefined (i.e., guard the
header assignment so it is omitted entirely when resolveOrderCookie returns
undefined).
- Around line 32-34: The current check returns checkout-cookie mode when any
cookie exists; change the condition in the orderplaced loader to require both
cookies (specifically ensure CheckoutDataAccess and Vtex_CHKO_Auth are present
on checkoutCookies) before returning stringify(checkoutCookies); otherwise fall
through to the auth fallback path. Locate the block that uses checkoutCookies
and replace the Object.keys(checkoutCookies).length > 0 check with an explicit
presence check for checkoutCookies.CheckoutDataAccess &&
checkoutCookies.Vtex_CHKO_Auth so partial cookie sets no longer short-circuit
authentication.
🪄 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: defaults

Review profile: CHILL

Plan: Pro

Run ID: 0445be84-a8e0-4ff1-aa0a-d80905409057

📥 Commits

Reviewing files that changed from the base of the PR and between dd2bbdc and 9d11d70.

📒 Files selected for processing (1)
  • vtex/loaders/orders/orderplaced.ts

Comment on lines +32 to +34
if (Object.keys(checkoutCookies).length > 0) {
return stringify(checkoutCookies);
}
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.

⚠️ Potential issue | 🟠 Major

Require both checkout cookies before choosing checkout-cookie mode.

At Line 32, the condition > 0 treats a partial set as valid. If only one of CheckoutDataAccess or Vtex_CHKO_Auth is present, the loader skips auth fallback and can still fail access.

Suggested fix
-  if (Object.keys(checkoutCookies).length > 0) {
+  const hasBothCheckoutCookies =
+    checkoutCookies[CHECKOUT_DATA_ACCESS_COOKIE] != null &&
+    checkoutCookies[VTEX_CHKO_AUTH] != null;
+
+  if (hasBothCheckoutCookies) {
     return stringify(checkoutCookies);
   }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (Object.keys(checkoutCookies).length > 0) {
return stringify(checkoutCookies);
}
const hasBothCheckoutCookies =
checkoutCookies[CHECKOUT_DATA_ACCESS_COOKIE] != null &&
checkoutCookies[VTEX_CHKO_AUTH] != null;
if (hasBothCheckoutCookies) {
return stringify(checkoutCookies);
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@vtex/loaders/orders/orderplaced.ts` around lines 32 - 34, The current check
returns checkout-cookie mode when any cookie exists; change the condition in the
orderplaced loader to require both cookies (specifically ensure
CheckoutDataAccess and Vtex_CHKO_Auth are present on checkoutCookies) before
returning stringify(checkoutCookies); otherwise fall through to the auth
fallback path. Locate the block that uses checkoutCookies and replace the
Object.keys(checkoutCookies).length > 0 check with an explicit presence check
for checkoutCookies.CheckoutDataAccess && checkoutCookies.Vtex_CHKO_Auth so
partial cookie sets no longer short-circuit authentication.

Comment on lines +42 to +43
return stringify(authCookies);
}
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.

⚠️ Potential issue | 🟡 Minor

Avoid sending an empty cookie header when no eligible cookies exist.

If neither checkout nor auth cookies are found, resolveOrderCookie returns "" and Line 55 still forwards it. Prefer returning undefined and omitting the header entirely.

Suggested fix
-function resolveOrderCookie(headers: Headers): string {
+function resolveOrderCookie(headers: Headers): string | undefined {
   const all = getCookies(headers);
@@
-  return stringify(authCookies);
+  return Object.keys(authCookies).length > 0 ? stringify(authCookies) : undefined;
 }
@@
-  const cookie = resolveOrderCookie(req.headers);
+  const cookie = resolveOrderCookie(req.headers);
+  const headers = cookie ? { cookie } : undefined;
@@
-      }, {
-        headers: { cookie },
-      }).then((res) => res.json());
+      }, {
+        headers,
+      }).then((res) => res.json());
@@
-  }, {
-    headers: { cookie },
-  }).then((res) => res.json());
+  }, {
+    headers,
+  }).then((res) => res.json());

Also applies to: 55-55

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@vtex/loaders/orders/orderplaced.ts` around lines 42 - 43, The
resolveOrderCookie function currently returns an empty string when no eligible
cookies are found, causing an empty "cookie" header to be forwarded; change
resolveOrderCookie to return undefined instead of "" when no cookies are
present, and update the caller that forwards the header (the code that sets the
"cookie" request/header using the return of resolveOrderCookie) to only add the
"cookie" header when the value is !== undefined (i.e., guard the header
assignment so it is omitted entirely when resolveOrderCookie returns undefined).

Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 1 file

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="vtex/loaders/orders/orderplaced.ts">

<violation number="1" location="vtex/loaders/orders/orderplaced.ts:32">
P1: This condition passes when only one of the two required checkout cookies (`CheckoutDataAccess`, `Vtex_CHKO_Auth`) is present, skipping the auth-cookie fallback. Since VTEX requires both checkout cookies to grant access, a partial set will still cause the request to fail. Check that both cookies are present before choosing checkout-cookie mode.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

),
);

if (Object.keys(checkoutCookies).length > 0) {
Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai bot Apr 1, 2026

Choose a reason for hiding this comment

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

P1: This condition passes when only one of the two required checkout cookies (CheckoutDataAccess, Vtex_CHKO_Auth) is present, skipping the auth-cookie fallback. Since VTEX requires both checkout cookies to grant access, a partial set will still cause the request to fail. Check that both cookies are present before choosing checkout-cookie mode.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At vtex/loaders/orders/orderplaced.ts, line 32:

<comment>This condition passes when only one of the two required checkout cookies (`CheckoutDataAccess`, `Vtex_CHKO_Auth`) is present, skipping the auth-cookie fallback. Since VTEX requires both checkout cookies to grant access, a partial set will still cause the request to fail. Check that both cookies are present before choosing checkout-cookie mode.</comment>

<file context>
@@ -11,6 +11,37 @@ interface Props {
+    ),
+  );
+
+  if (Object.keys(checkoutCookies).length > 0) {
+    return stringify(checkoutCookies);
+  }
</file context>
Suggested change
if (Object.keys(checkoutCookies).length > 0) {
const hasBothCheckoutCookies =
checkoutCookies[CHECKOUT_DATA_ACCESS_COOKIE] != null &&
checkoutCookies[VTEX_CHKO_AUTH] != null;
if (hasBothCheckoutCookies) {
Fix with Cubic

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