Skip to content

Commit c257fe4

Browse files
committed
* faq.markdown: Add a little extra information about escaping for XSS protection
1 parent 8a8c766 commit c257fe4

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

faq.markdown

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,23 @@ in your helpers as follows:
233233
def h(text)
234234
Rack::Utils.escape_html(text)
235235
end
236+
237+
def hattr(text)
238+
Rack::Utils.escape_path(text)
239+
end
236240
end
237241

238-
Now you can escape HTML in your templates like this:
242+
Now you can escape HTML entities inside outputted text in your templates in one of two ways:
243+
244+
<div><%= h scary_output %></div>
245+
246+
or using the `<%==` feature:
247+
248+
<div><%== scary_output %></div>
249+
250+
And you can escape text inside element attributes in your templates like this:
239251

240-
<%= h scary_output %>
252+
<a href="<%= hattr scary_output %>" >A nice safe link!</a>
241253

242254
Thanks to [Chris Schneider](http://www.gittr.com/index.php/archive/using-rackutils-in-sinatra-escape_html-h-in-rails/)
243255
for the tip!

0 commit comments

Comments
 (0)