|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +import { expect } from 'chai'; |
| 4 | +import { resolveTrustProxyHops } from '../../../src/utils/trust-proxy.js'; |
| 5 | + |
| 6 | +// Pure-function spec for the TRUST_PROXY config resolver. Lives under |
| 7 | +// tests/v2/integration so it runs in the V2 mocha bucket, but the helper |
| 8 | +// itself is version-agnostic. |
| 9 | +// |
| 10 | +// The behaviour matters because the previous implementation used |
| 11 | +// `parseInt(rawValue, 10)`, which silently produced `NaN` for booleans |
| 12 | +// and non-numeric strings. `app.set('trust proxy', NaN)` is treated as |
| 13 | +// falsy by Express, so an operator who set `TRUST_PROXY=true` thinking |
| 14 | +// they were enabling proxy trust would actually disable it without any |
| 15 | +// log message. |
| 16 | +describe('resolveTrustProxyHops', function () { |
| 17 | + const makeLog = () => { |
| 18 | + const warnings = []; |
| 19 | + return { |
| 20 | + warnings, |
| 21 | + logger: { |
| 22 | + warn: (msg) => warnings.push(msg), |
| 23 | + }, |
| 24 | + }; |
| 25 | + }; |
| 26 | + |
| 27 | + describe('valid integer inputs (no warning)', function () { |
| 28 | + const validCases = [ |
| 29 | + { label: '0 (number)', input: 0, expected: 0 }, |
| 30 | + { label: '1 (number)', input: 1, expected: 1 }, |
| 31 | + { label: '2 (number)', input: 2, expected: 2 }, |
| 32 | + { label: '"0" (string)', input: '0', expected: 0 }, |
| 33 | + { label: '"1" (string)', input: '1', expected: 1 }, |
| 34 | + { label: '" 2 " (padded string)', input: ' 2 ', expected: 2 }, |
| 35 | + { label: '10 (large hop count)', input: 10, expected: 10 }, |
| 36 | + ]; |
| 37 | + |
| 38 | + validCases.forEach(({ label, input, expected }) => { |
| 39 | + it(`returns ${expected} for ${label}`, function () { |
| 40 | + const { logger, warnings } = makeLog(); |
| 41 | + expect(resolveTrustProxyHops(input, logger)).to.equal(expected); |
| 42 | + expect(warnings, 'no warnings expected for valid input').to.have.length(0); |
| 43 | + }); |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + describe('empty / unset inputs default to 0 silently', function () { |
| 48 | + [ |
| 49 | + { label: 'undefined', input: undefined }, |
| 50 | + { label: 'null', input: null }, |
| 51 | + { label: '"" (empty string)', input: '' }, |
| 52 | + { label: '" " (whitespace-only string)', input: ' ' }, |
| 53 | + ].forEach(({ label, input }) => { |
| 54 | + it(`returns 0 for ${label} with no warning`, function () { |
| 55 | + const { logger, warnings } = makeLog(); |
| 56 | + expect(resolveTrustProxyHops(input, logger)).to.equal(0); |
| 57 | + expect(warnings, 'no warnings expected for unset input').to.have.length(0); |
| 58 | + }); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + describe('boolean inputs fall back to 0 with a warning', function () { |
| 63 | + // Booleans are the practical regression we are fixing: docker-entrypoint.sh |
| 64 | + // converts the env strings "true"/"false" to YAML booleans, and the |
| 65 | + // previous parseInt() produced NaN for both. |
| 66 | + [true, false].forEach((input) => { |
| 67 | + it(`returns 0 and warns for boolean \`${input}\``, function () { |
| 68 | + const { logger, warnings } = makeLog(); |
| 69 | + expect(resolveTrustProxyHops(input, logger)).to.equal(0); |
| 70 | + expect(warnings).to.have.length(1); |
| 71 | + expect(warnings[0]).to.match(/TRUST_PROXY/); |
| 72 | + expect(warnings[0]).to.include(String(input)); |
| 73 | + }); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('invalid number inputs fall back to 0 with a warning', function () { |
| 78 | + [ |
| 79 | + { label: 'NaN', input: Number.NaN }, |
| 80 | + { label: 'Infinity', input: Number.POSITIVE_INFINITY }, |
| 81 | + { label: '-Infinity', input: Number.NEGATIVE_INFINITY }, |
| 82 | + { label: '-1 (negative)', input: -1 }, |
| 83 | + { label: '1.5 (non-integer)', input: 1.5 }, |
| 84 | + ].forEach(({ label, input }) => { |
| 85 | + it(`returns 0 and warns for ${label}`, function () { |
| 86 | + const { logger, warnings } = makeLog(); |
| 87 | + expect(resolveTrustProxyHops(input, logger)).to.equal(0); |
| 88 | + expect(warnings).to.have.length(1); |
| 89 | + expect(warnings[0]).to.match(/TRUST_PROXY/); |
| 90 | + }); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + describe('non-integer string inputs fall back to 0 with a warning', function () { |
| 95 | + [ |
| 96 | + 'true', |
| 97 | + 'false', |
| 98 | + 'loopback', |
| 99 | + 'linklocal', |
| 100 | + '10.0.0.1', |
| 101 | + '1.5', |
| 102 | + '+2', |
| 103 | + '-1', |
| 104 | + '2 hops', |
| 105 | + 'two', |
| 106 | + ].forEach((input) => { |
| 107 | + it(`returns 0 and warns for "${input}"`, function () { |
| 108 | + const { logger, warnings } = makeLog(); |
| 109 | + expect(resolveTrustProxyHops(input, logger)).to.equal(0); |
| 110 | + expect(warnings).to.have.length(1); |
| 111 | + expect(warnings[0]).to.match(/TRUST_PROXY/); |
| 112 | + expect(warnings[0]).to.include(input); |
| 113 | + }); |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + describe('unsupported types fall back to 0 with a warning', function () { |
| 118 | + [ |
| 119 | + { label: 'array', input: [1, 2, 3] }, |
| 120 | + { label: 'object', input: { hops: 1 } }, |
| 121 | + { label: 'function', input: () => 1 }, |
| 122 | + ].forEach(({ label, input }) => { |
| 123 | + it(`returns 0 and warns for ${label}`, function () { |
| 124 | + const { logger, warnings } = makeLog(); |
| 125 | + expect(resolveTrustProxyHops(input, logger)).to.equal(0); |
| 126 | + expect(warnings).to.have.length(1); |
| 127 | + expect(warnings[0]).to.match(/TRUST_PROXY/); |
| 128 | + }); |
| 129 | + }); |
| 130 | + }); |
| 131 | + |
| 132 | + describe('default logger argument', function () { |
| 133 | + // Every other case explicitly injects a stub logger. This case covers |
| 134 | + // the production call-site shape (no second argument) so the default |
| 135 | + // logger path is exercised at least once. We do not assert against |
| 136 | + // the real logger's output because that would couple this spec to |
| 137 | + // winston transport behaviour; we only assert the resolved value. |
| 138 | + it('returns the correct integer without throwing when no logger is supplied', function () { |
| 139 | + expect(resolveTrustProxyHops(2)).to.equal(2); |
| 140 | + expect(resolveTrustProxyHops(0)).to.equal(0); |
| 141 | + }); |
| 142 | + |
| 143 | + it('falls back to 0 without throwing when no logger is supplied (invalid input)', function () { |
| 144 | + // The real logger should be invoked here; we accept whatever side |
| 145 | + // effects winston produces and only check the return value. |
| 146 | + expect(resolveTrustProxyHops(true)).to.equal(0); |
| 147 | + expect(resolveTrustProxyHops('loopback')).to.equal(0); |
| 148 | + }); |
| 149 | + }); |
| 150 | + |
| 151 | + describe('regression coverage for the parseInt() bug', function () { |
| 152 | + // The previous implementation was: |
| 153 | + // const trustProxy = parseInt(getConfig().APP.TRUST_PROXY ?? 0, 10); |
| 154 | + // which produced NaN for all of these inputs. |
| 155 | + const regressions = [true, false, 'true', 'false', 'loopback']; |
| 156 | + |
| 157 | + regressions.forEach((input) => { |
| 158 | + it(`does not produce NaN for legacy regression input ${JSON.stringify(input)}`, function () { |
| 159 | + const { logger } = makeLog(); |
| 160 | + const result = resolveTrustProxyHops(input, logger); |
| 161 | + expect(result).to.be.a('number'); |
| 162 | + expect(Number.isNaN(result), 'must not be NaN').to.equal(false); |
| 163 | + expect(Number.isFinite(result), 'must be finite').to.equal(true); |
| 164 | + }); |
| 165 | + }); |
| 166 | + }); |
| 167 | +}); |
0 commit comments