Trying to compare two arrays for equality without regard to order in a unit test is often very annoying. In Java, it would generally take four or five lines of code to express this in a test, completely obscuring the aspect of the code that I was trying to test. Ruby's Test::Unit turned that comparison into a one-liner, (expected - actual).empty?, a definite improvement but still a bit ugly. I was testing this in RSpec a couple days ago and figured there had to be a better way, and there is: actual.should =~ expected. I probably would have written a custom matcher for this rather than overload =~ if I was designing it myself, but either way, it's a big improvement in test expressiveness and readability.
Showing posts with label ruby. Show all posts
Showing posts with label ruby. Show all posts
Tuesday, June 05, 2012
Friday, February 24, 2012
Rails Parameter Filters
Secure programming 101 dictates that you should never write sensitive information to log files, and Rails makes this easy by allowing you to specify a list of sensitive fields in the filter_parameters property of your application configuration object (defined in the config/application.rb file). It's even nice enough to give you a sensible default (:password).
I got tripped up by this a couple days ago when a non-sensitive parameter that I didn't want to get filtered out of my logs was getting caught by the filter. I struggled with this for a while until I found the code responsible for the filtering, which clearly states in its comments that it filters out any parameter whose name matches the regular expression /<filter_param>/i. In other words, any parameter that contains one of your filter parameter strings will get filtered. It doesn't even have to be in the same case. Here's some example code demonstrating what's going on
The other thing I discovered is that other code that you include in your project (i.e. gems) can modify the list of filter parameters. For example, the clearance gem adds :token and :password to the filter parameters. To get the definitive list of all of the filter parameters in your application, launch the rails console and inspect the value of <ProjectName>::Application.config.filter_parameters.
I got tripped up by this a couple days ago when a non-sensitive parameter that I didn't want to get filtered out of my logs was getting caught by the filter. I struggled with this for a while until I found the code responsible for the filtering, which clearly states in its comments that it filters out any parameter whose name matches the regular expression /<filter_param>/i. In other words, any parameter that contains one of your filter parameter strings will get filtered. It doesn't even have to be in the same case. Here's some example code demonstrating what's going on
The other thing I discovered is that other code that you include in your project (i.e. gems) can modify the list of filter parameters. For example, the clearance gem adds :token and :password to the filter parameters. To get the definitive list of all of the filter parameters in your application, launch the rails console and inspect the value of <ProjectName>::Application.config.filter_parameters.
Labels:
programming,
rails,
ruby,
ruby on rails,
security
Subscribe to:
Posts (Atom)