博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HtmlEncode和JavaScriptEncode(预防XSS)
阅读量:4918 次
发布时间:2019-06-11

本文共 7618 字,大约阅读时间需要 25 分钟。

在数据添加到DOM时候,我们可以需要对内容进行HtmlEncode或JavaScriptEncode,以预防XSS攻击。

JavaScriptEncode

使用“\”对特殊字符进行转义,除数字字母之外,小于127的字符编码使用16进制“\xHH”的方式进行编码,大于用unicode(非常严格模式)。

//使用“\”对特殊字符进行转义,除数字字母之外,小于127使用16进制“\xHH”的方式进行编码,大于用unicode(非常严格模式)。var JavaScriptEncode = function(str){         var hex=new Array('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f');            function changeTo16Hex(charCode){        return "\\x" + charCode.charCodeAt(0).toString(16);    }        function encodeCharx(original) {                var found = true;        var thecharchar = original.charAt(0);        var thechar = original.charCodeAt(0);        switch(thecharchar) {            case '\n': return "\\n"; break; //newline            case '\r': return "\\r"; break; //Carriage return            case '\'': return "\\'"; break;            case '"': return "\\\""; break;            case '\&': return "\\&"; break;            case '\\': return "\\\\"; break;            case '\t': return "\\t"; break;            case '\b': return "\\b"; break;            case '\f': return "\\f"; break;            case '/': return "\\x2F"; break;            case '<': return "\\x3C"; break;            case '>': return "\\x3E"; break;            default:                found=false;                break;        }        if(!found){            if(thechar > 47 && thechar < 58){ //数字                return original;            }                        if(thechar > 64 && thechar < 91){ //大写字母                return original;            }            if(thechar > 96 && thechar < 123){ //小写字母                return original;            }                                if(thechar>127) { //大于127用unicode                var c = thechar;                var a4 = c%16;                c = Math.floor(c/16);                 var a3 = c%16;                c = Math.floor(c/16);                var a2 = c%16;                c = Math.floor(c/16);                var a1 = c%16;                return "\\u"+hex[a1]+hex[a2]+hex[a3]+hex[a4]+"";                    }            else {                return changeTo16Hex(original);            }                    }    }           var preescape = str;    var escaped = "";    var i=0;    for(i=0; i < preescape.length; i++){        escaped = escaped + encodeCharx(preescape.charAt(i));    }    return escaped;}

HtmlEncode

将字符转换成HTMLEntites,以对抗XSS。

