【问题标题】:JSON Data - Parsed Or 'Eval'edJSON 数据 - 已解析或“评估”
【发布时间】:2009-07-17 13:51:54
【问题描述】:

从安全角度来看,我可以将简单地对传入的 JSON 数据执行“评估”视为一个严重错误。如果你得到如下数据,你会遇到一些问题。

{ someData:((function() { 
    alert("i'm in ur code hackin' ur page"); 
})()) }

我想知道最流行的 Javascript 库是做什么的?是手动解析还是简单的评估?

[编辑]

我不是在问 是否应该评估/解析 - 我是在问一些流行的 Javascript 库使用什么方法(jQuery、Prototype 等......)

【问题讨论】:

    标签: javascript security json


    【解决方案1】:

    official JavaScript parser 的作用如下:

    // 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, ''))) {
    
    // In the third stage we use the eval function to compile the text into a
    // JavaScript structure. The '{' operator is subject to a syntactic ambiguity
    // in JavaScript: it can begin a block or an object literal. We wrap the text
    // in parens to eliminate the ambiguity.
    
        j = eval('(' + text + ')');
    
        ...
    

    除了现代浏览器中的内置 JSON parsing support 之外,这是所有(基于库的)安全 JSON 解析器所做的(即,eval 之前的正则表达式测试)。

    安全库(除了官方的 json2 实现)

    原型的isJSON函数。

    Mootools 的 JSON.decode 函数(同样,通过 regex test before eval)。

    不安全的库

    dojo 的fromJson 确实提供安全的evaling。 Here is their entire implementation (minus comments):

    dojo.fromJson = function(json) {
        return eval("(" + json + ")");
    }
    

    jQuery 不提供安全的 JSON eval'ing,但请参阅官方插件的 secureEvalJSON 函数(第 143 行)。

    【讨论】:

    • 这真是一个正则表达式
    • 实际上,一些 JSON 解析器具有跳过正则表达式的安全模式。这种模式速度更快,但也不太安全。
    • @Henrik:如果它不那么安全,你为什么称它为“安全模式”? ;)
    • 因为它假定输入已经是安全的。 (当它来自您控制的服务器时,这听起来很合理。)
    【解决方案2】:

    你绝对应该解析它! JSON 只是 JavaScript 的一个子集。但是eval 会评估任何 JavaScript 代码,而不是像 JSON 解析器那样的特定子集。

    【讨论】:

      【解决方案3】:

      改用evalJSON()
      据我所知,这基本上是在进行一些卫生检查后调用 eval()。

      【讨论】:

        【解决方案4】:

        来自http://code.google.com/p/json-sans-eval/

        一个快速且安全的 JSON 解析器 JavaScript?

        此 JSON 解析器不会尝试 验证 JSON,因此可能会返回一个 给出语法无效的结果 输入,但不使用 eval 所以是 确定性,并保证不会 修改除其以外的任何对象 返回值。

        有许多 JSON 解析器 JavaScript?在 json.org。这 任何时候都应该使用实现 安全是一个问题(当 JSON 可能 来自不受信任的来源),速度 是一个问题,并且错误 格式错误的 JSON 不是问题。

        这个实现

        • 优点 快速、安全
        • 缺点未验证

        json_parse.js

        • 专业人士验证,安全
        • 缺点慢

        json2.js

        • 优点 快速,一些验证
        • 缺点可能不安全

        json2.js 非常快,但可能 不安全,因为它调用 eval 来解析 JSON 数据,因此攻击者可能是 能够提供看起来很奇怪的 JS 像 JSON,但执行任意 javascript。

        如果你必须使用 json2.js 不受信任的数据,请确保您保留 你的 json2.js 版本是最新的,所以 你得到补丁,因为他们是 发布。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2010-10-03
          • 2014-07-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-08-13
          • 1970-01-01
          相关资源
          最近更新 更多