|
| 1 | +defmodule ErrorTracker.Integrations.PlugTest do |
| 2 | + use ErrorTracker.Test.Case |
| 3 | + |
| 4 | + alias ErrorTracker.Integrations.Plug, as: IntegrationPlug |
| 5 | + |
| 6 | + @fake_callstack [] |
| 7 | + |
| 8 | + setup do |
| 9 | + [conn: Phoenix.ConnTest.build_conn()] |
| 10 | + end |
| 11 | + |
| 12 | + test "it reports errors, including the request headers", %{conn: conn} do |
| 13 | + conn = conn |> Plug.Conn.put_req_header("accept", "application/json") |
| 14 | + |
| 15 | + IntegrationPlug.report_error( |
| 16 | + conn, |
| 17 | + {"an error from Phoenix", "something bad happened"}, |
| 18 | + @fake_callstack |
| 19 | + ) |
| 20 | + |
| 21 | + [error] = repo().all(ErrorTracker.Error) |
| 22 | + |
| 23 | + assert error.kind == "an error from Phoenix" |
| 24 | + assert error.reason == "something bad happened" |
| 25 | + |
| 26 | + [occurrence] = repo().all(ErrorTracker.Occurrence) |
| 27 | + assert occurrence.error_id == error.id |
| 28 | + |
| 29 | + %{"request.headers" => request_headers} = occurrence.context |
| 30 | + assert request_headers == %{"accept" => "application/json"} |
| 31 | + end |
| 32 | + |
| 33 | + test "it does not save sensitive request headers, to avoid storing them in cleartext", %{ |
| 34 | + conn: conn |
| 35 | + } do |
| 36 | + conn = |
| 37 | + conn |
| 38 | + |> Plug.Conn.put_req_header("cookie", "who stole the cookie from the cookie jar ?") |
| 39 | + |> Plug.Conn.put_req_header("authorization", "Bearer plz-dont-leak-my-secrets") |
| 40 | + |> Plug.Conn.put_req_header("safe", "this can be safely stored in cleartext") |
| 41 | + |
| 42 | + IntegrationPlug.report_error( |
| 43 | + conn, |
| 44 | + {"an error from Phoenix", "something bad happened"}, |
| 45 | + @fake_callstack |
| 46 | + ) |
| 47 | + |
| 48 | + [occurrence] = repo().all(ErrorTracker.Occurrence) |
| 49 | + |
| 50 | + header_names = occurrence.context |> Map.get("request.headers") |> Map.keys() |
| 51 | + |
| 52 | + assert "cookie" not in header_names |
| 53 | + assert "authorization" not in header_names |
| 54 | + |
| 55 | + assert "safe" in header_names |
| 56 | + end |
| 57 | +end |
0 commit comments