白名单编码防止XSS
在AntiXSS项目中,各个函数均采用白名单方式进行编码,比如一个 EncodeHtml() Java函数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | private static String EncodeHtml(String strInput) { if (strInput.length() == 0) { return EmptyString; } StringBuffer builder = new StringBuffer(strInput.length() * 2); CharacterIterator it = new StringCharacterIterator(strInput); for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) { if ((((ch > '`') && (ch < '{')) || ((ch > '@') && (ch < '['))) || (((ch == ' ') || ((ch > '/') && (ch < ':'))) || (((ch == '.') || (ch == ',')) || ((ch == '-') || (ch == '_'))))) { builder.append(ch); } else { builder.append("&#" + (int) ch + ";"); } } return builder.toString(); } |
根据ASCII,先排除了正常使用的字符,其他均进行编码。相比其他采用黑名单编码的自写函数要好的多。。
转载请注明:woyigui's blog [http://www.woyigui.cn/]
本文标题:白名单编码防止XSS
本文地址:http://www.woyigui.cn/2009/08/22/antixss/
最新评论