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

Honor HTTP header 'X-Real-IP' when available #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

pini-gh
Copy link

@pini-gh pini-gh commented Mar 2, 2021

Fix #5.

@nviennot
Copy link
Member

Clients can set the X-Real-IP header themselves, and spoof IPs (not that there's any harm).
Should this feature be gated by a config flag?

@pini-gh
Copy link
Author

pini-gh commented Mar 22, 2021

It makes sense. Something like that?

diff --git a/config/prod.exs b/config/prod.exs
index d2b0323..e116179 100644
--- a/config/prod.exs
+++ b/config/prod.exs
@@ -26,7 +26,8 @@ config :tmate, :websocket, Keyword.merge(websocket_ranch_opts,
   cowboy_opts: %{
     compress: true,
     proxy_header: System.get_env("USE_PROXY_PROTOCOL") == "1"},
-  base_url: System.get_env("WEBSOCKET_BASE_URL")
+  base_url: System.get_env("WEBSOCKET_BASE_URL"),
+  trust_x_real_ip: System.get_env("TRUST_X_REAL_IP") == "1"
 )
 
 config :tzdata, :autoupdate, :disabled
diff --git a/lib/tmate/ws_api/websocket.ex b/lib/tmate/ws_api/websocket.ex
index 2673f5d..f4c4d40 100644
--- a/lib/tmate/ws_api/websocket.ex
+++ b/lib/tmate/ws_api/websocket.ex
@@ -24,9 +24,10 @@ defmodule Tmate.WsApi.WebSocket do
           {mode, session} ->
             case Tmate.Session.ws_verify_auth(session) do
               :ok ->
+                trust_x_real_ip = Application.get_env(:tmate, :websocket)[:trust_x_real_ip]
                 ip = case req do
                   %{proxy_header: %{src_address: ip}} -> ip
-                  %{headers: %{"x-real-ip" => ipstring}} ->
+                  %{headers: %{"x-real-ip" => ipstring}} when trust_x_real_ip ->
                     {_, ip} = :inet.parse_address(ipstring |> to_charlist)
                     ip
                   %{peer: {ip, _port}} -> ip

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 this pull request may close these issues.

Please honor HTTP header "X-Real-IP" when it exists
2 participants