如何允许 Rails 在数据中包含 javascript:
我有一个广告 html 数据库,其中一些包含 Javascript 函数。有没有办法让 Rails 允许 javascript: 特定模型上特定属性的标签?
为了进一步澄清,我可以在编辑表单中显示 html,但是当我尝试提交时,我的浏览器 (Firefox) 表示连接已重置。 IE 也给我一个错误。唯一允许提交 html 的是从标签中删除 javascript: 。
我的猜测是,这是 Rails 的一项安全措施,不允许 javascript 注入,但是,我无法控制这些广告的 html,而且许多广告中都有 javascript。
如果我的猜测确实正确,是否有一种方法可以覆盖该模型的这一属性的安全性?还是我离目标太远了?
我在 Ruby 1.8.7 上使用 Rails 2.3.4
I have a database of ad html, and some of them contain Javascript functions. Is there a way to have rails allow javascript: tags for a particular attribute on a particular model?
To clarify further, I can bring the html up in an edit form, but when I try to submit, my browser (Firefox) says the connection is reset. IE gives me an error as well. The only thing that allows the html to be submitted is to remove the javascript: from the tag.
My guess is that this is a security measure by Rails to not allow javascript injection, however, I have no control over the html of these ads, and many have javascript in them.
If my guess is indeed correct, is there a way to override the security for this one attribute of this one model? Or am I way off target here?
I am using Rails 2.3.4 on Ruby 1.8.7
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
现在我从未使用过rails,但想法应该是相同的,你只想将javascript存储为字符串,然后当你显示它时,不要转义数据。更好的方法是链接到您的 javascript,然后将对函数的调用存储在数据库中。
Now I've never used rails but the idea should be the same, you just want to store the javascript as a string, and then when you display it, do not escape the data. A better way to do this would be to link to your javascript and then just store calls to the functions in your database.
如果没有看到任何代码,我猜您正在使用
sanitize
。不要进行清理,而是考虑传输和存储转义的 JavaScript,然后在实际需要使用它时取消转义。Without seeing any code, I'm guessing you are using
sanitize
. Instead of sanitizing, consider transmitting and storing your javascript escaped, then unescape it when you actually need to use it.