From 4b09b8c13c1c709bf0046134696211755a470866 Mon Sep 17 00:00:00 2001 From: samhatter Date: Fri, 26 Jun 2026 13:22:58 -0700 Subject: stricter and configurable cors --- config/slskd.example.yml | 4 ++++ src/slskd/Core/Options.cs | 60 +++++++++++++++++++++++++++++++++++++++++++++++ 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 @@ -2075,6 +2075,13 @@ namespace slskd [RequiresRestart] public bool Logging { get; init; } = false; + /// + /// Gets CORS options. + /// + [Validate] + [RequiresRestart] + public CorsOptions Cors { get; init; } = new CorsOptions(); + /// /// Gets authentication options. /// @@ -2371,6 +2378,59 @@ namespace slskd public string Password { get; init; } } } + + /// + /// CORS options. + /// + public class CorsOptions : IValidatableObject + { + /// + /// Gets the list of CORS allowed origins. + /// + [Argument(default, "cors-allowed-origins")] + [EnvironmentVariable("CORS_ALLOWED_ORIGINS")] + [Description("list of allowed CORS origins")] + public string[] AllowedOrigins { get; init; } = Array.Empty(); + + /// + /// Gets a value indicating whether CORS requests may include credentials. + /// + [Argument(default, "cors-allow-credentials")] + [EnvironmentVariable("CORS_ALLOW_CREDENTIALS")] + [Description("allow CORS credentials for allowed origins")] + public bool AllowCredentials { get; init; } = false; + + public IEnumerable Validate(ValidationContext validationContext) + { + var results = new List(); + 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; + } + } } /// 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().Error.Message); })); - app.UseCors("AllowAll"); + app.UseCors("AllowConfiguredOrigins"); if (OptionsAtStartup.Web.Https.Force) { -- cgit v1.2.3 From 410ef006445358218839ea1cb19e38c86feba827 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 22 Nov 2026 19:16:51 -0600 Subject: add CORS options --- config/slskd.example.yml | 4 +++ src/slskd/Core/Options.cs | 67 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) 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: # 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 @@ -2092,6 +2092,12 @@ namespace slskd [Validate] public WebAuthenticationOptions Authentication { get; init; } = new WebAuthenticationOptions(); + /// + /// CORS options. + /// + [Validate] + public CorsOptions Cors { get; init; } = new CorsOptions(); + /// /// Authentication options. /// @@ -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 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"); + } + } + } } /// -- cgit v1.2.3 From af9fa84280350b9b840163775d69f0c53aecf967 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 22 Nov 2026 19:19:16 -0600 Subject: configure CORS from options --- src/slskd/Program.cs | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 794a33eb..c2838dbe 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"))); + services.AddCors(options => options.AddPolicy("CORS", builder => + { + var cors = OptionsAtStartup.Web.Cors; + var origins = (cors.AllowedOrigins ?? []) + .Where(origin => !string.IsNullOrWhiteSpace(origin)) + .Select(origin => origin.Trim().TrimEnd('/')) + .ToArray(); + + builder + .AllowAnyHeader() + .AllowAnyMethod() + .DisallowCredentials() + .WithExposedHeaders("X-URL-Base", "X-Total-Count"); + + if (origins.Length > 0 && !origins.Contains("*")) + { + builder.WithOrigins(origins); + + if (cors.AllowCredentials) + { + builder.AllowCredentials(); + } + } + else + { + builder.SetIsOriginAllowed((origin) => true); // allow any origin + } + })); services.ConfigureTelemetry(); -- cgit v1.2.3 From dd5b6cf3c21ed6674cce5ba64c8d4dc375cf9071 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 22 Nov 2026 19:19:31 -0600 Subject: rough in tests --- .../Core/Options/WebOptions.CorsOptionsTests.cs | 209 +++++++++++++++++++++ 1 file changed, 209 insertions(+) create mode 100644 tests/slskd.Tests.Unit/Core/Options/WebOptions.CorsOptionsTests.cs diff --git a/tests/slskd.Tests.Unit/Core/Options/WebOptions.CorsOptionsTests.cs b/tests/slskd.Tests.Unit/Core/Options/WebOptions.CorsOptionsTests.cs new file mode 100644 index 00000000..a23a2818 --- /dev/null +++ b/tests/slskd.Tests.Unit/Core/Options/WebOptions.CorsOptionsTests.cs @@ -0,0 +1,209 @@ +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using Xunit; +using static slskd.Options; + +namespace slskd.Tests.Unit.Core.Options; + +using CorsOptions = WebOptions.CorsOptions; + +public class CorsOptionsTests +{ + private static List Validate(CorsOptions options) + { + var context = new ValidationContext(options); + var results = new List(); + Validator.TryValidateObject(options, context, results, validateAllProperties: true); + return results; + } + + public class AllowedOrigins + { + [Fact] + public void Null_origins_produces_no_errors() + { + var options = new CorsOptions { AllowedOrigins = null }; + Assert.Empty(Validate(options)); + } + + [Fact] + public void Empty_origins_produces_no_errors() + { + var options = new CorsOptions { AllowedOrigins = [] }; + Assert.Empty(Validate(options)); + } + + [Fact] + public void Single_wildcard_produces_no_errors() + { + var options = new CorsOptions { AllowedOrigins = ["*"] }; + Assert.Empty(Validate(options)); + } + + [Fact] + public void Multiple_wildcards_produces_error() + { + var options = new CorsOptions { AllowedOrigins = ["*", "*"] }; + var results = Validate(options); + Assert.Single(results); + Assert.Contains("only one wildcard", results[0].ErrorMessage); + } + + [Theory] + [InlineData("http://localhost:3000")] + [InlineData("https://example.com")] + [InlineData("https://sub.example.com")] + [InlineData("https://example.com:8443")] + [InlineData("http://192.168.1.10:5030")] + [InlineData("HTTPS://EXAMPLE.COM")] + public void Valid_origin_produces_no_errors(string origin) + { + var options = new CorsOptions { AllowedOrigins = [origin] }; + Assert.Empty(Validate(options)); + } + + [Fact] + public void Wildcard_and_valid_origin_produces_no_errors() + { + var options = new CorsOptions { AllowedOrigins = ["*", "https://example.com"] }; + Assert.Empty(Validate(options)); + } + + [Theory] + [InlineData("https://*.example.com")] + [InlineData("https://example.*")] + public void Origin_containing_wildcard_produces_error(string origin) + { + var options = new CorsOptions { AllowedOrigins = [origin] }; + var results = Validate(options); + Assert.NotEmpty(results); + Assert.Contains(results, r => r.ErrorMessage.Contains(origin) && r.ErrorMessage.Contains("wildcard")); + } + + [Theory] + [InlineData("foo")] + [InlineData("example.com")] + [InlineData("null")] + [InlineData("")] + [InlineData(" ")] + public void Invalid_uri_produces_error(string origin) + { + var options = new CorsOptions { AllowedOrigins = [origin] }; + Assert.NotEmpty(Validate(options)); + } + + [Theory] + [InlineData("ftp://example.com")] + [InlineData("ws://example.com")] + [InlineData("file:///c:/foo")] + public void Non_http_scheme_produces_error(string origin) + { + var options = new CorsOptions { AllowedOrigins = [origin] }; + var results = Validate(options); + Assert.Contains(results, r => r.ErrorMessage.Contains("HTTP or HTTPS")); + } + + [Theory] + [InlineData("https://example.com/")] + [InlineData("https://example.com/app")] + [InlineData("https://example.com?foo=bar")] + [InlineData("https://example.com#foo")] + [InlineData("https://example.com:443")] + [InlineData("http://example.com:80")] + [InlineData("https://alice@example.com")] + [InlineData("https://alice:secret@example.com")] + [InlineData("https://example.com@evil.com")] + public void Origin_that_never_matches_a_browser_request_produces_error(string origin) + { + var options = new CorsOptions { AllowedOrigins = [origin] }; + var results = Validate(options); + Assert.Single(results); + Assert.Contains(origin, results[0].ErrorMessage); + } + + [Fact] + public void Malformed_origin_error_suggests_browser_origin() + { + var options = new CorsOptions { AllowedOrigins = ["https://example.com/"] }; + var results = Validate(options); + Assert.Single(results); + Assert.EndsWith("https://example.com", results[0].ErrorMessage); + } + + [Fact] + public void Internationalized_origin_produces_no_errors() + { + var options = new CorsOptions { AllowedOrigins = ["https://münchen.de"] }; + Assert.Empty(Validate(options)); + } + + [Fact] + public void Multiple_invalid_origins_each_produce_an_error() + { + var options = new CorsOptions { AllowedOrigins = ["https://example.com/", "ftp://example.com"] }; + Assert.Equal(2, Validate(options).Count); + } + + [Fact] + public void Mixed_valid_and_invalid_origins_only_errors_on_invalid() + { + var options = new CorsOptions { AllowedOrigins = ["https://example.com", "https://other.com/"] }; + var results = Validate(options); + Assert.Single(results); + Assert.Contains("https://other.com/", results[0].ErrorMessage); + } + } + + public class AllowCredentials + { + [Fact] + public void Credentials_with_null_origins_produces_error() + { + var options = new CorsOptions { AllowedOrigins = null, AllowCredentials = true }; + var results = Validate(options); + Assert.Single(results); + Assert.Contains("Credentials", results[0].ErrorMessage); + } + + [Fact] + public void Credentials_with_empty_origins_produces_error() + { + var options = new CorsOptions { AllowedOrigins = [], AllowCredentials = true }; + var results = Validate(options); + Assert.Single(results); + Assert.Contains("Credentials", results[0].ErrorMessage); + } + + [Fact] + public void Credentials_with_wildcard_origin_produces_error() + { + var options = new CorsOptions { AllowedOrigins = ["*"], AllowCredentials = true }; + var results = Validate(options); + Assert.Single(results); + Assert.Contains("Credentials", results[0].ErrorMessage); + } + + [Fact] + public void Credentials_with_wildcard_and_specific_origin_produces_error() + { + var options = new CorsOptions { AllowedOrigins = ["*", "https://example.com"], AllowCredentials = true }; + var results = Validate(options); + Assert.Single(results); + Assert.Contains("Credentials", results[0].ErrorMessage); + } + + [Fact] + public void Credentials_with_specific_origins_produces_no_errors() + { + var options = new CorsOptions { AllowedOrigins = ["https://example.com", "http://localhost:3000"], AllowCredentials = true }; + Assert.Empty(Validate(options)); + } + + [Fact] + public void No_credentials_with_null_origins_produces_no_errors() + { + var options = new CorsOptions { AllowedOrigins = null, AllowCredentials = false }; + Assert.Empty(Validate(options)); + } + } +} -- cgit v1.2.3 From ed2fb233c03f315d86ffe9dec14b54cba23d244d Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 22 Nov 2026 22:24:50 -0600 Subject: break out of loop on malformed uri --- bin/test | 0 src/slskd/Core/Options.cs | 1 + 2 files changed, 1 insertion(+) mode change 100644 => 100755 bin/test diff --git a/bin/test b/bin/test old mode 100644 new mode 100755 diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 100f612a..b65fc383 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -2423,6 +2423,7 @@ namespace slskd if (!Uri.TryCreate(origin, default(UriCreationOptions), out var o)) { yield return new ValidationResult($"Origin {origin} is not a valid URI"); + continue; } if (!o.IsAbsoluteUri) -- cgit v1.2.3 From 5861fd95d40023504bb8105ecf986093fda68f76 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 24 Nov 2026 13:09:32 -0600 Subject: don't use a magic string for CORS policy --- src/slskd/Program.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 8b22a7af..4b985061 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -842,7 +842,7 @@ namespace slskd private static IServiceCollection ConfigureAspDotNetServices(this IServiceCollection services) { - services.AddCors(options => options.AddPolicy("CORS", builder => + services.AddCors(options => options.AddPolicy(nameof(OptionsAtStartup.Web.Cors), builder => { var cors = OptionsAtStartup.Web.Cors; var origins = (cors.AllowedOrigins ?? []) @@ -1098,7 +1098,7 @@ namespace slskd await context.Response.WriteAsJsonAsync(context.Features.Get().Error.Message); })); - app.UseCors("AllowConfiguredOrigins"); + app.UseCors(nameof(OptionsAtStartup.Web.Cors)); if (OptionsAtStartup.Web.Https.Force) { -- cgit v1.2.3 From 428bb614675c1a6b5fcecf5e0ccdafded7b30cde Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 24 Nov 2026 13:10:46 -0600 Subject: better guidance about wildcard --- src/slskd/Core/Options.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 69723b33..88301098 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -2415,9 +2415,17 @@ namespace slskd public IEnumerable Validate(ValidationContext validationContext) { - if (AllowedOrigins?.Count(x => x == "*") > 1) + if (AllowedOrigins?.Contains("*") ?? false) { - yield return new ValidationResult("Specify only one wildcard origin (\"*\")"); + 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 != "*")) -- cgit v1.2.3 From ca523f1e862a23c4a24cdf1fc87091a7bc1c71c4 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 24 Nov 2026 13:11:38 -0600 Subject: more CORS tests --- .../Core/Options/WebOptions.CorsOptionsTests.cs | 60 ++++++++++++++++++++-- 1 file changed, 56 insertions(+), 4 deletions(-) diff --git a/tests/slskd.Tests.Unit/Core/Options/WebOptions.CorsOptionsTests.cs b/tests/slskd.Tests.Unit/Core/Options/WebOptions.CorsOptionsTests.cs index a23a2818..782af616 100644 --- a/tests/slskd.Tests.Unit/Core/Options/WebOptions.CorsOptionsTests.cs +++ b/tests/slskd.Tests.Unit/Core/Options/WebOptions.CorsOptionsTests.cs @@ -63,10 +63,41 @@ public class CorsOptionsTests } [Fact] - public void Wildcard_and_valid_origin_produces_no_errors() + public void Wildcard_and_valid_origin_produces_error() { var options = new CorsOptions { AllowedOrigins = ["*", "https://example.com"] }; - Assert.Empty(Validate(options)); + var results = Validate(options); + Assert.Single(results); + Assert.Contains("other origins", results[0].ErrorMessage); + } + + [Fact] + public void Valid_origin_and_wildcard_produces_error() + { + var options = new CorsOptions { AllowedOrigins = ["https://example.com", "*"] }; + var results = Validate(options); + Assert.Single(results); + Assert.Contains("other origins", results[0].ErrorMessage); + } + + [Fact] + public void Multiple_wildcards_and_valid_origin_produces_both_errors() + { + var options = new CorsOptions { AllowedOrigins = ["*", "https://example.com", "*"] }; + var results = Validate(options); + Assert.Equal(2, results.Count); + Assert.Contains(results, r => r.ErrorMessage.Contains("only one wildcard")); + Assert.Contains(results, r => r.ErrorMessage.Contains("other origins")); + } + + [Fact] + public void Wildcard_and_invalid_origin_produces_errors_for_both() + { + var options = new CorsOptions { AllowedOrigins = ["*", "ftp://example.com"] }; + var results = Validate(options); + Assert.Equal(2, results.Count); + Assert.Contains(results, r => r.ErrorMessage.Contains("other origins")); + Assert.Contains(results, r => r.ErrorMessage.Contains("HTTP or HTTPS")); } [Theory] @@ -188,8 +219,15 @@ public class CorsOptionsTests { var options = new CorsOptions { AllowedOrigins = ["*", "https://example.com"], AllowCredentials = true }; var results = Validate(options); - Assert.Single(results); - Assert.Contains("Credentials", results[0].ErrorMessage); + Assert.Contains(results, r => r.ErrorMessage.Contains("Credentials")); + } + + [Fact] + public void Credentials_with_multiple_wildcards_produces_error() + { + var options = new CorsOptions { AllowedOrigins = ["*", "*"], AllowCredentials = true }; + var results = Validate(options); + Assert.Contains(results, r => r.ErrorMessage.Contains("Credentials")); } [Fact] @@ -205,5 +243,19 @@ public class CorsOptionsTests var options = new CorsOptions { AllowedOrigins = null, AllowCredentials = false }; Assert.Empty(Validate(options)); } + + [Fact] + public void No_credentials_with_empty_origins_produces_no_errors() + { + var options = new CorsOptions { AllowedOrigins = [], AllowCredentials = false }; + Assert.Empty(Validate(options)); + } + + [Fact] + public void No_credentials_with_wildcard_origin_produces_no_errors() + { + var options = new CorsOptions { AllowedOrigins = ["*"], AllowCredentials = false }; + Assert.Empty(Validate(options)); + } } } -- cgit v1.2.3 From d3d916810fcecd382c2d834b8ab9bf9888628456 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 24 Nov 2026 13:57:04 -0600 Subject: CORS docs --- docs/config.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/docs/config.md b/docs/config.md index d864458b..9a8ae564 100644 --- a/docs/config.md +++ b/docs/config.md @@ -992,6 +992,31 @@ web: cidr: 0.0.0.0/0,::/0 ``` +## Cross-Origin Resource Sharing (CORS) + +[CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CORS) controls whether a web page served from one origin (scheme, host, and port) is allowed by the browser to make requests to the API from another origin. This only applies to requests made by a browser; scripts, command-line tools, and other applications that call the API directly are not affected. + +Most users don't need to change these options. The web UI is served by the application itself, so requests it makes are not cross-origin. A common reason to configure CORS is to call the API from a custom web page or dashboard hosted somewhere else, such as `https://dashboard.example.com`. + +By default, the list of allowed origins is empty, which allows requests from any origin. A list containing a single wildcard (`*`) entry has the same effect. To restrict access, supply a list of the specific origins that should be allowed. + +Credentials (cookies and HTTP authentication) are not allowed in CORS requests by default. Credentials can only be allowed if a list of specific origins is supplied; attempting to allow credentials while all origins are allowed (an empty list or a wildcard) will cause a validation error. Note that JWTs and API keys are sent in headers and don't require this option. + +| Command-Line | Environment Variable | Description | +| --------------------------- | ------------------------------ | --------------------------------------------------- | +| `--cors-allowed-origins` | `SLSKD_CORS_ALLOWED_ORIGINS` | The list of allowed CORS origins | +| `--cors-allow-credentials` | `SLSKD_CORS_ALLOW_CREDENTIALS` | Determines whether CORS requests may include credentials | + +#### **YAML** +```yaml +web: + cors: + allowed_origins: + - https://dashboard.example.com + - http://localhost:3000 + allow_credentials: false +``` + # Filters A number of filters can be configured to control various aspects of how the application interacts with the Soulseek network. -- cgit v1.2.3 From 5129a0db393974cd82cd54ec16897ba201baaea5 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 24 Nov 2026 13:58:16 -0600 Subject: tweak example config --- config/slskd.example.yml | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/config/slskd.example.yml b/config/slskd.example.yml index 6cbca906..1f7e1a46 100644 --- a/config/slskd.example.yml +++ b/config/slskd.example.yml @@ -159,10 +159,6 @@ # url_base: / # content_path: wwwroot # logging: false -# cors: -# allowed_origins: -# - https://app.example.com -# allow_credentials: false # authentication: # disabled: false # username: slskd @@ -177,7 +173,7 @@ # cidr: 0.0.0.0/0,::/0 # cors: # allowed_origins: -# - * +# - https://app.example.com # allow_credentials: false # retention: # search: 10080 # 7 days, in minutes -- cgit v1.2.3 From 3d1a57c1b3bd22bdd480172d23c8b6fd350275a1 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 24 Nov 2026 14:05:58 -0600 Subject: require restart for cors options --- src/slskd/Core/Options.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 88301098..8c22eab7 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -2403,6 +2403,7 @@ namespace slskd [Argument(default, "cors-allowed-origins")] [EnvironmentVariable("CORS_ALLOWED_ORIGINS")] [Description("list of allowed CORS origins")] + [RequiresRestart] public string[] AllowedOrigins { get; init; } = null; /// @@ -2411,6 +2412,7 @@ namespace slskd [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 Validate(ValidationContext validationContext) -- cgit v1.2.3