# filter\_parameter\_logging on "dynamic" parameters

**URL:** <https://discuss.rubyonrails.org/t/filter-parameter-logging-on-dynamic-parameters/51177>\
**Category:** rubyonrails-talk\
**Created:** [April 25, 2010, 2:53pm UTC](https://discuss.rubyonrails.org/t/filter-parameter-logging-on-dynamic-parameters/51177 "2010-04-25T14:53:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [April 25, 2010, 2:53pm UTC](https://discuss.rubyonrails.org/t/filter-parameter-logging-on-dynamic-parameters/51177/1 "2010-04-25T14:53:09Z")

</div>

Hi,

I'm using the negative\_captcha plugin for my signup form on a website, and I'd like to have the parameters filtered out in my logs.

My problem is : negative\_captcha replaces the parameters names with some md5 hashes in a before\_filter and leaves the uncrypted parameter as a honeypot to identify bots. So if I use filter\_parameter\_logging :password, I end up with this line in my logs :

Parameters: {... "829334b5e733bd4eefa3d2e02337a7e1"=\>"PASSWORD", "password" =\> "[FILTERED]" ...}

That's obviously not what I want. I thought I could add @captcha.fields[:password] as a parameter, but filter\_parameter\_logging is a Class method so @captcha is nil when it's called.

I'm out of idea for this, would anyone have a solution or some tips about this ?

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [April 25, 2010, 3:14pm UTC](https://discuss.rubyonrails.org/t/filter-parameter-logging-on-dynamic-parameters/51177/2 "2010-04-25T15:14:31Z")

</div>

> That's obviously not what I want. I thought I could add @captcha.fields[:password] as a parameter, but filter\_parameter\_logging is a Class method so @captcha is nil when it's called.
> 
> I'm out of idea for this, would anyone have a solution or some tips about this ?

If you give filter\_parameter\_logging a block it will yield parameter names/values to that block and you can make the decision to filter on a case by case basis. Is that enough for you ?

Fred

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [April 25, 2010, 3:20pm UTC](https://discuss.rubyonrails.org/t/filter-parameter-logging-on-dynamic-parameters/51177/3 "2010-04-25T15:20:12Z")

</div>

Thanks for the quick reply 🙂

> If you give filter\_parameter\_logging a block it will yield parameter names/values to that block and you can make the decision to filter on a case by case basis. Is that enough for you ?

I've tried this, but again the fact that filter\_parameter\_logging is a class method does not help...

filter\_parameter\_logging (:some, :other, :parameters) { |k,v] &nbsp;&nbsp;v.replace "[FILTERED]" if @captcha.fields.include? k }

raises an exception saying @captcha is nil. Which is normal, as it is initialized in a before\_filter invoked after the call to filter\_parameter\_logging...

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [April 25, 2010, 3:21pm UTC](https://discuss.rubyonrails.org/t/filter-parameter-logging-on-dynamic-parameters/51177/4 "2010-04-25T15:21:34Z")

</div>

what I didn't mention and is the core of this problem, is that those captcha parameters are salted, so they are different for each and every client. That's why I need to access the initialized @captcha object...

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [April 25, 2010, 4:21pm UTC](https://discuss.rubyonrails.org/t/filter-parameter-logging-on-dynamic-parameters/51177/5 "2010-04-25T16:21:57Z")

</div>

> Thanks for the quick reply 🙂
> 
> \> If you give filter\_parameter\_logging a block it will yield parameter \> names/values to that block and you can make the decision to filter on \> a case by case basis. Is that enough for you ?
> 
> I've tried this, but again the fact that filter\_parameter\_logging is a class method does not help...
> 
> filter\_parameter\_logging (:some, :other, :parameters) { |k,v] v.replace "[FILTERED]" if @captcha.fields.include? k
> 
> }
> 
> raises an exception saying @captcha is nil. Which is normal, as it is initialized in a before\_filter invoked after the call to filter\_parameter\_logging...

Is it not possible to work out whether a parameter name looks like a captcha parameters ? Failing that, all filter\_parameter\_logging does is define an instance method called filter\_parameters - you could define such a method without the help of filter\_parameter\_logging (although you might want to inspire your self from the source for filter\_parameter\_logging to understand what the filter\_parameters method should look like)

Fred

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [May 16, 2010, 9:53pm UTC](https://discuss.rubyonrails.org/t/filter-parameter-logging-on-dynamic-parameters/51177/6 "2010-05-16T21:53:45Z")

</div>

Hi Fred,

I'm sorry I took so long to answer, I was busy with other developments...

Thanks for the filter\_parameters idea, that was the solution for me. Guessing whether a parameter name is a captcha worked well too but I couldn't be selective enough: I had to filter all parameters looking like a MD5 hash, whereas I'd like to keep emails appearing in my logs on signups in case I have to trace back a problem from a user.

So I defined a filter\_parameters method, which is mainly taken from Rails source. I defined my parameter\_filter out of a constant array to emulate filter\_parameter\_logging mechanism. This array contains the params names I was giving to filter\_parameter\_logging, plus the captcha params I want to filter out.

I actually don't know how and why my own filter\_parameters method gets called. Any pointer on that? Would you know of any way to keep a filter-like way of calling this? (like adding my\_own\_filter\_parameter\_logging :param1, :param2)

Thanks for your help! Olivier

Frederick Cheung wrote:
