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

Assembly.CodeBase does not produce a correctly escaped URI #38224

Closed
archanasoni opened this issue Jun 10, 2020 · 8 comments
Closed

Assembly.CodeBase does not produce a correctly escaped URI #38224

archanasoni opened this issue Jun 10, 2020 · 8 comments

Comments

@archanasoni
Copy link

Hi,

We have a customer case where their Application path contains '#'.
For example path is:
C:/Users/asoni/source/repos/Demo#App/DemoApp
and we get path for IBM.Data.DB2.Core.dll using below code:
typeof(DB2Connection).Assembly.CodeBase
Output is:

file:///C:/Users/asoni/source/repos/Demo#App/DemoApp/bin/Debug/netcoreapp3.1/IBM.Data.DB2.Core.dll

Now we use Uri class to get physical path:
string assemLoc = (new Uri(typeof(DB2Connection).Assembly.CodeBase)).LocalPath;

Output is:

C:\Users\asoni\source\repos\Demo

Expected output:

C:/Users/asoni/source/repos/Demo#App/DemoApp/bin/Debug/netcoreapp3.1/IBM.Data.DB2.Core.dll

If we don't have # in Application path it works perfectly fine..

Could you share what is the reason behind this behavior and How come we can overcome this issue if we want to allow # in path ?

@marcpopMSFT marcpopMSFT self-assigned this Jun 10, 2020
@marcpopMSFT marcpopMSFT transferred this issue from dotnet/sdk Jun 22, 2020
@jaredpar jaredpar transferred this issue from dotnet/roslyn Jun 22, 2020
@Dotnet-GitSync-Bot
Copy link
Collaborator

I couldn't figure out the best area label to add to this issue. Please help me learn by adding exactly one area label.

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the untriaged New issue has not been triaged by the area owner label Jun 22, 2020
@scalablecory
Copy link
Contributor

Doesn't appear to be an issue with Uri itself. Can you share a minimal repro?

I tried:

var u = new Uri("C:/Users/asoni/source/repos/Demo#App/DemoApp/bin/Debug/netcoreapp3.1/IBM.Data.DB2.Core.dll");
Console.WriteLine(u);
Console.WriteLine(u.LocalPath);

Which outputs:

file:///C:/Users/asoni/source/repos/Demo%23App/DemoApp/bin/Debug/netcoreapp3.1/IBM.Data.DB2.Core.dll
C:\Users\asoni\source\repos\Demo#App\DemoApp\bin\Debug\netcoreapp3.1\IBM.Data.DB2.Core.dll

@ghost
Copy link

ghost commented Jun 22, 2020

Tagging subscribers to this area: @dotnet/ncl
Notify danmosemsft if you want to be subscribed.

@scalablecory
Copy link
Contributor

@marcpopMSFT did you mean to self-assign this one?

@marcpopMSFT marcpopMSFT removed their assignment Jun 22, 2020
@marcpopMSFT
Copy link
Member

@scalablecory it was assigned to me in the sdk repo for triage and I forgot to unassign when transferring over

@archanasoni
Copy link
Author

@scalablecory here is repro:

var u = new Uri("file:///C:/Users/asoni/source/repos/Demo#App/DemoApp/bin/Debug/netcoreapp3.1/IBM.Data.DB2.Core.dll");
            Console.WriteLine(u);
            Console.WriteLine(u.LocalPath);

Output:

file:///C:/Users/asoni/source/repos/Demo#App/DemoApp/bin/Debug/netcoreapp3.1/IBM.Data.DB2.Core.dll
C:\Users\asoni\source\repos\Demo

@MihaZupan
Copy link
Member

Uri will parse the # in file:///C:/Users/foo#App/bar.dll as a fragment.
If # is meant as a character belonging to the file path, it should be escaped: file:///C:/Users/foo%23App/bar.dll.

Uri will correctly escape the # when you retrieve the absolute Uri:
new Uri("C:/Users/foo#App/bar.dll").AbsoluteUri => file:///C:/Users/foo%23App/bar.dll

So I would argue that the problem here is not with Uri, but Assembly.CodeBase.
Seeing as #31127 (comment) is approved, making CodeBase both obsolete and [EditorBrowsable(Never)], the customer should switch to using Assembly.Location.

Assembly.Location outputs the file path without the explicit file scheme, so the # is not treated as a fragment if used in Uri.
new Uri(typeof(Program).Assembly.Location).LocalPath

@scalablecory scalablecory changed the title System.Uri class gives wrong path when directory contains # Assembly.CodeBase does not produce a correctly escaped URI Jun 23, 2020
@steveharter steveharter removed the untriaged New issue has not been triaged by the area owner label Jun 29, 2020
@steveharter steveharter added this to the 5.0.0 milestone Jun 29, 2020
@GrabYourPitchforks GrabYourPitchforks modified the milestones: 5.0.0, Future Aug 12, 2020
@steveharter
Copy link
Member

Assembly.Location outputs the file path without the explicit file scheme, so the # is not treated as a fragment if used in Uri.
new Uri(typeof(Program).Assembly.Location).LocalPath

This was changed from a System.Uri potential issue to a reflection issue and Assembly.Location works fine. CodeBase is obsolete.

@ghost ghost locked as resolved and limited conversation to collaborators Nov 17, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
No open projects
Development

No branches or pull requests

7 participants