strong-tie/inbound-calls
0
1'use strict'2 3const { test, before } = require('node:test')4const sget = require('simple-get').concat5const fastify = require('..')6const helper = require('./helper')7 8const noop = () => {}9 10const sgetForwardedRequest = (app, forHeader, path, protoHeader, testCaseDone) => {11 const headers = {12 'X-Forwarded-For': forHeader,13 'X-Forwarded-Host': 'example.com'14 }15 if (protoHeader) {16 headers['X-Forwarded-Proto'] = protoHeader17 }18 sget({19 method: 'GET',20 headers,21 url: 'http://localhost:' + app.server.address().port + path22 }, testCaseDone || noop)23}24 25const testRequestValues = (t, req, options) => {26 if (options.ip) {27 t.assert.ok(req.ip, 'ip is defined')28 t.assert.strictEqual(req.ip, options.ip, 'gets ip from x-forwarded-for')29 }30 if (options.host) {31 t.assert.ok(req.host, 'host is defined')32 t.assert.strictEqual(req.host, options.host, 'gets host from x-forwarded-host')33 t.assert.ok(req.hostname)34 t.assert.strictEqual(req.hostname, options.host, 'gets hostname from x-forwarded-host')35 }36 if (options.ips) {37 t.assert.deepStrictEqual(req.ips, options.ips, 'gets ips from x-forwarded-for')38 }39 if (options.protocol) {40 t.assert.ok(req.protocol, 'protocol is defined')41 t.assert.strictEqual(req.protocol, options.protocol, 'gets protocol from x-forwarded-proto')42 }43 if (options.port) {44 t.assert.ok(req.port, 'port is defined')45 t.assert.strictEqual(req.port, options.port, 'port is taken from x-forwarded-for or host')46 }47}48 49let localhost50before(async function () {51 [localhost] = await helper.getLoopbackHost()52})53 54test('trust proxy, not add properties to node req', (t, done) => {55 t.plan(14)56 const app = fastify({57 trustProxy: true58 })59 t.after(() => app.close())60 61 app.get('/trustproxy', function (req, reply) {62 testRequestValues(t, req, { ip: '1.1.1.1', host: 'example.com', port: app.server.address().port })63 reply.code(200).send({ ip: req.ip, host: req.host })64 })65 66 app.get('/trustproxychain', function (req, reply) {67 testRequestValues(t, req, { ip: '2.2.2.2', ips: [localhost, '1.1.1.1', '2.2.2.2'], port: app.server.address().port })68 reply.code(200).send({ ip: req.ip, host: req.host })69 })70 71 app.listen({ port: 0 }, (err) => {72 app.server.unref()73 t.assert.ifError(err)74 75 sgetForwardedRequest(app, '1.1.1.1', '/trustproxy', undefined, completed)76 sgetForwardedRequest(app, '2.2.2.2, 1.1.1.1', '/trustproxychain', undefined, completed)77 78 let pending = 279 function completed () {80 if (--pending === 0) {81 done()82 }83 }84 })85})86 87test('trust proxy chain', (t, done) => {88 t.plan(9)89 const app = fastify({90 trustProxy: [localhost, '192.168.1.1']91 })92 93 app.get('/trustproxychain', function (req, reply) {94 testRequestValues(t, req, { ip: '1.1.1.1', host: 'example.com', port: app.server.address().port })95 reply.code(200).send({ ip: req.ip, host: req.host })96 })97 98 app.listen({ port: 0 }, (err) => {99 app.server.unref()100 t.assert.ifError(err)101 t.after(() => app.close())102 sgetForwardedRequest(app, '192.168.1.1, 1.1.1.1', '/trustproxychain', undefined, done)103 })104})105 106test('trust proxy function', (t, done) => {107 t.plan(9)108 const app = fastify({109 trustProxy: (address) => address === localhost110 })111 app.get('/trustproxyfunc', function (req, reply) {112 testRequestValues(t, req, { ip: '1.1.1.1', host: 'example.com', port: app.server.address().port })113 reply.code(200).send({ ip: req.ip, host: req.host })114 })115 116 app.listen({ port: 0 }, (err) => {117 app.server.unref()118 t.assert.ifError(err)119 t.after(() => app.close())120 sgetForwardedRequest(app, '1.1.1.1', '/trustproxyfunc', undefined, done)121 })122})123 124test('trust proxy number', (t, done) => {125 t.plan(10)126 const app = fastify({127 trustProxy: 1128 })129 app.get('/trustproxynumber', function (req, reply) {130 testRequestValues(t, req, { ip: '1.1.1.1', ips: [localhost, '1.1.1.1'], host: 'example.com', port: app.server.address().port })131 reply.code(200).send({ ip: req.ip, host: req.host })132 })133 134 app.listen({ port: 0 }, (err) => {135 app.server.unref()136 t.assert.ifError(err)137 t.after(() => app.close())138 sgetForwardedRequest(app, '2.2.2.2, 1.1.1.1', '/trustproxynumber', undefined, done)139 })140})141 142test('trust proxy IP addresses', (t, done) => {143 t.plan(10)144 const app = fastify({145 trustProxy: `${localhost}, 2.2.2.2`146 })147 app.get('/trustproxyipaddrs', function (req, reply) {148 testRequestValues(t, req, { ip: '1.1.1.1', ips: [localhost, '1.1.1.1'], host: 'example.com', port: app.server.address().port })149 reply.code(200).send({ ip: req.ip, host: req.host })150 })151 152 app.listen({ port: 0 }, (err) => {153 app.server.unref()154 t.assert.ifError(err)155 t.after(() => app.close())156 sgetForwardedRequest(app, '3.3.3.3, 2.2.2.2, 1.1.1.1', '/trustproxyipaddrs', undefined, done)157 })158})159 160test('trust proxy protocol', (t, done) => {161 t.plan(31)162 const app = fastify({163 trustProxy: true164 })165 app.get('/trustproxyprotocol', function (req, reply) {166 testRequestValues(t, req, { ip: '1.1.1.1', protocol: 'lorem', host: 'example.com', port: app.server.address().port })167 reply.code(200).send({ ip: req.ip, host: req.host })168 })169 app.get('/trustproxynoprotocol', function (req, reply) {170 testRequestValues(t, req, { ip: '1.1.1.1', protocol: 'http', host: 'example.com', port: app.server.address().port })171 reply.code(200).send({ ip: req.ip, host: req.host })172 })173 app.get('/trustproxyprotocols', function (req, reply) {174 testRequestValues(t, req, { ip: '1.1.1.1', protocol: 'dolor', host: 'example.com', port: app.server.address().port })175 reply.code(200).send({ ip: req.ip, host: req.host })176 })177 178 t.after(() => app.close())179 180 app.listen({ port: 0 }, (err) => {181 app.server.unref()182 t.assert.ifError(err)183 sgetForwardedRequest(app, '1.1.1.1', '/trustproxyprotocol', 'lorem')184 sgetForwardedRequest(app, '1.1.1.1', '/trustproxynoprotocol')185 // Allow for sgetForwardedRequest requests above to finish186 setTimeout(() => sgetForwardedRequest(app, '1.1.1.1', '/trustproxyprotocols', 'ipsum, dolor', done))187 })188})189 