Home > sanitizer

sanitizer

Sanitizer is a project mainly written in Ruby, based on the MIT license.

A simple plugin to add quick sanitizer methods

== Sanitizer

Simple plugin to sanitize methods outputs. You have two types of behaviours. The first one is to create a new method wrapping your original method, the second one is to replace the original method.

Don't worry, everything changed can be reverted anytime.

Let's take a look at some example

== Example

Let say you have a class like this

class Post def content "Content with some nasty script tag " end end

Now if you want to remove the script tags (not what's inside !!) you can do

Post.sanitize_the :content, /<script[^>]*>|</script>/

You will now have a new public instance method call "sanitized_content" that will call your "content" method and clean its output following your regex. In this case, you will have

post = Post.new post.sanitized_content # => "Content with some nasty script tag alert("I am nasty!");"

Now let's talk about the other way, replacing the original method. To do so, simply add a params :override at true

Post.sanitize_the :content, /<script[^>]*>|</script>/, :override => true

You won't have a method call "sanitized_content" in this case! This will replace you method "content" by a new one. But don't worry, your former is kept safe. If you wish to restore your class to its former state simply call the singleton method "remove_sanitized_method" on your class.

Post.sanitize_the :content, /<script[^>]*>|</script>/, :override => true post = Post.new post.content # => "Content with some nasty script tag alert("I am nasty!");" post.respond_to?(:sanitized_content) # => false Post.remove_sanitized_methods # removing all added methods and restore original ones post.content # => "Content with some nasty script tag "

Yes but what if you want to substitute the matches of the regex by something else instead of removing them ? Well then you can add another attribute :substitution.

Post.sanitize_the :content, /<script[^>]*>|</script>/, :override => true, :substitution => "[NASTY CONTENT]" post = Post.new post.content # => "Content with some nasty script tag [NASTY CONTENT]alert("I am nasty!");[NASTY CONTENT]"

== Installation

Pick the good one depending on your rails version

To install it as a plugin script/plugin install git://github.com/voxdei/sanitizer.git or rails plugin install git://github.com/voxdei/sanitizer.git

== Tests

Yeah ! It's now fully tested !

== The end

Copyright (c) 2010 Guillaume Petit, released under the MIT license