diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Core/Options.cs | 60 | ||||
| -rw-r--r-- | src/slskd/Program.cs | 36 |
2 files changed, 89 insertions, 7 deletions
diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 1f6ceac2..e8af8c0b 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -2087,6 +2087,13 @@ namespace slskd public bool Logging { get; init; } = false; /// <summary> + /// Gets CORS options. + /// </summary> + [Validate] + [RequiresRestart] + public CorsOptions Cors { get; init; } = new CorsOptions(); + + /// <summary> /// Gets authentication options. /// </summary> [Validate] @@ -2385,6 +2392,59 @@ namespace slskd public string Password { get; init; } } } + + /// <summary> + /// CORS options. + /// </summary> + public class CorsOptions : IValidatableObject + { + /// <summary> + /// Gets the list of CORS allowed origins. + /// </summary> + [Argument(default, "cors-allowed-origins")] + [EnvironmentVariable("CORS_ALLOWED_ORIGINS")] + [Description("list of allowed CORS origins")] + public string[] AllowedOrigins { get; init; } = Array.Empty<string>(); + + /// <summary> + /// Gets a value indicating whether CORS requests may include credentials. + /// </summary> + [Argument(default, "cors-allow-credentials")] + [EnvironmentVariable("CORS_ALLOW_CREDENTIALS")] + [Description("allow CORS credentials for allowed origins")] + public bool AllowCredentials { get; init; } = false; + + public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) + { + var results = new List<ValidationResult>(); + var origins = AllowedOrigins + .Where(origin => !string.IsNullOrWhiteSpace(origin)) + .Select(origin => origin.Trim()) + .ToArray(); + + if (AllowCredentials && origins.Length == 0) + { + results.Add(new ValidationResult("CORS credentials require at least one allowed origin")); + } + + foreach (var origin in origins) + { + if (origin == "*") + { + results.Add(new ValidationResult("CORS wildcard origins are not allowed; specify explicit origins")); + continue; + } + + if (!Uri.TryCreate(origin, UriKind.Absolute, out var parsed) || + (parsed.Scheme != Uri.UriSchemeHttp && parsed.Scheme != Uri.UriSchemeHttps)) + { + results.Add(new ValidationResult($"CORS origin '{origin}' must be an absolute http:// or https:// URL")); + } + } + + return results; + } + } } /// <summary> diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 794a33eb..8677c7ff 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -842,12 +842,34 @@ namespace slskd private static IServiceCollection ConfigureAspDotNetServices(this IServiceCollection services) { - services.AddCors(options => options.AddPolicy("AllowAll", builder => builder - .SetIsOriginAllowed((host) => true) - .AllowAnyHeader() - .AllowAnyMethod() - .AllowCredentials() - .WithExposedHeaders("X-URL-Base", "X-Total-Count"))); + var allowedCorsOrigins = OptionsAtStartup.Web.Cors.AllowedOrigins + .Where(origin => !string.IsNullOrWhiteSpace(origin)) + .Select(origin => origin.Trim().TrimEnd('/')) + .Distinct(StringComparer.OrdinalIgnoreCase) + .ToArray(); + + if (allowedCorsOrigins.Length == 0) + { + Log.Warning("CORS allowlist is empty; cross-origin browser requests are disabled"); + } + + services.AddCors(options => options.AddPolicy("AllowConfiguredOrigins", builder => + { + builder + .AllowAnyHeader() + .AllowAnyMethod() + .WithExposedHeaders("X-URL-Base", "X-Total-Count"); + + if (allowedCorsOrigins.Length > 0) + { + builder.WithOrigins(allowedCorsOrigins); + + if (OptionsAtStartup.Web.Cors.AllowCredentials) + { + builder.AllowCredentials(); + } + } + })); services.ConfigureTelemetry(); @@ -1076,7 +1098,7 @@ namespace slskd await context.Response.WriteAsJsonAsync(context.Features.Get<IExceptionHandlerPathFeature>().Error.Message); })); - app.UseCors("AllowAll"); + app.UseCors("AllowConfiguredOrigins"); if (OptionsAtStartup.Web.Https.Force) { |
