mirror of
https://github.com/azaion/satellite-provider.git
synced 2026-06-21 19:21:14 +00:00
1d89cd9997
AZ-353: Centralize 500 handling via GlobalExceptionHandler / AddProblemDetails / UseExceptionHandler. Sanitized ProblemDetails body carries a generic title, RFC9110 type link, and the request's TraceIdentifier as correlationId; the leaky exception message stays server-side in the ERR log entry. Strip per-endpoint try/catch (Exception) wrappers and the unused ILogger<Program> parameters they served. Preserve the typed ArgumentException catch in CreateRoute (AC-3). The handler maps BadHttpRequestException back to its framework-supplied StatusCode so model-binding / malformed-body failures stay 4xx instead of being promoted to 500. AZ-354: Extract CorsConfigurationValidator (pure static helpers) and wire it into Program.cs. Production with empty CorsConfig:AllowedOrigins and no CorsConfig:AllowAnyOrigin opt-in now throws InvalidOperationException at host startup. Development keeps the permissive default but logs a warning post-build. Adds the explicit CorsConfig:AllowAnyOrigin escape hatch. AZ-356: GetSatelliteTilesByMgrs and UploadImage now return Results.Problem(StatusCode 501) with ProblemDetails. Added .ProducesProblem(501) so swagger.json documents the not-implemented status. Tests: SatelliteProvider.Tests now references SatelliteProvider.Api (downward, idiomatic) so unit tests can reach the new helpers. +9 CorsConfigurationValidator unit tests, +3 GlobalExceptionHandler unit tests, +3 StubAndErrorContractTests integration tests (added to smoke + full suites). 58/58 unit + 5/5 smoke + 3/3 stub-contract pass. Code review verdict: PASS. Batch report: _docs/03_implementation/batch_08_report.md. Co-authored-by: Cursor <cursoragent@cursor.com>
42 lines
1.7 KiB
C#
42 lines
1.7 KiB
C#
namespace SatelliteProvider.Api;
|
|
|
|
public static class CorsConfigurationValidator
|
|
{
|
|
public const string MissingOriginsMessage =
|
|
"CORS is misconfigured: CorsConfig:AllowedOrigins is empty and CorsConfig:AllowAnyOrigin is not true. " +
|
|
"Refusing to start in Production with a permissive CORS policy. " +
|
|
"Set CorsConfig:AllowedOrigins to a non-empty array, or set CorsConfig:AllowAnyOrigin=true to opt in.";
|
|
|
|
public const string PermissiveDefaultWarning =
|
|
"CorsConfig:AllowedOrigins is empty and CorsConfig:AllowAnyOrigin is not true. " +
|
|
"Permissive CORS is being applied for environment {Environment}; do not run with this configuration in Production.";
|
|
|
|
public static void EnsureSafeForEnvironment(
|
|
string[] allowedOrigins,
|
|
bool allowAnyOrigin,
|
|
string environmentName)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(allowedOrigins);
|
|
ArgumentNullException.ThrowIfNull(environmentName);
|
|
|
|
if (allowedOrigins.Length == 0
|
|
&& !allowAnyOrigin
|
|
&& string.Equals(environmentName, "Production", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
throw new InvalidOperationException(MissingOriginsMessage);
|
|
}
|
|
}
|
|
|
|
public static bool ShouldUsePermissivePolicy(string[] allowedOrigins, bool allowAnyOrigin)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(allowedOrigins);
|
|
return allowAnyOrigin || allowedOrigins.Length == 0;
|
|
}
|
|
|
|
public static bool ShouldWarnAboutPermissiveDefault(string[] allowedOrigins, bool allowAnyOrigin)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(allowedOrigins);
|
|
return allowedOrigins.Length == 0 && !allowAnyOrigin;
|
|
}
|
|
}
|