# Prevent Logging Sensitive Data with Rails Parameter Filters

*Published on February 01, 2024*

Tags: Rails Internals

---


> He (Paul Allen) and Bill (Gates) would go “dumpster diving” in C-Cubed’s garbage to find discarded printouts with source code for the machine’s operating system — an area Paul saw as the “Holy Grail” of software, which he knew he could use to unlock the machine’s potential. -&nbsp;[Futurist](https://paulallen.com/Futurist/Microsoft.aspx)

Most applications have sensitive data they'd like to keep out of reach from prying eyes. It can be something as obvious as passwords and credit card numbers, or something less obvious but equally important, like Stripe API keys.

Exposing this information in log files is a very common way to leak sensitive data accidentally. Developers often like to log everything for debugging or troubleshooting purposes. Additionally, frameworks like Ruby on Rails log a lot of information at various stages of the request lifecycle.&nbsp;**If you're not careful, it's very easy to leak any sensitive data in the log files.**

Here's an example where the application logs out the user's password along with other information after they complete and submit the user registration form:

![Exposed password in logs](https://writesoftwarewell.com/media/xldygvsc2jfj60shfk6gnfut89m3.png "Exposed password in logs")

If some sneaky intruder gets their hands on your log files, all they gotta do is sift through the logs, and bam! Your application's and your users' private information is at risk. To prevent this, Rails lets you filter certain sensitive information from&nbsp;leaking into the logs. This article shows you how.

Here’s a list of topics we'll explore:

- Why you shouldn't log sensitive information in production
- How to filter logs in Rails with parameter filtering
- Selectively filter attributes with the same name
- Filter Attributes on ActiveRecord Models
- How Rails implements parameter filtering
- The&nbsp;`ParameterFilter`&nbsp;class API

## Why You Shouldn't Log Sensitive Stuff

> 💡**It's very easy to accidentally leak confidential information through logs.**

Imagine a user is making a payment on your web application. Once they enter their credit card details and hit submit, the website sends all this information in plain text to your server.

Upon receiving this request, the application logs information about this request, such as the user's IP address, user id, name, form data such as payment information, and much more.

These log files are handy when debugging the application, but in production, you may not want every bit of information to be stored in the log file.

Here're three reasons against logging sensitive information.

1. **It makes an attacker's life easy.**&nbsp;Since most logs are stored in plain text files for readability, if your servers are compromised and an attacker gets access to the log files, they can just search the file for anything they need.
2. **Data Privacy & Curious Employees.**&nbsp;It doesn't have to be hackers. It could just be a curious employee with access to the logs who might be interested in looking into customers' sensitive data. Even if the employee may not have bad intentions, it can land you in a data-privacy hot zone for exposing user-identifiable personal information.
3. **They are leaking to a third-party app.**&nbsp;Many applications use error tracking or performance monitoring systems such as AppSignal, New Relic, or Sentry. Whenever a request is made to your application, these tools grab all the user-submitted data and send it to their servers. This data could contain user-identifiable information.

For these reasons, you should prevent sensitive information from leaking into the log files.

Here're some possible solutions to address this problem.

1. **Manual Code Review:**&nbsp;A basic, but tedious way is to review your codebase to find all the places where you're logging information and filter it to log only essential information, stripping out everything else.
2. **Automate:**&nbsp;A better solution is to make your logger check for the presence of specific sensitive attributes on the data and replace them with \`***\`. This is better, but it's still a lot of work.
3. **Configure:**&nbsp;The best solution is to program and abstract it via configuration. That way, you list the sensitive information in one location (the configuration file), and don't have to worry about it ever.

If you're using Ruby on Rails, you're in luck, because that's what Rails does for you via parameter filtering.

Let's understand how it works.

## How to Filter Logs in Rails with Parameter Filter

Ruby on Rails makes it very easy to filter sensitive information from log files by providing a central location where you can configure this information.

For example, if you want to hide attributes named&nbsp;`password`&nbsp;and&nbsp;`credit_card`, add this line in the&nbsp;`filter_parameter_logging`&nbsp;initializer under the&nbsp;`config/initializers`&nbsp;directory.

```ruby
# config/initializers/filter_parameter_logging.rb

Rails.application.config.filter_parameters += [
  :password, :secret, :token, :_key, :crypt, :salt, :certificate, :otp, :ssn
]
```

This tells Rails to replace all instances of the provided parameters with the word&nbsp;`[FILTERED]`&nbsp;and hide their real values.

Let's reconsider our previous example. This time, when the user submits the payment form, Rails will still log the user's id and name, but it will replace the password parameter with the word&nbsp;`FILTERED`&nbsp;and its value won't appear in the logs.

Here's what the logs look like after we use parameter filtering to filter out the password and authentication tokens. Notice that the actual values are replaced with&nbsp;`[FILTERED]`&nbsp;mask.

![Parameter Filtering in Rails](https://writesoftwarewell.com/media/1ej1zowy4ps4ndtacd4ic9bquveb.png "Parameter Filtering in Rails")

## Selectively Filter Attributes with the Same Name

Here's a tricky situation. Let's say you have an attribute named&nbsp;`number`&nbsp;for both&nbsp;`Post`&nbsp;and&nbsp;`CreditCard`&nbsp;models. The Post number is public and need not be hidden, but the Credit Card number is very sensitive information you want to hide.

Simply adding&nbsp;`number`&nbsp;to the&nbsp;`filter_parameters`&nbsp;array will hide all numbers, which is not what you want. You still want to access Post numbers for debugging.

You can solve this problem by scoping the&nbsp;`number`&nbsp;attribute to the&nbsp;`CreditCard`&nbsp;model.

```ruby
config.filter_parameters += [ "credit_card.number" ]
```

Now Rails will only filter the credit card numbers without affecting the post numbers.

Additionally, you can pass a block (a function) to the&nbsp;`filter_parameters`&nbsp;array, giving you more control and flexibility while filtering. The following example retains the credit card number format while replacing the numbers with '\*'.

```ruby
# config/initializers/parameter_filtering.rb
 
Rails.application.config.filter_parameters += ->(key, value) { value.gsub!(/\d/, "*") if /credit_card/.match?(key) }
```

Pretty cool, right?&nbsp;**But wait, it gets even better!**

## Filter Attributes on ActiveRecord Models

You can use the&nbsp;`filter_attributes`&nbsp;method on your ActiveRecord models to tell Rails which columns should not be exposed.

```ruby
class User < ApplicationRecord
  self.filter_attributes += [ :secret, :token ]
end
```

Now, when you inspect a user, Rails will hide the values of the above attributes:

```ruby
> User.first
#=> #<id: 1, name: 'Akshay', secret: "[FILTERED]", token: "[FILTERED]">
```

If you're curious about how Rails implements this feature, keep reading.

## How Rails Implements Parameter Filtering

Before reading this, I encourage you to check my previous article,&nbsp;[A Brief Introduction to Rails Initializers](/introduction-to-rails-initializers). Understanding how initializers work in general will help you better understand the&nbsp;`parameter_filtering`&nbsp;initializer.

Let's revisit our code to define sensitive attributes in the configuration. We added the following code in the&nbsp;`filter_parameter_logging`&nbsp;initializer under the&nbsp;`config/initializers`&nbsp;directory.

```ruby
config.filter_parameters += [ "password" ]
```

The&nbsp;`ActiveRecord::Railtie`&nbsp;class (a fancy word for a class that initializes ActiveRecord) grabs the&nbsp;`filter_parameters`&nbsp;array from the config, and pushes it to the&nbsp;`filter_attributes`&nbsp;array.

```ruby
# activerecord/lib/active_record/railtie.rb

module ActiveRecord
  class Railtie < Rails::Railtie
    initializer "active_record.set_filter_attributes" do
      ActiveSupport.on_load(:active_record) do
        self.filter_attributes += Rails.application.config.filter_parameters
      end
    end
  end
end
```

The&nbsp;`filter_attributes`&nbsp;array is defined in the&nbsp;`ActiveRecord::Core`&nbsp;module that gets included in the&nbsp;`ActiveRecord::Base`&nbsp;class, the superclass of&nbsp;`ApplicationRecord`, the class from which all your active record domain classes inherit.

```ruby
# activerecord/lib/active_record/core.rb

module ActiveRecord
  module Core
    extend ActiveSupport::Concern
    
    included do
      self.filter_attributes = []
    end
  end
end
```

The&nbsp;`ActiveRecord::Core`&nbsp;module is a concern and adds the&nbsp;`filter_attributes`&nbsp;array on the `ActiveRecord::Base`&nbsp;class.

![rails-classes.png](https://writesoftwarewell.com/media/0pynuj7hjykshete4wme4seqpnlx.png "Rails Classes")

_**Don't really understand concerns?**_&nbsp;_Check out this article to learn more about concerns and how they work_: [Concerns in Rails: Everything You Need to Know](/how-rails-concerns-work-and-how-to-use-them)

Okay, so far we've seen how Rails added the&nbsp;`filter_attributes`&nbsp;array to your models' superclass. Now let's inspect how Rails uses this&nbsp;`filter_attributes`&nbsp;array for filtering sensitive information.

All the methods dealing with this array are included under the&nbsp;`ClassMethods`&nbsp;module. Hence they become class methods on the&nbsp;`ActiveRecord::Base`&nbsp;class.

```ruby
module ActiveRecord
  module Core
    extend ActiveSupport::Concern

    included do
      self.filter_attributes = []
    end
    
    module ClassMethods
      def filter_attributes 
      end

      def filter_attributes=(filter_attributes)
      end

      def inspection_filter
      end
    end
  end
end
```

Let's understand how they work in two simple steps:

**Step 1:**&nbsp;Set the&nbsp;`filter_attributes`, which specifies columns which shouldn't be exposed. Here, we initialize the class instance variable&nbsp;`@filter_attributes`.

```ruby
def filter_attributes=(filter_attributes)
  @inspection_filter = nil
  @filter_attributes = filter_attributes
end
```

**Step 2:**&nbsp;The&nbsp;`inspection_filter`&nbsp;method creates an instance of the `ActiveSupport::ParameterFilter` class, which is responsible for filtering the parameters. The mask is set to&nbsp;`[FILTERED]`&nbsp;by default.

Here's the simplified implementation of this method.

```ruby
def inspection_filter
  @inspection_filter ||= begin
    mask = InspectionMask.new(ActiveSupport::ParameterFilter::FILTERED)
    ActiveSupport::ParameterFilter.new(@filter_attributes, mask: mask)
  end
end
```

Whenever Rails needs to filter sensitive information, it uses the&nbsp;`inspection_filter`&nbsp;method.&nbsp;**There're only three places in the entire Rails codebase where**&nbsp;**`inspection_filter`**&nbsp;**is used.**

Here's the highly simplified version of these methods.

```ruby
# activerecord/lib/active_record/core.rb
def pretty_print(pp)
  value = inspection_filter.filter_param(attr_name, value)
end

# activerecord/lib/active_record/log_subscriber.rb
def filter(name, value)
  ActiveRecord::Base.inspection_filter.filter_param(name, value)
end

# activerecord/lib/active_record/attribute_methods.rb
def format_for_inspect(name, value)
  inspection_filter.filter_param(name, value)
end
```

Whenever you try to&nbsp;**pretty print, log, or inspect**&nbsp;an ActiveRecord model, Rails will use the `inspection_filter`&nbsp;to hide the value of the sensitive attributes.

We're not done yet. So far, we've been looking at the infrastructure code, i.e., how Rails defines and uses the filtered array of attributes. Now let's try to understand the core of the solution, the&nbsp;`ActiveSupport::ParameterFilter`&nbsp;class.

## How ParameterFilter Class Works

The&nbsp;`ActiveSupport::ParameterFilter`&nbsp;class replaces the values for specified keys from a hash-like object, if the keys match one of the pre-defined filter.

> 💡Matching based on nested keys is possible by using dot notation, e.g.&nbsp;`"credit_card.number"`.

To work with it, initialize an instance of this class with an array of keys and then call either either&nbsp;`filter(params)`&nbsp;or&nbsp;`filter_param(key, value)`&nbsp;on this instance. These methods replace the sensitive values with a mask, which is&nbsp;`[FILTERED]`&nbsp;by default.

- The&nbsp;`filter(params)`&nbsp;method replaces all values corresponding to filters with the mask
- The&nbsp;`filter_param(key, value)`&nbsp;replaces the value with a mask if the key belongs to the&nbsp;`filters`&nbsp;list, which is provided during the initialization.

Here's the external API of the&nbsp;`ParameterFilter`&nbsp;class.

```ruby
module ActiveSupport
  class ParameterFilter
    # default mask
    FILTERED = "[FILTERED]"
    
    def initialize(filters = [], mask: FILTERED)
    end
    
    # replace all values corresponding to filters with the mask
    def filter(params)
    end
    
    # replace the value with mask if key belongs to filters 
    def filter_param(key, value)
    end
  end
end
```

The following test shows how it works:

```ruby
test "parameter filter" do
  parameter_filter = ActiveSupport::ParameterFilter.new(['foo'])
    
  assert_equal ({ "foo" => "[FILTERED]", "bar" => "baz" }), parameter_filter.filter({ "foo" => "bar", "bar" => "baz" })
  assert_equal "[FILTERED]", parameter_filter.filter_param("foo", "bar")
end
```

You might be surprised to find that&nbsp;`ParameterFilter`&nbsp;class can also accept a block/lambda as one of the filters. During replacement, this block will be called with the provided key-value pair, and you can replace the value in place.

This example replaces the credit card numbers while preserving their format.

```ruby
test "parameter filter with block" do
  filter_params = []
  filter_params << ->(key, value) { value.gsub!(/\d/, "*") if /credit_card/.match?(key) }
    
  parameter_filter = ActiveSupport::ParameterFilter.new(filter_params)

  assert_equal ({ "credit_card" => "**** ****" }), parameter_filter.filter({ "credit_card" => "9999 9999" })
  assert_equal "*****", parameter_filter.filter_param("credit_card", "12345")
end
```

Internally, the&nbsp;`ParameterFilter`&nbsp;class uses the&nbsp;`CompiledFilter`&nbsp;class to filter the values with the mask. It accepts the&nbsp;`filters`&nbsp;and a mask to build a list of regular expressions that replace the values corresponding to filters with the provided mask.

And that's how Rails implements the parameter filtering feature to hide sensitive information. If you haven't used it, give it a try!

## Summary

- You should prevent sensitive, user-identifiable information from leaking into the log files. The log files are typically stored in plain text, and it's very easy to reveal confidential data accidentally.
- The parameter filtering feature in Rails allows you to filter important information from the logs.

If you'd like to read similar deep-dives into the Rails codebase, check out the&nbsp;[Rails Internals](/tag/rails-internals)&nbsp;tag on the blog. I love reading the Rails source code and frequently write new articles that show how Rails implements a certain feature.

