Skip to content

Commit 3fd3e10

Browse files
authored
fix: Improve sensitive header redaction (#171)
We now redact headers instead of just dropping them. This allows users of the Error Tracker to know wether a header was present or not. With the previous logic it was impossible to know wether the header was dropped or not present in the first place. We also redact the `set-cookie` header so we redact outgoing cookies and not only incoming cookies as per the `cookie` header. This PR replaces #166
1 parent f636e60 commit 3fd3e10

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

lib/error_tracker/integrations/plug.ex

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ defmodule ErrorTracker.Integrations.Plug do
111111
conn |> build_context() |> ErrorTracker.set_context()
112112
end
113113

114-
@sensitive_headers ["cookie", "authorization"]
114+
@sensitive_headers ~w[authorization cookie set-cookie]
115115

116116
defp build_context(%Plug.Conn{} = conn) do
117117
%{
@@ -120,7 +120,10 @@ defmodule ErrorTracker.Integrations.Plug do
120120
"request.query" => conn.query_string,
121121
"request.method" => conn.method,
122122
"request.ip" => remote_ip(conn),
123-
"request.headers" => conn.req_headers |> Map.new() |> Map.drop(@sensitive_headers),
123+
"request.headers" =>
124+
Map.new(conn.req_headers, fn {header, value} ->
125+
if header in @sensitive_headers, do: {header, "[REDACTED]"}, else: {header, value}
126+
end),
124127
# Depending on the error source, the request params may have not been fetched yet
125128
"request.params" => if(!is_struct(conn.params, Plug.Conn.Unfetched), do: conn.params)
126129
}

test/integrations/plug_test.exs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,8 @@ defmodule ErrorTracker.Integrations.PlugTest do
4747

4848
[occurrence] = repo().all(ErrorTracker.Occurrence)
4949

50-
header_names = occurrence.context |> Map.get("request.headers") |> Map.keys()
51-
52-
refute "cookie" in header_names
53-
refute "authorization" in header_names
54-
55-
assert "safe" in header_names
50+
assert occurrence.context["request.headers"]["cookie"] == "[REDACTED]"
51+
assert occurrence.context["request.headers"]["authorization"] == "[REDACTED]"
52+
assert occurrence.context["request.headers"]["safe"] != "[REDACTED]"
5653
end
5754
end

0 commit comments

Comments
 (0)