Skip to content

Commit

Permalink
chore: Add CSSSanitizer to sanitize_html
Browse files Browse the repository at this point in the history
  • Loading branch information
macdiesel committed Oct 28, 2024
1 parent 5468a2d commit 7fef60c
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions license_manager/apps/subscriptions/sanitize.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import bleach

from bleach.css_sanitizer import CSSSanitizer

def sanitize_html(html_content):
"""
Sanitize HTML content to allow only safe tags and attributes,
while disallowing JavaScript and unsafe protocols.
"""
# Define allowed tags and attributes
allowed_tags = bleach.ALLOWED_TAGS # Allow all standard HTML tags
allowed_tags = set.union(bleach.ALLOWED_TAGS, set({"span"})) # Allow all standard HTML tags
allowed_attrs = {"*": ["className", "class", "style", "id"]}
css_sanitizer = CSSSanitizer(allowed_css_properties=["color", "font-weight"])

# Clean the HTML content
sanitized_content = bleach.clean(
html_content,
tags=allowed_tags,
attributes=allowed_attrs,
strip=True, # Strip disallowed tags completely
protocols=["http", "https"], # Only allow http and https URLs
protocols=["http", "https"], # Only allow http and https URLs,
css_sanitizer=css_sanitizer,
)

# Use bleach.linkify to ensure no javascript: links in <a> tags
Expand Down

0 comments on commit 7fef60c

Please sign in to comment.