使用jQuery获取JSON数据安全吗? [英] Is getting JSON data with jQuery safe?

查看:88
本文介绍了使用jQuery获取JSON数据安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JSON允许您从AJAX调用中以多种格式检索数据。例如:

JSON allows you to retrieve data in multiple formats from an AJAX call. For example:

$.get(sourceUrl, data, callBack, 'json');

可用于从 sourceUrl

JSON是用于描述数据的简单JavaScript代码。这可以通过JavaScript解释器来回避,以获得数据结构。

JSON is the simply JavaScript code used to describe data. This could be evaled by a JavaScript interpreter to get a data structure back.

从远程来源评估代码通常是个坏主意。我知道JSON规范没有特别允许函数声明,但没有理由你不能在代码中包含一个并且有一个不安全和天真的消费者编译/执行代码。

It's generally a bad idea to evaluate code from remote sources. I know the JSON spec doesn't specifically allow for function declarations, but there's no reason you couldn't include one in code and have an unsafe and naive consumer compile/execute the code.

jQuery如何处理解析?它评估此代码吗?有哪些保护措施可以阻止某人黑客攻击 sourceUrl 并散布恶意代码?

How does jQuery handle the parsing? Does it evaluate this code? What safeguards are in place to stop someone from hacking sourceUrl and distributing malicious code?

推荐答案

我最后一次看到(2008年末)JQuery函数get()getJSON()等内部eval JSon字符串,因此暴露出与eval相同的安全问题。

The last time I looked (late 2008) the JQuery functions get() getJSON() etc internally eval the JSon string and so are exposed to the same security issue as eval.

因此,在使用任何形式的eval()之前,使用验证JSON字符串以确保它不包含任何狡猾的非JSON javascript代码的解析函数是一个非常好的主意。

Therefore it is a very good idea to use a parsing function that validates the JSON string to ensure it contains no dodgy non-JSON javascript code, before using eval() in any form.

你可以在 https://github.com/douglascrockford/JSON-js/blob/master/json2.js

参见 JSON和Broswer Security ,对这个领域进行了很好的讨论。

See JSON and Broswer Security for a good discussion of this area.

总之,使用JQuery的JSON函数而不解析输入JSON(使用上面的链接函数或类似函数)不是100%安全。

In summary, using JQuery's JSON functions without parsing the input JSON (using the above linked function or similar) is not 100% safe.

注意:如果getJSON中仍然缺少这种解析(最近可能已经添加),那么从JQuery参考文档中了解由于跨域功能而导致的风险更为重要:

NB: If this sort of parsing is still missing from getJSON (might have recently been added) it is even more important to understand this risk due to the cross domain capability, from the JQuery reference docs:


从jQuery 1.2开始,如果
指定一个JSONP回调,你可以加载位于另一个域的JSON
数据,是这样做的
:myurl?callback =?。
jQuery自动替换?
使用正确的方法名称来调用,
调用指定的回调。

As of jQuery 1.2, you can load JSON data located on another domain if you specify a JSONP callback, which can be done like so: "myurl?callback=?". jQuery automatically replaces the ? with the correct method name to call, calling your specified callback.

这篇关于使用jQuery获取JSON数据安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