Skip to content

Commit

Permalink
Merge branch 'master-security'
Browse files Browse the repository at this point in the history
* master-security:
  Ensure [] respects the status of the buffer.
  delete vulnerable AS::SafeBuffer#[]
  use AS::SafeBuffer#clone_empty for flushing the output_buffer
  add AS::SafeBuffer#clone_empty
  fix output safety issue with select options

Conflicts:
	actionpack/lib/action_view/helpers/tags/base.rb
  • Loading branch information
tenderlove committed Mar 1, 2012
2 parents ceb66b6 + 8ccaa34 commit 139963c
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 29 deletions.
2 changes: 1 addition & 1 deletion actionpack/lib/action_view/helpers/capture_helper.rb
Expand Up @@ -215,7 +215,7 @@ def with_output_buffer(buf = nil) #:nodoc:
def flush_output_buffer #:nodoc: def flush_output_buffer #:nodoc:
if output_buffer && !output_buffer.empty? if output_buffer && !output_buffer.empty?
response.body_parts << output_buffer response.body_parts << output_buffer
self.output_buffer = output_buffer[0,0] self.output_buffer = output_buffer.respond_to?(:clone_empty) ? output_buffer.clone_empty : output_buffer[0, 0]
nil nil
end end
end end
Expand Down
5 changes: 2 additions & 3 deletions actionpack/lib/action_view/helpers/tags/base.rb
Expand Up @@ -133,12 +133,11 @@ def select_content_tag(option_tags, options, html_options)


def add_options(option_tags, options, value = nil) def add_options(option_tags, options, value = nil)
if options[:include_blank] if options[:include_blank]
include_blank = options[:include_blank] if options[:include_blank].kind_of?(String) option_tags = content_tag('option', options[:include_blank].kind_of?(String) ? options[:include_blank] : nil, :value => '') + "\n" + option_tags
option_tags = content_tag(:option, include_blank, :value => '').safe_concat("\n").safe_concat(option_tags)
end end
if value.blank? && options[:prompt] if value.blank? && options[:prompt]
prompt = options[:prompt].kind_of?(String) ? options[:prompt] : I18n.translate('helpers.select.prompt', :default => 'Please select') prompt = options[:prompt].kind_of?(String) ? options[:prompt] : I18n.translate('helpers.select.prompt', :default => 'Please select')
option_tags = content_tag(:option, prompt, :value => '').safe_concat("\n").safe_concat(option_tags) option_tags = content_tag('option', prompt, :value => '') + "\n" + option_tags
end end
option_tags option_tags
end end
Expand Down
9 changes: 8 additions & 1 deletion actionpack/test/template/form_options_helper_test.rb
Expand Up @@ -509,7 +509,7 @@ def @post.to_param; 108; end


def test_select_under_fields_for_with_string_and_given_prompt def test_select_under_fields_for_with_string_and_given_prompt
@post = Post.new @post = Post.new
options = "<option value=\"abe\">abe</option><option value=\"mus\">mus</option><option value=\"hest\">hest</option>" options = "<option value=\"abe\">abe</option><option value=\"mus\">mus</option><option value=\"hest\">hest</option>".html_safe


output_buffer = fields_for :post, @post do |f| output_buffer = fields_for :post, @post do |f|
concat f.select(:category, options, :prompt => 'The prompt') concat f.select(:category, options, :prompt => 'The prompt')
Expand Down Expand Up @@ -665,6 +665,13 @@ def test_select_with_index_option
) )
end end


def test_select_escapes_options
assert_dom_equal(
'<select id="post_title" name="post[title]">&lt;script&gt;alert(1)&lt;/script&gt;</select>',
select('post', 'title', '<script>alert(1)</script>')
)
end

def test_select_with_selected_nil def test_select_with_selected_nil
@post = Post.new @post = Post.new
@post.category = "<mus>" @post.category = "<mus>"
Expand Down
38 changes: 22 additions & 16 deletions activesupport/lib/active_support/core_ext/string/output_safety.rb
Expand Up @@ -103,29 +103,41 @@ def initialize
end end
end end


def[](*args) def [](*args)
new_safe_buffer = super return super if args.size < 2
new_safe_buffer.instance_eval { @dirty = false }
new_safe_buffer if html_safe?
new_safe_buffer = super
new_safe_buffer.instance_eval { @html_safe = true }
new_safe_buffer
else
to_str[*args]
end
end end


