-
Notifications
You must be signed in to change notification settings - Fork 87
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
[BUG] Broken Response set-cookie
headers when cors is enabled
#156
Comments
set-cookie
headers when cors is enabled
Interesting report, thanks @ballercat. That header-utils logic is because I think unwinding that will be complicated, and it really would be much better to try to continue to use that flat format so far as possible, since fundamentally that is the raw format of HTTP on the wire (a list of pairs). Really, I think this is a node bug. If you call setHeaders and then call writeHeaders, and it tries to merge them, it shouldn't unexpectedly lose values along the way. I'm going to open a PR and get this fixed there. Beyond that, there are other mitigations we could try to make here (hooking writeHead where we already sort-of do so inside request-utils, or changing how the CORS headers themselves are added). I'm open to PRs to patch that if you'd like, if that's possible as a temporary fix that doesn't break or significantly complicate things, but really it would be better to fix it at the root instead. Node shouldn't just lose header values! |
Filed a Node bug here: nodejs/node#50387 |
Cool. Thanks for putting together that fix for Node. I think I'll disable the Thanks again! |
- Fix #39 - Address issue with `cors` mode see httptoolkit/mockttp#156
Good news: my proper fix for this has now been merged into Node, so this will work out of the box once that's released. Bad news: it's semver-major (potentially breaking change for some edge cases) which means I think it'll be included in the first release of v22, which isn't out until April. I think I'm going to close this here now regardless. This only affects cases where you use Regardless, in future with node updated this should work correctly for all cases, and then you can re-enable the cors option if you like (or sticking with the workaround indefinitely is fine too, there's no serious downsides). |
Thanks Tim! I forgot to follow-up after my last comment. Yeah for anyone potentially running into this in the future I went the route of disabling |
👋
First of, thanks for creating this awesome library. I've been hacking on top of it to build my own testing utility for playing back requests and have been very happy with it for the most part!
I recently ran into an issue when a request is forwarded via the server. Turns out that when cors is enabled Response headers that are meant to be sent as arrays don't work.
I created detailed reproduction here https://github.com/ballercat/mockttp-multi-header-issue
The headers are passed in as a flat array into
writeHead
and multiple values of the same header are discarded due to this block of code inside node_http_server.js
lib:https://github.com/nodejs/node/blob/04f9385311d7bc7df0beabf10541e599c2592c6f/lib/_http_server.js#L368-L380
Because
Access-Control-Allow-Origin
header is already set on the response (thanks to cors mode being enabled) every header in the list is set viathis.setHeader
. So the lastset-cookie
header will "win" while the others are overwritten. Whereas when there are no headers already present on the response the headers array is used properly and there is no problem. So when cors is disabled we hit this branchI tracked the problem down to this line in the
mockttp
library:mockttp/src/util/header-utils.ts
Line 130 in 30bfb86
Where each header value that is an array is "unwound". I have attempted simply removing this logic and everything is working fine, so I wonder why it's necessary. Ideally we could just leave array values as they are, then this isn't a terribly difficult patch 😓 (and I would be happy to make one if that's the case)
I'm hoping you have some advice. Because I'd like to keep
cors: true
enabled but then I lose the ability to properly pass through requests where the responses attempt to set multiple cookie values.Thanks!
The text was updated successfully, but these errors were encountered: