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

fix(cloudflare): split Set-Cookie header into multiple headers #989

Closed
wants to merge 7 commits into from

Conversation

oleghalin
Copy link
Contributor

@oleghalin oleghalin commented Feb 23, 2023

πŸ”— Linked issue

#988

❓ Type of change

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

Splits Set-Cookie for each upstream header provided.

Resolves #988

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@oleghalin
Copy link
Contributor Author

oleghalin commented Feb 23, 2023

I suppose that these changes should be applied also to other presets where .join(',') used. @pi0

@codecov
Copy link

codecov bot commented Feb 23, 2023

Codecov Report

Merging #989 (0583c8c) into main (1d0c120) will decrease coverage by 8.98%.
The diff coverage is n/a.

❗ Current head 0583c8c differs from pull request most recent head 1b5a4f6. Consider uploading reports for the commit 1b5a4f6 to get more accurate results

@@            Coverage Diff             @@
##             main     #989      +/-   ##
==========================================
- Coverage   76.64%   67.66%   -8.98%     
==========================================
  Files          71       60      -11     
  Lines        7241     6161    -1080     
  Branches      723      692      -31     
==========================================
- Hits         5550     4169    -1381     
- Misses       1690     1982     +292     
- Partials        1       10       +9     

see 51 files with indirect coverage changes

@oleghalin
Copy link
Contributor Author

oleghalin commented Feb 23, 2023

I checked codebase a little bit deeper and i think that it should be changed on unenv level and response.headers (which is HeadersObject https://github.com/unjs/unenv/blob/920c3869ac5f7e5d1fb0475bb1cccad18f0764e2/src/runtime/_internal/types.ts#L2) should implement native Headers instead of { [key: string]: string | string[] | undefined }; and with this we dont need to make such transformations on preset level.


for (const [k, v] of Object.entries(headers)) {
if (k === "set-cookie") {
for (const cookie of splitCookiesString(v)) {
Copy link
Member

Choose a reason for hiding this comment

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

We have recently introduced same util in h3. It might be one less dependency and bundle size if we expose it from h3 package.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, what is proper way to sync versions of h3 and current nitro? Should we wait for h3 version tag or?

Copy link
Member

Choose a reason for hiding this comment

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

Sure, releasing next version until this weekend. (at the same time thinking about your other comments, we need to really fix header normalization across entries)

Copy link
Contributor Author

@oleghalin oleghalin Feb 24, 2023

Choose a reason for hiding this comment

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

As i see normalize function looks same for all other entries, but i think something is different on provider level, i suppose that some of them do cookie splitting themselves. We can drop normalize function to utils and use it every entry until make changes with type change for HadersObject.

Copy link
Member

Choose a reason for hiding this comment

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

Indeed there are slight behavior diferences. Also we never properly tested header behavior on nitro-deploys πŸ˜“

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hello, @pi0. Do you have any plans for releasing new versions soon? Gonna continue work on module and project, but this issue blocks me testing any env except local one :)

Copy link
Member

Choose a reason for hiding this comment

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

Hi, dear @oleghalin. Sure. I am (at the moment!) trying workers via pages on nitro-deploys. That would make testing this PR possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, just in case wanna remind that it also waits for unjs/h3 release for splitCookieString helper :)

@pi0 pi0 changed the title fix(cloudflare): split Set-Cookie header into multiple headers fix(cloudflare): split Set-Cookie header into multiple headers Feb 24, 2023
@oleghalin
Copy link
Contributor Author

oleghalin commented Mar 2, 2023

Hello, @pi0. I also pushed to this PR change for not caching 404 responses if its asset directory, can you check it too please

which resolves #1001

@oleghalin
Copy link
Contributor Author

Hello, @pi0 any updates on it? Issue with Set-Cookie header and 404 caching its making Nuxt absolutely unusuable with Cloudlare Workers

@Stupremee
Copy link

Hey @oleghalin,
I just saw this PR because I have the same problems with Nuxt on CF Workers, which is soo annoying.

I noticed that the new h3 version used in nitro now exports the splitCookeString function, so maybe you can remove the set-cookie-parser dependency and this PR can move forward.

@oleghalin
Copy link
Contributor Author

Hello, @pi0. I just just updated my PR and dropped third-party dep

@oleghalin
Copy link
Contributor Author

Hey, @pi0. I suppose that it makes many problems for all users who use CF workers to deploy, can you take a look on it? It solves 2 big problems with caching and cookies

@j4tmr
Copy link

j4tmr commented Apr 30, 2023

+1 I had the same bug that bothered me for days,it's not just about nitro.routeRules, proxyRequest has the same bug

@Hebilicious
Copy link
Member

@oleghalin This is on my radar, apologies for the delay. Can you provide me with a reproduction ? I would like to see if this behaviour affects the other cloudflare presets too.

Copy link
Member

@Hebilicious Hebilicious left a comment

Choose a reason for hiding this comment

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

This doesn't include any tests or reproduction, so this is hard to test.

@oleghalin
Copy link
Contributor Author

Not sure why you need reproduction, Set-Cookie header should be merged into one because browsers read each Set-Cookie header and set 1 cookie per each Set-Cookie header, as soon as there is only 1 header delimited by coma it will write only first cookie. As I know Vercel and node server parse it on their end and thats why it works, but it doesn't work for CF.

Copy link
Member

Thanks for the additional information and the PR.
A reproduction is useful to test the proposed changes. Without one, it takes more maintainer time to review and get things merged.

@oleghalin
Copy link
Contributor Author

Cannot make repro right now, but referencing RFC for you to understand it better.

https://datatracker.ietf.org/doc/html/rfc6265#section-3

Copy link
Member

@Hebilicious Hebilicious left a comment

Choose a reason for hiding this comment

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

This is absolutely a necessary change!

);

// Fetch public assets from KV only
if (isPublicAssetURL(id)) {
Copy link
Member

Choose a reason for hiding this comment

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

I agree this needs to happen, but it should be handled in a different PR (https://github.com/unjs/nitro/pull/1236/files)

@Hebilicious Hebilicious mentioned this pull request Jul 17, 2023
7 tasks
@pi0 pi0 closed this in #1452 Jul 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Set Cookie merged into one header (Cloudflare Workers)
5 participants