diff options
| author | samhatter <sammyvincent2001@gmail.com> | 2026-06-26 13:22:58 -0700 |
|---|---|---|
| committer | samhatter <sammyvincent2001@gmail.com> | 2026-06-26 13:22:58 -0700 |
| commit | 4b09b8c13c1c709bf0046134696211755a470866 (patch) | |
| tree | 2db659b29726a7ec7ad05e9f03c8176111449539 | |
| parent | 3edca3166d53d1a7970f07c7014773d7d5408bed (diff) | |
| download | slskd-4b09b8c13c1c709bf0046134696211755a470866.tar.gz | |
stricter and configurable cors
| -rw-r--r-- | config/slskd.example.yml | 4 | ||||
| -rw-r--r-- | src/slskd/Core/Options.cs | 60 | ||||
| -rw-r--r-- | src/slskd/Program.cs | 36 |
3 files changed, 93 insertions, 7 deletions
diff --git a/config/slskd.example.yml b/config/slskd.example.yml index 475ac65a..99bfebfb 100644 --- a/config/slskd.example.yml +++ b/config/slskd.example.yml @@ -162,6 +162,10 @@ # url_base: / # content_path: wwwroot # logging: false +# cors: +# allowed_origins: +# - https://app.example.com +# allow_credentials: false # authentication: # disabled: false # username: slskd diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index d1062a2c..dce97f4e 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -2076,6 +2076,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] @@ -2371,6 +2378,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 0fc6b294..f0603102 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -825,12 +825,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(); @@ -1052,7 +1074,7 @@ namespace slskd await context.Response.WriteAsJsonAsync(context.Features.Get<IExceptionHandlerPathFeature>().Error.Message); })); - app.UseCors("AllowAll"); + app.UseCors("AllowConfiguredOrigins"); if (OptionsAtStartup.Web.Https.Force) { |
