Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add url_scrubber for redacting URLs #687

Merged
merged 2 commits into from
Feb 2, 2024

Conversation

paulstatezny
Copy link
Contributor

@whatyouhide indicated here that this PR would be welcome. 🙂

This PR adds support for a url_scrubber which works just like body_scrubber, header_scrubber, and cookie_scrubber.

The "default scrubber" behavior is to not redact anything. (Just runs Plug.Conn.request_url/1 as before.)

@@ -152,7 +175,7 @@ defmodule Sentry.PlugContext do
|> Plug.Conn.fetch_query_params()

%{
url: Plug.Conn.request_url(conn),
url: apply_fun_with_conn(conn, url_scrubber),
Copy link
Contributor Author

@paulstatezny paulstatezny Feb 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using apply_fun_with_conn is consistent with the other scrubbers. However, passing in nil for url_scrubber will cause %{} to be sent instead of Plug.Conn.request_url(conn).

Does this matter? Should apply_fun_with_conn get a new 3rd default argument that defaults to %{}?

defp apply_fun_with_conn(conn, function, default \\ %{})
defp apply_fun_with_conn(_conn, _function = nil, default), do: default
defp apply_fun_with_conn(conn, {module, fun}, _default), do: apply(module, fun, [conn])
defp apply_fun_with_conn(conn, fun, _default) when is_function(fun, 1), do: fun.(conn)
url: apply_fun_with_conn(conn, url_scrubber, Plug.Conn.request_url(conn)),

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes let's go with a third argument, but without the \\ clause. Just do apply_fun_with_conn(conn, function, default) and explicitly pass in %{} in the other instances where we call apply_fun_with_conn/2 today.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do! I like the explicitness.


It can also be passed in as a `{module, fun}` like so:

plug Sentry.PlugContext, header_scrubber: {MyModule, :scrub_headers}
plug Sentry.PlugContext, header_scrubber: {MySentryScrubber, :scrub_headers}
Copy link
Contributor Author

@paulstatezny paulstatezny Feb 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's an insignificant detail, but there was some inconsistency with what module names were used here in the docs. Since I was updating a new, related section and wanted to be consistent, I thought it felt clean to set them all to MySentryScrubber.

Happy to revert if this isn't preferred!

@paulstatezny
Copy link
Contributor Author

I think this test failure is a fluke. Cannot reproduce it on my machine.

@@ -152,7 +175,7 @@ defmodule Sentry.PlugContext do
|> Plug.Conn.fetch_query_params()

%{
url: Plug.Conn.request_url(conn),
url: apply_fun_with_conn(conn, url_scrubber),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes let's go with a third argument, but without the \\ clause. Just do apply_fun_with_conn(conn, function, default) and explicitly pass in %{} in the other instances where we call apply_fun_with_conn/2 today.

@@ -105,6 +109,13 @@ defmodule Sentry.PlugContextTest do
assert %{"not-secret" => "not-secret"} == Sentry.Context.get_all().request.cookies
end

test "allows configuring url scrubber" do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tiny but mighty 😉

Suggested change
test "allows configuring url scrubber" do
test "allows configuring URL scrubber" do

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

plug Sentry.PlugContext, cookie_scrubber: &MyCookieScrubber.scrub_cookies/1
plug Sentry.PlugContext, cookie_scrubber: &MySentryScrubber.scrub_cookies/1

### Scrubbing URLs
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to say that this has not always been available

Suggested change
### Scrubbing URLs
### Scrubbing URLs
*Available since v10.2.0.*

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! Thanks for the suggestion.

@paulstatezny
Copy link
Contributor Author

@whatyouhide Thank you for the feedback! Addressed all of it in de2fc94.

CI is failing in the same way as before; I think this is a fluke, as I cannot reproduce locally.

  1) test reporting from non-allowed child processes (Sentry.TestTest)
Error:      test/sentry/test_test.exs:145
     Assertion failed, no matching message after 1000ms
     The following variables were pinned:
       monitor_ref = #Reference<0.1770092520.2537553923.95488>
     The process mailbox is empty.
     code: assert_receive {:DOWN, ^monitor_ref, _, _, :normal}
     stacktrace:
       test/sentry/test_test.exs:171: (test)

Please let me know if you think I need to adjust the changes in any way!

@whatyouhide whatyouhide merged commit 037090c into getsentry:master Feb 2, 2024
0 of 3 checks passed
@whatyouhide
Copy link
Collaborator

@paulstatezny yeah CI is unrelated, no worries. Thank you! 💟

@paulstatezny paulstatezny deleted the ps/url-scrubber branch February 3, 2024 03:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants