|
| 1 | +/* |
| 2 | +* @Author: victorsun |
| 3 | +* @Date: 2017-09-07 14:12:02 |
| 4 | +* @Last Modified by: victorsun |
| 5 | +* @Last Modified time: 2017-09-09 14:12:02 |
| 6 | +*/ |
| 7 | + |
| 8 | + |
| 9 | +/* |
| 10 | +url转向验证 |
| 11 | +描述:对通过javascript语句载入(或转向)的页面进行验证,防止转到第三方网页和跨站脚本攻击 |
| 12 | +返回值:true -- 合法;false -- 非法 |
| 13 | +例: |
| 14 | +合法的值 |
| 15 | + http://xxx.csxiaoyao.com/hi/redirect.html?url=http://www.csxiaoyao.com |
| 16 | + http://xxx.csxiaoyao.com/hi/redirect.html?url=a.html |
| 17 | + http://xxx.csxiaoyao.com/hi/redirect.html?url=/a/1.html |
| 18 | +非法的值 |
| 19 | + http://xxx.csxiaoyao.com/hi/redirect.html?url=http://www.baidu.com |
| 20 | + http://xxx.csxiaoyao.com/hi/redirect.html?url=javascript:codehere |
| 21 | + http://xxx.csxiaoyao.com/hi/redirect.html?url=//www.csxiaoyao.com |
| 22 | +*/ |
| 23 | +function VaildURL(sUrl) |
| 24 | +{ |
| 25 | + return (/^(https?:\/\/)?[\w\-.]+\.(csxiaoyao|sunshinestudio)\.(com|cn)($|\/|\\)/i).test(sUrl)||(/^[\w][\w\/\.\-_%]+$/i).test(sUrl)||(/^[\/\\][^\/\\]/i).test(sUrl) ? true : false; |
| 26 | +} |
| 27 | + |
| 28 | +//html正文编码:对需要出现在HTML正文里(除了HTML属性外)的不信任输入进行编码 |
| 29 | +function HtmlEncode(sStr) |
| 30 | +{ |
| 31 | + sStr = sStr.replace(/&/g,"&"); |
| 32 | + sStr = sStr.replace(/>/g,">"); |
| 33 | + sStr = sStr.replace(/</g,"<"); |
| 34 | + sStr = sStr.replace(/"/g,"""); |
| 35 | + sStr = sStr.replace(/'/g,"'"); |
| 36 | + return sStr; |
| 37 | +} |
| 38 | + |
| 39 | +//html正文解码:对HtmlEncode函数的结果进行解码 |
| 40 | +function HtmlUnEncode(sStr) |
| 41 | +{ |
| 42 | + sStr = sStr.replace(/&/g,"&"); |
| 43 | + sStr = sStr.replace(/>/g,">"); |
| 44 | + sStr = sStr.replace(/</g,"<"); |
| 45 | + sStr = sStr.replace(/"/g,'"'); |
| 46 | + sStr = sStr.replace(/'/g,"'"); |
| 47 | + return sStr; |
| 48 | +} |
| 49 | + |
| 50 | +/* |
| 51 | +html属性编码:对需要出现在HTML属性里的不信任输入进行编码 |
| 52 | +注意: |
| 53 | +(1)该函数不适用于属性为一个URL地址的编码.这些标记包括:a/img/frame/iframe/script/xml/embed/object... |
| 54 | +属性包括:href/src/lowsrc/dynsrc/background/... |
| 55 | +(2)该函数不适用于属性名为 style="[Un-trusted input]" 的编码 |
| 56 | +*/ |
| 57 | +function HtmlAttributeEncode(sStr) |
| 58 | +{ |
| 59 | + sStr = sStr.replace(/&/g,"&"); |
| 60 | + sStr = sStr.replace(/>/g,">"); |
| 61 | + sStr = sStr.replace(/</g,"<"); |
| 62 | + sStr = sStr.replace(/"/g,"""); |
| 63 | + sStr = sStr.replace(/'/g,"'"); |
| 64 | + sStr = sStr.replace(/=/g,"="); |
| 65 | + sStr = sStr.replace(/`/g,"`"); |
| 66 | + return sStr; |
| 67 | +} |
| 68 | + |
| 69 | + |
| 70 | +/* |
| 71 | +对需要出现在一个URI的一部分的不信任输入进行编码 |
| 72 | +例如: |
| 73 | +<a href="http://search.msn.com/results.aspx?q1=[Un-trusted-input]& q2=[Un-trusted-input]">Click Here!</a> |
| 74 | +以下字符将会被编码: |
| 75 | +除[a-zA-Z0-9.-_]以外的字符都会被替换成URL编码 |
| 76 | +*/ |
| 77 | +function UriComponentEncode(sStr) |
| 78 | +{ |
| 79 | + sStr = encodeURIComponent(sStr); |
| 80 | + sStr = sStr.replace(/~/g,"%7E"); |
| 81 | + sStr = sStr.replace(/!/g,"%21"); |
| 82 | + sStr = sStr.replace(/\*/g,"%2A"); |
| 83 | + sStr = sStr.replace(/\(/g,"%28"); |
| 84 | + sStr = sStr.replace(/\)/g,"%29"); |
| 85 | + sStr = sStr.replace(/'/g,"%27"); |
| 86 | + sStr = sStr.replace(/\?/g,"%3F"); |
| 87 | + sStr = sStr.replace(/;/g,"%3B"); |
| 88 | + return sStr; |
| 89 | +} |
| 90 | + |
| 91 | + |
| 92 | +//用做过滤HTML标签里面的 比如这个例子里的<input value="XXXX"> XXXX就是要过滤的 |
| 93 | +String.prototype.escHtmlEp = function() { return this.replace(/[&'"<>\/\\\-\x00-\x1f\x80-\xff]/g, function(r){ return "&#"+r.charCodeAt(0)+";" }); }; |
| 94 | + |
| 95 | +//用做过滤直接放到HTML里的 |
| 96 | +String.prototype.escHtml = function() { return this.replace(/[&'"<>\/\\\-\x00-\x09\x0b-\x0c\x1f\x80-\xff]/g, function(r){ return "&#"+r.charCodeAt(0)+";" }).replace(/\r\n/g, "<BR>").replace(/\n/g, "<BR>").replace(/\r/g, "<BR>").replace(/ /g, " "); }; |
| 97 | + |
| 98 | +//用做过滤直接放到HTML里js中的 |
| 99 | +String.prototype.escScript = function() { return this.replace(/[\\"']/g, function(r){ return "\\"+r; }).replace(/%/g, "\\x25").replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\x01/g, "\\x01"); }; |
| 100 | + |
| 101 | +//用做过滤直接URL参数里的 比如 http://show8.qq.com/abc_cgi?a=XXX XXX就是要过滤的 |
| 102 | +String.prototype.escUrl = function() { return escape(this).replace(/\+/g, "%2B"); }; |
| 103 | + |
| 104 | +//用做过滤直接放到<a href="javascript:XXXX">中的 |
| 105 | +String.prototype.escHrefScript = function() { return this.escScript().escMiniUrl().escHtmlEp(); }; |
| 106 | + |
| 107 | +//用做过滤直接放到正则表达式中的 |
| 108 | +String.prototype.escRegexp = function() { return this.replace(/[\\\^\$\*\+\?\{\}\.\(\)\[\]]/g, function(a,b){ return "\\"+a; }); }; |
| 109 | + |
0 commit comments