1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-22 12:15:36 +01:00

allow octet streams

This commit is contained in:
Kyle Spearrin 2018-03-19 08:11:23 -04:00
parent a46b87c354
commit 9ed1ae9567

View File

@ -19,10 +19,15 @@ namespace Bit.Icons.Controllers
AllowAutoRedirect = false,
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
});
private static string _pngMediaType = "image/png";
private static string _icoMediaType = "image/x-icon";
private static string _jpegMediaType = "image/jpeg";
private static string _octetMediaType = "application/octet-stream";
private static readonly HashSet<string> _allowedMediaTypes = new HashSet<string>{
"image/png",
"image/x-icon",
"image/jpeg"
_pngMediaType,
_icoMediaType,
_jpegMediaType,
_octetMediaType
};
private readonly IMemoryCache _memoryCache;
private readonly IDomainMappingService _domainMappingService;
@ -56,8 +61,9 @@ namespace Bit.Icons.Controllers
var mappedDomain = _domainMappingService.MapDomain(uri.Host);
if(!_memoryCache.TryGetValue(mappedDomain, out Icon icon))
{
var iconUrl = $"{_iconsSettings.BestIconBaseUrl}/icon?url={mappedDomain}&size=16..32..200" +
$"&fallback_icon_url=https://raw.githubusercontent.com/bitwarden/web/master/src/images/fa-globe.png";
var iconUrl = new Uri($"{_iconsSettings.BestIconBaseUrl}/icon" +
$"?url={mappedDomain}&size=16..32..200&fallback_icon_url=" +
$"https://raw.githubusercontent.com/bitwarden/web/master/src/images/fa-globe.png");
var response = await _httpClient.GetAsync(iconUrl);
response = await FollowRedirectsAsync(response, 1);
if(!response.IsSuccessStatusCode ||
@ -73,6 +79,27 @@ namespace Bit.Icons.Controllers
Format = response.Content.Headers.ContentType.MediaType
};
if(icon.Format == _octetMediaType)
{
if(response.RequestMessage.RequestUri.AbsoluteUri.EndsWith(".ico"))
{
icon.Format = _icoMediaType;
}
else if(response.RequestMessage.RequestUri.AbsoluteUri.EndsWith(".png"))
{
icon.Format = _pngMediaType;
}
else if(response.RequestMessage.RequestUri.AbsoluteUri.EndsWith(".jpeg") ||
response.RequestMessage.RequestUri.AbsoluteUri.EndsWith(".jpg"))
{
icon.Format = _jpegMediaType;
}
else
{
return new NotFoundResult();
}
}
// Only cache smaller images (<= 50kb)
if(image.Length <= 50012)
{