LoginSignup
58
57

More than 5 years have passed since last update.

Railsでのhtmlのsanitize & escape

Last updated at Posted at 2014-02-25

概要

  • xssなどの対策のためhtmlをescapeしたり、sanitizeするかと思います
  • viewで行う分にはhelper使って行えばいいんですが、、、

方法

  • viewでescape
    • <%=~ %>を使えば勝手にescape(rails3以降)
  • 逆にviewでescapeしない
escapeしない
<p>
<%= "html".html_safe %>
</p>
# or
<p>
<%= raw "html" %>
</p>
  • viewでsanitize
sanitize
<p>
<%= sanitize "html" %>
</p>
  • controllerからhelperを呼ぶ方法
controllerからhelperを呼ぶ
ApplicationController.helpers.hogehoge();
# or
view_context.hogehoge();
  • controllerでescape
escape
ERB::Util.html_escape("html");
  • controllerでescapeしない
escapeしない
ApplicationController.helpers.raw("html");
# or
view_context.raw("html");
  • controllerでsanitizeする
sanitize
ApplicationController.helpers.sanitize("html");
# or
view_context.sanitize("html");

参考

58
57
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
58
57