Fix ordering to allow X-Forwarded-For to be correctly managed by Cloudflare

This commit is contained in:
Nick Maynard 2025-09-08 23:05:22 +01:00
parent 1b6da0aaa0
commit 2177256a2c
2 changed files with 15 additions and 6 deletions

View file

@ -2,6 +2,7 @@ import * as detect from '../detect';
import { expect } from '@jest/globals';
const IP = '127.0.0.1';
const BAD_IP = '127.127.127.127';
test('getIpAddress: Custom header', () => {
process.env.CLIENT_IP_HEADER = 'x-custom-ip-header';
@ -17,6 +18,12 @@ test('getIpAddress: Standard header', () => {
expect(detect.getIpAddress(new Headers({ 'x-forwarded-for': IP }))).toEqual(IP);
});
test('getIpAddress: CloudFlare header is lower priority than standard header', () => {
expect(
detect.getIpAddress(new Headers({ 'cf-connecting-ip': BAD_IP, 'x-forwarded-for': IP })),
).toEqual(IP);
});
test('getIpAddress: No header', () => {
expect(detect.getIpAddress(new Headers())).toEqual(null);
});