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

Avoid implicit innerHTML assignments #2636

Closed
blois opened this issue Dec 5, 2019 · 12 comments
Closed

Avoid implicit innerHTML assignments #2636

blois opened this issue Dec 5, 2019 · 12 comments

Comments

@blois
Copy link
Contributor

blois commented Dec 5, 2019

Since innerHTML assignments can lead to unintended side effects it should be clear to consumers of these APIs when data may lead to an innerHTML assignment.

An example of an assignment which should probably be clearer:

import ipywidgets as widgets
widgets.Checkbox(description="<marquee>this is html</marquee>")

Potential ways to address this:

  • Switch to textContent assignments. This is the easiest to audit and in many cases may be what users expect.
  • Use a sanitizer to sanitize the text before assigning to innerHTML. Good for cases where you want to allow rich content but not arbitrary code execution.
  • Require an opt-in to use innerHTML or to disable sanitization.

From a quick glance, a few places that could be worth changing:

@jasongrout
Copy link
Member

Thanks. This has also bothered me for a long time too.

@jasongrout jasongrout added this to the 8.0 milestone Dec 6, 2019
@jasongrout
Copy link
Member

I think a nontrivial fraction of users appreciate being able to do simple styling (bold, italics, colors, etc.) on things like descriptions or checkbox. The widget_selection example is sanitized a few lines above.

@jasongrout
Copy link
Member

Require an opt-in to use innerHTML or to disable sanitization.

I like this thought. I think the vast majority of cases are served by textContent. It's nice to have some formatting capability when needed, and for backwards compatibility at this point. I think we probably always ought to sanitize in controls, and have the opt-in to using innerHTML.

@jasongrout
Copy link
Member

Is there anyone willing to push this forward in the next week or so for 8.0?

@zerline
Copy link
Contributor

zerline commented Feb 11, 2020

Is there anyone willing to push this forward in the next week or so for 8.0?

If nobody more qualified is available, I can find the time.

@blois
Copy link
Contributor Author

blois commented Feb 11, 2020

I was hoping to be able to get a chance to work on this but have not been able to carve out any time yet.

I agree that a default of sanitizing with an option to skip sanitization would be a reasonable approach.

@jasongrout
Copy link
Member

Do you think we ought to default to using textContent, or innerHTML with sanitization and perhaps very limited whitelist (like font weight, colors, italics, underline, etc.)?

@blois
Copy link
Contributor Author

blois commented Feb 12, 2020

On a clean implementation I'd definitely recommend textContent as it avoids surprises when the text happens to contain chars such as <. Since this is a breaking change it depends on how often people are using the formatting capabilities.

@jasongrout
Copy link
Member

jasongrout commented Feb 12, 2020

@zerline - you may be the one I know that is most using formatting in descriptions, so it would be especially great to hear your thoughts.

Here is one idea: switch to using textContent by default for descriptions (so no formatting, but no surprises either, just plain text), and have a new description_html flag that when it is true, switches to using innerHTML (hopefully with some sanitization)?

@zerline
Copy link
Contributor

zerline commented Feb 14, 2020

@zerline - you may be the one I know that is most using formatting in descriptions

actually I don't, as my inputs usually have no description ..

so it would be especially great to hear your thoughts.

From my experience as a user and developer: We sometimes see icons within input descriptions. Or links to a footnote (anchors). Styling options should be accepted too (<i>, <b>, ... and <style>)

@vidartf
Copy link
Member

vidartf commented Aug 17, 2021

Did #2785 close this?

@blois
Copy link
Contributor Author

blois commented Aug 17, 2021

Yes- great work here, thanks @zerline!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants