Skip to content

Commit dd43e0a

Browse files
Add path normalization
1 parent 5956055 commit dd43e0a

1 file changed

Lines changed: 25 additions & 10 deletions

File tree

src/ImageSharp.Web/Providers/PhysicalFileSystemProvider.cs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,20 @@ public bool IsValidRequest(HttpContext context)
6262
/// <inheritdoc/>
6363
public Task<IImageResolver> GetAsync(HttpContext context)
6464
{
65-
string path = Path.Join(this.providerRootPath, context.Request.Path.Value);
66-
67-
// Check to see if the file exists
68-
var fileInfo = new FileInfo(path);
69-
if (!fileInfo.Exists)
65+
// Use Join because request path starts with a slash
66+
string fullPath = Path.GetFullPath(Path.Join(this.providerRootPath, context.Request.Path.Value));
67+
if (fullPath.StartsWith(this.providerRootPath, StringComparison.OrdinalIgnoreCase))
7068
{
71-
return Task.FromResult<IImageResolver>(null);
69+
// Check to see if the file exists
70+
var fileInfo = new FileInfo(fullPath);
71+
if (fileInfo.Exists)
72+
{
73+
var metadata = new ImageMetadata(fileInfo.LastWriteTimeUtc, fileInfo.Length);
74+
return Task.FromResult<IImageResolver>(new PhysicalFileSystemResolver(fileInfo, metadata));
75+
}
7276
}
7377

74-
var metadata = new ImageMetadata(fileInfo.LastWriteTimeUtc, fileInfo.Length);
75-
return Task.FromResult<IImageResolver>(new PhysicalFileSystemResolver(fileInfo, metadata));
78+
return Task.FromResult<IImageResolver>(null);
7679
}
7780

7881
/// <summary>
@@ -85,10 +88,22 @@ public Task<IImageResolver> GetAsync(HttpContext context)
8588
internal static string GetProviderRoot(PhysicalFileSystemProviderOptions providerOptions, string webRootPath, string contentRootPath)
8689
{
8790
string providerRoot = providerOptions.ProviderRootPath ?? webRootPath ?? "wwwroot";
88-
89-
return Path.IsPathFullyQualified(providerRoot)
91+
string fullPath = Path.IsPathFullyQualified(providerRoot)
9092
? providerRoot
9193
: Path.GetFullPath(providerRoot, contentRootPath);
94+
95+
return EnsureTrailingSlash(fullPath);
96+
}
97+
98+
internal static string EnsureTrailingSlash(string path)
99+
{
100+
if (!string.IsNullOrEmpty(path) &&
101+
path[path.Length - 1] != Path.DirectorySeparatorChar)
102+
{
103+
return path + Path.DirectorySeparatorChar;
104+
}
105+
106+
return path;
92107
}
93108
}
94109
}

0 commit comments

Comments
 (0)