1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Plugs.RemoteIpTest do
9 alias Pleroma.Web.Plugs.RemoteIp
11 import Pleroma.Tests.Helpers, only: [clear_config: 2]
14 clear_config(RemoteIp,
16 headers: ["x-forwarded-for"],
29 clear_config(RemoteIp, enabled: false)
31 %{remote_ip: remote_ip} = conn(:get, "/")
35 |> put_req_header("x-forwarded-for", "1.1.1.1")
38 assert conn.remote_ip == remote_ip
44 |> put_req_header("x-forwarded-for", "1.1.1.1")
47 assert conn.remote_ip == {1, 1, 1, 1}
50 test "custom headers" do
51 clear_config(RemoteIp, enabled: true, headers: ["cf-connecting-ip"])
55 |> put_req_header("x-forwarded-for", "1.1.1.1")
58 refute conn.remote_ip == {1, 1, 1, 1}
62 |> put_req_header("cf-connecting-ip", "1.1.1.1")
65 assert conn.remote_ip == {1, 1, 1, 1}
68 test "custom proxies" do
71 |> put_req_header("x-forwarded-for", "173.245.48.1, 1.1.1.1, 173.245.48.2")
74 refute conn.remote_ip == {1, 1, 1, 1}
76 clear_config([RemoteIp, :proxies], ["173.245.48.0/20"])
80 |> put_req_header("x-forwarded-for", "173.245.48.1, 1.1.1.1, 173.245.48.2")
83 assert conn.remote_ip == {1, 1, 1, 1}
86 test "proxies set without CIDR format" do
87 clear_config([RemoteIp, :proxies], ["173.245.48.1"])
91 |> put_req_header("x-forwarded-for", "173.245.48.1, 1.1.1.1")
94 assert conn.remote_ip == {1, 1, 1, 1}
97 test "proxies set `nonsensical` CIDR" do
98 clear_config([RemoteIp, :reserved], ["127.0.0.0/8"])
99 clear_config([RemoteIp, :proxies], ["10.0.0.3/24"])
103 |> put_req_header("x-forwarded-for", "10.0.0.3, 1.1.1.1")
104 |> RemoteIp.call(nil)
106 assert conn.remote_ip == {1, 1, 1, 1}