Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of IPv6 addresses in getClientIP #3381

Merged
merged 2 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions server/uptime-kuma-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,9 @@

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:/, "");
}
}

Expand Down Expand Up @@ -301,5 +301,5 @@
};

// Must be at the end
const { MonitorType } = require("./monitor-types/monitor-type");

Check warning on line 304 in server/uptime-kuma-server.js

View workflow job for this annotation

GitHub Actions / check-linters

'MonitorType' is assigned a value but never used
const { RealBrowserMonitorType } = require("./monitor-types/real-browser-monitor-type");
10 changes: 10 additions & 0 deletions test/backend.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});