简体   繁体   English

rails 3 转义 html 标签在作为 JSON 使用时在浏览器中未转义

[英]rails 3 escaped html tags unescaped in browser when served as JSON

I have a Rails 3.2 app that interacts with a 3rd party XML API for some data using ActiveResource.我有一个 Rails 3.2 应用程序,它与使用 ActiveResource 的某些数据的第 3 方 XML API 交互。 We want to ensure against XSS in that 3rd party XML, so we have enabled:我们要确保在第 3 方 XML 中防止 XSS,因此我们启用了:

ActiveSupport.escape_html_entities_in_json = true 

This seems to work in for the XML data and converts the < tag in a potentially dangerous script call to &lt;这似乎适用于 XML 数据并将潜在危险脚本调用中的 < 标记转换为&lt; like this:像这样:

&lt;script>alert('xss')&lt;/script>

(not sure why it only esacpes the lt TBH, but it seems to be by design) (不知道为什么它只能避开 lt TBH,但它似乎是设计使然)

Problem is that when I convert that XML to JSON & send to browser, the browser sees it as unescaped.问题是,当我将该 XML 转换为 JSON 并发送到浏览器时,浏览器会将其视为未转义的。 The string in JSON that is sent to browser looks like this:发送到浏览器的 JSON 字符串如下所示:

\u003Cscript\u003Ealert('xss')\u003C/script\u003E"

can anyone explain why the browser converts this back to the unescaped string, and suggest workarounds?谁能解释为什么浏览器将其转换回未转义的字符串,并建议解决方法?

FWIW I'm using Backbone, Jquery, JST & EJS FWIW 我正在使用 Backbone、Jquery、JST 和 EJS

If the string sent to the browser is like the one in the question, then it's not the browser converting it back, it's already sent unescaped.如果发送到浏览器的字符串与问题中的字符串类似,那么不是浏览器将其转换回来,而是已未转义发送。 However, I don't think the server needs to encode it, as it's just data, sent in an intermediary data format which is JSON.但是,我认为服务器不需要对其进行编码,因为它只是以 JSON 中间数据格式发送的数据。 It is correctly encoded for JSON.正确编码的JSON。 You have to be careful when you insert it into the DOM on the client though.但是,在将其插入客户端的 DOM 时必须小心。

In the template, make sure you use <%- instead of <%= and it should be fine unless you are generating javascript code of some sort , but that's probably an edge case.在模板中,确保使用 <%- 而不是 <%= ,除非您生成某种类型的 javascript 代码,否则它应该没问题,但这可能是一种边缘情况。

You can also choose to encode it in the response as you originally asked, in that case you should look into how it's converted from XML to JSON.您还可以选择按照最初要求在响应中对其进行编码,在这种情况下,您应该研究如何将其从 XML 转换为 JSON。 Be aware though, that different contexts may need different encodings (especially encoding requirements for writing into plain HTML vs in a Javascript context are very different), and the server sending the data may not (want to) know what the client wants to do with it.但是请注意,不同的上下文可能需要不同的编码(尤其是写入纯 HTML 和 Javascript 上下文的编码要求非常不同),并且发送数据的服务器可能不(想要)知道客户端想要做什么它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 转义的 html 在 jquery html 方法中再次未转义 - Escaped html is again unescaped in jquery html method 如何获取未转义的JSON内容的HTML转义的JSON数据 - How can i get html-escaped JSON data of unescaped JSON content 在某些浏览器中,由jquery .html()转义的转义的html字符未被转义 - Escaped html characters are being unescaped by jquery .html() in some browsers 带有转义html的Jquery - tag属性会自动转义 - Jquery - tag attribute with escaped html gets unescaped automatically 什么是“Escaped”和“Unescaped”输出 - What is “Escaped” & “Unescaped” output Rails json返回带有jbuilder html标签的 - Rails json return with jbuilder html tags 在淘汰赛中编写一个转义/未转义的Observable - Writing an escaped/unescaped Observable in Knockout 使用CanJS中的小胡子模板转义HTML标记 - HTML tags are escaped with mustache templates in CanJS 为什么 $('div:not(script)').contents().filter(function() 导致转义的 HTML 字符变为未转义? - Why is $('div :not(script)').contents().filter(function() causing escaped HTML characters to become unescaped? HTML 视频仅出现在 Firefox 中,从本地服务器提供时不会出现在任何其他浏览器中 - HTML video appearing only in Firefox, not in any other browser when served from a local server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM