package com.ruoyi.system.utils;
|
|
public class ProtocolHtmlSanitizerTest
|
{
|
public static void main(String[] args)
|
{
|
shouldKeepSupportedFormatting();
|
shouldRemoveDangerousContentAndAttributes();
|
}
|
|
private static void shouldKeepSupportedFormatting()
|
{
|
String html = "<div style=\"text-align: center; position: fixed\">入园承诺</div>\n"
|
+ "<div><span style=\"font-weight: bold; color: #111827\">第二行</span></div>";
|
|
String result = ProtocolHtmlSanitizer.clean(html);
|
|
require(result.contains("<div style=\"text-align: center\">入园承诺</div>"), "居中样式未保留");
|
require(result.contains("<span style=\"font-weight: bold; color: #111827\">第二行</span>"), "文字样式未保留");
|
require(!result.contains("position"), "非白名单样式未清除");
|
}
|
|
private static void shouldRemoveDangerousContentAndAttributes()
|
{
|
String html = "<div onclick=\"alert(1)\">正文<script>alert(1)</script>"
|
+ "<span style=\"background-image: url(javascript:alert(1))\">内容</span></div>";
|
|
String result = ProtocolHtmlSanitizer.clean(html);
|
|
require(result.contains("<div>正文<span>内容</span></div>"), "安全正文结构未保留");
|
require(!result.contains("script"), "危险标签未清除");
|
require(!result.contains("onclick"), "事件属性未清除");
|
require(!result.contains("javascript"), "危险样式内容未清除");
|
}
|
|
private static void require(boolean condition, String message)
|
{
|
if (!condition)
|
{
|
throw new AssertionError(message);
|
}
|
}
|
}
|