diff --git a/config/config.exs b/config/config.exs index 36dc5552..6cb61341 100644 --- a/config/config.exs +++ b/config/config.exs @@ -7,7 +7,11 @@ # General application configuration import Config -config :error_tracker, repo: MusicLibrary.Repo, otp_app: :music_library, enabled: false +config :error_tracker, + repo: MusicLibrary.Repo, + otp_app: :music_library, + enabled: false, + ignorer: MusicLibrary.ErrorIgnorer config :elixir, :time_zone_database, TimeZoneInfo.TimeZoneDatabase diff --git a/lib/music_library/error_ignorer.ex b/lib/music_library/error_ignorer.ex new file mode 100644 index 00000000..a2847a4d --- /dev/null +++ b/lib/music_library/error_ignorer.ex @@ -0,0 +1,18 @@ +defmodule MusicLibrary.ErrorIgnorer do + @moduledoc """ + Ignores errors that should not be tracked by ErrorTracker. + + Bot scanners and crawlers routinely hit non-existent paths, generating + NoRouteError exceptions that are not actionable. We ignore them here + rather than blocking specific paths in the endpoint. + """ + @behaviour ErrorTracker.Ignorer + + @ignored_kinds [ + to_string(Phoenix.Router.NoRouteError) + ] + + @impl true + def ignore?(%ErrorTracker.Error{kind: kind}, _context) when kind in @ignored_kinds, do: true + def ignore?(_error, _context), do: false +end diff --git a/lib/music_library_web/endpoint.ex b/lib/music_library_web/endpoint.ex index 4778f7b3..b4391192 100644 --- a/lib/music_library_web/endpoint.ex +++ b/lib/music_library_web/endpoint.ex @@ -43,8 +43,6 @@ defmodule MusicLibraryWeb.Endpoint do plug Phoenix.Ecto.CheckRepoStatus, otp_app: :music_library end - plug :reject_bot_scanners - plug Phoenix.LiveDashboard.RequestLogger, param_key: "request_logger", cookie_key: "request_logger" @@ -61,29 +59,4 @@ defmodule MusicLibraryWeb.Endpoint do plug Plug.Head plug Plug.Session, @session_options plug MusicLibraryWeb.Router - - defp reject_bot_scanners(%{request_path: "/wp-" <> _} = conn, _opts), - do: conn |> send_resp(404, "") |> halt() - - defp reject_bot_scanners(%{request_path: "/wordpress" <> _} = conn, _opts), - do: conn |> send_resp(404, "") |> halt() - - @blocked_paths [ - "/.env", - "/admin", - "/blog/wp-includes/wlwmanifest.xml", - "/files.php", - "/grsiuk.php", - "/jq.php", - "/style.php", - "/vgtyu.php", - "/vx.php", - "/xleet.php", - "/xmlrpc.php" - ] - - defp reject_bot_scanners(%{request_path: path} = conn, _opts) when path in @blocked_paths, - do: conn |> send_resp(404, "") |> halt() - - defp reject_bot_scanners(conn, _opts), do: conn end