eval 和 window.json.parse 处理responseText 之间的区别?
我手头有以下代码
var FinalCompleteData = eval("("+jsonresponse.responseText+")");
当我使用此代码时,我在 Fortify 中收到一个安全缺陷错误,说它可能会导致JavaScript 黑客攻击。因此,我将其更改为
var FinalCompleteData = window.json.parse(jsonresponse.responseText);
为此,Fortify 没有显示错误。 window.json.parse 方法有什么作用?
你能解释一下吗?提前致谢 :-)
I have the following code at hand
var finalCompleteData = eval("("+jsonresponse.responseText+")");
When I used this, I received a security flaw error in Fortify saying that it might lead to Javascript Hacking. So, I changed it to
var finalCompleteData = window.json.parse(jsonresponse.responseText);
For this, Fortify did not show the error. What the window.json.parse method do ?
Can you please explain. Thanks in advance :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
eval
将执行它应该评估的任何 JavaScript 代码,并且它以最高级别的安全性进行评估。这意味着,如果您的响应文本返回非 json 代码,但返回有效的 javascript,eval
将执行它。天空是极限,它可以添加新功能、更改变量、重定向页面。使用
window.json.parse
只会评估 json,因此输入恶意代码的风险要小得多。eval
will execute any JavaScript code which it is supposed to evaluate, and it evaluates with the highest level of security. This means that if your response text returns non-json code, but valid javascript, theeval
will execute it. The sky is the limit with this, it can add new functions, change variables, redirect the page.With
window.json.parse
only json will be evaluated, so the risk of rogue code getting entered is much much less.eval 能够运行任何类型的 javascript 代码 - 不仅仅是像 JSON.parse 那样的简单对象/数组(它检查内容 - 验证 json)。因此,在无法保证输入的地方应避免 eval。
eval is able to run any kind of javascript code - not just simple objects/arrays as JSON.parse would (it examines the contents - validating json). For this reason eval should be avoided in places where you cannot guarantee the input.
正如其他人提到的,
eval
将执行任何有效的 JavaScript 代码。因此,以下情况将引发警报:您本质上信任来自给定来源的任何输入,这通常是不安全的。恶意用户可能会利用 eval 并执行有害的 JavaScript。
然而,只有当传入的字符串是有效的 JSON 时,
JSON.parse
才会成功返回:因此,它不会按照
eval
的方式执行任何内容。As others have mentioned,
eval
will execute any valid JavaScript code. Thus the following would cause an alert:You're essentially trusting any input from a given source, which is not safe in general. A malicious user could take advantage of the eval and execute harmful JavaScript.
JSON.parse
, however, will only return successfully if the string passed in is valid JSON:Thus it's not executing just anything it's given the way
eval
is.