Packages
sentry
8.0.1
13.3.0
13.2.0
13.1.0
13.0.1
13.0.0
12.0.3
12.0.2
12.0.1
12.0.0
11.0.4
11.0.3
11.0.2
11.0.1
11.0.0
10.10.0
10.9.0
10.8.1
10.8.0
10.7.1
10.7.0
10.6.2
10.6.1
10.6.0
10.5.0
10.4.0
10.3.0
10.2.1
10.2.0
10.2.0-rc.2
10.2.0-rc.1
10.1.0
10.0.3
10.0.2
10.0.1
10.0.0
9.1.0
9.0.0
8.1.0
8.0.6
8.0.5
8.0.4
8.0.3
8.0.2
8.0.1
8.0.0
8.0.0-rc.2
8.0.0-rc.1
8.0.0-rc.0
retired
7.2.5
7.2.4
7.2.3
7.2.2
7.2.1
7.2.0
7.1.0
7.0.6
7.0.5
7.0.4
7.0.3
7.0.2
7.0.1
7.0.0
6.4.2
6.4.1
6.4.0
6.3.0
6.2.1
6.2.0
6.1.0
6.0.5
6.0.4
6.0.3
6.0.2
6.0.1
6.0.0
5.0.1
5.0.0
4.0.3
4.0.2
4.0.1
4.0.0
3.0.0
2.2.0
2.1.0
2.0.2
2.0.1
2.0.0
1.1.2
1.1.1
1.1.0
1.0.0
0.3.2
0.3.1
0.3.0
0.2.0
0.1.3
0.1.2
0.1.1
0.1.0
The Official Elixir client for Sentry
Current section
Files
Jump to
Current section
Files
lib/sentry/plug_context.ex
defmodule Sentry.PlugContext do
@moduledoc """
This module adds Sentry context metadata during the request in a Plug
application. It includes defaults for scrubbing sensitive data, and
options for customizing it by default.
It is intended for usage with `Sentry.PlugCapture` as metadata added here
will appear in events captured.
### Sending Post Body Params
In order to send post body parameters you should first scrub them of sensitive
information. By default, they will be scrubbed with
`Sentry.Plug.default_body_scrubber/1`. It can be overridden by passing
the `body_scrubber` option, which accepts a `Plug.Conn` and returns a map
to send. Setting `:body_scrubber` to `nil` will not send any data back.
If you would like to make use of Sentry's default scrubber behavior in a custom
scrubber, it can be called directly. An example configuration may look like
the following:
def scrub_params(conn) do
# Makes use of the default body_scrubber to avoid sending password
# and credit card information in plain text. To also prevent sending
# our sensitive "my_secret_field" and "other_sensitive_data" fields,
# we simply drop those keys.
Sentry.Plug.default_body_scrubber(conn)
|> Map.drop(["my_secret_field", "other_sensitive_data"])
end
Then pass it into Sentry.Plug:
plug Sentry.PlugContext, body_scrubber: &MyModule.scrub_params/1
You can also pass it in as a `{module, fun}` like so:
plug Sentry.PlugContext, body_scrubber: {MyModule, :scrub_params}
*Please Note*: If you are sending large files you will want to scrub them out.
### Headers Scrubber
By default Sentry will scrub Authorization and Authentication headers from all
requests before sending them. It can be configured similarly to the body params
scrubber, but is configured with the `:header_scrubber` key.
def scrub_headers(conn) do
# default is: Sentry.Plug.default_header_scrubber(conn)
#
# We do not want to include Content-Type or User-Agent in reported
# headers, so we drop them.
Enum.into(conn.req_headers, %{})
|> Map.drop(["content-type", "user-agent"])
end
Then pass it into Sentry.Plug:
plug Sentry.PlugContext, header_scrubber: &MyModule.scrub_headers/1
It can also be passed in as a `{module, fun}` like so:
plug Sentry.PlugContext, header_scrubber: {MyModule, :scrub_headers}
### Cookie Scrubber
By default Sentry will scrub all cookies before sending events.
It can be configured similarly to the headers scrubber, but is configured with the `:cookie_scrubber` key.
To configure scrubbing, we can set all configuration keys:
plug Sentry.PlugContext, header_scrubber: &MyModule.scrub_headers/1,
body_scrubber: &MyModule.scrub_params/1, cookie_scrubber: &MyModule.scrub_cookies/1
### Including Request Identifiers
If you're using Phoenix, Plug.RequestId, or another method to set a request ID
response header, and would like to include that information with errors
reported by Sentry.PlugContext, the `:request_id_header` option allows you to set
which header key Sentry should check. It will default to "x-request-id",
which Plug.RequestId (and therefore Phoenix) also default to.
plug Sentry.PlugContext, request_id_header: "application-request-id"
"""
if Code.ensure_loaded?(Plug) do
@behaviour Plug
end
@default_scrubbed_param_keys ["password", "passwd", "secret"]
@default_scrubbed_header_keys ["authorization", "authentication", "cookie"]
@credit_card_regex ~r/^(?:\d[ -]*?){13,16}$/
@scrubbed_value "*********"
@default_plug_request_id_header "x-request-id"
def init(opts) do
opts
end
def call(conn, opts) do
request = build_request_interface_data(conn, opts)
Sentry.Context.set_request_context(request)
conn
end
@spec build_request_interface_data(Plug.Conn.t(), keyword()) :: map()
def build_request_interface_data(conn, opts) do
body_scrubber = Keyword.get(opts, :body_scrubber, {__MODULE__, :default_body_scrubber})
header_scrubber = Keyword.get(opts, :header_scrubber, {__MODULE__, :default_header_scrubber})
cookie_scrubber = Keyword.get(opts, :cookie_scrubber, {__MODULE__, :default_cookie_scrubber})
request_id = Keyword.get(opts, :request_id_header) || @default_plug_request_id_header
conn =
Plug.Conn.fetch_cookies(conn)
|> Plug.Conn.fetch_query_params()
%{
url: Plug.Conn.request_url(conn),
method: conn.method,
data: handle_data(conn, body_scrubber),
query_string: conn.query_string,
cookies: handle_data(conn, cookie_scrubber),
headers: handle_data(conn, header_scrubber),
env: %{
"REMOTE_ADDR" => remote_address(conn.remote_ip),
"REMOTE_PORT" => Plug.Conn.get_peer_data(conn).port,
"SERVER_NAME" => conn.host,
"SERVER_PORT" => conn.port,
"REQUEST_ID" => Plug.Conn.get_resp_header(conn, request_id) |> List.first()
}
}
end
defp remote_address(address) do
address
|> :inet.ntoa()
|> case do
{:error, _} ->
""
address ->
to_string(address)
end
end
defp handle_data(_conn, nil), do: %{}
defp handle_data(conn, {module, fun}) do
apply(module, fun, [conn])
end
defp handle_data(conn, fun) when is_function(fun) do
fun.(conn)
end
@spec default_cookie_scrubber(Plug.Conn.t()) :: map()
def default_cookie_scrubber(_conn) do
%{}
end
@spec default_header_scrubber(Plug.Conn.t()) :: map()
def default_header_scrubber(conn) do
Enum.into(conn.req_headers, %{})
|> Map.drop(@default_scrubbed_header_keys)
end
@spec default_body_scrubber(Plug.Conn.t()) :: map()
def default_body_scrubber(conn) do
scrub_map(conn.params, @default_scrubbed_param_keys)
end
@doc """
Recursively scrubs a map that may have nested maps
Accepts a list of keys to scrub, and a list of options to configure
### Options
* `:scrubbed_values_regular_expressions` - A list of regular expressions.
Any binary values within the map that match any of the regular expressions
will be scrubbed. Defaults to `[~r/^(?:\d[ -]*?){13,16}$/]`.
* `:scrubbed_value` - The value to replace scrubbed values with.
Defaults to `"*********"`.
"""
@spec scrub_map(map(), list(String.t()), keyword()) :: map()
def scrub_map(map, scrubbed_keys, opts \\ []) do
scrubbed_values_regular_expressions =
Keyword.get(opts, :scrubbed_values_regular_expressions, [@credit_card_regex])
scrubbed_value = Keyword.get(opts, :scrubbed_value, @scrubbed_value)
Enum.into(map, %{}, fn {key, value} ->
value =
cond do
Enum.member?(scrubbed_keys, key) ->
scrubbed_value
is_binary(value) &&
Enum.any?(scrubbed_values_regular_expressions, &Regex.match?(&1, value)) ->
scrubbed_value
is_map(value) && Map.has_key?(value, :__struct__) ->
Map.from_struct(value)
|> scrub_map(scrubbed_keys, opts)
is_map(value) ->
scrub_map(value, scrubbed_keys, opts)
true ->
value
end
{key, value}
end)
end
end