GithubHelp home page GithubHelp logo

codemonkeykevin / xss_terminate Goto Github PK

View Code? Open in Web Editor NEW

This project forked from look/xss_terminate

0.0 3.0 1.0 106 KB

xss_terminate is a plugin in that makes stripping and sanitizing HTML stupid-simple. Install and forget. And forget about forgetting to h() your output, because you won‘t need to anymore.

Home Page: http://code.google.com/p/xssterminate/

License: MIT License

Ruby 100.00%

xss_terminate's Introduction

xss_terminate

xss_terminate is a plugin in that makes stripping and sanitizing HTML stupid-simple. Install and forget. And forget about forgetting to h() your output, because you won’t need to anymore.

But xss_terminate is also flexible. By default, it will strip all HTML tags from user input. This is usually what you want, but sometimes you need users to be able to enter HTML. The plugin allows you remove bad HTML with your choice of two whitelist-based sanitizers, or to skip HTML sanitization entirely on a per-field basis.

To install, do:

script/plugin install git://github.com/look/xss_terminate.git

HTML sanitization

A note on your choices.

  • Strip tags: removes all HTML using Rails’s built-in strip_tags method. Tags are removed, but their content is not.

  • Rails sanitization: Removes bad HTML with Rails’s built-in sanitize method. Bad tags are removed completely, including their content.

  • HTML5lib sanitization: Removes bad HTML after parsing it with HTML5lib, a library that parses HTML like browsers do. It should be very tolerant of invalid HTML. Bad tags are escaped, not removed.

  • Do nothing. You can chose not to process given fields.

Usage

Installing the plugin creates a before_save hook that will strip HTML tags from all string and text fields. No further configuration is necessary if this is what you want. To customize the behavior, you use the xss_terminate class method.

To exempt some fields from sanitization, use the :except option with a list of fields not to process:

class Comment < ActiveRecord::Base
  xss_terminate :except => [ :body ]
end

To sanitize HTML with Rails’s built-in sanitization, use the :sanitize option:

class Review < ActiveRecord::Base
  xss_terminate :sanitize => [ :body, :author_name]
end

To sanitize HTML with HTML5Lib (gem install html5 to get it), use the :html5lib_sanitize option with a list of fields to sanitize:

class Entry < ActiveRecord::Base
  xss_terminate :html5lib_sanitize => [ :body, :author_name ]
end

You can combine multiple options if you have some fields you would like skipped and others sanitized. Fields not listed in the option arrays will be stripped.

class Message < ActiveRecord::Base
  xss_terminate :except => [ :body ], :sanitize => [ :title ]
end

If you want to sanitize “fake fields”/attr_accessor/methods output you can include the method name in :sanitize_methods

class Message < ActiveRecord::Base
  xss_terminate :sanitize_methods => [:method]
end

class Message < ActiveRecord::Base
  xss_terminate :html5lib_sanitize => [ :method ], :sanitize_methods => [:method]
end

Sanitizing existing records

After installing xss_terminate and configuring it to your liking, you can run rake xss_terminate MODELS=Foo,Bar,Baz to execute it against your existing records. This will load each model found and save it again to invoke the before_save hook.

Unique features

xss_terminate is based on acts_as_sanitized. Here is what’s different:

  • Supports Rails 2.0-2.2 (may work on edge Rails, but I haven’t tested it)

  • Automatic. It is included with default options in ActiveReord::Base so all your models are sanitized.

  • It works with migrations. Columns are fetched when model is saved, not when the class is loaded.

  • You can decide whether to sanitize or strip tags on a field-by-field basis instead of model-by-model.

  • HTML5lib support.

TODO

  • Performance tests

  • Test suites with “real world” HTML

  • Test/make work with Rails 1.2.x (Rails 1.2 sanitization is crap, so you’d want to use HTML5lib)

Credits

Written by Luke Francl and based on acts_as_sanitized by Alex Payne.

HTML5Lib sanitization by Jacques Distler.

Bug fixes and reports from a cast of thousands.

License

MIT License, except for lib/html5lib_sanitize.rb which is under the Ruby license and copyright to Jacques Distler.

xss_terminate's People

Contributors

redinger avatar codemonkeykevin avatar shaliko avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.