使用安全json parser防止json注入
2009-05-19 14:17
267 查看
有些程序员如果没有很好的在javascript中解析json数据,往往会直接eval把json转成js对象,这时候如果json的数据中包含了被注入的恶意数据,则可能导致代码注入的问题。
正确的做法是分割出json里包含的特殊字符,然后再解析为对象
http://json.org/json2.js 中是通过正则来完成的。
目前不少写的好的框架和js解析函数都取用了这种做法。
所以,以后千万别直接eval了。
正确的做法是分割出json里包含的特殊字符,然后再解析为对象
http://json.org/json2.js 中是通过正则来完成的。
// 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 + ')');
目前不少写的好的框架和js解析函数都取用了这种做法。
所以,以后千万别直接eval了。
相关文章推荐
- 使用安全json parser防止json注入
- 使用安全json parser防止json注入
- 防止XSS注入script脚本,简单的方法;在项目中也可以使用的安全转码格式。
- 使用LINQ解除SQL注入安全问题
- mysql端口打开后,怕被爆破,感觉不安全,使用python监控日志防止暴力破解
- 使用puase等待信号要防止信号发生在puase之前。所以为了代码安全,使用sigaction
- 使用LINQ解除SQL注入安全问题
- Mysql使用SQL的安全问题,Mysql防止SQL注入
- PHP如何防止注入及开发安全
- iOS安全–使用static inline方式编译函数,防止静态分析
- spring中使用servlet拦截器实现防止sql注入
- PHP安全编程:防止SQL注入
- win32 (sdk) 安全的使用字符,防止字符缓冲区溢出。
- php 使用pdo防止注入的原理或注意事项
- 使用User-Agent防止HttpClient发送http请求时403 Forbidden和安全拦截
- [.NET] 使用Json.NET提供依赖注入功能(Dependence Injection)
- 使用Scala Parser解析JSON
- 如何防止ASP.NET应用程序中的SQL注入安全漏洞
- 使用jquery ajax对特殊字符进行转义,防止js注入
- PHP如何防止注入及开发安全