fix(remote_ip): log IPv6 in canonical hex via :inet.ntoa
IPv6 segments were being printed in decimal (e.g. 10772:1985:1024:45::1 for 2a14:7c1:400:2d::1), which made request_id logs unreadable.
This commit is contained in:
parent
5c178ef4b7
commit
10cbd8dd48
2 changed files with 23 additions and 2 deletions
|
|
@ -173,7 +173,12 @@ defmodule MicrowavepropWeb.Plugs.RemoteIp do
|
||||||
(e <<< 48) + (f <<< 32) + (g <<< 16) + h
|
(e <<< 48) + (f <<< 32) + (g <<< 16) + h
|
||||||
end
|
end
|
||||||
|
|
||||||
defp format_ip({a, b, c, d}), do: "#{a}.#{b}.#{c}.#{d}"
|
defp format_ip(ip) when tuple_size(ip) in [4, 8] do
|
||||||
defp format_ip({a, b, c, d, e, f, g, h}), do: "#{a}:#{b}:#{c}:#{d}:#{e}:#{f}:#{g}:#{h}"
|
case :inet.ntoa(ip) do
|
||||||
|
{:error, _} -> "unknown"
|
||||||
|
charlist -> List.to_string(charlist)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
defp format_ip(_), do: "unknown"
|
defp format_ip(_), do: "unknown"
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -249,6 +249,22 @@ defmodule MicrowavepropWeb.Plugs.RemoteIpTest do
|
||||||
assert conn.remote_ip == {127, 0, 0, 1}
|
assert conn.remote_ip == {127, 0, 0, 1}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "logs IPv6 peer in canonical hex form, not decimal" do
|
||||||
|
# Regression: a public IPv6 client like 2a14:7c1:400:2d::1 was being
|
||||||
|
# logged as "10772:1985:1024:45:0:0:0:1" because we stringified the
|
||||||
|
# 8-tuple in decimal. Logger metadata should hold the canonical hex.
|
||||||
|
peer = {0x2A14, 0x07C1, 0x0400, 0x002D, 0, 0, 0, 1}
|
||||||
|
|
||||||
|
:get
|
||||||
|
|> conn("/")
|
||||||
|
|> with_peer(peer)
|
||||||
|
|> call_plug()
|
||||||
|
|
||||||
|
assert {:remote_ip, ip_str} = List.keyfind(Logger.metadata(), :remote_ip, 0)
|
||||||
|
assert ip_str =~ "2a14"
|
||||||
|
refute ip_str =~ "10772"
|
||||||
|
end
|
||||||
|
|
||||||
test "single-host CIDR (/32) trusts only that exact IP" do
|
test "single-host CIDR (/32) trusts only that exact IP" do
|
||||||
conn =
|
conn =
|
||||||
:get
|
:get
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue