Merge pull request #4013 from crobibero/dynamic-cors
Allow CORS domains to be configured
This commit is contained in:
commit
d08ddbb8d2
49
Jellyfin.Server/Configuration/CorsPolicyProvider.cs
Normal file
49
Jellyfin.Server/Configuration/CorsPolicyProvider.cs
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using MediaBrowser.Controller.Configuration;
|
||||||
|
using Microsoft.AspNetCore.Cors.Infrastructure;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
|
||||||
|
namespace Jellyfin.Server.Configuration
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Cors policy provider.
|
||||||
|
/// </summary>
|
||||||
|
public class CorsPolicyProvider : ICorsPolicyProvider
|
||||||
|
{
|
||||||
|
private readonly IServerConfigurationManager _serverConfigurationManager;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="CorsPolicyProvider"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
|
||||||
|
public CorsPolicyProvider(IServerConfigurationManager serverConfigurationManager)
|
||||||
|
{
|
||||||
|
_serverConfigurationManager = serverConfigurationManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public Task<CorsPolicy> GetPolicyAsync(HttpContext context, string policyName)
|
||||||
|
{
|
||||||
|
var corsHosts = _serverConfigurationManager.Configuration.CorsHosts;
|
||||||
|
var builder = new CorsPolicyBuilder()
|
||||||
|
.AllowAnyMethod()
|
||||||
|
.AllowAnyHeader();
|
||||||
|
|
||||||
|
// No hosts configured or only default configured.
|
||||||
|
if (corsHosts.Length == 0
|
||||||
|
|| (corsHosts.Length == 1
|
||||||
|
&& string.Equals(corsHosts[0], CorsConstants.AnyOrigin, StringComparison.Ordinal)))
|
||||||
|
{
|
||||||
|
builder.AllowAnyOrigin();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
builder.WithOrigins(corsHosts)
|
||||||
|
.AllowCredentials();
|
||||||
|
}
|
||||||
|
|
||||||
|
return Task.FromResult(builder.Build());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -15,13 +15,15 @@ using Jellyfin.Api.Auth.LocalAccessPolicy;
|
||||||
using Jellyfin.Api.Auth.RequiresElevationPolicy;
|
using Jellyfin.Api.Auth.RequiresElevationPolicy;
|
||||||
using Jellyfin.Api.Constants;
|
using Jellyfin.Api.Constants;
|
||||||
using Jellyfin.Api.Controllers;
|
using Jellyfin.Api.Controllers;
|
||||||
|
using Jellyfin.Server.Configuration;
|
||||||
using Jellyfin.Server.Formatters;
|
using Jellyfin.Server.Formatters;
|
||||||
using Jellyfin.Server.Models;
|
using Jellyfin.Server.Middleware;
|
||||||
using MediaBrowser.Common.Json;
|
using MediaBrowser.Common.Json;
|
||||||
using MediaBrowser.Model.Entities;
|
using MediaBrowser.Model.Entities;
|
||||||
using Microsoft.AspNetCore.Authentication;
|
using Microsoft.AspNetCore.Authentication;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.AspNetCore.Cors.Infrastructure;
|
||||||
using Microsoft.AspNetCore.HttpOverrides;
|
using Microsoft.AspNetCore.HttpOverrides;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.OpenApi.Models;
|
using Microsoft.OpenApi.Models;
|
||||||
|
@ -138,10 +140,8 @@ namespace Jellyfin.Server.Extensions
|
||||||
public static IMvcBuilder AddJellyfinApi(this IServiceCollection serviceCollection, IEnumerable<Assembly> pluginAssemblies)
|
public static IMvcBuilder AddJellyfinApi(this IServiceCollection serviceCollection, IEnumerable<Assembly> pluginAssemblies)
|
||||||
{
|
{
|
||||||
IMvcBuilder mvcBuilder = serviceCollection
|
IMvcBuilder mvcBuilder = serviceCollection
|
||||||
.AddCors(options =>
|
.AddCors()
|
||||||
{
|
.AddTransient<ICorsPolicyProvider, CorsPolicyProvider>()
|
||||||
options.AddPolicy(ServerCorsPolicy.DefaultPolicyName, ServerCorsPolicy.DefaultPolicy);
|
|
||||||
})
|
|
||||||
.Configure<ForwardedHeadersOptions>(options =>
|
.Configure<ForwardedHeadersOptions>(options =>
|
||||||
{
|
{
|
||||||
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
|
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
|
||||||
|
|
|
@ -1,30 +0,0 @@
|
||||||
using Microsoft.AspNetCore.Cors.Infrastructure;
|
|
||||||
|
|
||||||
namespace Jellyfin.Server.Models
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Server Cors Policy.
|
|
||||||
/// </summary>
|
|
||||||
public static class ServerCorsPolicy
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Default policy name.
|
|
||||||
/// </summary>
|
|
||||||
public const string DefaultPolicyName = "DefaultCorsPolicy";
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Default Policy. Allow Everything.
|
|
||||||
/// </summary>
|
|
||||||
public static readonly CorsPolicy DefaultPolicy = new CorsPolicy
|
|
||||||
{
|
|
||||||
// Allow any origin
|
|
||||||
Origins = { "*" },
|
|
||||||
|
|
||||||
// Allow any method
|
|
||||||
Methods = { "*" },
|
|
||||||
|
|
||||||
// Allow any header
|
|
||||||
Headers = { "*" }
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -5,7 +5,6 @@ using Jellyfin.Api.TypeConverters;
|
||||||
using Jellyfin.Server.Extensions;
|
using Jellyfin.Server.Extensions;
|
||||||
using Jellyfin.Server.Implementations;
|
using Jellyfin.Server.Implementations;
|
||||||
using Jellyfin.Server.Middleware;
|
using Jellyfin.Server.Middleware;
|
||||||
using Jellyfin.Server.Models;
|
|
||||||
using MediaBrowser.Common.Net;
|
using MediaBrowser.Common.Net;
|
||||||
using MediaBrowser.Controller;
|
using MediaBrowser.Controller;
|
||||||
using MediaBrowser.Controller.Configuration;
|
using MediaBrowser.Controller.Configuration;
|
||||||
|
@ -116,7 +115,7 @@ namespace Jellyfin.Server
|
||||||
|
|
||||||
mainApp.UseResponseCompression();
|
mainApp.UseResponseCompression();
|
||||||
|
|
||||||
mainApp.UseCors(ServerCorsPolicy.DefaultPolicyName);
|
mainApp.UseCors();
|
||||||
|
|
||||||
if (_serverConfigurationManager.Configuration.RequireHttps
|
if (_serverConfigurationManager.Configuration.RequireHttps
|
||||||
&& _serverApplicationHost.ListenWithHttps)
|
&& _serverApplicationHost.ListenWithHttps)
|
||||||
|
|
|
@ -263,6 +263,11 @@ namespace MediaBrowser.Model.Configuration
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public long SlowResponseThresholdMs { get; set; }
|
public long SlowResponseThresholdMs { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the cors hosts.
|
||||||
|
/// </summary>
|
||||||
|
public string[] CorsHosts { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="ServerConfiguration" /> class.
|
/// Initializes a new instance of the <see cref="ServerConfiguration" /> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -372,6 +377,7 @@ namespace MediaBrowser.Model.Configuration
|
||||||
|
|
||||||
EnableSlowResponseWarning = true;
|
EnableSlowResponseWarning = true;
|
||||||
SlowResponseThresholdMs = 500;
|
SlowResponseThresholdMs = 500;
|
||||||
|
CorsHosts = new[] { "*" };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user