-
Notifications
You must be signed in to change notification settings - Fork 46.9k
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
False-positive security precaution warning (javascript:
URLs)
#16382
Comments
There is no way for React to know if |
My thinking is that we wouldn't want to add more runtime checks that slow down the app to account for every possible "safe" case. There's many variations on this pattern, and checking all of them will be costly. Checking just a few will annoy people that their particular pattern isn't checked against. So we're choosing to completely ban |
@gaearon If there's no confidence that there're real security issues caused by using
I wouldn't like to fix hundreds of warnings by replacing |
React has always added deprecation warnings in mainline minor releases by default. This is not a new policy. It's as old as React itself. It's strict mode that is new — but even there, we can't keep pushing things down to strict mode forever. Strict mode is more about preparing for Concurrent Mode, and is not about security features. If something is too noisy, strict mode won't help it because then nobody would use it. And since we actively want to disable this behavior in next major, it shouldn't be hidden behind a mode.
There are very real security issues that are being caused by this in React apps. Yes, there are a few safe concrete examples, but relying on them is a problem by itself. For example, it will prevent you from adopting Trusted Types later (which adds a lot more XSS defense). So we need to weed out this whole pattern from the ecosystem together. This is expected to take some work and we don't want to sugarcoat it.
There shouldn't be "hundreds of warnings". Have you run this code? The intention is to only warn once: react/packages/react-dom/src/shared/sanitizeURL.js Lines 41 to 42 in 868d02d
If you're seeing hundreds of warnings, something is very wrong and we need more details. See also this conversation: https://github.com/facebook/react/pull/15047/files#r264320611.
We're "allowing" that by logging a deprecation. You're free to keep using this feature for now, but you should be aware it would be eventually removed in a major version. Silencing the warning doesn't change that, and makes it harder to find where it needs to be fixed. |
The deprecation policy doesn't look consistent, some features have been deprecating for years:
Others are much faster:
I think it has to be clarified better in official guides, there're no references to Concurrent Mode in the doc. If you don't like Strict Mode, you can introduce CSP Mode or Security Mode.
There're hundreds of warnings in Jest unit tests, I can create a repro repository if you need. It's even worse in a real application - since you only see the first warning, you never know how many issues you need to fix until you fix them all. |
If there is a viable migration path, we usually remove the old API in the next major. In case of UNSAFE lifecycles, note that the old names will be removed in 17. But there is no automated “find and replace” migration path for the long tail which is why UNSAFE lifecycles will keep working. In case of JavaScript URLs we believe there is a viable migration path. It is easy to find them in your codebase (by searching for
Jest intentionally doesn’t preserve module state between different test runs. I understand it’s annoying. But how many actual components is this firing in? You probably have one or two components used all over the place which you can fix once and forget about it. And as I mentioned earlier, this pattern is easy to grep the codebase for so you shouldn’t have troubles finding the callsites. As the last resort you can always monkeypatch the console in your tests to filter it out until you’re ready to take it on. But I’d like to understand better whether you’re just frustrated by the warning, or if it’s legitimately challenging to fix in your project — and why. |
Unfortunately this assumption isn't correct.
It's not true: some
I wouldn't force developers to monkeypatch console, it doesn't looks like a solid solution.
There're real XSS issues caused by passing dynamic values to <a href={url}>Unsafe Link</a> And safe, static attributes that cannot be exploited by attackers: <a href="javascript:...">Safe Link</a> Ideally I would distinguish static (safe) and dynamic (unsafe) values. If it's not possible in React, I'd like to have a solution that allow me to turn on it and fix issues feature by feature. There's a real project with ~500.000 loc of jsx, there're ~100 static |
Same warning, i use |
I believe that specifying a fake link without a Therefore we recommend this pattern instead:
or
|
@sebmarkbage There're different styles applied for a link with/without |
Ran into this today, i would like to point out that using |
This is a fairly common pattern (for example) when devs want to attach behavior to anchor/href styles. Those cases are probably 99% of |
My use case is similar to many of the comments here, The solution of <a href="#" onClick={ev => {
ev.preventDefault();
this.props.onClick(ev);
return false; // old browsers, may not be needed
}} /> Compare to the simple <a href="javascript:void(0)" onClick={this.props.onClick} /> @gaearon I think this should be re-opened for discussion. |
Is there a React recommended way to get the previous behavior of
Allowing urls that are exactly |
Is there a way to only wrap I'd like to render this component in Having something like Are there any workarounds if using |
can someone please tell me how to disable this warning in dev mode ?? please :) now for what's to come, I +1 @IllusionMH on |
The problem with using the |
I also use the history intelligently so I can't use How do you use |
I started using |
how does one use dangerouslySetInnerHTML for this? all the documentation I've read says you use that feature with the __html key to set InnerHTML. I don't want to set innerHTML, I want to set the href attribute. +1 to whoever suggested a dangerouslySetHref prop... |
You'd need to use <span dangerouslySetInnerHTML={{__html: '<a ... ></a>' }} /> You can also do something like <a ref={node => {
if (node) {
// set its attributes via DOM API
}
}}>...</a> What is your actual use case for this? |
You can't. And you shouldn't. The solution is to not use |
|
It will be a hard error in the next major version. While you can always override |
The ref solution (#16382 (comment)) should work for you. |
<a href="#" onClick={ev => {
ev.preventDefault();
this.props.onClick(ev);
return false; // old browsers, may not be needed
}} /> First, the Second, yes, it would be two lines instead of one. That's the downside. The upside is that your app would not be vulnerable to common security issues once we start enforcing this. So it seems worth it. |
@gaearon thanks! In the end we've used |
I think we have a conclusion here:
We're not going to add exceptions for this rule, so I hope the above information helps. I'm going to lock this as resolved because we're going in circles and because people coming from search might miss this information. If you have a problem that you believe isn't addressed here, please file a new issue. |
Do you want to request a feature or report a bug?
Report a bug.
What is the current behavior?
React 16.9.0 deprecates
javascript:
URLs (@sebmarkbage in #15047). It was motivated by preventing XSS vulnerability that can be used by injecting client-side scripts:The following code cannot be exploited by attackers, it cannot be used to inject XSS:
React 16.9 reports the security precaution warning for the example:
What is the expected behavior?
I would expected that security precaution warnings aren't reported for values that cannot be controlled by attackers.
There were also concerns regarding common patterns like
javascript:void(0)
, see @gaearon comment:If there're tons of reported security issues, you definitely ignore something important.
For reference: Angular’s cross-site scripting security model
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
React 16.9.0 is affected. 16.8.6 doesn't report the warning.
The text was updated successfully, but these errors were encountered: