refactor: address review comments for join view
Some checks reported errors
continuous-integration/drone/push Build was killed
continuous-integration/drone/promote/production Build is passing

This commit is contained in:
Simon 2026-03-10 22:54:41 +01:00
parent f1d0526209
commit 021b709e6a
Signed by: simon
GPG key ID: 40E7A58C4AA1EDB2
12 changed files with 113 additions and 31 deletions

View file

@ -12,8 +12,8 @@ defmodule MvWeb.Endpoint do
]
socket "/live", Phoenix.LiveView.Socket,
websocket: [connect_info: [:peer_data, session: @session_options]],
longpoll: [connect_info: [:peer_data, session: @session_options]]
websocket: [connect_info: [:peer_data, :x_headers, session: @session_options]],
longpoll: [connect_info: [:peer_data, :x_headers, session: @session_options]]
# Serve at "/" the static files from "priv/static" directory.
#

View file

@ -15,6 +15,7 @@ defmodule MvWeb.JoinRateLimit do
- `{:deny, _retry_after_ms}` - rate limit exceeded
"""
def check(key) when is_binary(key) do
# Read at runtime so config can be changed without restart (e.g. in tests).
config = Application.get_env(:mv, :join_rate_limit, [])
scale_ms = Keyword.get(config, :scale_ms, 60_000)
limit = Keyword.get(config, :limit, 10)

View file

@ -213,9 +213,7 @@ defmodule MvWeb.JoinLive do
form_data =
params
|> Enum.filter(fn {key, _} -> key in allowlist_ids and key not in typed end)
|> Map.new()
|> Enum.map(fn {k, v} -> {k, String.trim(to_string(v))} end)
|> Map.new()
|> Map.new(fn {k, v} -> {k, String.trim(to_string(v))} end)
attrs = %{attrs | form_data: form_data}
{:ok, attrs}
@ -227,13 +225,37 @@ defmodule MvWeb.JoinLive do
if is_binary(v), do: String.trim(v), else: nil
end
# Prefer X-Forwarded-For / X-Real-IP when behind a reverse proxy; fall back to peer_data.
# Uses :inet.ntoa/1 for correct IPv4 and IPv6 string representation.
defp client_ip_from_socket(socket) do
case get_connect_info(socket, :peer_data) do
%{address: address} when is_tuple(address) ->
address |> Tuple.to_list() |> Enum.join(".")
_ ->
"unknown"
with nil <- client_ip_from_headers(socket),
%{address: address} when is_tuple(address) <- get_connect_info(socket, :peer_data) do
address |> :inet.ntoa() |> to_string()
else
ip when is_binary(ip) -> ip
_ -> "unknown"
end
end
defp client_ip_from_headers(socket) do
headers = get_connect_info(socket, :x_headers) || []
real_ip = header_value(headers, "x-real-ip")
forwarded = header_value(headers, "x-forwarded-for")
cond do
real_ip != nil -> real_ip
forwarded != nil -> String.split(forwarded, ~r/,\s*/) |> List.first() |> String.trim()
true -> nil
end
end
defp header_value(headers, name) do
name_lower = String.downcase(name)
headers
|> Enum.find_value(fn
{h, v} when is_binary(h) -> if String.downcase(h) == name_lower, do: String.trim(v)
_ -> nil
end)
end
end

View file

@ -24,6 +24,7 @@ defmodule MvWeb.Plugs.JoinFormEnabled do
end
end
# Same body as default ErrorHTML 404 (no custom error templates in this app).
defp send_404(conn) do
conn
|> put_resp_content_type("text/html")