fix: resolve 7 code quality issues from audit

- Add CancellationTokenSource timeout to DB migration loop (5 min)
- Make seed admin password configurable via env/config
- Improve ExceptionFilter with typed exception handling (409/499/500)
- Document Hangfire.PostgreSql CS0618 pragma with upgrade link
- Narrow CredentialProtector fallback catch to non-empty ciphertext
- Fix ApiBaseUrl in appsettings.json (:5000 -> :8080) for Docker consistency
- Improve HTTPS reverse proxy comment in middleware config

Build: 0 errors, 0 warnings | Tests: 799/799 passed
This commit is contained in:
Arnaud Dartois committed 2026-07-26 19:49:42 +00:00
1 parent e426897449
commit ef3ea9d1ca
5 files changed
+38 -16

No files matched your search

+21 -3
View File
@@ -41,6 +41,11 @@ var migrateOnStartup = builder.Configuration.GetValue<bool?>("Database:MigrateOn
?? app.Environment.IsProduction();
if (migrateOnStartup)
{
// Use a 5-minute overall timeout so startup doesn't hang indefinitely
// if the database is unreachable. Each retry respects this token.
using var migrationCts = new CancellationTokenSource(TimeSpan.FromMinutes(5));
var migrationCt = migrationCts.Token;
using (var scope = app.Services.CreateScope())
{
var logger = scope.ServiceProvider.GetRequiredService<ILogger<Program>>();
@@ -52,17 +57,23 @@ if (migrateOnStartup)
{
try
{
migrationCt.ThrowIfCancellationRequested();
logger.LogInformation("Applying database migrations (attempt {Attempt}/{MaxRetries})...", attempt, maxRetries);
db.Database.Migrate();
logger.LogInformation("Database migrations applied successfully");
break;
}
catch (OperationCanceledException)
{
logger.LogCritical("Database migration timed out after 5 minutes.");
throw;
}
catch (Exception ex) when (attempt < maxRetries)
{
logger.LogWarning(ex,
"Database migration attempt {Attempt}/{MaxRetries} failed. Retrying in {Delay}s...",
attempt, maxRetries, retryDelay.TotalSeconds);
await Task.Delay(retryDelay);
await Task.Delay(retryDelay, migrationCt);
retryDelay = TimeSpan.FromSeconds(retryDelay.TotalSeconds * 2);
}
catch (Exception ex)
@@ -136,7 +147,10 @@ using (var seedScope = app.Services.CreateScope())
Email = "admin@heimdall.test",
DisplayName = "Admin"
};
await userManager.CreateAsync(testAdmin, "Admin123!Pass");
var adminPassword = builder.Configuration["Seed:AdminPassword"]
?? Environment.GetEnvironmentVariable("SEED__ADMIN_PASSWORD")
?? "Admin123!Pass";
await userManager.CreateAsync(testAdmin, adminPassword);
await userManager.AddToRoleAsync(testAdmin, AppRoles.Admin);
}
@@ -366,7 +380,11 @@ public partial class Program {
app.UseHsts();
}
// Only enable HTTPS redirection if HTTPS is actually configured
// Only enable HTTPS redirection if HTTPS is actually configured on Kestrel.
// In production behind a reverse proxy (nginx, Traefik, Caddy), TLS is
// terminated at the proxy level — Kestrel runs on HTTP internally, so
// this block is intentionally skipped (ASPNETCORE_URLS has no https://).
// The proxy handles the HTTP to HTTPS redirect.
var urls = Environment.GetEnvironmentVariable("ASPNETCORE_URLS") ?? "";
if (urls.Contains("https://", StringComparison.OrdinalIgnoreCase))
{