Skip to content

Commit e6a9ed9

Browse files
committed
Fix: Using all IPs including x-forwarded-for when checking if the requester has access to metrics
1 parent 06543e8 commit e6a9ed9

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/api/routes/index.router.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,14 @@ const packageJson = JSON.parse(fs.readFileSync('./package.json', 'utf8'));
4848
const metricsIPWhitelist = (req: Request, res: Response, next: NextFunction) => {
4949
const metricsConfig = configService.get('METRICS');
5050
const allowedIPs = metricsConfig.ALLOWED_IPS?.split(',').map((ip) => ip.trim()) || ['127.0.0.1'];
51-
const clientIP = req.ip || req.connection.remoteAddress || req.socket.remoteAddress;
52-
53-
if (!allowedIPs.includes(clientIP)) {
51+
const clientIPs = [
52+
req.ip,
53+
req.connection.remoteAddress,
54+
req.socket.remoteAddress,
55+
req.headers['x-forwarded-for'],
56+
].filter((ip) => ip !== undefined);
57+
58+
if (allowedIPs.filter((ip) => clientIPs.includes(ip)) === 0) {
5459
return res.status(403).send('Forbidden: IP not allowed');
5560
}
5661

0 commit comments

Comments
 (0)