【发布时间】:2011-07-30 04:20:43
【问题描述】:
我读过http://yehudakatz.com/2010/02/01/safebuffers-and-rails-3-0/ 和http://asciicasts.com/episodes/204-xss-protection-in-rails-3,我很困惑。我已经在 asciicasts 中尝试了关于转义然后将内容键入为 html_safe 的解决方案。我不确定我做错了什么。
我正在使用 WYSIWYG editor 来更新带有内容的帖子。
def create
@post = Post.new(params[:post])
respond_to do |format|
if @post.save
format.html { redirect_to(@post, :notice => 'Post was successfully created.') }
else
format.html { render :action => "new" }
end
end
结束
非常开箱即用。现在,当我输入一些内容时,即“<p>hello, world</p>”,该确切的字符串出现在我的视图中(我使用的是部分,fwiw)。
<%= scrub post.content %>
我的 application_helper.rb 文件有这个方法:
def scrub(content)
"<strong>#{h(content)}</strong>".html_safe
end
当我输入“<script>mal</script>”时,它会转义脚本。为什么 HTML 会显示在视图上?这是视图中的样子:
<p>hello, world <script>mal</script></p>
谁能指出我正确的方向?我希望允许用户在网站将显示呈现的 HTML(如本网站)的地方进行简单的内容格式化,但要保护自己免受脚本的影响。
【问题讨论】:
-
哦,粗体在视图上显示为粗体。
标签是我能说的最好的所见即所得的。也许在保存之前我必须做一些事情?
-
看来 正在视图中正确呈现,但它被附加在助手中。与数据库中的帖子一起保存的
标记不会被呈现,也不会被剥离。
标签: html ruby-on-rails-3