aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-11-22 22:32:43 -0600
committerGitHub <noreply@github.com>2026-11-22 22:32:43 -0600
commite4d5df04e3a4f735c8172e52a8338472754af644 (patch)
tree4b26cdace1ba1b2e642885808bca728b6e62491a /src
parentf4accbf9f6437e49374682eacaf520010de5c775 (diff)
parent2b31f8a4d50ac347929cddb7170a0e54dfdf47c1 (diff)
downloadslskd-e4d5df04e3a4f735c8172e52a8338472754af644.tar.gz
Merge pull request #1775 from samhatter/stricter-cors
stricter and configurable cors
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Core/Options.cs60
-rw-r--r--src/slskd/Program.cs36
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)
{