Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

content_for and xss bug

#6302

This occurs on Rails 2.3.10

# this is safe
<% content_for :foo %>
 <%= malicious %>
<% end %>"
# this is not safe
<% content_for :foo, malicious %>

The reason is the way ActionPack implements content_for:

def content_for(name, content = nil, &block)
  ivar = "@content_for_#{name}"
  content = capture(&block) if block_given?
  instance_variable_set(ivar, "#{instance_variable_get(ivar)}#{content}".html_safe)
  nil
end

Notice it marks the string as html_safe even if it isn't.

Here's the workaround I've come up with, but there may be something more elegant:

def content_for(name, content = nil, &block)
  ivar = "@content_for_#{name}"
  content = capture(&block) if block_given?
  instance_variable_set(ivar, "#{instance_variable_get(ivar)}#{ERB::Util.h(content)}".html_safe)
  nil
end

Reported by Greg Hazel · January 17th, 2011 @ 08:19 PM

State: invalid
Milestone: none
Assigned to: Santiago Pastorino Santiago Pastorino
Importance: Low

Activity

  1. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to invalid
    • Importance changed from to Low

    Hey Greg, we should patch this in rails_xss can you patch that there?.
    Send me a pull request I'm closing this ticket from here, thanks.

    January 28th, 2011 @ 07:12 PM