Git Product home page Git Product logo

sanitize-rails's Introduction

Sanitize-Rails - sanitize .. on Rails. Build Status

An easy bridge to integrate Ryan Grove's HTML Whitelist Sanitizer in your Rails application.

Installation

Gemfile:

gem 'sanitize-rails', require: 'sanitize/rails'

Configuration

Pass the configuration to Sanitize calling Sanitize::Rails.configure in an initializer, say config/initializers/sanitizer.rb:

Sanitize::Rails.configure(
  elements:   [ ... ],
  attributes: { ... },
  ...
)

You may pass escape_entities: false if you don't want to escape html entities. Example: Hello & World will not be changed to Hello & World

Check out the example in the example/ directory.

Usage

ActionView sanitize helper is transparently overriden to use the Sanitize gem.

A sanitize helper is added to ActiveRecord, that installs on create/save callbacks that sanitize the given attributes before persisting them to the database. Example:

app/models/foo.rb:

class Foo < ActiveRecord::Base
  sanitizes :description # on save by default

  sanitizes :body,    on: :create
  sanitizes :remarks, on: :save
end

Testing

RSpec

spec/spec_helper.rb:

require 'sanitize/rails/matchers'

in spec code:

describe Post do
  # Simplest variant, single field and default values
  it { should sanitize_field :title }

  # Multiple fields
  it { should sanitize_fields :title, :body }

  # Specifing both text to sanitize and expected result
  it { should sanitize_field(:title).replacing('&copy;').with('ยฉ') }
end

You should pass field names to matcher in the same way as you do with the sanitize call in the model, otherwise sanitize method won't be found in model.

Test::Unit

test/test_helper.rb:

require 'sanitize/rails/test_helpers'

Sanitize::Rails::TestHelpers.setup(self,
  invalid: 'some <a>string',
  valid:   'some <a>string</a>'
)

your test:

assert_sanitizes Model, :field, :some_other_field

Compatibility

Tested with Rails 3.0 and ๐Ÿ†™ under Ruby 1.9.3 and ๐Ÿ†™.

License

MIT

๐Ÿ˜ƒ Have fun!

sanitize-rails's People

Contributors

damien avatar fabn avatar jaygen avatar michaelglass avatar pywebdesign avatar ryrych avatar tbprojects avatar vjt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

sanitize-rails's Issues

Matchers

Hi,

Thanks for creating this sanitize-rails gem! I'm using it with a lot of success in a project I'm currently working on.

Here's a gist I've put together for creating matchers:

https://gist.github.com/1179448

Would you be interested at all in adding this to the project? If so, I'll hack it into my fork and send a pull request.

Thanks again,

Evan

multiple configurations

Does this gem support different sanitization configurations on different fields? It looks like it supports only one global configuration.

License missing from gemspec

Some companies will only use gems with a certain license.
The canonical and easy way to check is via the gemspec
via e.g.

spec.license = 'MIT'
# or
spec.licenses = ['MIT', 'GPL-2']

There is even a License Finder to help companies ensure all gems they use
meet their licensing needs. This tool depends on license information being available in the gemspec.
Including a license in your gemspec is a good practice, in any case.

How did I find you?

I'm using a script to collect stats on gems, originally looking for download data, but decided to collect licenses too,
and make issues for missing ones as a public service :)
https://gist.github.com/bf4/5952053#file-license_issue-rb-L13 So far it's going pretty well

New version of sanitize

Unable to activate sanitize-rails-0.9.1, because sanitize-3.0.0 conflicts with sanitize (~> 2.0) (Gem::LoadError)

Mark cleaned strings as HTML safe

First off, thanks for putting this gem together @vjt! It saved me a good day of work integrating the sanitize gem into my employer's codebase.

Anyway, back on topic: I'd like to introduce a change to sanitize-rails that would automatically convert all cleaned strings into a SafeBuffer. This would mark any cleaned text/attributes as HTML safe and prevent already cleaned text from being escaped a second time by default when rendering said text in rails views.

Would a pull request I make for this be accepted? I notice there aren't any tests on this gem yet, so I can introduce the beginnings of a test suite with this feature as well.

"SanitizeFieldsMatcher implements a legacy RSpec matcher protocol"

When using sanitize-rails with RSpec 3.1.0, I get this warning:

Deprecation Warnings:

--------------------------------------------------------------------------------
Sanitize::Rails::Matchers::SanitizeFieldsMatcher implements a legacy RSpec matcher
protocol. For the current protocol you should expose the failure messages
via the `failure_message` and `failure_message_when_negated` methods.
(Used from /Volumes/Customer/apps/application/spec/models/my_spec.rb:5:in `block (2 levels) in <top (required)>')
--------------------------------------------------------------------------------

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.