|
| 1 | +importtype{Server}from'node:http' |
| 2 | +import{createServer,requestashttpRequest}from'node:http' |
| 3 | +import{createApp,toNodeListener}from'h3' |
| 4 | +import{afterAll,beforeAll,beforeEach,describe,expect,it,vi}from'vitest' |
| 5 | + |
| 6 | +/** |
| 7 | + * Tests for #791: proxy handler must strip hop-by-hop request headers per RFC 7230 §6.1. |
| 8 | + * |
| 9 | + * Hop-by-hop headers are connection-specific and must not be forwarded by a proxy: |
| 10 | + * connection, keep-alive, proxy-authenticate, proxy-authorization, |
| 11 | + * te, trailer, transfer-encoding, upgrade |
| 12 | + * |
| 13 | + * Additionally, any header named in the `Connection` header value must also be stripped. |
| 14 | + */ |
| 15 | + |
| 16 | +vi.mock('nitropack/runtime',()=>({ |
| 17 | +useRuntimeConfig: ()=>({ |
| 18 | +'nuxt-scripts-proxy': { |
| 19 | +proxyPrefix: '/_scripts/p', |
| 20 | +domainPrivacy: { |
| 21 | +'upstream.test': false, |
| 22 | +}, |
| 23 | +privacy: false, |
| 24 | +debug: false, |
| 25 | +}, |
| 26 | +}), |
| 27 | +useNitroApp: ()=>({ |
| 28 | +hooks: {callHook: async()=>{}}, |
| 29 | +}), |
| 30 | +})) |
| 31 | + |
| 32 | +describe('proxy handler - hop-by-hop request headers (#791)',()=>{ |
| 33 | +letproxyServer: Server |
| 34 | +letproxyPort: number |
| 35 | +letSKIP_REQUEST_HEADERS: Set<string> |
| 36 | + |
| 37 | +// Intercept the fetch call inside the handler so we can inspect the headers |
| 38 | +// that would actually be forwarded upstream (before the Node http client |
| 39 | +// re-adds its own connection/host headers). |
| 40 | +letlastForwardedHeaders: Record<string,string>={} |
| 41 | +letupstreamServer: Server |
| 42 | +letupstreamPort: number |
| 43 | +letrealFetch: typeofglobalThis.fetch |
| 44 | + |
| 45 | +beforeAll(async()=>{ |
| 46 | +upstreamServer=createServer((_req,res)=>{ |
| 47 | +res.writeHead(200,{'content-type': 'application/json'}) |
| 48 | +res.end('{}') |
| 49 | +}) |
| 50 | +awaitnewPromise<void>(resolve=>upstreamServer.listen(0,resolve)) |
| 51 | +upstreamPort=(upstreamServer.address()asany).port |
| 52 | + |
| 53 | +realFetch=globalThis.fetch |
| 54 | +globalThis.fetch=async(input: any,init?: any)=>{ |
| 55 | +constreqUrl=typeofinput==='string' ? input : input.url |
| 56 | +consturl=newURL(reqUrl) |
| 57 | +if(url.hostname==='upstream.test'){ |
| 58 | +// Capture the headers the proxy intended to forward |
| 59 | +consthdrs=(init?.headers??{})asRecord<string,string> |
| 60 | +lastForwardedHeaders={ ...hdrs} |
| 61 | +constredirected=`http://127.0.0.1:${upstreamPort}${url.pathname}${url.search}` |
| 62 | +returnrealFetch(redirected,{ ...init,headers: {}}) |
| 63 | +} |
| 64 | +returnrealFetch(input,init) |
| 65 | +} |
| 66 | + |
| 67 | +constmod=awaitimport('../../packages/script/src/runtime/server/proxy-handler') |
| 68 | +SKIP_REQUEST_HEADERS=mod.SKIP_REQUEST_HEADERS |
| 69 | + |
| 70 | +constapp=createApp() |
| 71 | +app.use(mod.default) |
| 72 | +proxyServer=createServer(toNodeListener(app)) |
| 73 | +awaitnewPromise<void>(resolve=>proxyServer.listen(0,resolve)) |
| 74 | +proxyPort=(proxyServer.address()asany).port |
| 75 | +}) |
| 76 | + |
| 77 | +beforeEach(()=>{ |
| 78 | +lastForwardedHeaders={} |
| 79 | +}) |
| 80 | + |
| 81 | +afterAll(()=>{ |
| 82 | +if(realFetch) |
| 83 | +globalThis.fetch=realFetch |
| 84 | +upstreamServer?.close() |
| 85 | +proxyServer?.close() |
| 86 | +}) |
| 87 | + |
| 88 | +it('exports SKIP_REQUEST_HEADERS containing all RFC 7230 §6.1 hop-by-hop headers',()=>{ |
| 89 | +expect(SKIP_REQUEST_HEADERS).toBeInstanceOf(Set) |
| 90 | +for(consthof[ |
| 91 | +'connection', |
| 92 | +'keep-alive', |
| 93 | +'proxy-authenticate', |
| 94 | +'proxy-authorization', |
| 95 | +'te', |
| 96 | +'trailer', |
| 97 | +'transfer-encoding', |
| 98 | +'upgrade', |
| 99 | +]){ |
| 100 | +expect(SKIP_REQUEST_HEADERS.has(h)).toBe(true) |
| 101 | +} |
| 102 | +}) |
| 103 | + |
| 104 | +// Use raw http.request because undici (global fetch) rejects forbidden hop-by-hop request headers. |
| 105 | +functionrawGet(headers: Record<string,string>){ |
| 106 | +returnnewPromise<void>((resolve,reject)=>{ |
| 107 | +constreq=httpRequest({ |
| 108 | +host: '127.0.0.1', |
| 109 | +port: proxyPort, |
| 110 | +path: '/_scripts/p/upstream.test/collect', |
| 111 | +method: 'GET', |
| 112 | + headers, |
| 113 | +},(res)=>{ |
| 114 | +res.resume() |
| 115 | +res.on('end',()=>resolve()) |
| 116 | +}) |
| 117 | +req.on('error',reject) |
| 118 | +req.end() |
| 119 | +}) |
| 120 | +} |
| 121 | + |
| 122 | +it('strips hop-by-hop request headers before forwarding upstream',async()=>{ |
| 123 | +awaitrawGet({ |
| 124 | +'connection': 'keep-alive, X-Custom-Hop', |
| 125 | +'keep-alive': 'timeout=5', |
| 126 | +'proxy-authorization': 'Bearer secret', |
| 127 | +'te': 'trailers', |
| 128 | +'x-custom-hop': 'should-not-forward', |
| 129 | +'accept': 'application/json', |
| 130 | +'user-agent': 'test-agent', |
| 131 | +}) |
| 132 | + |
| 133 | +for(consthof['connection','keep-alive','proxy-authorization','te','trailer','transfer-encoding','upgrade']){ |
| 134 | +expect(lastForwardedHeaders[h],`hop-by-hop "${h}" should not be forwarded`).toBeUndefined() |
| 135 | +} |
| 136 | +// Header named in Connection header is stripped too |
| 137 | +expect(lastForwardedHeaders['x-custom-hop']).toBeUndefined() |
| 138 | +// Non-hop-by-hop headers pass through |
| 139 | +expect(lastForwardedHeaders.accept).toBe('application/json') |
| 140 | +expect(lastForwardedHeaders['user-agent']).toBe('test-agent') |
| 141 | +}) |
| 142 | +}) |
0 commit comments