Steuere das Herunterladen von Originalen je Rolle, benenne Dateien richtig und zeige HTML-Mails gesichert im Sandbox-Rahmen.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0a5daf6713
commit
075525ee17
26 files changed
+494
-71
No files matched your search
+45
-4
@@ -23,8 +23,21 @@ _ALLOWED_TAGS = {
|
||||
"a", "p", "br", "div", "span", "strong", "em", "b", "i", "u",
|
||||
"ul", "ol", "li", "table", "thead", "tbody", "tr", "th", "td",
|
||||
"blockquote", "pre", "code", "h1", "h2", "h3", "h4", "h5", "h6", "img",
|
||||
"font", "center", "hr", "small", "sub", "sup", "s", "strike", "caption", "tfoot", "section", "article",
|
||||
}
|
||||
_VOID_TAGS = {"br", "img"}
|
||||
_VOID_TAGS = {"br", "img", "hr"}
|
||||
# Formatting a mail may keep. Nothing here loads anything, positions an element or runs code.
|
||||
_STYLE_PROPS = {
|
||||
"color", "background-color", "font-family", "font-size", "font-weight", "font-style", "text-align",
|
||||
"text-decoration", "line-height", "vertical-align", "white-space", "letter-spacing",
|
||||
"margin", "margin-top", "margin-bottom", "margin-left", "margin-right",
|
||||
"padding", "padding-top", "padding-bottom", "padding-left", "padding-right",
|
||||
"border", "border-top", "border-bottom", "border-left", "border-right", "border-color", "border-width",
|
||||
"border-style", "border-collapse", "width", "max-width", "height", "text-indent", "list-style-type",
|
||||
}
|
||||
_STYLE_VALUE = re.compile(r"^[#%a-z0-9 .,()\-+'\"]{1,100}$", re.IGNORECASE)
|
||||
_STYLE_DENIED = re.compile(r"url|expression|var\(|attr\(|image|@|javascript|behavior|\\", re.IGNORECASE)
|
||||
_COLOR = re.compile(r"^(#[0-9a-f]{3,8}|[a-z]{3,20}|rgb\(\s*\d{1,3}\s*,\s*\d{1,3}\s*,\s*\d{1,3}\s*\))$", re.IGNORECASE)
|
||||
_SAFE_LINK = re.compile(r"^(https?://|mailto:)", re.IGNORECASE)
|
||||
_DATA_IMAGE = re.compile(r"^data:image/(?:png|jpeg|gif|webp);base64,[a-z0-9+/=\s]+$", re.IGNORECASE)
|
||||
|
||||
@@ -275,17 +288,45 @@ def sanitize_html(value: str, images: dict[str, str] | None = None) -> str:
|
||||
return "".join(cleaner.parts).strip()
|
||||
|
||||
|
||||
def _safe_style(value: str) -> str:
|
||||
kept = []
|
||||
for declaration in (value or "").split(";"):
|
||||
prop, _, raw = declaration.partition(":")
|
||||
prop, raw = prop.strip().lower(), raw.strip().replace("!important", "").strip()
|
||||
if prop not in _STYLE_PROPS or not raw or _STYLE_DENIED.search(raw) or not _STYLE_VALUE.match(raw):
|
||||
continue
|
||||
kept.append(f"{prop}: {raw}")
|
||||
return "; ".join(kept)
|
||||
|
||||
|
||||
def _clean_attrs(tag: str, attrs, images: dict[str, str]) -> str | None:
|
||||
kept: list[str] = []
|
||||
for key, value in attrs:
|
||||
name = (key or "").lower()
|
||||
if name.startswith("on") or name in {"style", "srcset"}:
|
||||
if name.startswith("on") or name == "srcset":
|
||||
continue
|
||||
if name == "style":
|
||||
style = _safe_style(value or "")
|
||||
if style:
|
||||
kept.append(f' style="{html.escape(style, quote=True)}"')
|
||||
continue
|
||||
if name in {"bgcolor", "color"} and _COLOR.match((value or "").strip()):
|
||||
kept.append(f' {name}="{html.escape(value.strip(), quote=True)}"')
|
||||
continue
|
||||
if name in {"width", "height"} and re.fullmatch(r"\d{1,4}%?", (value or "").strip()):
|
||||
kept.append(f' {name}="{value.strip()}"')
|
||||
continue
|
||||
if name in {"border", "cellpadding", "cellspacing", "size"} and str(value or "").strip().isdigit():
|
||||
kept.append(f' {name}="{int(value)}"')
|
||||
continue
|
||||
if name in {"align", "valign"} and (value or "").strip().lower() in {"left", "right", "center", "justify", "top", "middle", "bottom"}:
|
||||
kept.append(f' {name}="{value.strip().lower()}"')
|
||||
continue
|
||||
if tag == "a" and name == "href":
|
||||
target = (value or "").strip()
|
||||
if not _SAFE_LINK.match(target):
|
||||
continue
|
||||
kept.append(f' href="{html.escape(target, quote=True)}"')
|
||||
kept.append(f' href="{html.escape(target, quote=True)}" title="{html.escape(target, quote=True)}"')
|
||||
elif tag == "img" and name == "src":
|
||||
source = (value or "").strip()
|
||||
if source.lower().startswith("cid:"):
|
||||
@@ -301,7 +342,7 @@ def _clean_attrs(tag: str, attrs, images: dict[str, str]) -> str | None:
|
||||
if tag == "img" and not any(item.startswith(" src=") for item in kept):
|
||||
return None
|
||||
if tag == "a":
|
||||
kept.append(' rel="noreferrer"')
|
||||
kept.append(' rel="noreferrer noopener" target="_blank"')
|
||||
return "".join(kept)
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user