import nh3 SAFE_CSS_PROPERTIES = { 'font-size', 'font-family', 'font-weight', 'font-style', 'color', 'background-color', 'vertical-align', 'text-decoration', 'text-decoration-line', 'text-decoration-skip-ink', 'line-height', 'margin-top', 'margin-bottom', 'padding-inline-start', 'white-space', 'white-space-collapse', 'text-wrap-mode', 'list-style-type', 'font-variant-numeric', 'font-variant-east-asian', 'font-variant-alternates', 'font-variant-position', 'font-variant-emoji', } SAFE_TAGS = { 'b', 'i', 'strong', 'em', 'u', 'strike', 'a', 'br', 'ol', 'ul', 'li', 'span', 'div', 'p', 'font', } SAFE_ATTRS = { 'a': {'href', 'target', 'rel'}, 'span': {'style', 'id', 'dir'}, 'div': {'style', 'id'}, 'p': {'style', 'id', 'dir', 'role'}, 'ol': {'style', 'dir'}, 'ul': {'style', 'dir'}, 'li': {'style', 'dir', 'aria-level'}, 'font': {'color', 'face'}, } SAFE_PROTOCOLS = {'http', 'https'} def sanitize_relationship_notes(html: str) -> str: """ Sanitize the relationship notes HTML by allowing only a restricted set of safe tags, attributes, and protocols. Args: html (str): Raw HTML string to sanitize. Returns: str: Cleaned HTML where disallowed tags and attributes are removed. """ return nh3.clean( html, tags=SAFE_TAGS, attributes=SAFE_ATTRS, url_schemes=SAFE_PROTOCOLS, link_rel=None, filter_style_properties=SAFE_CSS_PROPERTIES, )