def safe_concat(value) def safe_concat(value)
raise SafeConcatError if dirty? raise SafeConcatError unless html_safe?
original_concat(value) original_concat(value)
end end


def initialize(*) def initialize(*)
@dirty = false @html_safe = true
super super
end end


def initialize_copy(other) def initialize_copy(other)
super super
@dirty = other.dirty? @html_safe = other.html_safe?
end

def clone_empty
new_safe_buffer = self[0, 0]
new_safe_buffer.instance_variable_set(:@dirty, @dirty)
new_safe_buffer
end end


def concat(value) def concat(value)
if dirty? || value.html_safe? if !html_safe? || value.html_safe?
super(value) super(value)
else else
super(ERB::Util.h(value)) super(ERB::Util.h(value))
Expand All @@ -138,7 +150,7 @@ def +(other)
end end


def html_safe? def html_safe?
!dirty? defined?(@html_safe) && @html_safe
end end


def to_s def to_s
Expand All @@ -161,18 +173,12 @@ def #{unsafe_method}(*args, &block) # def capitalize(*args, &block)
end # end end # end
def #{unsafe_method}!(*args) # def capitalize!(*args) def #{unsafe_method}!(*args) # def capitalize!(*args)
@dirty = true # @dirty = true @html_safe = false # @html_safe = false
super # super super # super
end # end end # end
EOT EOT
end end
end end

protected

def dirty?
@dirty
end
end end
end end


Expand Down
38 changes: 30 additions & 8 deletions activesupport/test/safe_buffer_test.rb
Expand Up @@ -84,13 +84,13 @@ def test_titleize
assert_equal "hello&lt;&gt;", clean + @buffer assert_equal "hello&lt;&gt;", clean + @buffer
end end


test "Should concat as a normal string when dirty" do test "Should concat as a normal string when safe" do
clean = "hello".html_safe clean = "hello".html_safe
@buffer.gsub!('', '<>') @buffer.gsub!('', '<>')
assert_equal "<>hello", @buffer + clean assert_equal "<>hello", @buffer + clean
end end


test "Should preserve dirty? status on copy" do test "Should preserve html_safe? status on copy" do
@buffer.gsub!('', '<>') @buffer.gsub!('', '<>')
assert !@buffer.dup.html_safe? assert !@buffer.dup.html_safe?
end end
Expand All @@ -102,20 +102,42 @@ def test_titleize
assert_equal "<script>", result_buffer assert_equal "<script>", result_buffer
end end


test "Should raise an error when safe_concat is called on dirty buffers" do test "Should raise an error when safe_concat is called on unsafe buffers" do
@buffer.gsub!('', '<>') @buffer.gsub!('', '<>')
assert_raise ActiveSupport::SafeBuffer::SafeConcatError do assert_raise ActiveSupport::SafeBuffer::SafeConcatError do
@buffer.safe_concat "BUSTED" @buffer.safe_concat "BUSTED"
end end
end end


test "should not fail if the returned object is not a string" do test "Should not fail if the returned object is not a string" do
assert_kind_of NilClass, @buffer.slice("chipchop") assert_kind_of NilClass, @buffer.slice("chipchop")
end end


test "Should initialize @dirty to false for new instance when sliced" do test "clone_empty returns an empty buffer" do
dirty = @buffer[0,0].send(:dirty?) assert_equal '', ActiveSupport::SafeBuffer.new('foo').clone_empty
assert_not_nil dirty end
assert !dirty
test "clone_empty keeps the original dirtyness" do
assert @buffer.clone_empty.html_safe?
assert !@buffer.gsub!('', '').clone_empty.html_safe?
end

test "Should be safe when sliced if original value was safe" do
new_buffer = @buffer[0,0]
assert_not_nil new_buffer
assert new_buffer.html_safe?, "should be safe"
end

test "Should continue unsafe on slice" do
x = 'foo'.html_safe.gsub!('f', '<script>alert("lolpwnd");</script>')

# calling gsub! makes the dirty flag true
assert !x.html_safe?, "should not be safe"

# getting a slice of it
y = x[0..-1]

# should still be unsafe
assert !y.html_safe?, "should not be safe"
end end
end end

0 comments on commit 139963c

Please sign in to comment.