-
Notifications
You must be signed in to change notification settings - Fork 47.2k
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
Rename dangerouslySetInnerHTML #2134
Comments
|
I don't mind |
How about Plus |
@rymohr The reason for If the HTML is in any way invalid then you risk breaking the entire app, if the HTML also contains anything user-supplied you've very likely enabled a very serious security issue. Personally I think that means Also, here's the justification for |
It's not dangerous though if you're passing the html (user generated or markup generated) through a sanitizer first. The sanitizer I wrote both sanitizes the html and makes sure it's well formed. I understand the operation could be dangerous, but it's better to warn the developer in a way that doesn't make the library awkward to use. Why not just log a warning message to the console each time it is used in development mode instead? |
It does make sense to rename this property to something that doesn’t sound like a function. ⁂ It does not make sense to drop the “scary” part of the name. “It’s not dangerous if…” If something can be dangerous depending on how it’s used, then… it’s dangerous. Matches are dangerous, even though they can be used safely. Knives. Ovens. My motorcycle. Listen, I have a shameful and distant past as a PHP developer. PHP is absolutely chock-full of danger, and was even more dangerous in the past. Start with the fact that it outputs unescaped HTML by default, and the most standard-looking database library encourages users to naively interpolate strings into queries (hello, Bobby Tables). Now, concatenating strings into a query is not dangerous if you have escaped or safely generated those strings, but that doesn’t mean it isn’t dangerous, and it doesn’t mean that everyone writing that code knows that. When I taught myself PHP+MySQL from a printed book in early high school, I didn’t know about email header injection, and I was furious to find out that the code I wrote by following the book left my contact form open to be used for sending spam to arbitrary recipients! Now, don’t think that I have conflated making the right thing easy (read: escape by default & well-designed APIs) with taking the additional precaution of warning users of dangerous code. I am aware you are only debating the latter in this thread, and so am I. I think both are the right thing to do. I think you may be exhibiting a common programmer belief that I think is dangerous: The idea that since every developer should know X, Y, and Z before hacking in language/library/system A, that every developer working with A does know X, Y, and Z. I assure you that assumption is not true, and there are astounding numbers of people — some call themselves developers, others don’t — who simply google for a solution and paste code into their app, tweaking it until it “works”. And for those people, they should see the word “danger” in their code when they are pasting something that can open the only XSS hole in React. Any other attitude is setting those less experienced than us up for failure. React team did a smart thing with this name, and deserve applause. Congrats — you are writing ethical software. |
Well said, @alanhogan! Completely agreed. |
Looks like #2257 is the latest iteration of this issue |
I think The function createMarkup() { return {__html: getUsername()}; }
dangerousInnerHTML={createMarkup()}
// vs
dangerousInnerHTML={{__html: getUsername()}
// vs
dangerousInnerHTML={getUsername()} Leading underscores usually mean you're working with a private api that might change without warning. Not that what you're working with is inherently dangerous. |
I tend to agree with @rymohr: since the prop name is **danger**ouslySetInnerHTML, making the API itself awkward with On the other hand, it does prevent you from passing in an object you don't control without transforming it first, so maybe that's good? |
@appsforartists |
Really the "dangerous" is in the wrong place and it should be |
@spicyj 👍 (I guess |
No movement on this in the last couple years. I'm not sure it'd be worth the API churn, and it doesn't seem like a frequently-requested change. What do you think @gaearon @nhunzaker? If we wanted to make this change, it might be worth doing in the next major release. |
Let’s close for inactivity. We can come back to this if it organically comes up again but I don’t think this has a high priority. |
From #1515, thoughts?
dangerousInnerHTML
,unsafeInnerHTML
, (disasterousInnerHTML
:)) or something else? (I prefer the sound ofdangerous
)It also makes sense to add a
dangerousDefaultInnerHTML
(default
is the word we use elsewhere) which could be very useful for content-editables to avoid "hacks", etc.cc @sebmarkbage @zpao
The text was updated successfully, but these errors were encountered: