-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add CSSSanitizer to sanitize_html #731
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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"]} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [sanity check] |
||
css_sanitizer = CSSSanitizer(allowed_css_properties=["color", "font-weight"]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [suggestion] I wonder if it's worth supporting the default bleach css properties, similar to extending [suggestion x2] Regardless, for styling, I might recommend considering relying on the Paragon CSS utility classes vs. hardcoding any colors, font-weights, etc. (e.g., |
||
|
||
# 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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nit] Looks like this accepts a dict vs a list, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
css_sanitizer=css_sanitizer, | ||
) | ||
|
||
# Use bleach.linkify to ensure no javascript: links in <a> tags | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I believe
{"span"}
is equivalent toset({"span"})
.