-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
71fe999
commit 8d6aee7
Showing
5 changed files
with
90 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...aries.OrchardCore/Security/ContentSecurityPolicyAttributeContentSecurityPolicyProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using Lombiq.HelpfulLibraries.AspNetCore.Security; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc.Controllers; | ||
using Microsoft.AspNetCore.Mvc.Infrastructure; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Reflection; | ||
using System.Threading.Tasks; | ||
using static Lombiq.HelpfulLibraries.AspNetCore.Security.ContentSecurityPolicyDirectives; | ||
using static Lombiq.HelpfulLibraries.AspNetCore.Security.ContentSecurityPolicyDirectives.CommonValues; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection; | ||
|
||
/// <summary> | ||
/// Indicates that the action's view should have the <c>script-src: unsafe-eval</c> content security policy directive. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Method)] | ||
public sealed class ScriptUnsafeEvalAttribute : ContentSecurityPolicyAttribute | ||
{ | ||
public ScriptUnsafeEvalAttribute() | ||
: base(UnsafeEval, ScriptSrc) | ||
{ | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Indicates that the action's view should have the provided content security policy directive. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Method)] | ||
[SuppressMessage( | ||
"Performance", | ||
"CA1813:Avoid unsealed attributes", | ||
Justification = $"Inherited by {nameof(ScriptUnsafeEvalAttribute)}.")] | ||
public class ContentSecurityPolicyAttribute : Attribute | ||
{ | ||
/// <summary> | ||
/// Gets the fallback chain of the directive, excluding <see cref="DefaultSrc"/>. This is used to get the current | ||
/// value. | ||
/// </summary> | ||
public string[] DirectiveNames { get; } | ||
|
||
/// <summary> | ||
/// Gets the value to be added to the directive. The content is split into words and added to the current values | ||
/// without repetition. | ||
/// </summary> | ||
public string DirectiveValue { get; } | ||
|
||
public ContentSecurityPolicyAttribute(string directiveValue, params string[] directiveNames) | ||
{ | ||
DirectiveValue = directiveValue; | ||
DirectiveNames = directiveNames; | ||
} | ||
} | ||
|
||
public class ContentSecurityPolicyAttributeContentSecurityPolicyProvider : IContentSecurityPolicyProvider | ||
{ | ||
public ValueTask UpdateAsync(IDictionary<string, string> securityPolicies, HttpContext context) | ||
{ | ||
if (context.RequestServices.GetService<IActionContextAccessor>() is { ActionContext: { } actionContext } && | ||
actionContext.ActionDescriptor is ControllerActionDescriptor actionDescriptor) | ||
{ | ||
foreach (var attribute in actionDescriptor.MethodInfo.GetCustomAttributes<ContentSecurityPolicyAttribute>()) | ||
{ | ||
securityPolicies[ScriptSrc] = IContentSecurityPolicyProvider | ||
.GetDirective(securityPolicies, attribute.DirectiveNames) | ||
.MergeWordSets(attribute.DirectiveValue); | ||
} | ||
} | ||
|
||
return ValueTask.CompletedTask; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 0 additions & 38 deletions
38
...lLibraries.OrchardCore/Security/ScriptUnsafeEvalAttributeContentSecurityPolicyProvider.cs
This file was deleted.
Oops, something went wrong.