mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-21 04:46:26 +00:00
1. 重构后端API路由、CRUD与模块结构,整合日志管理,移除废弃demo代码 2. 优化前端组件类型定义、样式与路由配置,修复权限判断逻辑 3. 调整默认排序规则、滚动条样式与工具类函数,更新依赖与配置文件 4. 修复多处类型不匹配与默认值问题,完善表单与菜单验证逻辑
120 lines
2.1 KiB
Python
120 lines
2.1 KiB
Python
import bleach
|
|
from bleach.css_sanitizer import CSSSanitizer
|
|
|
|
ALLOWED_TAGS = [
|
|
"a",
|
|
"abbr",
|
|
"acronym",
|
|
"b",
|
|
"blockquote",
|
|
"br",
|
|
"code",
|
|
"col",
|
|
"colgroup",
|
|
"dd",
|
|
"del",
|
|
"dl",
|
|
"dt",
|
|
"em",
|
|
"h1",
|
|
"h2",
|
|
"h3",
|
|
"h4",
|
|
"h5",
|
|
"h6",
|
|
"hr",
|
|
"i",
|
|
"img",
|
|
"li",
|
|
"ol",
|
|
"p",
|
|
"pre",
|
|
"s",
|
|
"span",
|
|
"strike",
|
|
"strong",
|
|
"sub",
|
|
"sup",
|
|
"table",
|
|
"tbody",
|
|
"td",
|
|
"tfoot",
|
|
"th",
|
|
"thead",
|
|
"tr",
|
|
"tt",
|
|
"u",
|
|
"ul",
|
|
"video",
|
|
"source",
|
|
"div",
|
|
"font",
|
|
]
|
|
|
|
ALLOWED_ATTRIBUTES = {
|
|
"*": ["class", "style", "id", "data-*"],
|
|
"a": ["href", "title", "target", "rel"],
|
|
"abbr": ["title"],
|
|
"acronym": ["title"],
|
|
"img": ["src", "alt", "title", "width", "height"],
|
|
"video": ["src", "controls", "width", "height", "poster"],
|
|
"source": ["src", "type"],
|
|
"font": ["color", "size", "face"],
|
|
"td": ["width", "height", "colspan", "rowspan"],
|
|
"th": ["width", "height", "colspan", "rowspan"],
|
|
"col": ["width", "span"],
|
|
"colgroup": ["span"],
|
|
}
|
|
|
|
ALLOWED_STYLES = [
|
|
"color",
|
|
"background-color",
|
|
"font-size",
|
|
"font-family",
|
|
"font-weight",
|
|
"font-style",
|
|
"text-decoration",
|
|
"text-align",
|
|
"margin",
|
|
"margin-left",
|
|
"margin-right",
|
|
"margin-top",
|
|
"margin-bottom",
|
|
"padding",
|
|
"padding-left",
|
|
"padding-right",
|
|
"padding-top",
|
|
"padding-bottom",
|
|
"border",
|
|
"border-color",
|
|
"border-width",
|
|
"border-style",
|
|
"width",
|
|
"height",
|
|
"line-height",
|
|
"display",
|
|
]
|
|
|
|
|
|
def sanitize_html(content: str) -> str:
|
|
"""
|
|
清理 HTML 内容,移除潜在的 XSS 攻击代码。
|
|
|
|
参数:
|
|
- content (str): 需要清理的 HTML 内容
|
|
|
|
返回:
|
|
- str: 清理后的安全 HTML 内容
|
|
"""
|
|
if not content:
|
|
return content
|
|
|
|
return bleach.clean(
|
|
content,
|
|
tags=ALLOWED_TAGS,
|
|
attributes=ALLOWED_ATTRIBUTES,
|
|
css_sanitizer=CSSSanitizer(allowed_css_properties=ALLOWED_STYLES),
|
|
strip=True,
|
|
strip_comments=True,
|
|
)
|