mirror of
https://github.com/UNITRONIX/BetterDesk.git
synced 2026-09-10 09:35:39 +00:00
7c449d7976
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.
35 lines
1.2 KiB
JavaScript
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();
|
|
}
|
|
});
|
|
});
|