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: parse "Set-Cookie" response header with commas correctly #2075

Merged
merged 8 commits into from Mar 15, 2024

Conversation

ushiboy
Copy link
Contributor

@ushiboy ushiboy commented Mar 7, 2024

Fixed to handle Set-Cookies containing commas.
If TypeScript is 5.2 or higher, getSetCookie in Headers can be used, but this has been fixed as a temporary measure at this time.

see also:

// so that it can be parsed correctly even if the Set-Cookie value contains commas.
// This is a temporary solution until the getSetCookie method of Headers
// is available in TypeScript 5.2 or later.
const responseCookies = Array.from(init.headers.entries()).reduce<string[]>(
Copy link
Contributor

Choose a reason for hiding this comment

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

Since we build with 5.2+, and consumers shouldn't be getting typed output for this, can we just use headers.getSetCookie() or does that actually cause a consumer failure downstream?

consumer typescript shouldn't need to know I think, but we shouldbe able to still use it as long as getSetCookie is defined on the Headers prototype in the target environments

Copy link
Contributor

Choose a reason for hiding this comment

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

{
  H: Object [Headers] {
    append: [Function: append],
    delete: [Function: delete],
    get: [Function: get],
    has: [Function: has],
    set: [Function: set],
    keys: [Function: keys],
    values: [Function: values],
    entries: [Function: entries],
    forEach: [Function: forEach]
  }
}

looks like our jsdom version doesn't have getSetCookie implemented, but typescript is fine with it

Copy link
Contributor

Choose a reason for hiding this comment

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

What we implemented in Headers-polyfill was this https://github.com/mswjs/headers-polyfill/blob/67f31364053574f5b1b805aa0eb3cbd5cd49d915/src/Headers.ts#L182-L194

I think headers-polyfill is a dependency of msw still, and we could do something like HeadersPolyfill.prototype.getSetCookie.call(init.headers) as the lightest fix that avoids consumer issues

I think we could easily just ship it raw to users as init.headers.getSetCookie() but we're likely to run into many cases of tests failing due to the lack of definition in jsdom, which would require consumers to polyfill Headers in their tests, which adds a step, at the cost of potentially a few bytes.

The other option is that we suggest headers-polyfill be used for environments that don't have the correct headers implementations in place (jsdom, it's always you).

@kettanaito any preferences? I generally prefer just using getSetCookie directly, but also expect a lot of "this broke" commentary when/if we ship that

Copy link
Contributor Author

@ushiboy ushiboy Mar 13, 2024

Choose a reason for hiding this comment

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

It seems that the latest version of jsdom Headers does not yet implement getSetCookie.
https://github.com/jsdom/jsdom/blob/24.0.0/lib/jsdom/living/fetch/Headers-impl.js

I too think it is better to use headers-polyfill to deal with this temporarily.
But it seems that HeadersPolyfill.prototype.getSetCookie.call(init.headers) would cause eslint to error.

msw/src/core/utils/HttpResponse/decorators.ts
  0:0  error  Parsing error: DeprecationError: 'originalKeywordKind' has been deprecated since v5.0.0 and can no longer be used. Use 'identifierToKeywordKind(identifier)' instead

✖ 1 problem (1 error, 0 warnings)

Copy link
Contributor

Choose a reason for hiding this comment

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

weird, that looks like @typescript-eslint/parser is probably failing somewhere and maybe a version update fixes it, i'll try landing an update

Copy link
Contributor

Choose a reason for hiding this comment

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

#2084

I think updating the ts-eslint dependencies like in that PR should fix this error, which isn't related to the dependency at all, but the linter breaking

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, @mattcosta7
I have changed to use headers-polyfill.

