forked from juunas11/aspnetcore-security-headers
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(CSP): add 'trusted-types' CSP directive support
add possibility to specify 'trusted-types' and 'require-trusted-types-for' CSP directives re juunas11#57
- Loading branch information
Showing
6 changed files
with
186 additions
and
2 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
64 changes: 64 additions & 0 deletions
64
src/Joonasw.AspNetCore.SecurityHeaders/Csp/Builder/CspTrustedTypesBuilder.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,64 @@ | ||
using System; | ||
using Joonasw.AspNetCore.SecurityHeaders.Csp.Options; | ||
|
||
namespace Joonasw.AspNetCore.SecurityHeaders.Csp.Builder | ||
{ | ||
/// <summary> | ||
/// Builder for Content Security Policy | ||
/// rules related to Trusted Types. | ||
/// </summary> | ||
public class CspTrustedTypesBuilder | ||
{ | ||
private readonly CspTrustedTypesOptions _options = new CspTrustedTypesOptions(); | ||
|
||
/// <summary> | ||
/// Disallows creating any Trusted Type policy (same as not specifying any <c>policyName</c>). | ||
/// </summary> | ||
public void DisallowAll() | ||
{ | ||
_options.AllowNone = true; | ||
} | ||
|
||
/// <summary> | ||
/// Allow any unique policy name ('allow-duplicates' may relax that further) | ||
/// </summary> | ||
/// <returns>The builder for call chaining</returns> | ||
public CspTrustedTypesBuilder WithAnyUniquePolicy() | ||
{ | ||
_options.AllowAny = true; | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Allow CSS from the given | ||
/// <paramref name="policyName"/>. | ||
/// </summary> | ||
/// <param name="policyName">A valid policy name consists only of alphanumeric characters, or one of "-#=_/@.%". </param> | ||
/// <returns>The builder for call chaining</returns> | ||
public CspTrustedTypesBuilder WithPolicyName(string policyName) | ||
{ | ||
if (policyName == null) throw new ArgumentNullException(nameof(policyName)); | ||
if (policyName.Length == 0) throw new ArgumentException("Policy Name can't be empty", nameof(policyName)); | ||
|
||
_options.TrustedPolicies.Add(policyName); | ||
return this; | ||
} | ||
|
||
public CspTrustedTypesBuilder AllowDuplicates() | ||
{ | ||
_options.AllowDuplicates = true; | ||
return this; | ||
} | ||
|
||
public CspTrustedTypesBuilder RequireTrustedTypesForScript() | ||
{ | ||
_options.RequireTrustedTypesForScript = true; | ||
return this; | ||
} | ||
|
||
public CspTrustedTypesOptions BuildOptions() | ||
{ | ||
return _options; | ||
} | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
src/Joonasw.AspNetCore.SecurityHeaders/Csp/Options/CspTrustedTypesOptions.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,94 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Joonasw.AspNetCore.SecurityHeaders.Csp.Options | ||
{ | ||
/// <summary> | ||
/// This directive declares an allow-list of trusted type policy names created with | ||
/// <c>TrustedTypes.createPolicy</c> from Trusted Types API | ||
/// </summary> | ||
public class CspTrustedTypesOptions | ||
{ | ||
/// <summary> | ||
/// Collection of sources from where these resources can be loaded. | ||
/// </summary> | ||
/// <remark> | ||
/// A valid policy name consists only of alphanumeric characters, or one of "-#=_/@.%". | ||
/// </remark> | ||
public ICollection<string> TrustedPolicies { get; set; } | ||
|
||
/// <summary> | ||
/// If <c>true</c> allows for creating policies with a name that was already used. | ||
/// </summary> | ||
public bool AllowDuplicates { get; set; } | ||
|
||
/// <summary> | ||
/// Disallows creating any Trusted Type policy (same as not specifying any <c>policyName</c>). | ||
/// </summary> | ||
public bool AllowNone { get; set; } | ||
|
||
/// <summary> | ||
/// A star (*) as a policy name instructs the user agent to allow any unique policy name | ||
/// (<c>'allow-duplicates'</c> may relax that further). | ||
/// </summary> | ||
public bool AllowAny { get; set; } | ||
|
||
/// <summary> | ||
/// Instructs user agents to control the data passed to DOM XSS sink functions, like Element.innerHTML setter. | ||
/// Those functions only accept non-spoofable, typed values created by Trusted Type policies, and reject strings. | ||
/// </summary> | ||
public bool RequireTrustedTypesForScript { get; set; } | ||
|
||
public CspTrustedTypesOptions() | ||
{ | ||
TrustedPolicies = new List<string>(); | ||
} | ||
|
||
private ICollection<string> GetParts() | ||
{ | ||
ICollection<string> parts = new List<string>(); | ||
|
||
if (AllowNone) | ||
{ | ||
parts.Add("'none'"); | ||
} | ||
else | ||
{ | ||
if (AllowAny) | ||
{ | ||
parts.Add("*"); | ||
} | ||
|
||
foreach (string allowedSource in TrustedPolicies) | ||
{ | ||
parts.Add(allowedSource); | ||
} | ||
|
||
if (AllowDuplicates) | ||
{ | ||
parts.Add("'allow-duplicates'"); | ||
} | ||
} | ||
return parts; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override string ToString() | ||
{ | ||
ICollection<string> parts = GetParts(); | ||
|
||
if (parts.Count == 0) | ||
{ | ||
return string.Empty; | ||
} | ||
|
||
var result = "trusted-types " + string.Join(" ", parts); | ||
|
||
if (RequireTrustedTypesForScript) | ||
{ | ||
result += "; require-trusted-types-for 'script'"; | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
} |
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
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