When I generate a default scaffold, the display tags on show.html.erb have
<%=h @broker.name %>
I know the difference between <%
and <%=
. What's the "h" do?
When I generate a default scaffold, the display tags on show.html.erb have
<%=h @broker.name %>
I know the difference between <%
and <%=
. What's the "h" do?
html escape. It's a method that converts things like < and > into numerical character references so that rendering won't break your html.
<%=h
is actually 2 things happening. You're opening an erb tag (<%=
) and calling the Rails method h
to escape all symbols.
These two calls are equivalent:
<%=h person.first_name %>
<%= h(person.first_name) %>
The h
method is commonly used to escape HTML and Javascript from user-input forms.
h
is a method alias for html_escape
from the ERB::Util class.
There is also a method in Rack to escape HTML Rack::Utils.escape_html
in case you are in Metal and want to escape some HTML.
Way late to the party but I'm adding a further explanation to what html_escape
is doing to hopefully help other noobs like myself understand what's happening. Rails 3 and later automatically escape all output now and so there are much fewer situations where html_escape
aka h()
will be needed. The most notable of which is when you intend to use the html_safe
method when building links with html in a presenter class etc. For example:
#some_view.html.erb
<span><%= @user.name %></span> #This is 100% fine and will be automatically escaped by Rails 3+
#Output => <span>Brian Kunzig</span>
#Now say we want a link with html that we need preserved! OMG WHAT ARE DO??
<%=link_to "<span><i class='fa fa-user'></i>#{@user.name}</span>".html_safe #DANGER!!!
The link above can cause serious problems and open you up to all sorts of xss (cross-site scripting) attacks. The most simple example, if a user saved their name as "<script>alert('omg');</script>"
and you used html_safe
on it, it will cause any page rendering their supposed name to get an alert saying 'omg'! This is a major problem. To avoid this do:
<%=link_to "<span><i class='fa fa-user'></i>#{h(@user.name)}</span>".html_safe #Winning!
By escaping the potentially tainted data supplied by a user we're homefree!
h is just alias for html_escape. It is a utility method commonly used to escape html and javascript from user input forms. It converts special charactes into numerical character references so that rendering won't break your html.
For example having
<%= h "<p>Hello World</p>" %>
will output
<p>Hello World</p>
as text to view, paragraph won't be applied. it wil be encoded as
<p>Hello World</p>.
h
?h
is an alias for html_escape
Source code.
# a user goes to an online app.
# writes something on a form. submits the form.
# user writes something malicious on the form:
malicious_string = '<script> steal_everyones_password_hehe </script>'
# the string is saved in the database
# without modification (let's suppose)
# when a html page is retrieved,
# the string is displayed in a html document
# the browser must display the string.
# When it does, you don't want
# the malicious javascript to be run.
This would be really bad because the script would run:
<html>
<script> javascript_runs_and_steals_passwords! </script>
</html>
We need to make that string safe. We do this by ESCAPING from the danger:
html_escape(malicious_string)
# => "<script> if_users_type_in_this_malacious_script </script>"
# notice how the < and > disappear?
# The browswer will not recognise this as a script tag.
html_escape(malicious_string).html_safe?
# => true (now it's safe)
# you can display this string in your webpage without fear
(malicious_string).html_safe?
# => not safe, because we haven't escaped it!
html_safe
method?WARNING: html_safe
DOES NOT make something safe, if it is inherently dangerous - you're telling Rails: "no need to escape this string -- I know what i'm doing":
html_safe(user_input) # Danger!
Never ever mark any user_input as safe (unless you have a really good reason)!
It will display that code, without escaping it.
html_safe(i_know_what_im_doing) # ok, I hope you really do, though!`
# When in doubt, escape from danger:**
h(malicious_string) # => you're safe
html_escape(malicious_string) # => you're safe