@kettanaito kettanaito changed the title fix: Set-Cookie responses containing commas are not handled correctly fix: parse "Set-Cookie" response header with commas correctly Mar 15, 2024
const responseCookies = init.headers.get('Set-Cookie')?.split(',') || []
// Temporary measure until jsdom environment is improved
// and getSetCookie of Headers can be used directly.
const responseCookies = HeadersPolyfill.prototype.getSetCookie.call(
Copy link
Member

Choose a reason for hiding this comment

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

We shouldn't tailor to technologies that violate standard support. We should rely on .getSetCookie() because it's a supported method. If the underlying environment doesn't support it, it's the underlying environment that should be fixing it (i.e. including any necessary polyfills). We mustn't rely on headers-polyfill anymore. It's used only for its utilities around transforming headers.

The only concern is what @mattcosta7 raised about the TypeScript supporting this method. It doesn't exist on earlier versions of TS:

And seems to only land in 5.2.

The Headers.prototype.getSetCookie can also be used only since Node.js 19 (which translates to v20 for stable releases) while MSW supports v18.

With this in mind, we cannot ship .getSetCookie() method as a part of MSW—it will break for a lot of people because it won't exist type-wise and runtime-wise.

We need to implement the logic/fix ourselves until TS 5.2 and Node.js v20 become the minimal supported versions, which is years in the future.

Copy link
Member

Choose a reason for hiding this comment

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

TL;DR

What you're doing is fine. Just the reasoning should be mentioned more prominently above this line.

Can you please include a comment above this HeadersPolyfill usage briefly describing why we rely on a polyfill here? Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

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

FWIW - typescript not supporting this in 4.7-5.1 shouldn't matter for consumers using those versions, because this call is encapsulated from their tooling (it's internal and generally node_modules aren't typechecked except at their .d.ts level which shouldn't contain any information about this call to getSetCookie

But with neither node18, jest not vitest able to support this correctly, we'd basically break everyone's tests the second we shipped it which would be unideal.

I'm very much onboard that jsdom needs to support this - although maybe one day people will test browser code in a browser....one day

We could also inline logic from the headerpolyfill here instead of using that directly, as another option to get away from the polyfill - it's just implemented there already

Copy link
Member

Choose a reason for hiding this comment

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

FWIW - typescript not supporting this in 4.7-5.1 shouldn't matter for consumers using those versions, because this call is encapsulated from their tooling

This is only true if they have skipLibCheck: true, which is not the default and many people still have it disabled, which will cause compilation problems. Granted, easy to fix but creates an unnecessary stress for consumers.

I'm very much onboard that jsdom needs to support this - although maybe one day people will test browser code in a browser....one day

Fingers crossed!

We could also inline logic from the headerpolyfill here instead of using that directly, as another option to get away from the polyfill - it's just implemented there already

See no reason to duplicate code. We depend on headers-polyfill anyway right now, its implementation is tested, we should use it.

kettanaito
kettanaito previously approved these changes Mar 15, 2024
Copy link
Member

@kettanaito kettanaito left a comment

Choose a reason for hiding this comment

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

This change is fantastic! Please, just add the comment above the HeadersPolyfill usage that explains we are using it due to the TS/runtime compatibility, not due to JSDOM.

@ushiboy
Copy link
Contributor Author

ushiboy commented Mar 15, 2024

@kettanaito
I have added a comment granting an explanation. Please check again.

Copy link
Member

@kettanaito kettanaito left a comment

Choose a reason for hiding this comment

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

Fantastic work. Thank you so much, @ushiboy @mattcosta7! Let's get this fixed.

@kettanaito kettanaito merged commit 23c16e6 into mswjs:main Mar 15, 2024
11 checks passed
@kettanaito
Copy link
Member

Welcome to the contributors, @ushiboy! 👏

@ushiboy ushiboy deleted the feature/getting-response-cookies branch March 16, 2024 00:43
@kettanaito
Copy link
Member

Released: v2.2.4 🎉

This has been released in v2.2.4!

Make sure to always update to the latest version (npm i msw@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.

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.

Set-Cookie responses containing commas are not handled correctly
3 participants