Skip to content

Commit 6f855c2

Browse files
committed
allow extra html field
1 parent 5bcd017 commit 6f855c2

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
# 上传最大容量限制,单位MB
3737
"ProxyFix": is_true(envs.get("sapic_proxyfix")),
3838
# 信任代理标头
39+
"AllowTags": envs.get("sapic_allowtags", ""),
40+
# 站点设置部分参数额外允许的HTML标签的属性
41+
"AllowStyles": envs.get("sapic_allowstyles", ""),
42+
# 站点设置部分参数额外允许的HTML标签的样式
3943
}
4044

4145

src/utils/tool.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
from user_agents import parse as user_agents_parse
2828
from bleach import clean as bleach_clean
2929
from bleach.sanitizer import ALLOWED_TAGS, ALLOWED_ATTRIBUTES
30+
from bleach.css_sanitizer import CSSSanitizer
31+
from typing import Optional, Dict, List
3032
from version import __version__ as PICBED_VERSION
3133
from .log import Logger
3234
from ._compat import string_types, text_type, urlparse, is_true
@@ -573,14 +575,23 @@ def send(self, subject, message, to_addrs, from_name=None):
573575

574576
def bleach_html(
575577
html,
576-
tags=ALLOWED_TAGS,
577-
attrs=ALLOWED_ATTRIBUTES,
578-
css=None,
578+
tags: Optional[List[str]] = None,
579+
attrs: Optional[Dict[str, List[str]]] = None,
580+
css: Optional[CSSSanitizer] = None,
579581
):
582+
"""清洗HTML,设置中仅允许部分标签、属性和样式。"""
583+
from config import GLOBAL
584+
585+
_tags = tags or ALLOWED_TAGS
586+
_ext_tags = parse_valid_comma(GLOBAL["AllowTags"])
587+
_ext_styles = parse_valid_comma(GLOBAL["AllowStyles"])
588+
_tags.extend(_ext_tags)
589+
if isinstance(css, CSSSanitizer) and _ext_styles:
590+
css.allowed_css_properties.extend(_ext_styles)
580591
return bleach_clean(
581592
html,
582-
tags=tags,
583-
attributes=attrs,
593+
tags=_tags,
594+
attributes=attrs or ALLOWED_ATTRIBUTES,
584595
css_sanitizer=css,
585596
)
586597

0 commit comments

Comments
 (0)