Skip to content

Commit dc29e73

Browse files
committed
fix: getClientIp returns null, if user is using private IP
1 parent 862ab90 commit dc29e73

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed

adminforth/auth.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import crypto from 'crypto';
44
import AdminForth from './index.js';
55
import { IAdminForthAuth } from './types/Back.js';
66
import { afLogger } from './modules/logger.js';
7+
import is_ip_private from 'private-ip'
78

89
// Function to generate a password hash using PBKDF2
910
function 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) {

adminforth/package-lock.json

Lines changed: 52 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

adminforth/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
"pg": "^8.11.5",
8585
"pino": "^10.1.0",
8686
"pino-pretty": "^13.1.3",
87+
"private-ip": "^3.0.2",
8788
"rate-limiter-flexible": "^8.1.0",
8889
"recast": "^0.23.11",
8990
"ws": "^8.18.0"

0 commit comments

Comments
 (0)