Skip to content
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

Merged
merged 2 commits into from
Oct 28, 2024
Merged

Add CSSSanitizer to sanitize_html #731

merged 2 commits into from
Oct 28, 2024

Conversation

macdiesel
Copy link
Contributor

Description

Bleach documentation suggests that if you want to use stye elements in your HTML tags you must also include a CSS sanitizer when calling bleach.clean.

https://bleach.readthedocs.io/en/latest/clean.html#allowed-attributes-attributes

Note

If you allow style, you need to also sanitize css. See [Sanitizing CSS](https://bleach.readthedocs.io/en/latest clean.html#clean-chapter-sanitizing-css) for details.

@macdiesel macdiesel changed the title chore: Add CSSSanitizer to sanitize_html Add CSSSanitizer to sanitize_html Oct 28, 2024
@macdiesel macdiesel force-pushed the bbeggs/VSF_expiration branch from 9436e63 to 7fef60c Compare October 28, 2024 18:57
Copy link
Member

@brobro10000 brobro10000 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving to unblock


# 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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I stand corrected, the docs are stating a list, despite examples.
Screenshot 2024-10-28 at 3 04 18 PM


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"]}
Copy link
Member

@adamstankiewicz adamstankiewicz Oct 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[sanity check] className is from JSX. When React renders JSX, it converts to class in the resulting HTML. I'm not sure including the JSX className attribute in this HTML field would have an effect, as class does, when it renders. The dangerouslySetInnerHTML assumes the input is raw HTML; it would not transpile JSX -> HTML, afaik.

allowed_attrs = {"*": ["className", "class", "style", "id"]}
css_sanitizer = CSSSanitizer(allowed_css_properties=["color", "font-weight"])
Copy link
Member

Choose a reason for hiding this comment

The 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 bleach.ALLOWED_TAGS. Looks like not supplying allowed_css_properties would fallback to the defaults.

[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., font-weight-bold, text-brand, etc.) within the custom HTML.


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
Copy link
Member

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 to set({"span"}).

@macdiesel macdiesel force-pushed the bbeggs/VSF_expiration branch from 95eb176 to 3b2f8a7 Compare October 28, 2024 19:24
@macdiesel macdiesel force-pushed the bbeggs/VSF_expiration branch from 3b2f8a7 to c2b2aab Compare October 28, 2024 19:27
@macdiesel macdiesel merged commit 72316ce into master Oct 28, 2024
5 checks passed
@macdiesel macdiesel deleted the bbeggs/VSF_expiration branch October 28, 2024 19:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants