From 600c7acdc1da3ffbb6bbb09d0cb085f6706063b8 Mon Sep 17 00:00:00 2001 From: Lukas Metzger Date: Wed, 25 Dec 2019 15:25:39 +0100 Subject: [PATCH] Fixed client ip error on X-Forwarded-For --- backend/src/middlewares/ClientIp.php | 5 ++--- backend/test/tests/remote-ip.js | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/backend/src/middlewares/ClientIp.php b/backend/src/middlewares/ClientIp.php index 6aa4335..421fd05 100644 --- a/backend/src/middlewares/ClientIp.php +++ b/backend/src/middlewares/ClientIp.php @@ -38,9 +38,8 @@ class ClientIp $ip = $_SERVER['REMOTE_ADDR']; for ($i = count($parts) - 1; $i >= 0; $i--) { - if (in_array($parts[$i], $proxys) && $i > 0) { - $ip = $parts[$i - 1]; - } else { + if (!in_array($parts[$i], $proxys)) { + $ip = $parts[$i]; break; } } diff --git a/backend/test/tests/remote-ip.js b/backend/test/tests/remote-ip.js index 3606ec0..a4a8c54 100644 --- a/backend/test/tests/remote-ip.js +++ b/backend/test/tests/remote-ip.js @@ -41,6 +41,6 @@ test.run(async function () { }); assert.equal(res.status, 200); - assert.equal(res.data, { ip: '127.0.0.1' }, 'X-Forwarded-For Test 3'); + assert.equal(res.data, { ip: '1.2.3.4' }, 'X-Forwarded-For Test 3'); }); }); \ No newline at end of file