Skip to content
Myr

fix(proxy): Correct hop-by-hop header handling per RFC 9110 (#4459)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* chore: fix hop-by-headers * test(proxy): fix test for hop-by-hop headers * Update src/helper/proxy/index.ts * reflect comments * ci: apply automated fixes * handling invalid header * throw error ---------

authored by sugar-cat · committed by GitHub · parent 4b796cf · Browse files · 2 files+115 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Taku Amano <taku@taaas.jp>
  • Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>

Diff stats

2 files+115 added−7 removed

  • tests:+59 added−2 removed
  • other:+56 added−5 removed

2 files+115 added−7 removed

modified:src/helper/proxy/index.test.ts+59 added-2 removed
138 unmodified lines
139 })140141 it('remove hop-by-hop headers', async () => {142 const app = new Hono()143 app.get('/proxy/:path', (c) => proxy(`https://example.com/${c.req.param('path')}`))143 app.get('/proxy/:path', (c) => proxy(`https://example.com/${c.req.param('path')}`, c.req))144145 const res = await app.request('/proxy/hop-by-hop', {146 headers: {147 Host: 'example.com',147 Connection: 'keep-alive',148 Connection: 'keep-alive, custom-header',149 'Keep-Alive': 'timeout=5, max=1000',150 'Proxy-Authorization': 'Basic 123456',151 'Custom-Header': 'test',152 'Allowed-Custom-Header': 'test',153 },154 })155 const req = (global.fetch as ReturnType<typeof vi.fn>).mock.calls[0][0]156157 expect(req.headers.get('Host')).toBe('example.com')158 expect(req.headers.get('Connection')).toBeNull()159 expect(req.headers.get('Keep-Alive')).toBeNull()160 expect(req.headers.get('Proxy-Authorization')).toBeNull()161 expect(req.headers.get('Custom-Header')).toBe('test')162 expect(req.headers.get('Allowed-Custom-Header')).toBe('test')163164 expect(res.headers.get('Transfer-Encoding')).toBeNull()165 })166167 it('remove hop-by-hop headers with strictConnectionProcessing', async () => {168 const app = new Hono()169 app.get('/proxy/:path', (c) =>170 proxy(`https://example.com/${c.req.param('path')}`, {171 ...c.req,172 strictConnectionProcessing: true,173 })174 )175176 const res = await app.request('/proxy/hop-by-hop', {177 headers: {178 Host: 'example.com',179 Connection: 'keep-alive, custom-header',180 'Keep-Alive': 'timeout=5, max=1000',181 'Proxy-Authorization': 'Basic 123456',182 'Custom-Header': 'test',183 'Allowed-Custom-Header': 'test',184 },185 })186 const req = (global.fetch as ReturnType<typeof vi.fn>).mock.calls[0][0]187188 expect(req.headers.get('Host')).toBe('example.com')189 expect(req.headers.get('Connection')).toBeNull()190 expect(req.headers.get('Keep-Alive')).toBeNull()191 expect(req.headers.get('Proxy-Authorization')).toBeNull()192 expect(req.headers.get('Custom-Header')).toBeNull()193 expect(req.headers.get('Allowed-Custom-Header')).toBe('test')194195 expect(res.headers.get('Transfer-Encoding')).toBeNull()196 })197198 it('invalid hop-by-hop headers with strictConnectionProcessing', async () => {199 const app = new Hono()200 app.get('/proxy/:path', (c) =>201 proxy(`https://example.com/${c.req.param('path')}`, {202 ...c.req,203 strictConnectionProcessing: true,204 })205 )206207 const res = await app.request('/proxy/hop-by-hop', {208 headers: {209 Host: 'example.com',210 Connection: 'keep-alive, invalid-header invalid-header',211 'Keep-Alive': 'timeout=5, max=1000',212 },213 })214215 expect(res.status).toBe(400)216 })217218 it('specify hop-by-hop header by options', async () => {219 const app = new Hono()220 app.get('/proxy/:path', (c) =>221 proxy(`https://example.com/${c.req.param('path')}`, {
115 unmodified lines
modified:src/helper/proxy/index.ts+56 added-5 removed