-
Notifications
You must be signed in to change notification settings - Fork 31
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
extend() static and instance method for Sanitizer #229
Comments
The concept of using a pre-configuration, and then allowing to modify it slightly with adding allowlists and blocklists is great and really necessary for practical use. The use cases will be generally the same, but often slightly different in the details, so this is exactly the right solution. Personally, I think this is going to be so common that I would make the
directly part of the options/parameters of the standard sanitize function, not via an extra |
In the last meeting, we briefly discussed which options we might have here. Basic designs I can think of:
|
My current favourite is adding restrict/extend to the config object: So if you want to add e.g. When trying to write down spec-level rules for how Converting between allow- and remove-lists requires some definition of "all", and we currently use the spec-derived list of all known elements (or attributes) for that. That works, except for unknown elements (... or attrs). If we fix this, we should be able to arbitrarily convert between the different forms. If, instead, we record the set of 'known' elements in the config object then we can losslessly convert between allow- and block-lists. If on top of that we have a bool The result would something like:
If we think it's easier to use, one could also adopt set operations directly: union; intersection; difference; negate. |
I don't think we necessarily need The idea I had is that this method is passed the set of changes you want to make to the current policy. I could see supporting these operations (probably in the form of dictionary members that have very similar values to the SanitizerConfig dictionary):
|
Thanks, I now understand your proposal much better. I like that this specifies operations on an existing data structure, but in one go (rather than many individual calls). That indeed doesn't need separate A minor nitpick: I think you have a pretty much complete list of set/add/remove operations; except for elements w/ attributes, which only has a setter. Not sure if that's much of a use case, though. :) I do think we ended up with quite different designs: In my design, a sanitizer config is basically a mapping of DOM nodes to E.g. If we take one of the use cases from the meeting, disallowing form content: Someone needs to describe what constitutes "form content". In my design, that's a config. And anyone can remove that from theirs (or from the builtin) with Some examples: const forms = { elements: ["form", "input", …] }
const fetchy = { elements: [{name: "img", removeAttributes: ["src", "srcset"]}, {name: "input", removeAttributes: ["formaction"]}, … ]}
const basic = { elements: ["p", "span", "b", "em"] }
// Default, but without forms or stuff that fetches.
new Sanitizer().restrict(forms).restrict(fetchy);
// Allow basic formatting and forms, but nothing else.
const nothing = new Sanitizer({elements: [], attributes; []});
nothing.extend(basic).extend(forms); // Only basic content and forms.
// Allow forms, but only the forms stuff that doesn't cause fetches.
new Sanitizer().extend(forms).restrict(fetchy); I think this allows for nice separation of concerns, where anyone can build configs for particular subsets of markup they find interesting, and makes it possible for everyone else to combine multiple such concerns in a way that yields predictable and useful results. |
That's fair. And that would allow for |
This has been fixed by 7e2f127. |
To address the problem in #228 I suggest we add an
extend()
static (and instance) method that takes a configuration as argument. However, in this configuration the allow and blocklists serve as additions and subtractions to the existing configuration. (So you can have bothelements
andblockElements
for instance.)The
extend()
static method is a convenience for constructing a Sanitizer object without any argument passed (giving you the default) and then invoking the instanceextend()
method upon that with the argument passed.The text was updated successfully, but these errors were encountered: