Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

[dev] handle multiple value headers #1412

Closed
EverlastingBugstopper opened this issue Jun 25, 2020 · 3 comments · Fixed by #1413
Closed

[dev] handle multiple value headers #1412

EverlastingBugstopper opened this issue Jun 25, 2020 · 3 comments · Fixed by #1413
Labels
bug Something isn't working
Milestone

Comments

@EverlastingBugstopper
Copy link
Contributor

I'm unable to get wrangler dev to return multiple Set-Cookie headers in a worker response. I've tried in both of the following ways:

explicitly appending them

// not working, only last gets sent (`cookie2`)

resp.headers.append('Set-Cookie', 'cookie1=true')
resp.headers.append('Set-Cookie', 'cookie2=true')

passing them in the options object of the response

// not working, only last gets sent (`cookie2`)

headers: {
   'Set-Cookie': 'cookie1=true',
   'Set-Cookie': 'cookie2=true'
}

I've so far only been trying to get this to work using wrangler dev... perhaps it's a wider Workers issue/limitation? Perhaps I'm missing something obvious here?

Originally posted by @yet-another-dev in #1047 (comment)


The issue here is that we do not properly handle response headers that have multiple values. We iterate over them all and end up overwriting each response header with the following one, which is why @yet-another-dev only sees the last cookie header in the response

@EverlastingBugstopper
Copy link
Contributor Author

further update - it looks like this is only a problem for Set-Cookie specifically. Other headers with multiple values are coalesed by the Workers Runtime into a single header field. If the key was my-header instead of Set-Cookie, it would look like my-header: value1, value2 and it wouldn't be a problem for wrangler dev to handle, since there will only ever be one my-header.

But Set-Cookie is special! From RFC 6265:

"Origin servers SHOULD NOT fold multiple Set-Cookie header fields into a single header field. The usual mechanism for folding HTTP headers fields (i.e., as defined in [RFC2616]) might change the semantics of the Set-Cookie header field because the %x2C (",") character is used by Set-Cookie in a way that conflicts with such folding."

This means that Set-Cookie headers will be returned individually by the Workers Runtime, making wrangler dev's previous "insert not append" approach broken.

@Sharkgamedev
Copy link

I'm unable to get wrangler dev to return multiple Set-Cookie headers in a worker response. I've tried in both of the following ways:

explicitly appending them

// not working, only last gets sent (`cookie2`)

resp.headers.append('Set-Cookie', 'cookie1=true')
resp.headers.append('Set-Cookie', 'cookie2=true')

passing them in the options object of the response

// not working, only last gets sent (`cookie2`)

headers: {
   'Set-Cookie': 'cookie1=true',
   'Set-Cookie': 'cookie2=true'
}

I've so far only been trying to get this to work using wrangler dev... perhaps it's a wider Workers issue/limitation? Perhaps I'm missing something obvious here?

Originally posted by @yet-another-dev in #1047 (comment)

The issue here is that we do not properly handle response headers that have multiple values. We iterate over them all and end up overwriting each response header with the following one, which is why @yet-another-dev only sees the last cookie header in the response

Is this a problem again? I'm using the headers: { 'Set-Cookie': 'cookie1=true', 'Set-Cookie': 'cookie2=true' }
approach and It's only getting cookie2! Quite frustrating!

@Sharkgamedev
Copy link

UPDATE: When trying the resp.headers.apend(); method it works flawlessly so I assume this is a regression? I'm going to open a new issue.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants