snok / asgi-correlation-id

Request ID propagation for ASGI apps

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

The headers with same key are overwritten

lakshaythareja opened this issue · comments

Starlette responses adds cookies as headers with the same key.
self.raw_headers.append((b"set-cookie", cookie_val.encode("latin-1")))

In your middleware handle outgoing request over writes the same key with only the last value.

        async def handle_outgoing_request(message: Message) -> None:
            if message['type'] == 'http.response.start':
                headers = {k.decode(): v.decode() for (k, v) in message['headers']}
                headers[self.header_name] = correlation_id.get()
                headers['Access-Control-Expose-Headers'] = self.header_name
                response_headers = Headers(headers=headers)
                message['headers'] = response_headers.raw
            await send(message)

If I recall correctly, the intended behaviour was for headers = {k.decode(): v.decode() for (k, v) in message['headers']} to capture existing headers, and to add to that before writing back to the message headers. In other words, it should add, but not change unrelated header values.

I'm not sure I understand the issue 100%, can you elaborate on what exactly the issue is?

But this middleware does not set a set-cookie header. Are you referring to another header being overwritten?

Could you open a PR with a failing test to illustrate what is wrong? I'm not able to replicate the issue on my own

You can check this image, here in message there are 4 headers with key as set-cookie and different values:
image

headers = {k.decode(): v.decode() for (k, v) in message['headers']}
replaces the same key with different values with just 1 key and 1 value.

I see. That seems worth fixing! Would you be interested in creating a PR with a fix? 🙂

@sondrelg can you please merge the PR and release a new version I will then start using it.

Yes I'll review and release it early tomorrow morning (~15 hours from now) 👍 Thanks for the effort @lakshaythareja

v1.1.3 should be out within the next minute, so will consider this closed 👍