Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting cookies with special characters #978

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

abhi12299
Copy link

This is a fix for vercel/next.js#70523

What?

Access the ResponseCookie object using the NextResponse class in a nextjs middleware like so:

const response = NextResponse.next()
const data = {
  value: "bar 50%"
}

response.cookies.set({
  name: 'foo',
  value: JSON.stringify(data)
})

You'll notice that the cookie being set crashes the application. The reason for this is explained below:

This double invocation of decodeURIComponent throws an error and crashes the application if the cookie contains special characters.

Copy link

changeset-bot bot commented Sep 29, 2024

⚠️ No Changeset found

Latest commit: 6dcc952

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

vercel bot commented Sep 29, 2024

@abhi12299 is attempting to deploy a commit to the Vercel Team on Vercel.

A member of the Team first needs to authorize it.

Copy link

vercel bot commented Oct 7, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
edge-runtime ⬜️ Skipped (Inspect) Oct 7, 2024 9:33am

@wyattjoh
Copy link
Member

wyattjoh commented Oct 8, 2024

It's important for this particular framework that it adheres to the published RFC 6265 which requires that cookie values are URL encoded. We'd prefer in these cases to have the application throw an error during parsing if the cookie values can't be parsed rather than allowing un-encoded values from the cookie parser itself.

If the issue is stemming from double-decoding I'd rather solve that specifically rather than have it silently fail decoding.

@abhi12299
Copy link
Author

abhi12299 commented Oct 8, 2024

as per my limited understanding of this codebase, i would assume that the headers being set for set-cookie would still encode the cookie value. see here:

const stringified = `${c.name}=${encodeURIComponent(c.value ?? '')}`

i may be wrong, but the outbound headers still encode the cookie value - this issue stems from double decoding. Am i missing something obvious here?

@Sathosk
Copy link

Sathosk commented Oct 25, 2024

What is the purpose of calling decodeURIComponent when defining the new cookie object? The function parseCookie is already returning a new map with all values decoded. It seems redundant to me, or I'm missing something?

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