devp / sanitize_attributes
- Source
- Commits
- Network (1)
- Issues (0)
- Downloads (0)
- Wiki (1)
- Graphs
-
Tree:
796d8f7
commit 796d8f7d592b7fddd61932e21fbb06227f128f63
tree ee1d6f22cb1e7d60c4645c7f34c19430e59223af
parent 4d52bd0b99a5aae14def20972eb0a9f4dd95207d
tree ee1d6f22cb1e7d60c4645c7f34c19430e59223af
parent 4d52bd0b99a5aae14def20972eb0a9f4dd95207d
| name | age | message | |
|---|---|---|---|
| |
MIT-LICENSE | ||
| |
README.rdoc | ||
| |
Rakefile | ||
| |
init.rb | ||
| |
lib/ | ||
| |
test/ |
README.rdoc
SanitizeAttributes
This is a simple plugin for ActiveRecord models to define sanitizable attributes. When an object is saved, those attributes will be run through whatever filter you’ve defined. You can define a default filter for all sanitizations.
Sanitization only happens for non-nil attributes. (Because a nil attribute may be valid for your model, and the sanitzers should only have to worry about working with strings.)
This was made to implement anti-XSS validation. My current gem of choice is Sanitize: github.com/rgrove/sanitize/tree/master
Example
# probably in environment.rb
SanitizeAttributes.define_default_sanitization_method do |text|
"PLACEHOLDER"
end
# app/models/nacho.rb
class Nacho
sanitize_attributes :foo, :bar
end
# app/models/burrito.rb
class Burrito
sanitize_attributes :baz
sanitize_attributes :title, :body do |text|
Sanitize.clean(text)
end
define_default_sanitization_method_for_class do |text|
"ANOTHER PLACEHOLDER"
end
end
# results...
n = Nacho.create!(:foo => 'something')
n.foo # => "PLACEHOLDER"
b = Burrito.create!(:baz=>'<script>alert(1)</script>', :title=>'<script>alert(1)</script>')
b.baz # => "ANOTHER PLACEHOLDER"
b.title # => "alert(1)"
b.body # => unprocessed; sanitzation filters are only run on non-nil values
Future Work
The following would be cool:
- allowing strings/symbols for sanitization methods, not just blocks
Nacho.default_sanitization_method_for_class :microwave # uses Nacho.microwave Nacho.default_sanitization_method_for_class "Sanitize.clean"
Etc
© 2009 Dev Purkayastha, released under the MIT license

