aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Core/Options.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/slskd/Core/Options.cs')
-rw-r--r--src/slskd/Core/Options.cs60
1 files changed, 60 insertions, 0 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>