Skip to content

Commit 4d6f3f9

Browse files
committed
Fix Linux-only absolute path handling bug
1 parent d0a6f97 commit 4d6f3f9

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

Solutions/Corvus.Json/Corvus.Json/JsonUri.cs

+16-1
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,22 @@ private static string FormatUri(Uri value)
498498

499499
private static bool TryParseUri(string text, [NotNullWhen(true)] out Uri? value)
500500
{
501-
return Uri.TryCreate(text, UriKind.Absolute, out value);
501+
// Uri.TryCreate considers full-qualified file paths to be acceptable as absolute Uris.
502+
// This means that on Linux "/abc" is considered an acceptable absolute Uri! (This is
503+
// conceptually equivalent to "C:\abc" being an absolute Uri on Windows, but it's more
504+
// of a problem because a lot of relative Uris of the kind you come across on the web
505+
// look exactly like Unix file paths.)
506+
// https://github.com/dotnet/runtime/issues/22718
507+
// However, this only needs to be a problem if you insist that the Uri is absolute.
508+
// If you accept either absolute or relative Uris, it will intepret "/abc" as a
509+
// relative Uri on either Windows or Linux. It only interprets it as an absolute Uri
510+
// if you pass UriKind.Absolute when parsing.
511+
// This is why we take the peculiar-looking step of passing UriKind.RelativeOrAbsolute
512+
// and then rejecting relative Uris. This causes this method to reject "/abc" on all
513+
// platforms. Back when we passed UriKind.Absolute, this code incorrectly accepted
514+
// "abc".
515+
return Uri.TryCreate(text, UriKind.RelativeOrAbsolute, out value) &&
516+
value.IsAbsoluteUri;
502517
}
503518
}
504519
}

Solutions/testenvironments.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"version": "1",
3+
"environments": [
4+
{
5+
"name": "linux dotnet-core-sdk-6.0",
6+
"type": "docker",
7+
"dockerImage": "mcr.microsoft.com/dotnet/sdk:6.0"
8+
},
9+
{
10+
"name": "WSL-Ubuntu",
11+
"type": "wsl",
12+
"wslDistribution": "Ubuntu-22.04"
13+
}
14+
]
15+
}

0 commit comments

Comments
 (0)