File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -50,6 +50,16 @@ describe('Proxy Middleware', () => {
5050 } ,
5151 } )
5252 )
53+ } else if ( req . url === 'https://example.com/connection-listed' ) {
54+ return Promise . resolve (
55+ new Response ( 'ok' , {
56+ headers : {
57+ Connection : 'x-connection-scoped, bad name' ,
58+ 'X-Connection-Scoped' : 'should not be forwarded' ,
59+ 'X-Normal' : 'kept' ,
60+ } ,
61+ } )
62+ )
5363 } else if ( req . url === 'https://example.com/set-cookie' ) {
5464 return Promise . resolve (
5565 new Response ( 'ok' , {
@@ -233,6 +243,16 @@ describe('Proxy Middleware', () => {
233243 expect ( res . headers . get ( 'Transfer-Encoding' ) ) . toBeNull ( )
234244 } )
235245
246+ it ( 'remove response headers listed in the Connection header' , async ( ) => {
247+ const app = new Hono ( )
248+ app . get ( '/proxy/:path' , ( c ) => proxy ( `https://example.com/${ c . req . param ( 'path' ) } ` ) )
249+ const res = await app . request ( '/proxy/connection-listed' )
250+ expect ( res . status ) . toBe ( 200 )
251+ expect ( res . headers . get ( 'Connection' ) ) . toBeNull ( )
252+ expect ( res . headers . get ( 'X-Connection-Scoped' ) ) . toBeNull ( )
253+ expect ( res . headers . get ( 'X-Normal' ) ) . toBe ( 'kept' )
254+ } )
255+
236256 it ( 'modify header' , async ( ) => {
237257 const app = new Hono ( )
238258 app . get ( '/proxy/:path' , ( c ) =>
Original file line number Diff line number Diff line change @@ -173,6 +173,18 @@ export const proxy: ProxyFetch = async (input, proxyInit) => {
173173
174174 const res = await ( customFetch || fetch ) ( req )
175175 const resHeaders = new Headers ( res . headers )
176+
177+ // https://datatracker.ietf.org/doc/html/rfc9110#section-7.6.1
178+ // Remove headers listed in the response's own Connection header (MUST per RFC 9110)
179+ const connectionValue = resHeaders . get ( 'connection' )
180+ if ( connectionValue ) {
181+ connectionValue
182+ . split ( ',' )
183+ . map ( ( h ) => h . trim ( ) )
184+ . filter ( ( h ) => ALLOWED_TOKEN_PATTERN . test ( h ) )
185+ . forEach ( ( h ) => resHeaders . delete ( h ) )
186+ }
187+
176188 hopByHopHeaders . forEach ( ( header ) => {
177189 resHeaders . delete ( header )
178190 } )
You can’t perform that action at this time.
0 commit comments