mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 19: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
+3
-7
@@ -8,7 +8,6 @@ using Lumina.Plugins.CoverArtArchive.Core.Settings;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Plugins.CoverArtArchive.Common.DependencyInjection;
|
||||
@@ -46,12 +45,9 @@ internal static class CoverArtArchiveServices
|
||||
// is fetched only once during a run.
|
||||
services.TryAddSingleton<CoverArtArchiveResponseCache>();
|
||||
|
||||
// Automatic redirects are disabled on purpose: the metadata endpoints answer with a temporary redirect to the Internet Archive, and the client
|
||||
// follows that redirect itself, checking every hop against CoverArtArchiveHosts. Letting the primary handler follow redirects automatically
|
||||
// would make it follow whatever Location the response carries, which is server controlled, and would bypass that host check, which is the SSRF
|
||||
// control here; the base URL being a constant does not make the redirect target trustworthy.
|
||||
services.AddHttpClient<CoverArtArchiveHttpClient>()
|
||||
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler { AllowAutoRedirect = false });
|
||||
// The client follows the redirects of the Cover Art Archive itself, confined to the hosts the archive may delegate its responses to, so it opts out of the
|
||||
// host redirect handling. The host still validates and pins the address of every request and confirms that it is public.
|
||||
services.AddHttpClient<CoverArtArchiveHttpClient>();
|
||||
|
||||
// Register the artwork provider as a keyed by pluginId transient service, so it can be resolved specifically among other IArtworkProvider implementations.
|
||||
services.AddKeyedTransient<IArtworkProvider, CoverArtArchiveAlbumArtworkProvider>(pluginId);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Plugins.Contracts.Common.Http;
|
||||
using Lumina.Plugins.CoverArtArchive.Common.Models.Contracts.Responses;
|
||||
using Lumina.Plugins.CoverArtArchive.Common.Models.DTO.Settings;
|
||||
using Lumina.Plugins.CoverArtArchive.Core.Settings;
|
||||
@@ -118,8 +119,8 @@ internal sealed class CoverArtArchiveHttpClient
|
||||
|
||||
Uri requestUri = new(new Uri(BASE_URL, UriKind.Absolute), relativeUrl);
|
||||
|
||||
// The metadata endpoints answer with a temporary redirect to the Internet Archive, so the redirect is followed explicitly, which keeps the
|
||||
// automatic redirects disabled and confines the following to the hosts the Cover Art Archive is allowed to delegate its responses to.
|
||||
// The metadata endpoints answer with a temporary redirect to the Internet Archive, so the redirect is followed explicitly, which keeps the automatic
|
||||
// redirects disabled, opts out of the host redirect handling, and confines the following to the hosts the Cover Art Archive may delegate its responses to.
|
||||
for (int redirectCount = 0; redirectCount <= MAXIMUM_REDIRECT_COUNT; redirectCount++)
|
||||
{
|
||||
HttpResponseMessage response = await SendWithRetriesAsync(settings, requestUri, cancellationToken).ConfigureAwait(false);
|
||||
@@ -164,10 +165,14 @@ internal sealed class CoverArtArchiveHttpClient
|
||||
{
|
||||
HttpRequestMessage request = new(HttpMethod.Get, requestUri);
|
||||
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
// The client follows the redirects itself, confined to the hosts the Cover Art Archive may delegate its responses to, so it opts out of the
|
||||
// host redirect handling and receives the redirect response instead.
|
||||
request.Options.Set(OutboundRequestOptions.FollowRedirects, false);
|
||||
response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (HttpRequestException) when (!isLastAttempt)
|
||||
catch (HttpRequestException exception) when (!isLastAttempt && !IsPolicyRejection(exception))
|
||||
{
|
||||
// A rejection by the host outbound address policy is deterministic and is reported as ConfigurationLimitExceeded, so it is not retried.
|
||||
await Task.Delay(GetRetryDelay(attempt, null), cancellationToken).ConfigureAwait(false);
|
||||
continue;
|
||||
}
|
||||
@@ -238,6 +243,22 @@ internal sealed class CoverArtArchiveHttpClient
|
||||
return TimeSpan.FromMilliseconds(250 * Math.Pow(2, attempt));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the provided exception, or one of its inner exceptions, reports a rejection by the host outbound address policy.
|
||||
/// </summary>
|
||||
/// <param name="exception">The exception to inspect.</param>
|
||||
/// <returns><see langword="true"/> when the exception chain contains a policy rejection, otherwise <see langword="false"/>.</returns>
|
||||
/// <remarks>
|
||||
/// SocketsHttpHandler wraps an exception thrown by the connect callback in an HttpRequestException with the ConnectionError error, so the policy rejection is only visible on the inner exception.
|
||||
/// </remarks>
|
||||
private static bool IsPolicyRejection(HttpRequestException exception)
|
||||
{
|
||||
for (Exception? currentException = exception; currentException is not null; currentException = currentException.InnerException)
|
||||
if (currentException is HttpRequestException httpRequestException && httpRequestException.HttpRequestError == HttpRequestError.ConfigurationLimitExceeded)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Applies the request headers configured in the <paramref name="settings"/> onto the underlying HTTP client, unless they are already set.
|
||||
/// </summary>
|
||||
|
||||
Reference in new issue
Block a user