Fixed an issue where invalid characters in the path could cause FileExtensionContentTypeProvider.TryGetExtension to throw an exception.

This commit is contained in:
Brent Newbury 2015-11-30 23:39:01 +00:00 committed by Chris R
parent efd40862f0
commit 657a5ab26b
2 changed files with 29 additions and 1 deletions

View File

@ -426,7 +426,7 @@ namespace Microsoft.AspNet.StaticFiles
/// <returns>True if MIME type could be determined</returns>
public bool TryGetContentType(string subpath, out string contentType)
{
string extension = Path.GetExtension(subpath);
string extension = GetExtension(subpath);
if (extension == null)
{
contentType = null;
@ -434,5 +434,25 @@ namespace Microsoft.AspNet.StaticFiles
}
return Mappings.TryGetValue(extension, out contentType);
}
private static string GetExtension(string path)
{
// Don't use Path.GetExtension as that may throw an exception if there are
// invalid characters in the path. Invalid characters should be handled
// by the FileProviders
if (string.IsNullOrWhiteSpace(path))
{
return null;
}
int index = path.LastIndexOf('.');
if (index < 0)
{
return null;
}
return path.Substring(index);
}
}
}

View File

@ -62,5 +62,13 @@ namespace Microsoft.AspNet.StaticFiles
Assert.True(provider.TryGetContentType(@"\second.css\example.txt", out contentType));
Assert.Equal("text/plain", contentType);
}
[Fact]
public void InvalidCharactersAreIgnored()
{
var provider = new FileExtensionContentTypeProvider();
string contentType;
Assert.True(provider.TryGetContentType($"{new string(System.IO.Path.GetInvalidPathChars())}.txt", out contentType));
}
}
}