逃离</脚本>;javascript中的标记
我使用的是主干网,这是在页面加载停止时传递集合的一般方式逃离</脚本>;javascript中的标记,javascript,ruby-on-rails,ruby,backbone.js,haml,Javascript,Ruby On Rails,Ruby,Backbone.js,Haml,我使用的是主干网,这是在页面加载停止时传递集合的一般方式 window.router = new Routers.ManageRouter({store: #{@store.to_json}); 在有人决定将文本“alert(“owned”)”添加到其中一个存储字段之前,这是一个很好的方法。最后一个显然关闭了javascript。如何才能避免这种情况 :javascript $(function() { window.router = new Dotz.Routers.
window.router = new Routers.ManageRouter({store: #{@store.to_json});
在有人决定将文本“alert(“owned”)
”添加到其中一个存储字段之前,这是一个很好的方法。最后一个
显然关闭了javascript。如何才能避免这种情况
:javascript
$(function() {
window.router = new Dotz.Routers.ManageRouter({store: #{@store.to_json}});
Backbone.history.start();
});
上述输出:
<script>
//<![CDATA[
$(function() {
window.router = new Dotz.Routers.ManageRouter({store: '{"_id":"4f3300e19c2ee41d9a00001c", "points_text":"<script>alert(\"hey\");</script>"'});
Backbone.history.start();
});
//]]>
</script>
//
您忘记了“”
在
块中,任何/代码后面都要有一个名称,而不仅仅是code/script/code,因此您需要转义它可能出现的任何地方。例如:/p
p正在运行:/p
precodeirb:02.0>a=[“foo”]
irb:03.0>将a.to_json#置于无魔法状态
#=>[“foo”]
irb:04.0>需要“主动支持”
irb:05.0>ActiveSupport::JSON::Encoding.escape\u html\u entities\u in\u JSON=true
irb:06.0>将a.to#u json#与魔法结合起来
#=>[“\u003Cb\u003efo\u003C/b\u003E”]
它生成的JSON比解决这个特定问题所需的更详细,但它是有效的。神奇的词语是:
ActiveSupport.escape_html_entities_in_json = true
尽管标记为已弃用,但这在当前的rails版本中仍然有效(请参见myrails c
):
ruby-1.9.3-head:001>:Rails.version
=> "3.2.1"
ruby-1.9.3-head:002>[“”].to_json
=> "[\"\"]"
ruby-1.9.3-head:003>ActiveSupport.escape\u html\u entities\u in\u json=true
=>正确
ruby-1.9.3-head:004>[“”].to_json
=>“[\”\\u003C\\u003E\”]
没有。它仍然会呈现类似{id:'324234',text:''…
while将关闭脚本您可以发布呈现页面的HTML吗?此表单很奇怪;可能他们或您有一个向后兼容垫片。它在原始ActiveSupport v3.0.7中不起作用;但是我将删除对您的话的否决票,因为它有效。这在我的Rails 3.2.13应用程序中非常有效。I想知道为什么ActiveSupport团队会反对这样一个有用的、与安全相关的功能。
:javascript
var foo = { store: #{@store.to_json.gsub('</','<\/')} };
irb:02.0> s = "<b>foo</b>" # Here's a dangerous string
#=> "<b>foo</b>"
irb:03.0> a = [s] # Wrapped in an array, for fun.
#=> ["<b>foo</b>"]
irb:04.0> json = a.to_json.gsub( '</', '<\/' ) # Sanitized
irb:05.0> puts json # This is what would come out in your HTML; safe!
#=> ["<b>foo<\/b>"]
irb:06.0> puts JSON.parse(json).first # Same as the original? Yes! Yay!
#=> <b>foo</b>
ActiveSupport::JSON::Encoding.escape_html_entities_in_json = true
irb:02.0> a = ["<b>foo</b>"]
irb:03.0> puts a.to_json # Without the magic
#=> ["<b>foo</b>"]
irb:04.0> require 'active_support'
irb:05.0> ActiveSupport::JSON::Encoding.escape_html_entities_in_json = true
irb:06.0> puts a.to_json # With the magic
#=> ["\u003Cb\u003Efoo\u003C/b\u003E"]
ActiveSupport.escape_html_entities_in_json = true
ruby-1.9.3-head :001 > ::Rails.version
=> "3.2.1"
ruby-1.9.3-head :002 > ["<>"].to_json
=> "[\"<>\"]"
ruby-1.9.3-head :003 > ActiveSupport.escape_html_entities_in_json = true
=> true
ruby-1.9.3-head :004 > ["<>"].to_json
=> "[\"\\u003C\\u003E\"]"