snippetrubyMinor
filter out password in xml string
Viewed 0 times
xmlpasswordfilterstringout
Problem
We're storing the xml communications with an external service in a text in the db and in a
Is there a better, safer way of doing it?
before_create I've got the following:# filter opera password
def remove_password!
self.request.gsub! /UserPassword\>[^\[FILTERED]</'
endIs there a better, safer way of doing it?
Solution
That depends on how confident you are that neither of these cases will be true:
An example of the last would be:
It seems your assumptions are safe, but if you want to be certain, go with
By eliminating the greediness of the regex, you can match the exact tag you mean and reduce the assumptions that will come back and bite you.
- Another tag like will be introduced that you will want to leave unfiltered
- No nested tags will appear within the tag
An example of the last would be:
SueIt seems your assumptions are safe, but if you want to be certain, go with
self.request.gsub! /\.+?\/gm, '[FILTERED]'By eliminating the greediness of the regex, you can match the exact tag you mean and reduce the assumptions that will come back and bite you.
Code Snippets
<UserPassword><for name="boy">Sue</for></UserPassword>self.request.gsub! /\<UserPassword\>.+?\</UserPassword\>/gm, '<UserPassword>[FILTERED]</UserPassword>'Context
StackExchange Code Review Q#6914, answer score: 3
Revisions (0)
No revisions yet.