mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 18:21:10 +00:00
Hardens outbound host validation against SSRF
- resolves host names and validates every address, with a connect callback that pins the checked address - applies the guard to every outbound HTTP client, with distinct rejection and resolution failures and warning logs - scopes the MusicBrainz private base URL permission to the configured host and blocks link local and cloud metadata endpoints - keeps provider redirects bounded and strips credentials and hop-by-hop headers on cross-origin redirects - adds unit, security, and end-to-end coverage, and updates the manual
This commit is contained in:
1 parent
ecdb39cc6a
commit
24af00579c
28 files changed
+2977
-162
No files matched your search
+28
-8
@@ -129,22 +129,23 @@ public class CoverArtArchiveServicesTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddCoverArtArchiveArtworkProvider_WhenTheClientIsResolved_ShouldDisableAutomaticRedirectsOnItsPrimaryHandler()
|
||||
public void AddCoverArtArchiveArtworkProvider_WhenTheHostConfiguresHttpClientDefaults_ShouldKeepThePrimaryHandlerOfTheHost()
|
||||
{
|
||||
// Arrange
|
||||
ServiceCollection services = new();
|
||||
StubPrimaryHttpMessageHandler hostPrimaryHandler = new();
|
||||
services.ConfigureHttpClientDefaults(builder => builder.ConfigurePrimaryHttpMessageHandler(() => hostPrimaryHandler));
|
||||
services.AddCoverArtArchiveArtworkProvider(pluginId: Guid.NewGuid());
|
||||
ServiceProvider serviceProvider = services.BuildServiceProvider();
|
||||
using (IServiceScope scope = serviceProvider.CreateScope())
|
||||
{
|
||||
// Act
|
||||
CoverArtArchiveHttpClient client = scope.ServiceProvider.GetRequiredService<CoverArtArchiveHttpClient>();
|
||||
HttpClientHandler primaryHandler = GetPrimaryHandler(client);
|
||||
|
||||
// Assert
|
||||
// Automatic redirects must stay disabled on the primary handler, otherwise it would follow a server controlled redirect on its own,
|
||||
// before the client can check the target host against the Cover Art Archive and the Internet Archive, bypassing that control.
|
||||
Assert.False(primaryHandler.AllowAutoRedirect);
|
||||
// The host owns the primary handler, because it is what disables automatic redirects and connects only to addresses validated to be public.
|
||||
// The plugin must not replace it, otherwise it would follow a server controlled redirect on its own and bypass the guard of the host.
|
||||
Assert.Same(hostPrimaryHandler, GetPrimaryHandler(client));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -170,14 +171,14 @@ public class CoverArtArchiveServicesTests
|
||||
/// Gets the primary HTTP message handler of the provided client, unwrapping the delegating handlers built by the HTTP client factory around it.
|
||||
/// </summary>
|
||||
/// <param name="client">The client whose primary handler is retrieved.</param>
|
||||
/// <returns>The primary HTTP client handler of the client.</returns>
|
||||
private static HttpClientHandler GetPrimaryHandler(CoverArtArchiveHttpClient client)
|
||||
/// <returns>The primary HTTP message handler of the client.</returns>
|
||||
private static HttpMessageHandler GetPrimaryHandler(CoverArtArchiveHttpClient client)
|
||||
{
|
||||
HttpClient httpClient = (HttpClient)GetFieldOfType(client, typeof(HttpClient))!;
|
||||
HttpMessageHandler? handler = (HttpMessageHandler?)GetFieldOfType(httpClient, typeof(HttpMessageHandler));
|
||||
while (handler is DelegatingHandler delegatingHandler)
|
||||
handler = delegatingHandler.InnerHandler;
|
||||
return Assert.IsType<HttpClientHandler>(handler);
|
||||
return handler ?? throw new InvalidOperationException("The HTTP client has no primary handler.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -202,4 +203,23 @@ public class CoverArtArchiveServicesTests
|
||||
}
|
||||
throw new InvalidOperationException($"No field assignable to {fieldType} was found on {instance.GetType()} or its base types.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Primary HTTP message handler that identifies the handler the host configured, so a test can verify that the plugin does not replace it.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
private sealed class StubPrimaryHttpMessageHandler : HttpMessageHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// Sends the provided request. The tests never send a request through this handler.
|
||||
/// </summary>
|
||||
/// <param name="request">The request to send.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>The response of the request.</returns>
|
||||
/// <exception cref="NotSupportedException">Always thrown, because this handler only identifies the handler configured by the host.</exception>
|
||||
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
+52
@@ -1,4 +1,5 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Plugins.Contracts.Common.Http;
|
||||
using Lumina.Plugins.CoverArtArchive.Common.Models.Contracts.Responses;
|
||||
using Lumina.Plugins.CoverArtArchive.Core;
|
||||
using Lumina.Plugins.CoverArtArchive.Core.Api;
|
||||
@@ -54,6 +55,20 @@ public class CoverArtArchiveHttpClientTests
|
||||
Assert.Equal("coverartarchive.org", handler.Requests[0].RequestUri!.Host);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetReleaseArtworkAsync_WhenCalled_ShouldOptOutOfTheHostRedirectHandling()
|
||||
{
|
||||
// Arrange
|
||||
(CoverArtArchiveHttpClient client, StubHttpMessageHandler handler) = CreateClient();
|
||||
|
||||
// Act
|
||||
await client.GetReleaseArtworkAsync(s_releaseId, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(handler.Requests[0].Options.TryGetValue(OutboundRequestOptions.FollowRedirects, out bool shouldFollowRedirects));
|
||||
Assert.False(shouldFollowRedirects);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetReleaseGroupArtworkAsync_WhenTheReleaseGroupHasArtwork_ShouldDeserializeIt()
|
||||
{
|
||||
@@ -298,6 +313,43 @@ public class CoverArtArchiveHttpClientTests
|
||||
Assert.Equal(2, handler.CallCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetReleaseArtworkAsync_WhenTheHostRejectsTheRequest_ShouldNotRetry()
|
||||
{
|
||||
// Arrange
|
||||
(CoverArtArchiveHttpClient client, StubHttpMessageHandler handler) = CreateClient(
|
||||
(request, callIndex) => throw new HttpRequestException(HttpRequestError.ConfigurationLimitExceeded, "Rejected."));
|
||||
|
||||
// Act
|
||||
Task Act()
|
||||
{
|
||||
return client.GetReleaseArtworkAsync(s_releaseId, CancellationToken.None);
|
||||
}
|
||||
|
||||
// Assert
|
||||
await Assert.ThrowsAsync<HttpRequestException>(Act);
|
||||
Assert.Equal(1, handler.CallCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetReleaseArtworkAsync_WhenTheConnectPolicyWrapsTheRejection_ShouldNotRetry()
|
||||
{
|
||||
// Arrange
|
||||
HttpRequestException rejection = new(HttpRequestError.ConfigurationLimitExceeded, "Rejected.");
|
||||
(CoverArtArchiveHttpClient client, StubHttpMessageHandler handler) = CreateClient(
|
||||
(request, callIndex) => throw new HttpRequestException(HttpRequestError.ConnectionError, "Connection error.", rejection));
|
||||
|
||||
// Act
|
||||
Task Act()
|
||||
{
|
||||
return client.GetReleaseArtworkAsync(s_releaseId, CancellationToken.None);
|
||||
}
|
||||
|
||||
// Assert
|
||||
await Assert.ThrowsAsync<HttpRequestException>(Act);
|
||||
Assert.Equal(1, handler.CallCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetReleaseArtworkAsync_WhenEveryAttemptFails_ShouldThrowAfterExhaustingTheRetries()
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user