代码改变世界

表单提交特殊字符处理

2017-05-26 17:41  taozsay  阅读(2107)  评论(0编辑  收藏  举报

项目中经常会用到表单提交特殊字符过滤来预防XXS漏洞

之前经常每次用的时候查下,解决了,就完了。这次记录下。

1.前台做过滤

1  function fiterJSONStr(str) {
2     str = str.replace(/\\/g, '\\\\');
3     str = str.replace(/&/g, 'jq==');//后端需要做下处理
4     str = str.replace(/'/g, '’');
5     str = str.replace(/"/g, '”');
6     str = str.replace(/</g, '');
7     str = str.replace(/>/g, '');
8     return str.replace(/"/g, '“');
9 }

2. 后端处理

使用.NET MVC 只需要在方法前面加上[ValidateInput(false)] 属性

然后还需要再web.config 添加 <httpRuntime requestValidationMode="2.0" />

 

以上。

 

原文:http://www.cnblogs.com/bigbrid/p/6909716.html 

作者:hito