Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I html_escape text data in a sinatra app?

Tags:

ruby

xss

sinatra

I have a small Sinatra app which generates html fragments for me from an ERB template.

How do I html_escape the output?

The <%=h somestring %> helper does not exist in Sinatra.

like image 420
marshally Avatar asked Jan 23 '10 15:01

marshally


2 Answers

Rack::Utils includes a HTML escape method. http://www.sinatrarb.com/faq.html#escape_html

like image 146
fatnic Avatar answered Sep 25 '22 07:09

fatnic


require 'CGI'

get '/html' do
  erb :view
end

def h(html)
  CGI.escapeHTML html
end

__END__
@@view
  <% File.open('my.html') do |f| %>
   <%=h f.read() %>
  <% end %>
like image 21
ruvan Avatar answered Sep 25 '22 07:09

ruvan