1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-02 08:32:50 -05:00

Rewrite Icon fetching (#3023)

* Rewrite Icon fetching

* Move validation to IconUri, Uri, or UriBuilder

* `dotnet format` 🤖

* PR suggestions

* Add not null compiler hint

* Add twitter to test case

* Move Uri manipulation to UriService

* Implement MockedHttpClient

Presents better, fluent handling of message matching and response
building.

* Add redirect handling tests

* Add testing to models

* More aggressively dispose content in icon link

* Format 🤖

* Update icon lockfile

* Convert to cloned stream for HttpResponseBuilder

Content was being disposed when HttResponseMessage was being disposed.
This avoids losing our reference to our content and allows multiple
usages of the same `MockedHttpMessageResponse`

* Move services to extension

Extension is shared by testing and allows access to services from
our service tests

* Remove unused `using`

* Prefer awaiting asyncs for better exception handling

* `dotnet format` 🤖

* Await async

* Update tests to use test TLD and ip ranges

* Remove unused interfaces

* Make assignments static when possible

* Prefer invariant comparer to downcasing

* Prefer injecting interface services to implementations

* Prefer comparer set in HashSet initialization

* Allow SVG icons

* Filter out icons with unknown formats

* Seek to beginning of MemoryStream after writing it

* More appropriate to not return icon if it's invalid

* Add svg icon test
This commit is contained in:
Matt Gibson
2023-08-08 15:29:40 -04:00
committed by GitHub
parent ca368466ce
commit 4377c7a897
31 changed files with 1685 additions and 522 deletions

View File

@ -0,0 +1,38 @@
using System.Net;
using Bit.Icons.Models;
using Bit.Icons.Services;
using Bit.Test.Common.MockedHttpClient;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Net.Http.Headers;
using NSubstitute;
using Xunit;
namespace Bit.Icons.Test.Models;
public class IconHttpRequestTests
{
[Fact]
public async Task FetchAsync_FollowsTwoRedirectsAsync()
{
var handler = new MockedHttpMessageHandler();
var request = handler
.Fallback
.WithStatusCode(HttpStatusCode.Redirect)
.WithContent("text/html", "<html><head><title>Redirect 2</title></head><body><a href=\"https://icon.test\">Redirect 3</a></body></html>")
.WithHeader(HeaderNames.Location, "https://icon.test");
var clientFactory = Substitute.For<IHttpClientFactory>();
clientFactory.CreateClient("Icons").Returns(handler.ToHttpClient());
var uriService = Substitute.For<IUriService>();
uriService.TryGetUri(Arg.Any<Uri>(), out Arg.Any<IconUri>()).Returns(x =>
{
x[1] = new IconUri(new Uri("https://icon.test"), IPAddress.Parse("192.0.2.1"));
return true;
});
var result = await IconHttpRequest.FetchAsync(new Uri("https://icon.test"), NullLogger<IIconFetchingService>.Instance, clientFactory, uriService);
Assert.Equal(3, request.NumberOfResponses); // Initial + 2 redirects
}
}

View File

@ -0,0 +1,101 @@
using System.Net;
using AngleSharp.Html.Parser;
using Bit.Icons.Models;
using Bit.Icons.Services;
using Bit.Test.Common.Helpers;
using Bit.Test.Common.MockedHttpClient;
using Microsoft.Extensions.Logging.Abstractions;
using NSubstitute;
using Xunit;
namespace Bit.Icons.Test.Models;
public class IconHttpResponseTests
{
private readonly IUriService _mockedUriService;
private static readonly IHtmlParser _parser = new HtmlParser();
public IconHttpResponseTests()
{
_mockedUriService = Substitute.For<IUriService>();
_mockedUriService.TryGetUri(Arg.Any<Uri>(), out Arg.Any<IconUri>()).Returns(x =>
{
x[1] = new IconUri(new Uri("https://icon.test"), IPAddress.Parse("192.0.2.1"));
return true;
});
}
[Fact]
public async Task RetrieveIconsAsync_Processes200LinksAsync()
{
var htmlBuilder = new HtmlBuilder();
var headBuilder = new HtmlBuilder("head");
for (var i = 0; i < 200; i++)
{
headBuilder.Append(UnusableLinkNode());
}
headBuilder.Append(UsableLinkNode());
htmlBuilder.Append(headBuilder);
var response = GetHttpResponseMessage(htmlBuilder.ToString());
var sut = CurriedIconHttpResponse()(response);
var result = await sut.RetrieveIconsAsync(new Uri("https://icon.test"), "icon.test", _parser);
Assert.Empty(result);
}
[Fact]
public async Task RetrieveIconsAsync_Processes10IconsAsync()
{
var htmlBuilder = new HtmlBuilder();
var headBuilder = new HtmlBuilder("head");
for (var i = 0; i < 11; i++)
{
headBuilder.Append(UsableLinkNode());
}
htmlBuilder.Append(headBuilder);
var response = GetHttpResponseMessage(htmlBuilder.ToString());
var sut = CurriedIconHttpResponse()(response);
var result = await sut.RetrieveIconsAsync(new Uri("https://icon.test"), "icon.test", _parser);
Assert.Equal(10, result.Count());
}
private static string UsableLinkNode()
{
return "<link rel=\"icon\" href=\"https://icon.test/favicon.ico\" />";
}
private static string UnusableLinkNode()
{
// Empty href links are not usable
return "<link rel=\"icon\" href=\"\" />";
}
private static HttpResponseMessage GetHttpResponseMessage(string content)
{
return new HttpResponseMessage(HttpStatusCode.OK)
{
RequestMessage = new HttpRequestMessage(HttpMethod.Get, "https://icon.test"),
Content = new StringContent(content)
};
}
private Func<HttpResponseMessage, IconHttpResponse> CurriedIconHttpResponse()
{
return (HttpResponseMessage response) => new IconHttpResponse(response, NullLogger<IIconFetchingService>.Instance, UsableIconHttpClientFactory(), _mockedUriService);
}
private static IHttpClientFactory UsableIconHttpClientFactory()
{
var substitute = Substitute.For<IHttpClientFactory>();
var handler = new MockedHttpMessageHandler();
handler.Fallback
.WithStatusCode(HttpStatusCode.OK)
.WithContent("image/png", new byte[] { 137, 80, 78, 71 });
substitute.CreateClient("Icons").Returns(handler.ToHttpClient());
return substitute;
}
}

View File

@ -0,0 +1,85 @@
using System.Net;
using AngleSharp.Dom;
using Bit.Icons.Models;
using Bit.Icons.Services;
using Microsoft.Extensions.Logging;
using NSubstitute;
using Xunit;
namespace Bit.Icons.Test.Models;
public class IconLinkTests
{
private readonly IElement _element;
private readonly Uri _uri = new("https://icon.test");
private readonly ILogger<IIconFetchingService> _logger = Substitute.For<ILogger<IIconFetchingService>>();
private readonly IHttpClientFactory _httpClientFactory;
private readonly IUriService _uriService;
private readonly string _baseUrlPath = "/";
public IconLinkTests()
{
_element = Substitute.For<IElement>();
_httpClientFactory = Substitute.For<IHttpClientFactory>();
_uriService = Substitute.For<IUriService>();
_uriService.TryGetUri(Arg.Any<Uri>(), out Arg.Any<IconUri>()).Returns(x =>
{
x[1] = new IconUri(new Uri("https://icon.test"), IPAddress.Parse("192.0.2.1"));
return true;
});
}
[Fact]
public void WithNoHref_IsNotUsable()
{
_element.GetAttribute("href").Returns(string.Empty);
var result = new IconLink(_element, _uri, _baseUrlPath).IsUsable();
Assert.False(result);
}
[Theory]
[InlineData(null, false)]
[InlineData("", false)]
[InlineData(" ", false)]
[InlineData("unusable", false)]
[InlineData("ico", true)]
public void WithNoRel_IsUsable(string extension, bool expectedResult)
{
SetAttributeValue("href", $"/favicon.{extension}");
var result = new IconLink(_element, _uri, _baseUrlPath).IsUsable();
Assert.Equal(expectedResult, result);
}
[Theory]
[InlineData("icon", true)]
[InlineData("stylesheet", false)]
public void WithRel_IsUsable(string rel, bool expectedResult)
{
SetAttributeValue("href", "/favicon.ico");
SetAttributeValue("rel", rel);
var result = new IconLink(_element, _uri, _baseUrlPath).IsUsable();
Assert.Equal(expectedResult, result);
}
[Fact]
public void FetchAsync_Unvalidated_ReturnsNull()
{
var result = new IconLink(_element, _uri, _baseUrlPath).FetchAsync(_logger, _httpClientFactory, _uriService);
Assert.Null(result.Result);
}
private void SetAttributeValue(string attribute, string value)
{
var attr = Substitute.For<IAttr>();
attr.Value.Returns(value);
_element.Attributes[attribute].Returns(attr);
}
}

View File

@ -0,0 +1,22 @@
using System.Net;
using Bit.Icons.Models;
using Xunit;
namespace Bit.Icons.Test.Models;
public class IconUriTests
{
[Theory]
[InlineData("https://icon.test", "1.1.1.1", true)]
[InlineData("https://icon.test:4443", "1.1.1.1", false)] // Non standard port
[InlineData("http://test", "1.1.1.1", false)] // top level domain
[InlineData("https://icon.test", "127.0.0.1", false)] // IP is internal
[InlineData("https://icon.test", "::1", false)] // IP is internal
[InlineData("https://1.1.1.1", "::1", false)] // host is IP
public void IsValid(string uri, string ip, bool expectedResult)
{
var result = new IconUri(new Uri(uri), IPAddress.Parse(ip)).IsValid;
Assert.Equal(expectedResult, result);
}
}