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 style attr sanitizer #535

Closed
wants to merge 3 commits into from
Closed

Add style attr sanitizer #535

wants to merge 3 commits into from

Conversation

g-k
Copy link
Collaborator

@g-k g-k commented May 15, 2020

As discussed in #248, we'd like to move away from the regex based CSS sanitizer but don't necessarily have a maintained CSS parser with Python 2 support to switch to (also some users don't necessarily want full CSS parsing).

Changes in this PR:

  • add a css_style_attr_sanitizer param/arg to bleach Cleaner and BleachSanitizerFilter to make it possible to override sanitize_css without changing Cleaner.clean
  • update the arbitrary style goal

We'd then cut a major release that:

r? @jdufresne

cc @willkg @peterbe

@g-k g-k requested a review from jdufresne May 15, 2020 16:22
@@ -1,6 +1,21 @@
Bleach changes
==============

Version 3.2.0.dev0 (May 15th, 2020)
Copy link
Member

Choose a reason for hiding this comment

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

I only set the date here if it's been released. Otherwise it gets confusing in the documentation as to what was released and what's in the master branch that hasn't been released, yet.

So for this, I'd use "In development" or something along those lines.

@@ -18,9 +18,9 @@


# yyyymmdd
__releasedate__ = '20200429'
__releasedate__ = '20200515'
Copy link
Member

Choose a reason for hiding this comment

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

I only set the date here for releases. When it's in development, I leave this as the empty string.

Using different CSS parser and sanitizer with `css_style_attr_sanitizer`
------------------------------------------------------------------------

The argument `css_style_attr_sanitizer` can ``bleach.sanitizer.Cleaner`` be used
Copy link
Member

Choose a reason for hiding this comment

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

This sentence doesn't parse in my head. Do you mean something like this?

The argument css_style_attr_sanitizer in bleach.sanitizer.Cleaner can be used ..


>>> from bleach import Cleaner

>>> def sanitize_css(style):
Copy link
Member

Choose a reason for hiding this comment

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

I'd rename this to my_sanitize_css to make it clearer it's the overridden one rather than the Cleaner instance method.

site's authors or users consider customization instead of defacement.

Consequently, bleach requires you to opt in if you want your users to
be able to change nearly anything in a ``style`` attribute.
Copy link
Member

Choose a reason for hiding this comment

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

This is much more useful than what we had before. 👏

@willkg
Copy link
Member

willkg commented May 22, 2020

I like this, but I wonder if the ongoing "Bleach does it right by default but then lets users do whatever with warnings of dire but vague consequences" architecture changes we're making should also be accompanied by something that can help users know when their overrides create bad situations.

Could we create a test harness that people could pass a cleaner to and it helps them figure out if they've got vulnerabilities?

Maybe we can base it on the OWASP-derived test cases we've got now? (https://github.com/mozilla/bleach/tree/master/tests/data) Maybe we can base it on the website harness we've got? Maybe there's a page safety site out there already we could use?

I'm just wondering out loud. There's no way users on average understand the problem domain enough to know a bad decision when they're overriding Bleach thing.

@g-k
Copy link
Collaborator Author

g-k commented Jun 11, 2020 via email

@willkg
Copy link
Member

willkg commented Feb 7, 2022

I don't think we want to make these changes.

In another PR, @g-k mentioned reevaluating what it is we're protecting against with CSS sanitization and moving forward from there.

I do like the idea of:

take a cue from https://cryptography.io/en/latest/#layout and split the
API into a safe and potentially a more stable "recipes" layer (just
bleach.clean, bleach.linkify, and maybe some other top-level functions
taking a limited subset of args) and a more dangerous "hazmat" layer with
all the knobs and buttons to tweak and override things. Security bugs
against the recipes layer would be higher severity.

However, that's a big backwards-incompatible project, so I won't have time for that any time soon.

I wrote up issue #633 to cover redoing css handling.

@willkg willkg closed this Feb 7, 2022
@g-k g-k deleted the add-style-attr-sanitizer branch February 8, 2022 13:28
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.

None yet

2 participants