Open MihaZupan opened 2 years ago
Tagging subscribers to this area: @dotnet/ncl See info in area-owners.md if you want to be subscribed.
Author: | MihaZupan |
---|---|
Assignees: | MihaZupan |
Labels: | `bug`, `area-System.Net`, `tenet-compatibility` |
Milestone: | - |
Triage:
Uri.TryCreate
will be sufficient for all of these 99.9% cases.
IsWellFormedOriginalString
is needed to be called by the user explicitly. We believe most of the cases use it just because it looks like a good thing to call.IsWellFormedOriginalString
is the fact it rejects implicit file path (e.g. c:\\mydirectory
)Uri.TryCreate
) is not sufficient for them. However, users who use it for no good reason may be getting wrong answers sometimes, causing them pain and wasted time in investigations.Given all the above, it is not worth it to fix it this late of 7.0 release (non-trivial risk, non-trivial cost, medium-benefit). We should address it early in 8.0 to get bake time due to risk of the change.
@karelz @MihaZupan All 4 of those issues closed "in favor of" this one are referring to Uri.IsWellFormedUriString
, not Uri.IsWellFormedOriginalString
, so just to be clear they are not duplicates. I'm not sure if the rationale in the triage comments above applies to both or not:
We have hard time to imagine almost ANY scenario where IsWellFormedOriginalString is needed to be called by the user explicitly. We believe most of the cases use it just because it looks like a good thing to call.
I'm less familiar with IsWellFormedOriginalString
, but IsWellFormedUriString
is static and the use case is fairly obvious: validation. This is the one people are complaining about, and I would suggest you re-open one of the 4 that were closed, or at least provide different rationale for closing them. I'd suggest reopening #21626 as it has the most activity and upvotes. Thanks.
The two methods share the same issues. IsWellFormedUriString
is implemented as
Uri.TryCreate(uriString, uriKind, out result) && result.IsWellFormedOriginalString()
IsWellFormedUriString is static and the use case is fairly obvious: validation.
Right, but was the intended validation exactly what IsWellFormedOriginalString
is doing, or whether a Uri seems valid and can be created via Uri.TryCreate
?
Thanks for the clarification. Karel's comment that "it looks like a good thing to call" is certainly true in the case of IsWellFormedUriString
. Without that knowledge of the implementation details, it seems appropriate when you just want to make sure a user-entered URI string is valid and you don't need a Uri
object in the moment. (Perhaps someone is entering their web site URL in a form and you just want validate and save it, for example.)
I just think the general tone that "we can't imagine why anyone would ever call this" is likely much more applicable to IsWellFormedOriginalString
than it is to IsWellFormedUriString
....again, without knowledge of the implementation. :)
Just to give some context on how people could end up experiencing this problem:
Microsofts OData.NET library uses Uri.IsWellFormedUriString
(matching issue)
In my case it is used to talk to Microsofts Business Central webservices which contain tenant names in their URL that can contain non ASCII characters, spaces etc.. This issue is specific to the library and could be fixed with a workaround but has a big impact on a fraction of users in the meantime.
Example URL:
"http://192.168.0.1:1234/Instance/ODataV4/Company('123-Customer Place Süd-Ost')/"
Thanks for linking the issue. From the linked code, it does seem like this is one of those usages that fall into the We believe most of the cases use it just because it looks like a good thing to call.
category.
The OData
library should consider dropping this check entirely. As-is, it is checking Uri.IsWellFormedUriString(baseUri.AbsoluteUri, UriKind.Absolute)
which should just always be true
, as Uri.AbsoluteUri
is always generating a well-formed string.
That is, this check is doing nothing in this case, except for potentially hitting this false-negative bug.
@karelz
I am not sure if I should open a new issue (this one looks similar enough), but Uri.TryCreate
seems fundamentally broken to me.
Minimal repro (tested on .Net Framework 4.8):
Uri.TryCreate(@"<li>No certified downloads were found for this configuration. To include beta downloads in your search, click <a href='Find.aspx?lang=en-us'>here</a>.</li>", UriKind.RelativeOrAbsolute, out Uri Result);
To my surprise that call returns true
, and the result is... well, see for yourself:
This issue is only talking about IsValidOriginalString, so it's unrelated to your problem.
If you try to create a relative Uri, we'll accept most everything as valid input. If you don't want that, you should likely specify UriKind.Absolute
instead.
This issue is only talking about IsValidOriginalString, so it's unrelated to your problem.
Thanks for clarification, I will create a new issue.
If you try to create a relative Uri, we'll accept most everything as valid input. If you don't want that, you should likely specify
UriKind.Absolute
instead.
What should I do if I want both absolute or relative? Roll my own check? I mean, why is there even an option for relative Uri TryCreate
when there's absolutely no validation for it? I'd expect at least to check against allowed character list and return false for those that aren't (taking also invalid UTF-8 encodings into account).
Just for reference:
Uri.IsWellFormedUriString(@"<li>No certified downloads were found for this configuration. To include beta downloads in your search, click <a href='Find.aspx?lang=en-us'>here</a>.</li>", UriKind.RelativeOrAbsolute);
Returns False, as it should.
@julian94 I understand.
However, the documentation for Uri.TryCreate
does not say what kind of validation (if any) is performed when constructing an Uri
. At the minimum, this should be clarified. I also believe that Uri.TryCreate
should not succeed in creating an invalid Uri
(relative or absolute).
I did create an issue just in case someone would like to close it with wontfix
with extreme prejudice :-)
https://github.com/dotnet/runtime/issues/78381
Sadly, we won't make it in 8.0 due to circumstances. Moving to Future for now.
IsWellFormedOriginalString
(and therefore alsoIsWellFormedUriString
) will report false negatives in the following cases:" <>`^{|}
setEscapeUnescapeIri
removes the escaping from these characters, butCheckCanonical
later reports that they should have been escaped (which they were, but we unescaped them)http://a/%41%22
(%41
isA
(unreserved) and%22
is"
)[0, 1F]
range, the;/?:@&=+$,#[]!'()*%\
set (reserved +%
,\
), the" <>`^
set or outside the IRI rangeEscapeUnescapeIri
removes some escaping andCheckCanonical
then sees a mismatch of escaped, unescaped and non-ASCII charactershttp://a/ű%20
(ü
is non-ASCII and%20
isCase 1. occurred in #70929. Case 2. occurred in #21626, #34031, #37634, #64249, VS Feedback.
My suggestion as a workaround is to not use
IsWellFormedOriginalString
at all. Odds are that in most cases you don't care about the sort of validation that it does anyway. Consider using something like this instead: