Korjaa getClientIp() X-Forwarded-For parsinta + näytä IP virheessä

X-Forwarded-For voi sisältää useita IP:tä pilkulla erotettuna.
Otetaan nyt vain ensimmäinen (asiakkaan oikea IP). Lisäksi
näytetään havaittu IP virheviestissä debuggausta varten.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-11 08:53:32 +02:00
parent 8b8237ed52
commit 2b4591c49f

11
api.php
View File

@@ -127,7 +127,14 @@ function companyFile(string $filename): string {
} }
function getClientIp(): string { function getClientIp(): string {
return $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0'; $xff = $_SERVER['HTTP_X_FORWARDED_FOR'] ?? '';
if ($xff) {
// X-Forwarded-For voi sisältää useita IP:itä: "client, proxy1, proxy2" — otetaan ensimmäinen
$parts = explode(',', $xff);
$ip = trim($parts[0]);
if (filter_var($ip, FILTER_VALIDATE_IP)) return $ip;
}
return $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0';
} }
/** /**
@@ -1163,7 +1170,7 @@ switch ($action) {
if (empty($allowedCompanies)) { if (empty($allowedCompanies)) {
dbRecordLoginAttempt($ip); dbRecordLoginAttempt($ip);
http_response_code(403); http_response_code(403);
echo json_encode(['error' => 'IP-osoitteesi ei ole sallittu.']); echo json_encode(['error' => 'IP-osoitteesi (' . $ip . ') ei ole sallittu.']);
break; break;
} }
$userCompanies = $allowedCompanies; $userCompanies = $allowedCompanies;