Fix rate limiter header check bug

Fixed ArgumentError in rate limiter by replacing cond statement with
proper case pattern matching. The cond was incorrectly evaluating
assignment expressions as truthy values instead of checking if headers
were empty lists.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Graham McIntire 2025-07-25 09:30:59 -05:00
parent fb34553fe9
commit 935b70b4b1
No known key found for this signature in database

View file

@ -41,20 +41,22 @@ defmodule AprsmeWeb.Plugs.RateLimiter do
defp get_key(conn, :ip) do defp get_key(conn, :ip) do
# Check headers in order of preference # Check headers in order of preference
cond do case {get_req_header(conn, "cf-connecting-ip"), get_req_header(conn, "x-forwarded-for"),
get_req_header(conn, "x-real-ip")} do
# Cloudflare header takes precedence # Cloudflare header takes precedence
cf_ip = get_req_header(conn, "cf-connecting-ip") != [] -> {[cf | _], _, _} ->
hd(cf_ip) cf
# Then standard forwarded headers # Then standard X-Forwarded-For header
forwarded = get_req_header(conn, "x-forwarded-for") != [] -> {[], [forwarded | _], _} ->
forwarded |> hd() |> String.split(",") |> List.first() |> String.trim() forwarded |> String.split(",") |> List.first() |> String.trim()
real_ip = get_req_header(conn, "x-real-ip") != [] -> # Then X-Real-IP header
hd(real_ip) {[], [], [real | _]} ->
real
# Fall back to remote_ip # Fall back to remote_ip
true -> {[], [], []} ->
conn.remote_ip |> :inet.ntoa() |> to_string() conn.remote_ip |> :inet.ntoa() |> to_string()
end end
end end