|
| 1 | +import net from 'net' |
| 2 | +import http from 'http' |
| 3 | +import { createNext, NextInstance } from 'e2e-utils' |
| 4 | +import { findPort, retry } from 'next-test-utils' |
| 5 | + |
| 6 | +describe('rewrite-request-smuggling', () => { |
| 7 | + if ((global as any).isNextDeploy) { |
| 8 | + it('should skip deploy', () => {}) |
| 9 | + return |
| 10 | + } |
| 11 | + |
| 12 | + let backend: http.Server |
| 13 | + let backendPort: number |
| 14 | + let intermediary: http.Server |
| 15 | + let intermediaryPort: number |
| 16 | + let next: NextInstance |
| 17 | + const backendRequests: string[] = [] |
| 18 | + |
| 19 | + async function sendSmugglingPayload({ |
| 20 | + nextPort, |
| 21 | + connectionHeader, |
| 22 | + method = 'DELETE', |
| 23 | + rewritePath = '/rewrites/poc', |
| 24 | + }: { |
| 25 | + nextPort: number |
| 26 | + connectionHeader: string |
| 27 | + method?: 'DELETE' | 'OPTIONS' |
| 28 | + rewritePath?: string |
| 29 | + }) { |
| 30 | + const smuggledRequest = Buffer.from( |
| 31 | + `GET /secret HTTP/1.1\r\nHost: 127.0.0.1:${nextPort}\r\n\r\n`, |
| 32 | + 'latin1' |
| 33 | + ) |
| 34 | + const chunkSize = Buffer.from( |
| 35 | + `${smuggledRequest.length.toString(16).toUpperCase()}\r\n`, |
| 36 | + 'latin1' |
| 37 | + ) |
| 38 | + |
| 39 | + const payload = Buffer.concat([ |
| 40 | + Buffer.from( |
| 41 | + `${method} ${rewritePath} HTTP/1.1\r\nHost: 127.0.0.1:${nextPort}\r\nTransfer-Encoding: chunked\r\nConnection: ${connectionHeader}\r\n\r\n`, |
| 42 | + 'latin1' |
| 43 | + ), |
| 44 | + chunkSize, |
| 45 | + smuggledRequest, |
| 46 | + Buffer.from('\r\n0\r\n\r\n', 'latin1'), |
| 47 | + ]) |
| 48 | + |
| 49 | + await new Promise<void>((resolve, reject) => { |
| 50 | + const socket = net.createConnection({ |
| 51 | + host: '127.0.0.1', |
| 52 | + port: nextPort, |
| 53 | + }) |
| 54 | + |
| 55 | + socket.once('connect', () => { |
| 56 | + socket.write(payload) |
| 57 | + }) |
| 58 | + socket.once('error', reject) |
| 59 | + socket.setTimeout(1000, () => socket.destroy()) |
| 60 | + socket.once('close', () => resolve()) |
| 61 | + }) |
| 62 | + } |
| 63 | + |
| 64 | + beforeAll(async () => { |
| 65 | + backendPort = await findPort() |
| 66 | + intermediaryPort = await findPort() |
| 67 | + |
| 68 | + backend = http.createServer((req, res) => { |
| 69 | + backendRequests.push(`${req.method} ${req.url}`) |
| 70 | + |
| 71 | + if (req.url?.startsWith('/rewrites/')) { |
| 72 | + res.statusCode = 200 |
| 73 | + res.end('rewrite-ok') |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + if (req.url === '/secret') { |
| 78 | + res.statusCode = 200 |
| 79 | + res.end('secret') |
| 80 | + return |
| 81 | + } |
| 82 | + |
| 83 | + res.statusCode = 404 |
| 84 | + res.end('not-found') |
| 85 | + }) |
| 86 | + |
| 87 | + intermediary = http.createServer((req, res) => { |
| 88 | + const connectionHeader = Array.isArray(req.headers['connection']) |
| 89 | + ? req.headers['connection'].join(',') |
| 90 | + : req.headers['connection'] || '' |
| 91 | + const hopByHopHeaders = connectionHeader |
| 92 | + .split(',') |
| 93 | + .map((h) => h.trim().toLowerCase()) |
| 94 | + .filter(Boolean) |
| 95 | + const stripTransferEncodingUnconditionally = |
| 96 | + req.url?.startsWith('/rewrites/non-rfc-strip') || false |
| 97 | + |
| 98 | + const forwardHeaders: Record<string, string | string[]> = {} |
| 99 | + for (const [key, value] of Object.entries(req.headers)) { |
| 100 | + if (key === 'connection') continue |
| 101 | + if (stripTransferEncodingUnconditionally && key === 'transfer-encoding') |
| 102 | + continue |
| 103 | + if (hopByHopHeaders.includes(key)) continue |
| 104 | + if (value !== undefined) { |
| 105 | + forwardHeaders[key] = value |
| 106 | + } |
| 107 | + } |
| 108 | + forwardHeaders.connection = stripTransferEncodingUnconditionally |
| 109 | + ? connectionHeader.toLowerCase().includes('close') |
| 110 | + ? 'close' |
| 111 | + : 'keep-alive' |
| 112 | + : 'keep-alive' |
| 113 | + |
| 114 | + const proxyReq = http.request( |
| 115 | + { |
| 116 | + hostname: '127.0.0.1', |
| 117 | + port: backendPort, |
| 118 | + method: req.method, |
| 119 | + path: req.url, |
| 120 | + headers: forwardHeaders, |
| 121 | + }, |
| 122 | + (proxyRes) => { |
| 123 | + res.writeHead(proxyRes.statusCode || 500, proxyRes.headers) |
| 124 | + proxyRes.pipe(res) |
| 125 | + } |
| 126 | + ) |
| 127 | + |
| 128 | + proxyReq.on('error', () => { |
| 129 | + res.statusCode = 502 |
| 130 | + res.end('Bad Gateway') |
| 131 | + }) |
| 132 | + |
| 133 | + req.pipe(proxyReq) |
| 134 | + }) |
| 135 | + |
| 136 | + await new Promise<void>((resolve, reject) => { |
| 137 | + backend.listen(backendPort, '127.0.0.1', resolve) |
| 138 | + backend.once('error', reject) |
| 139 | + }) |
| 140 | + |
| 141 | + await new Promise<void>((resolve, reject) => { |
| 142 | + intermediary.listen(intermediaryPort, '127.0.0.1', resolve) |
| 143 | + intermediary.once('error', reject) |
| 144 | + }) |
| 145 | + |
| 146 | + next = await createNext({ |
| 147 | + files: __dirname, |
| 148 | + env: { |
| 149 | + TEST_INTERMEDIARY_PORT: String(intermediaryPort), |
| 150 | + }, |
| 151 | + }) |
| 152 | + }) |
| 153 | + |
| 154 | + afterAll(async () => { |
| 155 | + await next?.destroy() |
| 156 | + await new Promise<void>((resolve) => intermediary.close(() => resolve())) |
| 157 | + await new Promise<void>((resolve) => backend.close(() => resolve())) |
| 158 | + }) |
| 159 | + |
| 160 | + it('does not smuggle a second request when using keep-alive only', async () => { |
| 161 | + backendRequests.length = 0 |
| 162 | + |
| 163 | + const nextPort = Number(new URL(next.url).port) |
| 164 | + await sendSmugglingPayload({ nextPort, connectionHeader: 'keep-alive' }) |
| 165 | + |
| 166 | + await retry(async () => { |
| 167 | + expect(backendRequests).toContain('DELETE /rewrites/poc') |
| 168 | + }) |
| 169 | + expect(backendRequests).not.toContain('GET /secret') |
| 170 | + }) |
| 171 | + |
| 172 | + it('does not smuggle a second request with keep-alive, upgrade', async () => { |
| 173 | + backendRequests.length = 0 |
| 174 | + |
| 175 | + const nextPort = Number(new URL(next.url).port) |
| 176 | + await sendSmugglingPayload({ |
| 177 | + nextPort, |
| 178 | + connectionHeader: 'keep-alive, upgrade', |
| 179 | + }) |
| 180 | + |
| 181 | + await retry(async () => { |
| 182 | + expect(backendRequests).toContain('DELETE /rewrites/poc') |
| 183 | + }) |
| 184 | + expect(backendRequests).not.toContain('GET /secret') |
| 185 | + }) |
| 186 | + |
| 187 | + it('does not smuggle a second request with Transfer-Encoding, upgrade', async () => { |
| 188 | + backendRequests.length = 0 |
| 189 | + |
| 190 | + const nextPort = Number(new URL(next.url).port) |
| 191 | + await sendSmugglingPayload({ |
| 192 | + nextPort, |
| 193 | + connectionHeader: 'Transfer-Encoding, upgrade', |
| 194 | + }) |
| 195 | + |
| 196 | + await retry(async () => { |
| 197 | + expect(backendRequests).toContain('DELETE /rewrites/poc') |
| 198 | + }) |
| 199 | + expect(backendRequests).not.toContain('GET /secret') |
| 200 | + }) |
| 201 | + |
| 202 | + it('does not smuggle a second request for OPTIONS with Transfer-Encoding, upgrade', async () => { |
| 203 | + backendRequests.length = 0 |
| 204 | + |
| 205 | + const nextPort = Number(new URL(next.url).port) |
| 206 | + await sendSmugglingPayload({ |
| 207 | + nextPort, |
| 208 | + method: 'OPTIONS', |
| 209 | + connectionHeader: 'Transfer-Encoding, upgrade', |
| 210 | + }) |
| 211 | + |
| 212 | + await retry(async () => { |
| 213 | + expect(backendRequests).toContain('OPTIONS /rewrites/poc') |
| 214 | + }) |
| 215 | + expect(backendRequests).not.toContain('GET /secret') |
| 216 | + }) |
| 217 | + |
| 218 | + it('does not smuggle a second request when an intermediary strips transfer-encoding unconditionally', async () => { |
| 219 | + backendRequests.length = 0 |
| 220 | + |
| 221 | + const nextPort = Number(new URL(next.url).port) |
| 222 | + await sendSmugglingPayload({ |
| 223 | + nextPort, |
| 224 | + method: 'OPTIONS', |
| 225 | + rewritePath: '/rewrites/non-rfc-strip', |
| 226 | + connectionHeader: 'keep-alive, upgrade', |
| 227 | + }) |
| 228 | + |
| 229 | + await retry(async () => { |
| 230 | + expect(backendRequests).toContain('OPTIONS /rewrites/non-rfc-strip') |
| 231 | + }) |
| 232 | + expect(backendRequests).not.toContain('GET /secret') |
| 233 | + }) |
| 234 | +}) |
0 commit comments