aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-11-22 19:16:51 -0600
committerJP Dillingham <jp@dillingham.me>2026-11-22 19:16:51 -0600
commit410ef006445358218839ea1cb19e38c86feba827 (patch)
treee0812b78f26306eec2338a8217bc3391d5fbded6
parentf4accbf9f6437e49374682eacaf520010de5c775 (diff)
downloadslskd-410ef006445358218839ea1cb19e38c86feba827.tar.gz
add CORS options
-rw-r--r--config/slskd.example.yml4
-rw-r--r--src/slskd/Core/Options.cs67
2 files changed, 71 insertions, 0 deletions
diff --git a/config/slskd.example.yml b/config/slskd.example.yml
index 1edcb919..40018508 100644
--- a/config/slskd.example.yml
+++ b/config/slskd.example.yml
@@ -171,6 +171,10 @@
# key: <some example string between 16 and 255 characters>
# role: readonly # readonly, readwrite, administrator
# cidr: 0.0.0.0/0,::/0
+# cors:
+# allowed_origins:
+# - *
+# allow_credentials: false
# retention:
# search: 10080 # 7 days, in minutes
# transfers:
diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs
index 1f6ceac2..100f612a 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>
+ /// CORS options.
+ /// </summary>
+ [Validate]
+ public CorsOptions Cors { get; init; } = new CorsOptions();
+
+ /// <summary>
/// Authentication options.
/// </summary>
public class WebAuthenticationOptions : IValidatableObject
@@ -2385,6 +2391,67 @@ namespace slskd
public string Password { get; init; }
}
}
+
+ public class CorsOptions : IValidatableObject
+ {
+ [Argument(default, "cors-allowed-origins")]
+ [EnvironmentVariable("CORS_ALLOWED_ORIGINS")]
+ [Description("CORS allowed origins, comma separated")]
+ [RequiresRestart]
+ public string[] AllowedOrigins { get; init; } = null;
+
+ [Argument(default, "cors-allow-credentials")]
+ [EnvironmentVariable("CORS_ALLOW_CREDENTIALS")]
+ [Description("CORS allow credentials (cookies, basic auth)")]
+ [RequiresRestart]
+ public bool AllowCredentials { get; init; } = false;
+
+ public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
+ {
+ if (AllowedOrigins?.Count(x => x == "*") > 1)
+ {
+ yield return new ValidationResult("Specify only one wildcard origin (\"*\")");
+ }
+
+ 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");
+ }
+
+ 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>