fix(vtex): parse Max-Age and Expires in Set-Cookie parser#8
Open
fix(vtex): parse Max-Age and Expires in Set-Cookie parser#8
Conversation
parseSingleSetCookie was silently dropping Max-Age and Expires
attributes. This caused proxySetCookie to rebuild Set-Cookie headers
without expiration info, breaking logout (VTEX sends Max-Age=0 to
expire auth cookies) and any deletion flow.
Also fixes attr.split("=") which breaks on Expires date values
containing "=" — now uses indexOf for first-occurrence split.
Made-with: Cursor
There was a problem hiding this comment.
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/utils/cookies.ts">
<violation number="1" location="vtex/utils/cookies.ts:34">
P1: `Max-Age` parsing treats an empty/missing value as `0` (`Number("")`), which can incorrectly expire cookies. Validate that the value is present and numeric before assigning `cookie.maxAge`.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
| else if (lower === "samesite") cookie.sameSite = v as Cookie["sameSite"]; | ||
| else if (lower === "max-age") { | ||
| const n = Number(v); | ||
| if (!Number.isNaN(n)) cookie.maxAge = n; |
There was a problem hiding this comment.
P1: Max-Age parsing treats an empty/missing value as 0 (Number("")), which can incorrectly expire cookies. Validate that the value is present and numeric before assigning cookie.maxAge.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At vtex/utils/cookies.ts, line 34:
<comment>`Max-Age` parsing treats an empty/missing value as `0` (`Number("")`), which can incorrectly expire cookies. Validate that the value is present and numeric before assigning `cookie.maxAge`.</comment>
<file context>
@@ -20,13 +20,22 @@ function parseSingleSetCookie(raw: string): Cookie | null {
else if (lower === "samesite") cookie.sameSite = v as Cookie["sameSite"];
+ else if (lower === "max-age") {
+ const n = Number(v);
+ if (!Number.isNaN(n)) cookie.maxAge = n;
+ } else if (lower === "expires") {
+ const d = new Date(v);
</file context>
Suggested change
| if (!Number.isNaN(n)) cookie.maxAge = n; | |
| if (v !== "" && Number.isInteger(n)) cookie.maxAge = n; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
parseSingleSetCookieinvtex/utils/cookies.tswas silently droppingMax-AgeandExpiresattributes when parsing Set-Cookie headers. This causedproxySetCookie(which parses then rebuilds cookies viasetCookie()) to produce headers without expiration info, breaking logout flows where VTEX sendsMax-Age=0to expire auth cookies.attr.split("=")breaks onExpiresdate values that contain commas and other characters. Now usesattr.indexOf("=")for first-occurrence split.Note: The casaevideo-storefront currently works around this with
proxySetCookieRaw(raw string replacement), but this fix is needed for correctness in apps-start itself and for other consumers ofproxySetCookie.Test plan
parseSingleSetCookiecorrectly parsesMax-Age=0from a logout Set-CookieparseSingleSetCookiecorrectly parsesExpires=Thu, 01 Jan 1970 00:00:00 GMTsetCookieround-trips Max-Age and Expires correctly after parsingMade with Cursor
Summary by cubic
Fixes the VTEX Set-Cookie parser to keep Max-Age and Expires so cookies retain expiration info and logout flows work correctly. Also makes attribute parsing robust by splitting on the first "=" only.
Max-AgeandExpiresinparseSingleSetCookie, enabling proper round-tripping throughproxySetCookieandsetCookie.attr.split("=")with first-occurrence split to handleExpiresdate values with commas and spaces.Written for commit d706b79. Summary will update on new commits.