Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/api/routes/index.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,14 @@ const packageJson = JSON.parse(fs.readFileSync('./package.json', 'utf8'));
const metricsIPWhitelist = (req: Request, res: Response, next: NextFunction) => {
const metricsConfig = configService.get('METRICS');
const allowedIPs = metricsConfig.ALLOWED_IPS?.split(',').map((ip) => ip.trim()) || ['127.0.0.1'];
const clientIP = req.ip || req.connection.remoteAddress || req.socket.remoteAddress;

if (!allowedIPs.includes(clientIP)) {
const clientIPs = [
req.ip,
req.connection.remoteAddress,
req.socket.remoteAddress,
req.headers['x-forwarded-for'],
Comment on lines +51 to +55
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚨 suggestion (security): Consider normalizing and validating IPs from 'x-forwarded-for'.

Split the 'x-forwarded-for' header on commas, trim whitespace from each IP, and validate their formats to ensure accurate and secure IP handling.

].filter((ip) => ip !== undefined);

if (allowedIPs.filter((ip) => clientIPs.includes(ip)) === 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): The IP check logic is incorrect; filter returns an array, not a count.

Update the condition to check 'allowedIPs.filter(...).length === 0' so it correctly determines when no client IPs are allowed.

return res.status(403).send('Forbidden: IP not allowed');
}

Expand Down
Loading