Skip to content
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

fix: corsify issue "can't modify immutable headers" #252

Open
wants to merge 1 commit into
base: v5.x
Choose a base branch
from

Conversation

nicolasvienot
Copy link

@nicolasvienot nicolasvienot commented Jun 27, 2024

Description

A fix was implemented in 74f63ab to clone the response in corsify in order to avoid this error:

{
  "status": 500,
  "error": "Can't modify immutable headers."
}

Unfortunately, it seems not to be enough, and we're still having the issue with Cloudflare when a response is modified by corsify. A solution we found and implemented is to recreate a response by using new Response(...).

Applying the fix here.

Related Issue

Link to the related issue: related to #242

Type of Change (select one and follow subtasks)

  • Bug fix (non-breaking change which fixes an issue)

@nicolasvienot
Copy link
Author

I can open a new issue if needed, please let me know 👍
it might also be related to: #249

@nicolasvienot nicolasvienot marked this pull request as ready for review June 27, 2024 10:06
@kwhitley
Copy link
Owner

kwhitley commented Jul 6, 2024

Thanks @nicolasvienot - can you give me minimal repro steps to see this immutable headers issue on a Worker? I use CORS on virtually all my Workers (which is what I exclusively develop on these days) and am not running into this...

I'd like to ensure this is absolutely necessary before adding so many bytes! :)

@aonnikov
Copy link

aonnikov commented Oct 15, 2024

@kwhitley I think you can use this sample to reproduce the issue:

import { Router, cors } from 'itty-router'

const router = Router({
  before: [preflight],
  finally: [corsify]
})

router.get('/sample', ({ params }) => {
  const cache = caches.default
  const cached = await cache.match(request)
  if (cached !== undefined) {
    return cached
  }

  const response = new Response(null, { status: 200 })
  ctx.waitUntil(cache.put(request, response.clone()))

  return response
})

return await router.fetch(request).catch(error)

When you access the first time, it returns 200, but the next requests will return 500 error.

{
  "status": 500,
  "error": "Can't modify immutable headers."
}

And there will be a warning message in worker logs:

A ReadableStream branch was created but never consumed. Such branches can be created, for instance, by calling the tee() method on a ReadableStream, or by calling the clone() method on a Request or Response object. If a branch is created but never consumed, it can force the runtime to buffer the entire body of the stream in memory, which may cause the Worker to exceed its memory limit and be terminated. To avoid this, ensure that all branches created are consumed.

 * Unused stream created:
    at corsify (file:///.../temp/node_modules/.pnpm/[email protected]/node_modules/src/src/cors.ts:81:44)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants