@@ -4,6 +4,7 @@ import crypto from 'crypto';
44import AdminForth from './index.js' ;
55import { IAdminForthAuth } from './types/Back.js' ;
66import { afLogger } from './modules/logger.js' ;
7+ import is_ip_private from 'private-ip'
78
89// Function to generate a password hash using PBKDF2
910function calcPasswordHash ( password , salt , iterations = 100000 , keyLength = 64 , digest = 'sha512' ) {
@@ -44,20 +45,22 @@ class AdminForthAuth implements IAdminForthAuth {
4445 this . adminforth = adminforth ;
4546 }
4647
47- getClientIp ( headers : object ) {
48+ getClientIp ( headers : object ) {
4849 const clientIpHeader = this . adminforth . config . auth . clientIpHeader ;
4950
5051 const headersLower = Object . keys ( headers ) . reduce ( ( acc , key ) => {
5152 acc [ key . toLowerCase ( ) ] = headers [ key ] ;
5253 return acc ;
5354 } , { } ) ;
55+
56+ let ip : string | null = null ;
5457 if ( clientIpHeader ) {
55- return headersLower [ clientIpHeader . toLowerCase ( ) ] || 'unknown' ;
58+ ip = headersLower [ clientIpHeader . toLowerCase ( ) ] ;
5659 } else {
5760 // first try common headers which can't bee spoofed, in other words
5861 // most common to nginx/traefik/apache
5962 // then fallback to less secure headers
60- return headersLower [ 'x-forwarded-for' ] ?. split ( ',' ) . shift ( ) . trim ( ) ||
63+ ip = headersLower [ 'x-forwarded-for' ] ?. split ( ',' ) . shift ( ) . trim ( ) ||
6164 headersLower [ 'x-real-ip' ] ||
6265 headersLower [ 'x-client-ip' ] ||
6366 headersLower [ 'x-cluster-client-ip' ] ||
@@ -69,6 +72,11 @@ class AdminForthAuth implements IAdminForthAuth {
6972 headersLower [ 'x-host' ] ||
7073 null ;
7174 }
75+ const isIpPrivate = is_ip_private ( ip )
76+ if ( isIpPrivate ) {
77+ return null ;
78+ }
79+ return ip ;
7280 }
7381
7482 removeAuthCookie ( response ) {
0 commit comments