Files
BetterDesk/web-nodejs/tests/parseTrustProxy.test.js
UNITRONIX 7c449d7976 Fix console crash when TRUST_PROXY=true in .env (#163)
Parse TRUST_PROXY env values safely so the string "true" maps to a single
proxy hop instead of crashing Express at startup with invalid IP address.
2026-06-05 02:14:40 +02:00

35 lines
1.2 KiB
JavaScript

'use strict';
const express = require('express');
const { parseTrustProxy } = require('../lib/parseTrustProxy');
describe('parseTrustProxy', () => {
it('defaults to false when unset', () => {
expect(parseTrustProxy(undefined)).toBe(false);
expect(parseTrustProxy('')).toBe(false);
});
it('maps common boolean strings to Express-safe values', () => {
expect(parseTrustProxy('false')).toBe(false);
expect(parseTrustProxy('true')).toBe(1);
expect(parseTrustProxy('yes')).toBe(1);
expect(parseTrustProxy('Y')).toBe(1);
});
it('passes numeric hop counts through', () => {
expect(parseTrustProxy('2')).toBe(2);
});
it('passes Express keywords and CIDR values through', () => {
expect(parseTrustProxy('loopback')).toBe('loopback');
expect(parseTrustProxy('10.0.0.0/8')).toBe('10.0.0.0/8');
});
it('does not throw when applied to Express (issue #163)', () => {
for (const raw of [undefined, 'false', 'true', '1', 'loopback']) {
const app = express();
expect(() => app.set('trust proxy', parseTrustProxy(raw))).not.toThrow();
}
});
});