While researching the issue of JSON vs XML, I came across this question. Now one of the reasons to prefer JSON was listed as the ease of conversion in Javascript, namely with the eval(). Now this immediately struck me as potentially problematic from a security perspective.

So I started doing some research into the security aspects of JSON and across this blog post about how JSON is not as safe as people think it is. This part stuck out:

Update: If you are doing JSON 100% properly, then you will only have objects at the top level. Arrays, Strings, Numbers, etc will all be wrapped. A JSON object will then fail to eval() because the JavaScript interpreter will think it's looking at a block rather than an object. This goes a long way to protecting against these attacks, however it's still best to protect your secure data with un-predictable URLs.

Ok, so that's a good rule to start with: JSON objects at the top level should always be objects and never arrays, numbers or strings. Sounds like a good rule to me.

当涉及到与JSON和Ajax相关的安全性时,是否还有其他需要做或要避免的事情?

上面引用的最后一部分提到了不可预测的URL.有人有更多关于这方面的信息吗,尤其是你是如何用PHP实现的?我在Java方面的经验比PHP丰富得多,在Java中很容易(因为你可以将一系列URL映射到一个servlet),而我所做的所有PHP都将一个URL映射到PHP脚本.

Also, how exactly do you use unpredictable URLs to increase security?

推荐答案

The main security hole from the blog (CSRF), is not JSON specific. It's just as big a hole using XML instead. Indeed, it's just as bad with no asynchronous calls at all; regular links are just as vulnerable.

When people talk about unique URLs, they generally DON'T mean http://yourbank.com/json-api/your-name/big-long-key-unique-to-you/statement. Instead, it's more common to make something else about the request unique; namely a value in the FORM post, or a URL parameter.

通常,这需要在服务器端的表单中插入一个随机令牌,然后在发出请求时进行判断.

数组/对象对我来说是新闻:

Script-Tags: The attacker can embed a script tag pointing at a remote server and the browser will effectively eval() the reply for you, however it throws away the response and since JSON is all response, you're safe.

In that case, your site doesn't need to use JSON at all to be vulnerable. But yeah, if an attacker can insert random HTML into your site, you're toast.

Json相关问答推荐

使用SQL查询从SON中查找第n个密钥对值

Vega-Lite:文本笔画在外部

当有嵌套数组而没有嵌套数组时,展平JSON

PowerShell脚本未按预期生成预期的JSON输出

无法从JSON解析ZonedDateTime,但可以使用格式化程序很好地解析

Powershell ConvertFrom-Json 意外地从包含字符串的单个项目数组生成字符串而不是对象数组

如何将属性拆分为嵌套的JSON内容?

Android 如何判断小时时间是否在两个时间之间?

JOLT分裂和数组数据

如何编写 jolt 规范以将不同的对象转换为数组

PowerShell:如何将哈希表输出为 json 并使用 foreach 循环将其存储在变量中?

如何将动态复杂 json 解析为dart 对象或模型

如何限制resolveJsonModule的范围?

Golang / Go - 如果 struct 没有字段,如何将其编组为空?

将 YAML 文件转换为 Python JSON 对象

如何判断 Json 对象中是否存在键并获取其值

如何在 jQuery 中循环遍历 JSON 数组?

Newtonsoft Json 将值 {null} 转换为类型System.Int32时出错

关于使用 $ref 的 JSON 模式

为什么 RestTemplate 不将响应表示绑定到 PagedResources?