Fixed an issue where invalid characters in the path could cause FileExtensionContentTypeProvider.TryGetExtension to throw an exception.
This commit is contained in:
parent
efd40862f0
commit
657a5ab26b
|
|
@ -426,7 +426,7 @@ namespace Microsoft.AspNet.StaticFiles
|
||||||
/// <returns>True if MIME type could be determined</returns>
|
/// <returns>True if MIME type could be determined</returns>
|
||||||
public bool TryGetContentType(string subpath, out string contentType)
|
public bool TryGetContentType(string subpath, out string contentType)
|
||||||
{
|
{
|
||||||
string extension = Path.GetExtension(subpath);
|
string extension = GetExtension(subpath);
|
||||||
if (extension == null)
|
if (extension == null)
|
||||||
{
|
{
|
||||||
contentType = null;
|
contentType = null;
|
||||||
|
|
@ -434,5 +434,25 @@ namespace Microsoft.AspNet.StaticFiles
|
||||||
}
|
}
|
||||||
return Mappings.TryGetValue(extension, out contentType);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -62,5 +62,13 @@ namespace Microsoft.AspNet.StaticFiles
|
||||||
Assert.True(provider.TryGetContentType(@"\second.css\example.txt", out contentType));
|
Assert.True(provider.TryGetContentType(@"\second.css\example.txt", out contentType));
|
||||||
Assert.Equal("text/plain", 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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue