1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-29 13:25:17 +01:00

dont try to fetch ip address icons

This commit is contained in:
Kyle Spearrin 2018-08-25 17:30:45 -04:00
parent f74dfb5fbb
commit 7fd79388e2
2 changed files with 14 additions and 1 deletions

View File

@ -69,7 +69,7 @@ namespace Bit.Icons.Controllers
// Only cache not found and smaller images (<= 50kb) // Only cache not found and smaller images (<= 50kb)
if(_iconsSettings.CacheEnabled && (icon == null || icon.Image.Length <= 50012)) if(_iconsSettings.CacheEnabled && (icon == null || icon.Image.Length <= 50012))
{ {
_logger.LogWarning("Cache the icon for {0}.", domain); _logger.LogInformation("Cache icon for {0}.", domain);
_memoryCache.Set(mappedDomain, icon, new MemoryCacheEntryOptions _memoryCache.Set(mappedDomain, icon, new MemoryCacheEntryOptions
{ {
AbsoluteExpirationRelativeToNow = new TimeSpan(_iconsSettings.CacheHours, 0, 0), AbsoluteExpirationRelativeToNow = new TimeSpan(_iconsSettings.CacheHours, 0, 0),

View File

@ -8,6 +8,7 @@ using System.Threading.Tasks;
using Bit.Icons.Models; using Bit.Icons.Models;
using AngleSharp.Parser.Html; using AngleSharp.Parser.Html;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System.Text.RegularExpressions;
namespace Bit.Icons.Services namespace Bit.Icons.Services
{ {
@ -31,10 +32,16 @@ namespace Bit.Icons.Services
private readonly HashSet<string> _allowedMediaTypes; private readonly HashSet<string> _allowedMediaTypes;
private readonly HttpClient _httpClient; private readonly HttpClient _httpClient;
private readonly ILogger<IIconFetchingService> _logger; private readonly ILogger<IIconFetchingService> _logger;
private readonly Regex _ipRegex;
public IconFetchingService(ILogger<IIconFetchingService> logger) public IconFetchingService(ILogger<IIconFetchingService> logger)
{ {
_logger = logger; _logger = logger;
_ipRegex = new Regex("^" +
"(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\." +
"(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\." +
"(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\." +
"(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$");
_allowedMediaTypes = new HashSet<string> _allowedMediaTypes = new HashSet<string>
{ {
_pngMediaType, _pngMediaType,
@ -53,6 +60,12 @@ namespace Bit.Icons.Services
public async Task<IconResult> GetIconAsync(string domain) public async Task<IconResult> GetIconAsync(string domain)
{ {
if(_ipRegex.IsMatch(domain))
{
_logger.LogWarning("IP address: {0}.", domain);
return null;
}
if(!Uri.TryCreate($"https://{domain}", UriKind.Absolute, out var parsedHttpsUri)) if(!Uri.TryCreate($"https://{domain}", UriKind.Absolute, out var parsedHttpsUri))
{ {
_logger.LogWarning("Bad domain: {0}.", domain); _logger.LogWarning("Bad domain: {0}.", domain);