|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const { test } = require('node:test') |
| 4 | +const { tspl } = require('@matteo.collina/tspl') |
| 5 | +const { createServer } = require('node:http') |
| 6 | +const { once } = require('node:events') |
| 7 | +const { request } = require('..') |
| 8 | + |
| 9 | +test('Cross-origin redirects clear forbidden headers', async (t) => { |
| 10 | + const { strictEqual } = tspl(t, { plan: 6 }) |
| 11 | + |
| 12 | + const server1 = createServer((req, res) => { |
| 13 | + strictEqual(req.headers.cookie, undefined) |
| 14 | + strictEqual(req.headers.authorization, undefined) |
| 15 | + strictEqual(req.headers['proxy-authorization'], undefined) |
| 16 | + |
| 17 | + res.end('redirected') |
| 18 | + }).listen(0) |
| 19 | + |
| 20 | + const server2 = createServer((req, res) => { |
| 21 | + strictEqual(req.headers.authorization, 'test') |
| 22 | + strictEqual(req.headers.cookie, 'ddd=dddd') |
| 23 | + |
| 24 | + res.writeHead(302, { |
| 25 | + ...req.headers, |
| 26 | + Location: `http://localhost:${server1.address().port}` |
| 27 | + }) |
| 28 | + res.end() |
| 29 | + }).listen(0) |
| 30 | + |
| 31 | + t.after(() => { |
| 32 | + server1.close() |
| 33 | + server2.close() |
| 34 | + }) |
| 35 | + |
| 36 | + await Promise.all([ |
| 37 | + once(server1, 'listening'), |
| 38 | + once(server2, 'listening') |
| 39 | + ]) |
| 40 | + |
| 41 | + const res = await request(`http://localhost:${server2.address().port}`, { |
| 42 | + maxRedirections: 1, |
| 43 | + headers: { |
| 44 | + Authorization: 'test', |
| 45 | + Cookie: 'ddd=dddd', |
| 46 | + 'Proxy-Authorization': 'test' |
| 47 | + } |
| 48 | + }) |
| 49 | + |
| 50 | + const text = await res.body.text() |
| 51 | + strictEqual(text, 'redirected') |
| 52 | +}) |
0 commit comments