|
| 1 | +import type { Server } from 'node:http' |
| 2 | +import { createServer, request as httpRequest } 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 | + let proxyServer: Server |
| 34 | + let proxyPort: number |
| 35 | + let SKIP_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 | + let lastForwardedHeaders: Record<string, string> = {} |
| 41 | + let upstreamServer: Server |
| 42 | + let upstreamPort: number |
| 43 | + let realFetch: typeof globalThis.fetch |
| 44 | + |
| 45 | + beforeAll(async () => { |
| 46 | + upstreamServer = createServer((_req, res) => { |
| 47 | + res.writeHead(200, { 'content-type': 'application/json' }) |
| 48 | + res.end('{}') |
| 49 | + }) |
| 50 | + await new Promise<void>(resolve => upstreamServer.listen(0, resolve)) |
| 51 | + upstreamPort = (upstreamServer.address() as any).port |
| 52 | + |
| 53 | + realFetch = globalThis.fetch |
| 54 | + globalThis.fetch = async (input: any, init?: any) => { |
| 55 | + const reqUrl = typeof input === 'string' ? input : input.url |
| 56 | + const url = new URL(reqUrl) |
| 57 | + if (url.hostname === 'upstream.test') { |
| 58 | + // Capture the headers the proxy intended to forward |
| 59 | + const hdrs = (init?.headers ?? {}) as Record<string, string> |
| 60 | + lastForwardedHeaders = { ...hdrs } |
| 61 | + const redirected = `http://127.0.0.1:${upstreamPort}${url.pathname}${url.search}` |
| 62 | + return realFetch(redirected, { ...init, headers: {} }) |
| 63 | + } |
| 64 | + return realFetch(input, init) |
| 65 | + } |
| 66 | + |
| 67 | + const mod = await import('../../packages/script/src/runtime/server/proxy-handler') |
| 68 | + SKIP_REQUEST_HEADERS = mod.SKIP_REQUEST_HEADERS |
| 69 | + |
| 70 | + const app = createApp() |
| 71 | + app.use(mod.default) |
| 72 | + proxyServer = createServer(toNodeListener(app)) |
| 73 | + await new Promise<void>(resolve => proxyServer.listen(0, resolve)) |
| 74 | + proxyPort = (proxyServer.address() as any).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 (const h of [ |
| 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 | + function rawGet(headers: Record<string, string>) { |
| 106 | + return new Promise<void>((resolve, reject) => { |
| 107 | + const req = 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 | + await rawGet({ |
| 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 (const h of ['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