var HtmlEncode = function(str){    var hex = new Array('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f');    var preescape = str;    var escaped = "";    for(var i = 0; i < preescape.length; i++){        var p = preescape.charAt(i);        escaped = escaped + escapeCharx(p);    }        return escaped;                        function escapeCharx(original){        var found=true;        var thechar=original.charCodeAt(0);        switch(thechar) {            case 10: return "
"; break; //newline case 32: return " "; break; //space case 34:return """; break; //" case 38:return "&"; break; //& case 39:return "'"; break; //' case 47:return "/"; break; // / case 60:return "<"; break; //< case 62:return ">"; break; //> case 198:return "Æ"; break; case 193:return "Á"; break; case 194:return "Â"; break; case 192:return "À"; break; case 197:return "Å"; break; case 195:return "Ã"; break; case 196:return "Ä"; break; case 199:return "Ç"; break; case 208:return "Ð"; break; case 201:return "É"; break; case 202:return "Ê"; break; case 200:return "È"; break; case 203:return "Ë"; break; case 205:return "Í"; break; case 206:return "Î"; break; case 204:return "Ì"; break; case 207:return "Ï"; break; case 209:return "Ñ"; break; case 211:return "Ó"; break; case 212:return "Ô"; break; case 210:return "Ò"; break; case 216:return "Ø"; break; case 213:return "Õ"; break; case 214:return "Ö"; break; case 222:return "Þ"; break; case 218:return "Ú"; break; case 219:return "Û"; break; case 217:return "Ù"; break; case 220:return "Ü"; break; case 221:return "Ý"; break; case 225:return "á"; break; case 226:return "â"; break; case 230:return "æ"; break; case 224:return "à"; break; case 229:return "å"; break; case 227:return "ã"; break; case 228:return "ä"; break; case 231:return "ç"; break; case 233:return "é"; break; case 234:return "ê"; break; case 232:return "è"; break; case 240:return "ð"; break; case 235:return "ë"; break; case 237:return "í"; break; case 238:return "î"; break; case 236:return "ì"; break; case 239:return "ï"; break; case 241:return "ñ"; break; case 243:return "ó"; break; case 244:return "ô"; break; case 242:return "ò"; break; case 248:return "ø"; break; case 245:return "õ"; break; case 246:return "ö"; break; case 223:return "ß"; break; case 254:return "þ"; break; case 250:return "ú"; break; case 251:return "û"; break; case 249:return "ù"; break; case 252:return "ü"; break; case 253:return "ý"; break; case 255:return "ÿ"; break; case 162:return "¢"; break; case '\r': break; default: found=false; break; } if(!found){ if(thechar>127) { var c=thechar; var a4=c%16; c=Math.floor(c/16); var a3=c%16; c=Math.floor(c/16); var a2=c%16; c=Math.floor(c/16); var a1=c%16; return "&#x"+hex[a1]+hex[a2]+hex[a3]+hex[a4]+";"; } else{ return original; } } }}

Test

<script>alert('1哈哈' /);</script>

这些编码后的内容都能在页面上显示正常。 

番外

还有人弄了简单HtmlEncode,有两种方式。

1. 用浏览器内部转换器实现html转码(但我觉得这种方式有风险的,因为内部转换器可能有漏洞)。

2. 只转一部分html字符(这种方式不完整)。

var HtmlUtil = {    htmlEncode:function (html){        var temp = document.createElement ("div");        (temp.textContent != undefined ) ? (temp.textContent = html) : (temp.innerText = html);        var output = temp.innerHTML;        temp = null;        return output;    },    htmlDecode:function (text){        var temp = document.createElement("div");        temp.innerHTML = text;        var output = temp.innerText || temp.textContent;        temp = null;        return output;    },    htmlEncodeByRegExp:function (str){        var s = "";        if(str.length == 0) return "";        s = str.replace(/&/g,"&");        s = s.replace(//g,">");        s = s.replace(/ /g," ");        s = s.replace(/\'/g,"'");        s = s.replace(/\"/g,""");        return s;    },    htmlDecodeByRegExp:function (str){        var s = "";        if(str.length == 0) return "";        s = str.replace(/&/g,"&");        s = s.replace(/</g,"<");        s = s.replace(/>/g,">");        s = s.replace(/ /g," ");        s = s.replace(/'/g,"\'");        s = s.replace(/"/g,"\"");        return s;    }};

 

本文为原创文章,转载请保留原出处,方便溯源,如有错误地方,谢谢指正。

本文地址 :

转载于:https://www.cnblogs.com/lovesong/p/5211667.html

你可能感兴趣的文章
java语言将任意一个十进制数数字转换为二进制形式,并输出转换后的结果
查看>>
java相关。关于jsp中使用el表达式的格式,谢谢!
查看>>
GetDlgItem的用法小结
查看>>
java带包编译
查看>>
树状数组详解(重拾笔记)
查看>>
javascript深入理解js闭包
查看>>
PLSQL
查看>>
ASP.NET Core 应用程序Startup类介绍
查看>>
【python】-类的继承
查看>>
同一个POD中默认共享哪些名称空间
查看>>
HTTP协议详解
查看>>
xdebug调试的原理
查看>>
php 日期时间运算比较
查看>>
C#类、接口、虚方法和抽象方法
查看>>
Linq C#增删改查
查看>>
[转]第一章 Windows Shell是什么 【来源:http://blog.csdn.net/wangqiulin123456/article/details/7987862】...
查看>>
iOS获取设备UUID和IDFA
查看>>
模糊查询
查看>>
linux 出现:-bash-3.2$提示符
查看>>
jsp电子商务 购物车实现之二 登录和分页篇
查看>>