@@ -79,6 +79,18 @@ defmodule Sentry.PlugContext do
79
79
which Plug.RequestId (and therefore Phoenix) also default to.
80
80
81
81
plug Sentry.PlugContext, request_id_header: "application-request-id"
82
+
83
+ ### Remote Address Reader
84
+
85
+ Sentry.PlugContext includes a request's originating IP address under the `REMOTE_ADDR`
86
+ Environment key in Sentry. By default it is read from the `x-forwarded-for` HTTP header,
87
+ and if this header is not present, it is read from `conn.remote_ip`.
88
+
89
+ If you wish to read this value differently (e.g. from a different HTTP header),
90
+ or modify it in some other way (e.g. by masking it), you can configure this behavior
91
+ by passing the `:remote_address_reader` option:
92
+
93
+ plug Sentry.PlugContext, remote_address_reader: &MyModule.read_ip/1
82
94
"""
83
95
84
96
if Code . ensure_loaded? ( Plug ) do
@@ -111,6 +123,9 @@ defmodule Sentry.PlugContext do
111
123
112
124
request_id = Keyword . get ( opts , :request_id_header ) || @ default_plug_request_id_header
113
125
126
+ remote_address_reader =
127
+ Keyword . get ( opts , :remote_address_reader , { __MODULE__ , :default_remote_address_reader } )
128
+
114
129
conn =
115
130
Plug.Conn . fetch_cookies ( conn )
116
131
|> Plug.Conn . fetch_query_params ( )
@@ -123,7 +138,7 @@ defmodule Sentry.PlugContext do
123
138
cookies: handle_data ( conn , cookie_scrubber ) ,
124
139
headers: handle_data ( conn , header_scrubber ) ,
125
140
env: % {
126
- "REMOTE_ADDR" => remote_address ( conn ) ,
141
+ "REMOTE_ADDR" => handle_data ( conn , remote_address_reader ) ,
127
142
"REMOTE_PORT" => remote_port ( conn ) ,
128
143
"SERVER_NAME" => conn . host ,
129
144
"SERVER_PORT" => conn . port ,
@@ -132,7 +147,8 @@ defmodule Sentry.PlugContext do
132
147
}
133
148
end
134
149
135
- defp remote_address ( conn ) do
150
+ @ spec default_remote_address_reader ( Plug.Conn . t ( ) ) :: String . t ( )
151
+ def default_remote_address_reader ( conn ) do
136
152
if header_value = get_header ( conn , "x-forwarded-for" ) do
137
153
header_value
138
154
|> String . split ( "," )
0 commit comments