I am running Rails 3 and trying to filter sensitive information out of our logs which are JSON blobs that are passed as post parameters. For example, user creation might take a post param called user
with a string value that is a JSON object. One of the keys in the JSON object is password
and we want to filter this out of our logs. The best way I found to do this was to add a block to our filter_params, like so:
keys_to_filter = ['password', 'password_confirmation']
config.filter_parameters << lambda do |k,v|
if v.is_a? String
keys_to_filter.each do |key|
# Match "key":"<filter_out>", or "key":"<filter_out>"}, allowing for whitespace
v.sub!(/("\s*#{key}\s*")\s*:\s*"[^,\}]*"\s*([,\}])/, "\\1:\"[FILTERED]\"\\2")
end
end
end
This adds a block to the filter_params, which causes an error which is described in another question: Rails: ParameterFilter::compiled_filter tries to dup symbol
It appears that it is not safe to pass a block to filter_parameters, so I'm wondering if there is another way to solve this problem.
filter_parameters
to fail in findingpassword
.Parameters: {"{\"user\":{\"first_name\":\"Barry\",\"last_name\":\"Hess\",\"email\":\"[email protected]\",\"password\":\"notfilterediswear\"}}"}
– ErlandsonContent-Type
(or complete lack of it) in API calls meaning Rails didn't format properly to the log. ProperContent-Type
, proper parameter filtering. – Erlandson