-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
System.Uri does not allow * (or other sub-delims) in hostname #64707
Comments
Tagging subscribers to this area: @dotnet/ncl Issue DetailsDescriptionPer RFC 3987 (and 3986), the host name is allowed to contain certain delimiter characters according to the ABNF grammar:
However, attempting to create a Uri containing these in the host portion throws an exception. Reproduction Steps
Expected behaviorThis is a grammatically valid URI, and should be parsed. Actual behaviorUnhandled exception. System.UriFormatException: Invalid URI: The hostname could not be parsed. Regression?Not a regression AFAICT Known WorkaroundsNo response Configuration.NET 6 Other informationNo response
|
You are probably right in that this is a possible What is the use-case for such a |
In my case, I was using Uri.IsWellFormed to verify that a string is valid URI according to the RFC. As the documentation states:
My code in part of a JSON Schema validator, and the JSON Schema spec specifies the RFC as the definition for valid/invalid values. The user reporting the problem with my code is using this value in their appsettings.json for an ASP.NET Core project for a Kestrel endpoint, e.g. {
"Kestrel": {
"Endpoints": {
"MyHttpsEndpoint": {
"Url": "https://*:5001", // warning on this line
"SslProtocols": [ "Tls12", "Tls13" ],
"Certificate": {
"Path": "<path to .pfx file>",
"Password": "$CREDENTIAL_PLACEHOLDER$"
}
}
}
}
} The JSON Schema for appsettings.json defines the "Url" property here as using the "uri" format defined in the JSON schema spec. If this is out of scope for the System.Uri class to handle, that's fine, I'll can look into finding or writing another parser. I didn't see any remarks in the (admittedly long) documentation about non-conformance to the specifications. |
This also affects the Our use-case was a configuration tool that both reads and writes configuration files that contain host URLs, and the plan was to simply put Our workaround is to assume |
Triage: We admit it would be nice to have ability to parse just according to the RFCs (skipping DNS validation). |
I think we're hitting this issue in our vscode C# extension. We're getting passed a URI like We use URIs here because vscode sends us document identifiers as URI formatted strings. They can represent a real file on disk or a virtual document and we use the URI type to get info on it (e.g. local path if it has one). Original issue - dotnet/vscode-csharp#6256 |
@dibarbet thanks for your details. I think that Uri validation of hostnames likely kicks in here. Do you have a reasonable workaround in your code base? (e.g. treating such special Uris as string) |
I don't have a good workaround unfortunately - the URI conversion comes from the LSP protocol library we use (internal link). This library contains C# type definitions for the LSP spec that we use to deserialize LSP message, and is used in VS, Roslyn, vscode C#, etc. Switching the protocol libraries to use string instead of the URI type would be a large undertaking, for example the base document identifier type, used by almost all other definitions, is based on a URI (internal link). cc also @olegtk as an FYI and to see if he had any opinions. Switching over to string might be a good long term move anyway (due to other issues with encoded paths). But I don't think it will be simple. |
OK, I could imagine that in future versions we could solve it in some way. However, it will be in future versions of .NET, so you will have to wait and upgrade. |
@karelz @MihaZupan Do you guys think something could be done here? This really is unpleasant on our side as we have a lot of APIs designed around System.Uri, and now we're painfully finding out that it doesn't work for all the types of Uris our clients use (like vscode). Thanks! |
To add on to that, while we are able to move the parsing of the URI out from a critical path (e.g. treat it as a string), at some point we do actually need to parse them to extract information like the scheme, file path, or query parameters from the URI strings. Other than writing our own URI parser, there doesn't seem to be a great way to workaround this issue in This does seem to be a pretty common issue for VSCode extensions written in C#. For example O# created it's own URI parser to handle VSCode URIs. And it looks like the bicep extension has feature limitations due to this issue - Azure/bicep#13274 Anyway - just wanted to provide more information on the cost this has on us |
Agreed, we should fix it given the pain it causes. Thanks @CyrusNajmabadi for the detailed info -- it definitely helps in prioritization. |
Key question: If we fix just .NET 10, is it going to help all these scenarios in a way they won't be unhappy too much? (at least not more than now) |
I've sat here for a while and thought about this question, and I still don't think I have a clear answer (at least for us). We ran into this with a .NET Framework application (and a fix there would be cool, but fairly unrealistic), but we also have a very trivial workaround (just string-replace before/after with something that is valid - 2 lines difference). Plus, it only affects utility applications meant to give our end users a nicer configuration UI rather than a bare JSON or XML file (which, at this point, have nothing stopping us from upgrading them to .NET 8 or .NET 10 when it's out). So, TL;DR: I don't know. But: I'd like to request that |
A fix in .NET 10 will definitely help for Roslyn's VSCode scenarios (we can upgrade our server to .NET 10 when it releases). And most of the problematic URIs we get come from our VSCode side. It won't help us in VS at the moment, but we also rarely encounter these kinds of URIs in VS. And adding to .NET framework I assume is not feasible. |
Description
Per RFC 3987 (and 3986), the host name is allowed to contain certain delimiter characters according to the ABNF grammar:
However, attempting to create a Uri containing these in the host portion throws an exception.
Reproduction Steps
Expected behavior
This is a grammatically valid URI, and should be parsed.
Actual behavior
Regression?
Not a regression AFAICT
Known Workarounds
No response
Configuration
.NET 6
Other information
No response
The text was updated successfully, but these errors were encountered: