Correctly normalize file path from network shares (dotnet/aspnetcore-tooling#298)

* Correctly normalize file path from network shares

* Feedback
\n\nCommit migrated from 8ee676d84c
This commit is contained in:
Ajay Bhargav Baaskaran 2019-03-05 15:13:54 -08:00 committed by GitHub
parent 8775af3309
commit 1657080589
1 changed files with 9 additions and 3 deletions

View File

@ -52,7 +52,7 @@ namespace Microsoft.AspNetCore.Razor.Language
var file = new FileInfo(absolutePath);
if (!absolutePath.StartsWith(absoluteBasePath))
{
throw new InvalidOperationException($"The file '{file.FullName}' is not a descendent of the base path '{absoluteBasePath}'.");
throw new InvalidOperationException($"The file '{absolutePath}' is not a descendent of the base path '{absoluteBasePath}'.");
}
var relativePhysicalPath = file.FullName.Substring(absoluteBasePath.Length + 1); // Include leading separator
@ -68,9 +68,15 @@ namespace Microsoft.AspNetCore.Razor.Language
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(path));
}
var absolutePath = path;
if (!absolutePath.StartsWith(Root, StringComparison.OrdinalIgnoreCase))
var absolutePath = path.Replace('\\', '/');
// Check if the given path is an absolute path. It is absolute if,
// 1. It starts with Root or
// 2. It is a network share path and starts with a '//'. Eg. //servername/some/network/folder
if (!absolutePath.StartsWith(Root, StringComparison.OrdinalIgnoreCase) &&
!absolutePath.StartsWith("//", StringComparison.OrdinalIgnoreCase))
{
// This is not an absolute path. Strip the leading slash if any and combine it with Root.
if (path[0] == '/' || path[0] == '\\')
{
path = path.Substring(1);