- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我注意到 jQuery parseJSON 基本上做了一个简单的正则表达式“检查”:
parseJSON: function( data ) {
if ( typeof data !== "string" || !data ) {
return null;
}
// Make sure leading/trailing whitespace is removed (IE can't handle it)
data = jQuery.trim( data );
// Make sure the incoming data is actual JSON
// Logic borrowed from http://json.org/json2.js
if ( /^[\],:{}\s]*$/.test(data.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@")
.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]")
.replace(/(?:^|:|,)(?:\s*\[)+/g, "")) ) {
// Try to use the native JSON parser first
return window.JSON && window.JSON.parse ?
window.JSON.parse( data ) :
(new Function("return " + data))();
} else {
jQuery.error( "Invalid JSON: " + data );
}
},
如果它通过了“检查”并且它是一个现代浏览器,则使用 native JSON 解析器。否则,我假设像 IE6 这样的浏览器会自动调用一个新函数并返回对象。
问题 #1:由于这只是一个简单的正则表达式测试,是否容易出现某种模糊的边缘情况漏洞利用?对于至少不支持原生 JSON 解析的浏览器,我们真的不应该使用一个完整的解析器吗?
问题 #2:(new Function("return "+ data ))()
相对于 eval("( "+ 文本 + ")")
?
最佳答案
如评论中所述,jQuery 的 JSON 解析器直接从 json2.js“借用”了测试 JSON 字符串是否有效的逻辑。这使得它与最常见的非本地实现“一样安全”,无论如何都是相当严格的:
// In the second stage, we run the text against regular expressions that look
// for non-JSON patterns. We are especially concerned with '()' and 'new'
// because they can cause invocation, and '=' because it can cause mutation.
// But just to be safe, we want to reject all unexpected forms.
// We split the second stage into 4 regexp operations in order to work around
// crippling inefficiencies in IE's and Safari's regexp engines. First we
// replace the JSON backslash pairs with '@' (a non-JSON character). Second, we
// replace all simple value tokens with ']' characters. Third, we delete all
// open brackets that follow a colon or comma or that begin the text. Finally,
// we look to see that the remaining characters are only whitespace or ']' or
// ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.
if (/^[\],:{}\s]*$/.
test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@').
replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
我不明白的是,为什么 jQuery 在检查 native 实现之前运行正则表达式/替换,而 native 实现无论如何都会检查正确的 JSON 语法。如果 native 实现不可用,似乎只执行此操作会加快速度。
问题2是answered very well by bobince在另一个问题中:
It's not really a big difference, but the feeling is that eval is ‘worse’ than new Function. Not in terms of security — they're both equally useless in the face of untrusted input, but then hopefully your webapp is not returning untrusted JSON strings — but in terms of language-level weirdness, and hence resistance to optimisation.
查看 Nick Craver's answer还有 John Resig 的直接引述。
关于javascript - 应该使用 jQuery 的 parseJSON/getJSON 方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3238842/
我正在尝试获取客户端的 IP 地址和 GPS 坐标。使用 jQuery,我有这个: $.getJSON("http://jsonip.appspot.com/", function(data) {
这个问题已经有答案了: Why does JQuery.getJSON() have a success and a done function? (2 个回答) 已关闭 6 年前。 我想知道这两个代
所以首先,是的,我知道有几个答案,但没有一个能够解决我的问题。首先我将展示我的代码HTML: Shop
假设我有以下 JavaScript: (function($) { $.getJSON(url, function(data) { $.each(data.rows, function(i
是否可以使用 jQuery 在另一个 getJSON 请求中使用 getJSON 请求? 像这样: // Population the Requests List // jQuery AJAX cal
我有以下代码,该代码已针对此问题进行了简化。基本上我有一个循环,在每次迭代中调用 jquery getJSON 函数,调用 API 端点来获取一些天气数据。问题是,当 getJSON 请求被触发时,我
我有一个使用 getJSON 的函数,但它没有像我预期的那样工作。 function balbla(name, param) { $.getJSON("/blabla.json?nam
我有一段代码,例如: $.getJSON("http://mysite.org/polls/saveLanguageTest?url=" + escape(window.location.href)
我正在使用jquery.getJSON() ,但我不知道如何进行错误处理。这些是我需要处理的一些情况。 1)如果返回的数据为null怎么办? 2)如果返回的数据不能解析json怎么办? 3) 如果返回
我正在通过参与一个测试项目(包括 SubSonic 和 jQuery)来学习 asp.net mvc。 我遇到的问题是,每次我想要返回的不仅仅是简单字符串(例如 Json 对象)时,我都会遇到困难,因
执行跨域查询,如果运行的URL不可用(404),如何执行某个功能?我尝试这样的事情: $.getJSON({ url:'example.php?callback=?', statusCode: { 4
我在 jQuery 中搜索了相关主题,但没有找到任何方法来解决我的问题。 $(document).ready(function(){ $("#inputForm").submit(functi
当调用 yahoo Web 服务 (http://boss.yahooapis.com/ysearch) 返回数据集时,是否可以设置超时并在超时后退出例程? jQuery.getJSON("http:
我正在使用 jQuery getJSON() 函数。这个函数获取数据没有问题。但有时等待,等待等待......我的加载栏在页面中心显示加载加载。 所以 jQuery ajax() 函数有一个超时变量。
我有一个 html 代码: asd $('button').click( function() { $.getJSON('/schedule/test/', function
目标:我所追求的是每次在数据库中添加某些内容时(在 $.ajax 到 Submit_to_db.php 之后),从数据库获取数据并刷新 main.php(通过 draw_polygon 更明显)。 所
我已经阅读了文档并用谷歌搜索了此内容,但没有看到问题所在。我正在尝试从本地 json 文件获取一些数据。我已在 JSONLint 验证了响应数据 代码 $(document).ready(functi
我在json.getJSON方法上遇到麻烦。这是我当前的代码: var jqxhr = $.getJSON("http://127.0.0.1:5002?callback=?", function()
我需要进行跨域请求,并且 getJSON 有问题。 $.getJSON("http://usr:pwd@10.0.1.xx/cgi-bin/remote/request.cgi?m=json&r=gr
这个问题已经有答案了: How do I return the response from an asynchronous call? (42 个回答) 已关闭 8 年前。 这是我的问题 我有一个充满
我是一名优秀的程序员,十分优秀!