html防止注入转译,增加不断开空格(nbsp)转译

This commit is contained in:
LuisStruggle 2022-11-24 10:53:32 +08:00
parent c72757b813
commit 31bcd02732
4 changed files with 17 additions and 5 deletions

View File

@ -176,7 +176,7 @@ public interface StrPool {
/**
* 字符串常量HTML 空格转义 {@code " " -> " "}
* 字符串常量HTML 不间断空格转义 {@code " " -> " "}
*/
String HTML_NBSP = XmlUtil.NBSP;

View File

@ -67,7 +67,7 @@ import java.util.Map;
public class XmlUtil {
/**
* 字符串常量XML 空格转义 {@code " " -> " "}
* 字符串常量XML 不间断空格转义 {@code " " -> " "}
*/
public static final String NBSP = " ";

View File

@ -26,10 +26,11 @@ public class HtmlUtil {
public static final String RE_HTML_MARK = "(<[^<]*?>)|(<[\\s]*?/[^<]*?>)|(<[^<]*?/[\\s]*?>)";
public static final String RE_SCRIPT = "<[\\s]*?script[^>]*?>.*?<[\\s]*?\\/[\\s]*?script[\\s]*?>";
private static final char[][] TEXT = new char[64][];
private static final char[][] TEXT = new char[256][];
static {
for (int i = 0; i < 64; i++) {
// ascii码值最大的是0x7f=127扩展ascii码值最大的是0xFF=255因为ASCII码使用指定的7位或8位二进制数组合来表示128或256种可能的字符标准ASCII码也叫基础ASCII码
for (int i = 0; i < 256; i++) {
TEXT[i] = new char[] { (char) i };
}
@ -39,6 +40,7 @@ public class HtmlUtil {
TEXT['&'] = AMP.toCharArray(); // &
TEXT['<'] = LT.toCharArray(); // 小于号
TEXT['>'] = GT.toCharArray(); // 大于号
TEXT[' '] = NBSP.toCharArray(); // 不断开空格non-breaking space缩写nbspASCII值是32是用键盘输入的空格ASCII值是160不间断空格 &nbsp所产生的空格作用是在页面换行时不被打断
}
/**
@ -190,7 +192,7 @@ public class HtmlUtil {
char c;
for (int i = 0; i < len; i++) {
c = text.charAt(i);
if (c < 64) {
if (c < 256) {
buffer.append(TEXT[c]);
} else {
buffer.append(c);

View File

@ -134,6 +134,16 @@ public class HtmlUtilTest {
Assert.assertEquals("'", HtmlUtil.unescape("&apos;"));
}
@Test
public void escapeTest2() {
char c = ' '; // 不断开空格non-breaking space缩写nbsp)
Assert.assertEquals(c, 160);
String html = "<html><body> </body></html>";
String escape = HtmlUtil.escape(html);
Assert.assertEquals("&lt;html&gt;&lt;body&gt;&nbsp;&lt;/body&gt;&lt;/html&gt;", escape);
Assert.assertEquals(" ", HtmlUtil.unescape("&nbsp;"));
}
@Test
public void filterTest() {
String html = "<alert></alert>";