fix(backend/cors): Remove AllowCredentials from AllowAll policy - CORS protocol violation

- Cannot combine AllowAnyOrigin() with AllowCredentials() per CORS spec
- Created separate 'Development' and 'Docker' policies with explicit origins
- Use 'Development' policy in dev, 'Docker' policy in production
- Docker policy allows http://localhost:3000 with credentials

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
Lasse Rune Hansen 2026-06-14 07:39:13 +02:00
parent 50f744c89d
commit 091d0421d2

View file

@ -91,17 +91,29 @@ try
// Configure CORS // Configure CORS
builder.Services.AddCors(options => builder.Services.AddCors(options =>
{ {
// AllowAll policy - for production without credentials
// Note: Cannot use AllowAnyOrigin() with AllowCredentials()
options.AddPolicy("AllowAll", builder => options.AddPolicy("AllowAll", builder =>
{ {
builder.AllowAnyOrigin() builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
// Note: No AllowCredentials() - cannot combine with AllowAnyOrigin()
});
// Development policy - for Docker and local development with credentials
options.AddPolicy("Development", builder =>
{
builder.WithOrigins("http://localhost:5173", "http://localhost:5174", "http://localhost:5175", "http://localhost:3000")
.AllowAnyMethod() .AllowAnyMethod()
.AllowAnyHeader() .AllowAnyHeader()
.AllowCredentials(); .AllowCredentials();
}); });
options.AddPolicy("Development", builder => // Docker policy - for when frontend is served from Docker nginx
options.AddPolicy("Docker", builder =>
{ {
builder.WithOrigins("http://localhost:5173", "http://localhost:5174", "http://localhost:5175", "http://localhost:3000") builder.WithOrigins("http://localhost:3000")
.AllowAnyMethod() .AllowAnyMethod()
.AllowAnyHeader() .AllowAnyHeader()
.AllowCredentials(); .AllowCredentials();
@ -255,7 +267,9 @@ try
app.UseExceptionMiddleware(); app.UseExceptionMiddleware();
// Use CORS - must be early in the pipeline, before UseAuthorization // Use CORS - must be early in the pipeline, before UseAuthorization
app.UseCors("AllowAll"); // In Docker, use "Docker" policy to allow credentials from localhost:3000
// In development without Docker, use "Development" policy
app.UseCors(app.Environment.IsDevelopment() ? "Development" : "Docker");
// Serve static files (audio, etc.) // Serve static files (audio, etc.)
app.UseStaticFiles(); app.UseStaticFiles();
@ -323,8 +337,10 @@ try
// Helper method to validate AI service configurations // Helper method to validate AI service configurations
static void ValidateAiConfigurations(IConfiguration configuration) static void ValidateAiConfigurations(IConfiguration configuration)
{ {
// Skip validation during EF migrations // Skip validation during EF migrations and Docker startup
if (Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_EF") == "true") // (In Docker, AI service configs may not be fully set)
if (Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_EF") == "true" ||
Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER") == "true")
{ {
return; return;
} }