Skip to content

set.headers appends to Response headers instead of merging, causing duplicate valuesΒ #1590

@truehazker

Description

@truehazker

What version of Elysia is running?

[email protected]

What platform is your computer?

Darwin 25.1.0 arm64 arm

What environment are you using

1.3.2

Are you using dynamic mode?

no

What steps can reproduce the bug?

When a handler returns a Response with custom headers, and set.headers contains the same header key, values are appended instead of merged:

import { Elysia } from 'elysia'

const app = new Elysia()
    .onRequest(({ set }) => {
        set.headers['Content-Type'] = 'application/json'
    })
    .get('/', () => {
        return new Response('data', {
            headers: { 'Content-Type': 'text/plain' }
        })
    })

const res = await app.handle(new Request('http://localhost/'))
console.log(res.headers.get('Content-Type'))
// Output: "text/plain, application/json"

What is the expected behavior?

Headers should be merged without duplication. Either:

  • Response headers take precedence β†’ text/plain
  • Or set.headers takes precedence β†’ application/json

Not both appended together.

What do you see instead?

Content-Type: text/plain, application/json

This is invalid for most headers. Content-Type should have a single value.

Additional information

The issue is in src/adapter/utils.ts in createResponseHandler(). It uses response.headers.append() which adds values instead of replacing:

for (const key in set.headers)
    response.headers.append(key, set.headers[key]) // append creates duplicates

Should use proper merge logic that handles conflicts.

Have you try removing the node_modules and bun.lockb and try again yet?

yes

Fix implementation

#1591

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions