|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const { tspl } = require('@matteo.collina/tspl') |
| 4 | +const { test, after } = require('node:test') |
| 5 | +const { createServer } = require('node:http') |
| 6 | +const { once } = require('node:events') |
| 7 | +const { Client, errors } = require('..') |
| 8 | + |
| 9 | +function makeInjectingFn (payload) { |
| 10 | + const fn = function () {} |
| 11 | + fn.toString = () => payload |
| 12 | + return fn |
| 13 | +} |
| 14 | + |
| 15 | +function makeToPrimitiveFn (payload) { |
| 16 | + const fn = function () {} |
| 17 | + fn[Symbol.toPrimitive] = () => payload |
| 18 | + return fn |
| 19 | +} |
| 20 | + |
| 21 | +test('rejects function header value with CRLF via toString', async (t) => { |
| 22 | + t = tspl(t, { plan: 1 }) |
| 23 | + |
| 24 | + const client = new Client('http://localhost:8080') |
| 25 | + after(() => client.destroy()) |
| 26 | + |
| 27 | + t.rejects(client.request({ |
| 28 | + path: '/', |
| 29 | + method: 'GET', |
| 30 | + headers: { |
| 31 | + 'x-safe': makeInjectingFn('legit\r\ntransfer-encoding: chunked') |
| 32 | + } |
| 33 | + }), new errors.InvalidArgumentError('invalid x-safe header')) |
| 34 | + |
| 35 | + await t.completed |
| 36 | +}) |
| 37 | + |
| 38 | +test('rejects function header value with CRLF via Symbol.toPrimitive', async (t) => { |
| 39 | + t = tspl(t, { plan: 1 }) |
| 40 | + |
| 41 | + const client = new Client('http://localhost:8080') |
| 42 | + after(() => client.destroy()) |
| 43 | + |
| 44 | + t.rejects(client.request({ |
| 45 | + path: '/', |
| 46 | + method: 'GET', |
| 47 | + headers: { |
| 48 | + 'x-safe': makeToPrimitiveFn('legit\r\nx-injected: yes') |
| 49 | + } |
| 50 | + }), new errors.InvalidArgumentError('invalid x-safe header')) |
| 51 | + |
| 52 | + await t.completed |
| 53 | +}) |
| 54 | + |
| 55 | +test('rejects function header value in array element with CRLF via toString', async (t) => { |
| 56 | + t = tspl(t, { plan: 1 }) |
| 57 | + |
| 58 | + const client = new Client('http://localhost:8080') |
| 59 | + after(() => client.destroy()) |
| 60 | + |
| 61 | + t.rejects(client.request({ |
| 62 | + path: '/', |
| 63 | + method: 'GET', |
| 64 | + headers: { |
| 65 | + 'x-safe': [makeInjectingFn('legit\r\nx-injected: via-array')] |
| 66 | + } |
| 67 | + }), new errors.InvalidArgumentError('invalid x-safe header')) |
| 68 | + |
| 69 | + await t.completed |
| 70 | +}) |
| 71 | + |
| 72 | +test('rejects function header value in array element with CRLF via Symbol.toPrimitive', async (t) => { |
| 73 | + t = tspl(t, { plan: 1 }) |
| 74 | + |
| 75 | + const client = new Client('http://localhost:8080') |
| 76 | + after(() => client.destroy()) |
| 77 | + |
| 78 | + t.rejects(client.request({ |
| 79 | + path: '/', |
| 80 | + method: 'GET', |
| 81 | + headers: { |
| 82 | + 'x-safe': [makeToPrimitiveFn('legit\r\nx-injected: via-array-primitive')] |
| 83 | + } |
| 84 | + }), new errors.InvalidArgumentError('invalid x-safe header')) |
| 85 | + |
| 86 | + await t.completed |
| 87 | +}) |
| 88 | + |
| 89 | +test('still accepts number and boolean header values', async (t) => { |
| 90 | + t = tspl(t, { plan: 2 }) |
| 91 | + |
| 92 | + const server = createServer({ joinDuplicateHeaders: true }, (req, res) => { |
| 93 | + t.strictEqual(req.headers['x-num'], '123') |
| 94 | + t.strictEqual(req.headers['x-bool'], 'true') |
| 95 | + res.end('ok') |
| 96 | + }) |
| 97 | + after(() => server.close()) |
| 98 | + |
| 99 | + server.listen(0) |
| 100 | + await once(server, 'listening') |
| 101 | + |
| 102 | + const client = new Client(`http://localhost:${server.address().port}`) |
| 103 | + after(() => client.close()) |
| 104 | + |
| 105 | + const { body } = await client.request({ |
| 106 | + path: '/', |
| 107 | + method: 'GET', |
| 108 | + headers: { |
| 109 | + 'x-num': 123, |
| 110 | + 'x-bool': true |
| 111 | + } |
| 112 | + }) |
| 113 | + await body.dump() |
| 114 | + |
| 115 | + await t.completed |
| 116 | +}) |
0 commit comments