Created
June 6, 2012 23:59
-
-
Save charlieschwabacher/2885591 to your computer and use it in GitHub Desktop.
Proposed module to sanitize and escape attributes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module SanitizeAttributes | |
#override setters of some string or text attributes to sanitize w/ Sanitize.clean before setting | |
def sanitize_attribute(attribute) | |
self.send(:define_method, "#{attribute}=") do |arg| | |
self[attribute] = Sanitize.clean arg | |
end | |
end | |
def sanitize_attributes(*attributes) | |
attributes.map {|a| sanitize_attribute(a)} | |
end | |
#override setters of some string or text attributes to escape w/ CGI.escape before setting | |
def escape_attribute(attribute) | |
self.send(:define_method, "#{attribute}=") do |arg| | |
self[attribute] = CGI.escape arg | |
end | |
end | |
def escape_attributes(*attributes) | |
attributes.map {|a| escape_attribute(a)} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So this is what I have come up with to handle sanitizing attributes on our models. Model definitions would look like:
and then after defining properties, we would do something like: