11/*
2- * @Author : victorsun
2+ * @Author : csxiaoyao
33* @Date : 2017-09-07 14:12:02
4- * @Last Modified by: victorsun
4+ * @Last Modified by: csxiaoyao
55* @Last Modified time: 2017-09-09 14:12:02
66*/
77
88
99/*
10- url转向验证
11- 描述:对通过javascript语句载入(或转向)的页面进行验证,防止转到第三方网页和跨站脚本攻击
12- 返回值:true -- 合法;false -- 非法
13- 例:
14- 合法的值
10+ url转向验证
11+ 描述:对通过javascript语句载入(或转向)的页面进行验证,防止转到第三方网页和跨站脚本攻击
12+ 返回值:true -- 合法;false -- 非法
13+ 例:
14+ 合法的值
1515 http://xxx.csxiaoyao.com/hi/redirect.html?url=http://www.csxiaoyao.com
1616 http://xxx.csxiaoyao.com/hi/redirect.html?url=a.html
1717 http://xxx.csxiaoyao.com/hi/redirect.html?url=/a/1.html
18- 非法的值
18+ 非法的值
1919 http://xxx.csxiaoyao.com/hi/redirect.html?url=http://www.baidu.com
2020 http://xxx.csxiaoyao.com/hi/redirect.html?url=javascript:codehere
2121 http://xxx.csxiaoyao.com/hi/redirect.html?url=//www.csxiaoyao.com
@@ -25,7 +25,7 @@ function VaildURL(sUrl)
2525 return ( / ^ ( h t t p s ? : \/ \/ ) ? [ \w \- . ] + \. ( c s x i a o y a o | s u n s h i n e s t u d i o ) \. ( c o m | c n ) ( $ | \/ | \\ ) / i) . test ( sUrl ) || ( / ^ [ \w ] [ \w \/ \. \- _ % ] + $ / i) . test ( sUrl ) || ( / ^ [ \/ \\ ] [ ^ \/ \\ ] / i) . test ( sUrl ) ? true : false ;
2626}
2727
28- //html正文编码:对需要出现在HTML正文里(除了HTML属性外)的不信任输入进行编码
28+ //html正文编码:对需要出现在HTML正文里(除了HTML属性外)的不信任输入进行编码
2929function HtmlEncode ( sStr )
3030{
3131 sStr = sStr . replace ( / & / g, "&" ) ;
@@ -36,7 +36,7 @@ function HtmlEncode(sStr)
3636 return sStr ;
3737}
3838
39- //html正文解码:对HtmlEncode函数的结果进行解码
39+ //html正文解码:对HtmlEncode函数的结果进行解码
4040function HtmlUnEncode ( sStr )
4141{
4242 sStr = sStr . replace ( / & a m p ; / g, "&" ) ;
@@ -48,11 +48,11 @@ function HtmlUnEncode(sStr)
4848}
4949
5050/*
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]" 的编码
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]" 的编码
5656*/
5757function HtmlAttributeEncode ( sStr )
5858{
@@ -68,11 +68,11 @@ function HtmlAttributeEncode(sStr)
6868
6969
7070/*
71- 对需要出现在一个URI的一部分的不信任输入进行编码
72- 例如:
71+ 对需要出现在一个URI的一部分的不信任输入进行编码
72+ 例如:
7373<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编码
74+ 以下字符将会被编码:
75+ 除[a-zA-Z0-9.-_]以外的字符都会被替换成URL编码
7676*/
7777function UriComponentEncode ( sStr )
7878{
@@ -89,21 +89,21 @@ function UriComponentEncode(sStr)
8989}
9090
9191
92- //用做过滤HTML标签里面的 比如这个例子里的<input value="XXXX"> XXXX就是要过滤的
92+ //用做过滤HTML标签里面的 比如这个例子里的<input value="XXXX"> XXXX就是要过滤的
9393String . prototype . escHtmlEp = function ( ) { return this . replace ( / [ & ' " < > \/ \\ \- \x00 - \x1f \x80 - \xff ] / g, function ( r ) { return "&#" + r . charCodeAt ( 0 ) + ";" } ) ; } ;
9494
95- //用做过滤直接放到HTML里的
95+ //用做过滤直接放到HTML里的
9696String . 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, " " ) ; } ;
9797
98- //用做过滤直接放到HTML里js中的
98+ //用做过滤直接放到HTML里js中的
9999String . 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" ) ; } ;
100100
101- //用做过滤直接URL参数里的 比如 http://show8.qq.com/abc_cgi?a=XXX XXX就是要过滤的
101+ //用做过滤直接URL参数里的 比如 http://show8.qq.com/abc_cgi?a=XXX XXX就是要过滤的
102102String . prototype . escUrl = function ( ) { return escape ( this ) . replace ( / \+ / g, "%2B" ) ; } ;
103103
104- //用做过滤直接放到<a href="javascript:XXXX">中的
104+ //用做过滤直接放到<a href="javascript:XXXX">中的
105105String . prototype . escHrefScript = function ( ) { return this . escScript ( ) . escMiniUrl ( ) . escHtmlEp ( ) ; } ;
106106
107- //用做过滤直接放到正则表达式中的
107+ //用做过滤直接放到正则表达式中的
108108String . prototype . escRegexp = function ( ) { return this . replace ( / [ \\ \^ \$ \* \+ \? \{ \} \. \( \) \[ \] ] / g, function ( a , b ) { return "\\" + a ; } ) ; } ;
109109
0 commit comments