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

Please honor HTTP header "X-Real-IP" when it exists #5

Open
pini-gh opened this issue Mar 1, 2021 · 1 comment · May be fixed by #6
Open

Please honor HTTP header "X-Real-IP" when it exists #5

pini-gh opened this issue Mar 1, 2021 · 1 comment · May be fixed by #6

Comments

@pini-gh
Copy link

pini-gh commented Mar 1, 2021

HI,

My tmate instance sits behind an Nginx reverse proxy, and tmate-websocket reports the proxy's IP address instead of the client's.

I can't use proxy protocol, but the HTTP header X-Real-IP is set. It would be great if tmate-websocket could use it when available.

Looking at the code, it might be just a one line addition iinto lib/tmate/ws_api/websocket.ex:

diff --git a/lib/tmate/ws_api/websocket.ex b/lib/tmate/ws_api/websocket.ex
index 2d667fd..f502091 100644
--- a/lib/tmate/ws_api/websocket.ex
+++ b/lib/tmate/ws_api/websocket.ex
@@ -26,6 +26,7 @@ defmodule Tmate.WsApi.WebSocket do
               :ok ->
                 ip = case req do
                   %{proxy_header: %{src_address: ip}} -> ip
+                  %{headers: %{"x-real-ip" => ip}} -> ip
                   %{peer: {ip, _port}} -> ip
                 end
                 ip = :inet_parse.ntoa(ip) |> to_string

Please bear with me as I don't know Elixir, Phoenix, cowboy et al. I spent some time digging into the source code and reading documentation, but this patch proposal is just a wild guess. I don't even know how to build the app.

Anyway. In case this is something as simple as that, this fix would be very much appreciated.

Thanks in advance.

@pini-gh
Copy link
Author

pini-gh commented Mar 1, 2021

I've successfully tested this patch:

diff --git a/lib/tmate/ws_api/websocket.ex b/lib/tmate/ws_api/websocket.ex
index 2d667fd..2673f5d 100644
--- a/lib/tmate/ws_api/websocket.ex
+++ b/lib/tmate/ws_api/websocket.ex
@@ -26,6 +26,9 @@ defmodule Tmate.WsApi.WebSocket do
               :ok ->
                 ip = case req do
                   %{proxy_header: %{src_address: ip}} -> ip
+                  %{headers: %{"x-real-ip" => ipstring}} ->
+                    {_, ip} = :inet.parse_address(ipstring |> to_charlist)
+                    ip
                   %{peer: {ip, _port}} -> ip
                 end
                 ip = :inet_parse.ntoa(ip) |> to_string

Not sure this is the best way of doing it, but it works for me. Would you consider such a PR?

pini-gh added a commit to pini-gh/tmate-websocket that referenced this issue Mar 2, 2021
@pini-gh pini-gh linked a pull request Mar 2, 2021 that will close this issue
pini-gh added a commit to pini-gh/tmate-websocket that referenced this issue Nov 7, 2021
And because X-Real-IP can be spoofed, gate this feature with TRUST_X_REAL_IP
environment variable.

Fix tmate-io#5.
pini-gh added a commit to pini-gh/tmate-websocket that referenced this issue Jan 22, 2023
And because X-Real-IP can be spoofed, gate this feature with TRUST_X_REAL_IP
environment variable.

Fix tmate-io#5.
pini-gh added a commit to pini-gh/tmate-websocket that referenced this issue Jan 22, 2023
And because X-Real-IP can be spoofed, gate this feature with TRUST_X_REAL_IP
environment variable.

Fix tmate-io#5.
pini-gh added a commit to pini-gh/tmate-websocket that referenced this issue Jan 22, 2023
And because X-Real-IP can be spoofed, gate this feature with TRUST_X_REAL_IP
environment variable.

Fix tmate-io#5.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant