From cdf6922bddaf5a99759b2d751b7177556b30b442 Mon Sep 17 00:00:00 2001 From: nthumann Date: Fri, 7 Jul 2023 23:38:10 +0200 Subject: [PATCH 1/2] Fix handling of IPv6 addresses in getClientIP --- server/uptime-kuma-server.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/uptime-kuma-server.js b/server/uptime-kuma-server.js index b45a749b9..728b8f158 100644 --- a/server/uptime-kuma-server.js +++ b/server/uptime-kuma-server.js @@ -249,9 +249,9 @@ class UptimeKumaServer { return (typeof forwardedFor === "string" ? forwardedFor.split(",")[0].trim() : null) || socket.client.conn.request.headers["x-real-ip"] - || clientIP.replace(/^.*:/, ""); + || clientIP.replace(/^::ffff:/, ""); } else { - return clientIP.replace(/^.*:/, ""); + return clientIP.replace(/^::ffff:/, ""); } } From d55794e1a5669a7cf84fcfbc539cd95e997c8e04 Mon Sep 17 00:00:00 2001 From: nthumann Date: Sat, 8 Jul 2023 17:46:26 +0200 Subject: [PATCH 2/2] Add test cases for IPv6 addresses in getClientIP --- test/backend.spec.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/backend.spec.js b/test/backend.spec.js index 644a0fd08..0132fb770 100644 --- a/test/backend.spec.js +++ b/test/backend.spec.js @@ -306,6 +306,16 @@ describe("Test uptimeKumaServer.getClientIP()", () => { ip = await server.getClientIP(fakeSocket); expect(ip).toBe("203.0.113.195"); + fakeSocket.client.conn.remoteAddress = "2001:db8::1"; + fakeSocket.client.conn.request.headers = {}; + ip = await server.getClientIP(fakeSocket); + expect(ip).toBe("2001:db8::1"); + + fakeSocket.client.conn.remoteAddress = "::ffff:127.0.0.1"; + fakeSocket.client.conn.request.headers = {}; + ip = await server.getClientIP(fakeSocket); + expect(ip).toBe("127.0.0.1"); + await Database.close(); }, 120000); });