使用安全json parser防止json注入
来源:互联网 发布:9.9元天天特价淘宝包 编辑:程序博客网 时间:2024/05/20 18:54
今天在网上瞎逛又看到了一个不错的东西。
有些程序员如果没有很好的在javascript中解析json数据,往往会直接eval把json转成js对象,这时候如果json的数据中包含了被注入的恶意数据,则可能导致代码注入的问题。
正确的做法是分割出json里包含的特殊字符,然后再解析为对象
http://json.org/json2.js 中是通过正则来完成的。
所以,以后千万别直接eval了。
有些程序员如果没有很好的在javascript中解析json数据,往往会直接eval把json转成js对象,这时候如果json的数据中包含了被注入的恶意数据,则可能导致代码注入的问题。
正确的做法是分割出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注入
- 使用Scala Parser解析JSON
- json parser
- json linux c parser
- json parser 数据转化
- Android - JSON Parser Tutorial
- Simplify-Core --Json解析(Json parser)
- Json安全
- iOS安全:防止dyld注入
- UI 13 数据解析 Parser DOM JSON
- Json-c编写一个简单的 Json parser
- 【JSON】JSON 使用
- json安全输出解决方案
- JSON WEB TOKEN安全
- servlet/jquery/json 防止乱码
- 使用JSON
- 使用json
- json使用
- 关于document.domain的一点tips
- 在分享中,拨号,选择壁纸来源等系统打开方式中添加如自己的程序
- 京东&adsense 广告 [incomplete]
- Ctrl+Enter提交
- Linux下signal通信研究(操作系统期中论文之自选题目研究)
- 使用安全json parser防止json注入
- WebKit之HTML元素支持流程
- JQuery 常见用法
- Browser Security Handbook
- [转][数论]Farey Sequence深入学习
- LINUX ARM平台的GPIO
- 新W3C标准中 AJAX 跨域实现以及隐患
- P3P Header Tips
- 年假总述