Skip to content

Bug: Response.set_cookie does not deduplicate cookies #4862

Description

@Vitaly312

Description

Current behavior

The set_cookie method does not upsert the new cookie, just append.

❯ curl -i http://localhost:8000/
HTTP/1.1 200 OK
...
set-cookie: foo=bar; Path=/; SameSite=lax
set-cookie: foo=foo; Path=/; SameSite=lax

Expected behavior

Cookies should be deduplicated to avoid redundant set-cookie headers in the response.

❯ curl -i http://localhost:8000/
HTTP/1.1 200 OK
...
set-cookie: foo=foo; Path=/; SameSite=lax

URL to code causing the issue

No response

MCVE

from litestar import Litestar, Response, get


@get("/", sync_to_thread=False)
def test_handler() -> Response:
    return Response({}, cookies={"foo": "bar"})


async def after_request(response: Response) -> Response:
    response.set_cookie(key="foo", value="foo")
    return response


app = Litestar(
    route_handlers=[test_handler],
    after_request=after_request,
)

Steps to reproduce

Screenshots

No response

Logs


Litestar Version

litestar: the main branch
python: 3.12

Platform

  • Linux
  • Mac
  • Windows
  • Other (Please specify in the description above)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bug 🐛This is something that is not working as expected

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions