|
27 | 27 | from user_agents import parse as user_agents_parse |
28 | 28 | from bleach import clean as bleach_clean |
29 | 29 | from bleach.sanitizer import ALLOWED_TAGS, ALLOWED_ATTRIBUTES |
| 30 | +from bleach.css_sanitizer import CSSSanitizer |
| 31 | +from typing import Optional, Dict, List |
30 | 32 | from version import __version__ as PICBED_VERSION |
31 | 33 | from .log import Logger |
32 | 34 | from ._compat import string_types, text_type, urlparse, is_true |
@@ -573,14 +575,23 @@ def send(self, subject, message, to_addrs, from_name=None): |
573 | 575 |
|
574 | 576 | def bleach_html( |
575 | 577 | 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, |
579 | 581 | ): |
| 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) |
580 | 591 | return bleach_clean( |
581 | 592 | html, |
582 | | - tags=tags, |
583 | | - attributes=attrs, |
| 593 | + tags=_tags, |
| 594 | + attributes=attrs or ALLOWED_ATTRIBUTES, |
584 | 595 | css_sanitizer=css, |
585 | 596 | ) |
586 | 597 |
|
|
0 commit comments