diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-11-24 14:29:52 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-24 14:29:52 -0600 |
| commit | ab98c6e9e24f638d08950c335e8c860b091debf3 (patch) | |
| tree | a797e73c89e42306e0121b573955726e2768eedb /src/slskd/Core/Options.cs | |
| parent | f4accbf9f6437e49374682eacaf520010de5c775 (diff) | |
| parent | 3d1a57c1b3bd22bdd480172d23c8b6fd350275a1 (diff) | |
| download | slskd-ab98c6e9e24f638d08950c335e8c860b091debf3.tar.gz | |
Merge pull request #1938 from slskd/cors-merge
Add the ability to change HTTP CORS options
Diffstat (limited to 'src/slskd/Core/Options.cs')
| -rw-r--r-- | src/slskd/Core/Options.cs | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 1f6ceac2..8c22eab7 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -2093,6 +2093,12 @@ namespace slskd public WebAuthenticationOptions Authentication { get; init; } = new WebAuthenticationOptions(); /// <summary> + /// Gets CORS options. + /// </summary> + [Validate] + public CorsOptions Cors { get; init; } = new CorsOptions(); + + /// <summary> /// Authentication options. /// </summary> public class WebAuthenticationOptions : IValidatableObject @@ -2385,6 +2391,85 @@ 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")] + [RequiresRestart] + public string[] AllowedOrigins { get; init; } = null; + + /// <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")] + [RequiresRestart] + public bool AllowCredentials { get; init; } = false; + + public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) + { + if (AllowedOrigins?.Contains("*") ?? false) + { + if (AllowedOrigins.Count(x => x == "*") > 1) + { + yield return new ValidationResult("Specify only one wildcard origin (\"*\")"); + } + + if (AllowedOrigins.Any(x => x != "*")) + { + yield return new ValidationResult("Specifying wildcard origin (\"*\") with other origins has no effect; remove either the wildcard or all non-wildcard origins"); + } + } + + foreach (var origin in (AllowedOrigins ?? []).Where(o => o != "*")) + { + if (origin.Contains('*')) + { + yield return new ValidationResult($"Origin {origin} must not contain a wildcard"); + } + + if (!Uri.TryCreate(origin, default(UriCreationOptions), out var o)) + { + yield return new ValidationResult($"Origin {origin} is not a valid URI"); + continue; + } + + if (!o.IsAbsoluteUri) + { + yield return new ValidationResult($"Origin {origin} must be an absolute URI"); + } + + string[] validSchemes = ["http", "https"]; + + if (!validSchemes.Contains(o.Scheme, StringComparer.OrdinalIgnoreCase)) + { + yield return new ValidationResult($"Origin {origin} must use the HTTP or HTTPS scheme"); + } + + var policy = new Microsoft.AspNetCore.Cors.Infrastructure.CorsPolicyBuilder().WithOrigins(origin).Build(); + var browserOrigin = $"{o.Scheme}://{o.IdnHost}{(o.IsDefaultPort ? string.Empty : $":{o.Port}")}"; + + if (!policy.IsOriginAllowed(browserOrigin)) + { + yield return new ValidationResult($"Origin {origin} is malformed and will never match a browser request; rewrite it as {browserOrigin}"); + } + } + + if ((AllowedOrigins is null || AllowedOrigins.Length == 0 || AllowedOrigins.Contains("*")) && AllowCredentials) + { + yield return new ValidationResult("Credentials can not be allowed while allowing all origins"); + } + } + } } /// <summary> |
