-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from Lombiq/issue/TDEAL-16
TDEAL-16: Add Content-Security-Policy provider.
- Loading branch information
Showing
4 changed files
with
47 additions
and
4 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
41 changes: 41 additions & 0 deletions
41
Lombiq.JsonEditor/Services/JsonEditorContentSecurityPolicyProvider.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,41 @@ | ||
using Lombiq.HelpfulLibraries.AspNetCore.Security; | ||
using Lombiq.JsonEditor.Constants; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using static Lombiq.HelpfulLibraries.AspNetCore.Security.ContentSecurityPolicyDirectives; | ||
using static Lombiq.HelpfulLibraries.AspNetCore.Security.ContentSecurityPolicyDirectives.CommonValues; | ||
|
||
namespace Lombiq.JsonEditor.Services; | ||
|
||
/// <summary> | ||
/// Permits <c>blob: data:</c> access to the <see cref="WorkerSrc"/> on pages that include the <see | ||
/// cref="ResourceNames.Library"/> script. | ||
/// </summary> | ||
public class JsonEditorContentSecurityPolicyProvider : ResourceManagerContentSecurityPolicyProvider | ||
{ | ||
protected override string ResourceType => "script"; | ||
protected override string ResourceName => ResourceNames.Library; | ||
protected override IReadOnlyCollection<string> DirectiveNameChain { get; } = new[] { WorkerSrc, ScriptSrc }; | ||
protected override string DirectiveValue => $"{Blob} {Data}"; | ||
|
||
protected override ValueTask ThenUpdateAsync( | ||
IDictionary<string, string> securityPolicies, | ||
HttpContext context, | ||
bool resourceExists) | ||
{ | ||
// Fixes "[Severe] blob:https://localhost:9391/6a0aeee0-8ec7-449c-86b4-7668d046d24c 0 Refused to load the | ||
// script 'data:application/javascript;base64,...' because it violates the following Content Security Policy | ||
// directive" error. | ||
if (resourceExists) | ||
{ | ||
securityPolicies[ScriptSrc] = IContentSecurityPolicyProvider | ||
.GetDirective(securityPolicies, ScriptSrc) | ||
.MergeWordSets(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