π Keep credentials carried in a Rails path segment out of the log.
Rails' config.filter_parameters filters query strings and form bodies from the log, but never path segments. A join code at /join/:code, a session transfer id, a signed token at /mailboxes/:id/forwardings/:token/confirmation all reach the production log in full. Declare which segments of which routes are credentials, and the log shows [FILTERED] in their place.
filter_path_parameters reproduces the path filter of a private gem, with the same public surface.
- Installation
- Why
- Declaring Routes
- A Prefix
- Filtering a Path Yourself
- Testing Your Routes
- What Is Filtered
- What Is Not Filtered
- Why These Are Overrides
- The Proxy in Front
- Testing
- History
- Contributing
- License
Add this line to your application's Gemfile:
gem "filter_path_parameters"Requires Ruby >= 3.4 and Rails >= 8.0.
Four lines carry the path of a request into the log, and filter_parameters touches none of them:
Started GET "/join/k7m2x9qp4" for ..., whichRails::Rack::Loggerwrites fromrequest.filtered_path- the
pathof theprocess_action.action_controllerevent, set fromrequest.filtered_path, which every structured log subscriber reads Redirected to https://app.example/join/k7m2x9qp4, fromresponse.filtered_locationActionController::RoutingError (No route matches [GET] "/join/k7m2x9qp4/xmlrpc.php"), raised with the rawPATH_INFOwhen no route matches, so a scanner probing a join link gets a 404 whose logged message still holds the code
A join link before:
Started GET "/800966886/join/k7m2x9qp4?utm=mail" for 203.0.113.9 at 2026-10-21 09:12:04 +0000
Processing by JoinCodesController#new as HTML
Parameters: {"code" => "[FILTERED]"}
Redirected to https://app.example/800966886/join/k7m2x9qp4/account
And after:
Started GET "/800966886/join/[FILTERED]?utm=mail" for 203.0.113.9 at 2026-10-21 09:12:04 +0000
Processing by JoinCodesController#new as HTML
Parameters: {"code" => "[FILTERED]"}
Redirected to https://app.example/800966886/join/[FILTERED]/account
Only what is logged changes. The request path, routing, request.path, fullpath, original_url and what the app serves are untouched.
In config/routes.rb, wrap the routes in filter_path_parameters and name the segments that are credentials. Every route drawn inside the block is marked, nested ones included:
filter_path_parameters :code do
get "join/:code", to: "join_codes#new", as: :join
post "join/:code", to: "join_codes#create"
end
filter_path_parameters :token, :forwarding_token do
resources :forwardings, param: :token, only: :create do
resource :confirmation, only: %i[ show create ]
end
endA path is filtered when it starts with a declared route's segments, whether or not anything routes the rest, so /join/k7m2x9qp4/xmlrpc.php becomes /join/[FILTERED]/xmlrpc.php. A declared segment is one whole path segment, format suffix included: /join/k7m2x9qp4.json becomes /join/[FILTERED]. A glob (*path) takes the rest of the path. A dynamic segment that is not declared, like the :id in /mailboxes/:id/forwardings/:token, matches any segment and is kept. Whatever follows the last declared segment is left alone. Rails squeezes repeated slashes before routing, so //join/k7m2x9qp4 is matched too, and a literal in another case is filtered rather than logged.
On a request a declared route serves, its declared names are filtered from the request's parameters too, so filter_path_parameters :id around resources :transfers logs Parameters: {"id" => "[FILTERED]"} for a transfer while id stays visible on every other route, without adding id to filter_parameters. A key of that name nested in the body is filtered too.
Filtering more than needed is fine; too little is not. Blocks nest, and the names of each block apply to the routes drawn inside it.
Routes reload in development; the declarations are rebuilt with every draw and never accumulate across reloads.
An optional regexp matched at the start of the path and kept unchanged; the declared routes are matched after it. For an app that mounts tenants at a prefix its own middleware moves from PATH_INFO to SCRIPT_NAME before routing, so that the routes themselves are drawn unprefixed:
# config/initializers/filter_path_parameters.rb
FilterPathParameters.prefix = /\A(\/\d{6,})/The prefix is optional in the match, so a path without it, say a Referer of /join/k7m2x9qp4, is filtered too.
FilterPathParameters.filter_path("/800966886/join/k7m2x9qp4") # => "/800966886/join/[FILTERED]"
FilterPathParameters.filter_path("/join/k7m2x9qp4/xmlrpc.php") # => "/join/[FILTERED]/xmlrpc.php"
FilterPathParameters.filter_path("/boards/42") # => "/boards/42", not declared
FilterPathParameters.filter_url("https://x.test/800966886/join/abc?x=1")
# => "https://x.test/800966886/join/[FILTERED]?x=1"filter_url filters the path of a URL and keeps its scheme, host, query and fragment as they are. It takes a bare path too, and a string that is no URL comes back unchanged rather than raising.
A test pinning that a credential path stays filtered, with the real route helpers:
class CredentialPathsTest < ActiveSupport::TestCase
include FilterPathParameters::Assertions
include Rails.application.routes.url_helpers
test "a join code is filtered" do
assert_path_filtered join_path("made-up-code", script_name: "/800966886"), "made-up-code"
end
endrequest.filtered_path: theStartedline, thepathof theprocess_action.action_controllerevent, and whatever reads eitherrequest.filtered_parameterson a declared route: theParametersline, theparamsof theprocess_action.action_controllerevent, and what an error reporter reads from the requestresponse.filtered_location: theRedirected toline and thelocationof the same event- the message of
ActionController::RoutingError, in the exception itself, so the lineActionDispatch::DebugExceptionslogs and whatever an error reporter is handed are both filtered - the values of declared names in the message of
ActionController::UrlGenerationError, which a route helper raises with the values it was given
Rails' own filtering of the query string and of the redirect location stays as it is; the path filter runs on top of it.
- the message of any other exception, such as
ActionController::BadRequestfor a segment that is not valid UTF-8, or an app's ownActiveRecord::RecordNotFound - the full URL an error reporter takes from the request,
request.urlorrequest.original_url, andrequest.filtered_env, which holds the raw path - the
Refererheader, when a page at a declared path links elsewhere - in development and test, the
Startedline of the first request, when routes are loaded lazily and nothing is declared yet, whichconfig.eager_load = truecloses
Configure an error reporter to filter its URL and headers with FilterPathParameters.filter_url.
A declared route's path is not filtered, or not in full, when the route has one of these shapes:
- an optional group before a declared segment, as in
scope "(:locale)": a path without the group is left whole - routes declared inside a mounted engine, or in an app served under a sub-URI, whose paths carry a prefix the route does not;
FilterPathParameters.prefixcovers a fixed one - a constraint on a declared segment that allows
/: only the value's first segment is filtered
Rails has no hook for any of this: filtered_path and filtered_location are computed and read inside Rails, and the routing errors are raised with the raw path or the values a route helper was given. Each override is a module prepended from the railtie, calls super, and filters the result. It is the same thing the private gem it reproduces does, and test/overrides_test.rb pins that each method is still there on the Rails in the bundle, so a Rails that renames one fails loudly instead of leaking quietly.
| Rails method | Override | For |
|---|---|---|
ActionDispatch::Http::FilterParameters#filtered_path |
FilterPathParameters::Request |
the Started line and the process_action event |
ActionDispatch::Http::FilterParameters#parameter_filter |
FilterPathParameters::Request |
the declared names in a declared route's parameters |
ActionDispatch::Http::FilterRedirect#filtered_location |
FilterPathParameters::Response |
the Redirected to line |
ActionController::RoutingError#initialize |
FilterPathParameters::RoutingError |
the No route matches message |
ActionController::UrlGenerationError#initialize |
FilterPathParameters::UrlGenerationError |
the values in a route helper's No route matches message |
ActionDispatch::Routing::RouteSet#clear! |
FilterPathParameters::RouteSet |
dropping a route set's declarations when its routes are cleared for a redraw |
filter_path_parameters itself is a method added to ActionDispatch::Routing::Mapper, not an override.
The proxy in front of the app logs the raw path before the app sees it. kamal-proxy's --log-filter-path (branch log-filter-path), which Kamal sets through proxy.logging.filter_paths, is the matching setting there.
bundle install
rakeThe tests run against the Rails main the Gemfile pins, in a small in-process application.
View the changelog.
Everyone is encouraged to help improve this project:
- Report bugs
- Fix bugs and submit pull requests
- Write, clarify, or fix documentation
- Suggest or add new features
MIT. See LICENSE.