Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6622237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:22:49+00:00 2026-05-25T21:22:49+00:00

How would I use sanitize, but tell it to disallow some enabled by default

  • 0

How would I use sanitize, but tell it to disallow some enabled by default tags? The documentation states that I can put this in my application.rb

config.after_initialize do
  ActionView::Base.sanitized_allowed_tags.delete 'div'
end

Can I instead pass this as an argument to sanitize?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:22:50+00:00Added an answer on May 25, 2026 at 9:22 pm

    Yes you can specify which tags and attributes to allow on a per-call basis. From the fine manual:

    Custom Use (only the mentioned tags and attributes are allowed, nothing else)

    <%= sanitize @article.body, :tags => %w(table tr td), :attributes => %w(id class style) %>
    

    But the problem with that is that :tags has to include all the tags you want to allow.

    The sanitize documentation says to

    See ActionView::Base for full docs on the available options.

    but the documentation is a lie, ActionView::Base says nothing about the available options.

    So, as usual, we have to go digging through the source and hope they don’t silently change the interface. Tracing through the code a bit yields this:

    def tokenize(text, options)
      options[:parent] = []
      options[:attributes] ||= allowed_attributes
      options[:tags]       ||= allowed_tags
      super
    end
    
    def process_node(node, result, options)
      result << case node
        when HTML::Tag
          if node.closing == :close
            options[:parent].shift
          else
            options[:parent].unshift node.name
          end
    
          process_attributes_for node, options
    
          options[:tags].include?(node.name) ? node : nil
        else
          bad_tags.include?(options[:parent].first) ? nil : node.to_s.gsub(/</, "&lt;")
      end
    end
    

    The default value for options[:tags] in tokenize and the way options[:tags] is used in process_node are of interest and tell us that if options[:tags] has anything then it has to include the entire set of allowed tags and there aren’t any other options for controlling the tag set.

    Also, if we look at sanitize_helper.rb, we see that sanitized_allowed_tags is just a wrapper for the allowed_tags in the whitelist sanitizer:

    def sanitized_allowed_tags
      white_list_sanitizer.allowed_tags
    end
    

    You should be able to add your own helper that does something like this (untested off-the-top-of-my-head code):

    def sensible_sanitize(html, options)
      if options.include? :not_tags
        options[:tags] = ActionView::Base.sanitized_allowed_tags - options[:not_tags]
      end
      sanitize html, options
    end
    

    and then you could

    <%= sensible_sanitize @stuff, :not_tags => [ 'div' ] %>
    

    to use the standard default tags except for <div>.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some namespaced custom tags(developed with radius gem) that i would like to
I thought I would use a stored routine to clean up some of my
I am writing a Facebook application that would use a Postgres DB along with
Is it possible to use a RegEx to validate, or sanitize Base64 data? That's
I currently use MySql, but would prefer an ODBC solution to make it future
I would use the System.Linq.Dynamic . I added the specified Dynamic.vb file, that starts
How you would use jQuery to implement a div that executes a link when
I would use one of the readily available jQuery plugins, but none of them
so i use mysql_real_escape_string to sanitize some user input before entering it into the
I would use a multi dimensional gaussian modell for regression. Rasmussen has a book

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.