[backend/core] Limit HttpClient buffer size to 1MiB, except DriveService from this policy (until proper limits are in place there)

This commit is contained in:
Laura Hausmann 2024-07-28 22:06:33 +02:00
parent f1b39981c6
commit 15d955c478
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
3 changed files with 17 additions and 1 deletions

View file

@ -86,6 +86,7 @@ public static class ServiceExtensions
// Singleton = instantiated once across application lifetime
services
.AddSingleton<HttpClient, CustomHttpClient>()
.AddSingleton<UnrestrictedHttpClient>()
.AddSingleton<HttpRequestService>()
.AddSingleton<CronService>()
.AddSingleton<QueueService>()

View file

@ -31,6 +31,9 @@ public class CustomHttpClient : HttpClient
DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", options.Value.UserAgent);
Timeout = TimeSpan.FromSeconds(30);
// Protect against DoS attacks
MaxResponseContentBufferSize = 1024 * 1024; // 1MiB
// Configure FastFallback
FastFallbackHandler.Logger = loggerFactory.CreateLogger<FastFallback>();
FastFallbackHandler.Security = security;
@ -363,3 +366,15 @@ public class CustomHttpClient : HttpClient
}
}
}
public class UnrestrictedHttpClient : CustomHttpClient
{
public UnrestrictedHttpClient(
IOptions<Config.InstanceSection> options,
IOptionsMonitor<Config.SecuritySection> security,
ILoggerFactory loggerFactory
) : base(options, security, loggerFactory)
{
MaxResponseContentBufferSize = int.MaxValue;
}
}

View file

@ -17,7 +17,7 @@ public class DriveService(
[SuppressMessage("ReSharper", "SuggestBaseTypeForParameterInConstructor")]
IOptionsSnapshot<Config.StorageSection> storageConfig,
IOptions<Config.InstanceSection> instanceConfig,
HttpClient httpClient,
UnrestrictedHttpClient httpClient,
QueueService queueSvc,
ILogger<DriveService> logger,
ImageProcessor imageProcessor