Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added :sanitize_javascript template option #767

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/haml/filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ def render_with_options(text, options)
text = text.rstrip
text.gsub!("\n", "\n#{indent}")

if options[:sanitize_javascript]
text.gsub!(/<(\/)?script>/i, '')
end

%!<script#{type}>\n#{" //<![CDATA[\n" if options[:cdata]}#{indent}#{text}\n#{" //]]>\n" if options[:cdata]}</script>!
end
end
Expand Down
10 changes: 8 additions & 2 deletions lib/haml/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ class Options
:cdata => false,
:parser_class => ::Haml::Parser,
:compiler_class => ::Haml::Compiler,
:trace => false
:trace => false,
:sanitize_javascript => false
}

@valid_formats = [:html4, :html5, :xhtml]

@buffer_option_keys = [:autoclose, :preserve, :attr_wrapper, :ugly, :format,
:encoding, :escape_html, :escape_attrs, :hyphenate_data_attrs, :cdata]
:encoding, :escape_html, :escape_attrs, :hyphenate_data_attrs, :cdata,
:sanitize_javascript]

# The default option values.
# @return Hash
Expand Down Expand Up @@ -177,6 +179,10 @@ def self.buffer_option_keys
# the path will be the full path.
attr_accessor :trace

# Escapes any <script> tags within the :javascript filters. This is an
# additional security against XSS
attr_accessor :sanitize_javascript

def initialize(values = {}, &block)
defaults.each {|k, v| instance_variable_set :"@#{k}", v}
values.each {|k, v| send("#{k}=", v) if defaults.has_key?(k) && !v.nil?}
Expand Down
2 changes: 1 addition & 1 deletion test/filters_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,4 @@ class RubyFilterTest < MiniTest::Unit::TestCase
html = "7\n"
assert_equal(html, render(haml))
end
end
end