Skip to content

Commit

Permalink
fix: don't delete headers in a redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Oct 11, 2022
1 parent 660365d commit 64daac1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
8 changes: 5 additions & 3 deletions packages/runtime/src/edge-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,11 @@ function getDispatchFetchCode() {
response.waitUntil = () => Promise.all(event.awaiting);
response.headers.delete('content-encoding');
response.headers.delete('transform-encoding');
response.headers.delete('content-length');
if (response.status < 300 || response.status >= 400 ) {
response.headers.delete('content-encoding');
response.headers.delete('transform-encoding');
response.headers.delete('content-length');
}
return response;
}
Expand Down
40 changes: 40 additions & 0 deletions packages/runtime/tests/edge-runtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,46 @@ beforeAll(async () => {
;({ EdgeRuntime } = await import('../src/edge-runtime'))
})

test("don't expose servers headers", async () => {
const runtime = new EdgeRuntime()

runtime.evaluate(`
addEventListener("fetch", (event) => {
event.respondWith(new Response('success', {
headers: {
'content-encoding': 'gzip',
'transform-encoding': 'compress',
'content-length': 7
}
}))
})
`)

const res = await runtime.dispatchFetch('https://localhost.com')
expect(res.status).toBe(200)
expect(await res.text()).toBe('success')
expect(Array.from(res.headers)).toEqual([
['content-type', 'text/plain;charset=UTF-8'],
])
})

test("don't expose servers headers in a redirect", async () => {
const runtime = new EdgeRuntime()

runtime.evaluate(`
addEventListener("fetch", (event) => {
event.respondWith(Response.redirect(new URL('https://example.vercel.sh')));
})
`)

const res = await runtime.dispatchFetch('https://localhost.com')

expect(Array.from(res.headers)).toEqual([
['location', 'https://example.vercel.sh/'],
])
expect(res.status).toBe(302)
})

test('allows to add FetchEvent handlers', async () => {
const runtime = new EdgeRuntime()

Expand Down

0 comments on commit 64daac1

Please sign in to comment.