-
Notifications
You must be signed in to change notification settings - Fork 44
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 #43 from MrMDavidson/add-require-sri-for
Adds support for "require-sri-for" value
- Loading branch information
Showing
8 changed files
with
154 additions
and
6 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
30 changes: 30 additions & 0 deletions
30
src/Joonasw.AspNetCore.SecurityHeaders/Csp/Builder/CspRequireSriBuilder.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,30 @@ | ||
using Joonasw.AspNetCore.SecurityHeaders.Csp.Options; | ||
|
||
namespace Joonasw.AspNetCore.SecurityHeaders.Csp.Builder { | ||
public class CspRequireSriBuilder { | ||
private readonly CspRequireSriOptions _options = new CspRequireSriOptions(); | ||
|
||
/// <summary> | ||
/// Require subresource integrity attributes for scripts loaded on this page | ||
/// </summary> | ||
public CspRequireSriBuilder ForScripts() | ||
{ | ||
_options.ForScripts = true; | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Require subresource integrity attributes for styles loaded on this page | ||
/// </summary> | ||
public CspRequireSriBuilder ForStyles() | ||
{ | ||
_options.ForStyles = true; | ||
return this; | ||
} | ||
|
||
public CspRequireSriOptions BuildOptions() | ||
{ | ||
return _options; | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/Joonasw.AspNetCore.SecurityHeaders/Csp/Options/CspRequireSriOptions.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,36 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Joonasw.AspNetCore.SecurityHeaders.Csp.Options { | ||
public class CspRequireSriOptions { | ||
/// <summary> | ||
/// If <c>true</c> subresource integrity attributes will be required for scripts loaded from this page | ||
/// </summary> | ||
public bool ForScripts { get; set; } | ||
|
||
/// <summary> | ||
/// If <c>true</c> subresource integrity attributes will be required for stylesheets loaded from this page | ||
/// </summary> | ||
public bool ForStyles { get; set; } | ||
|
||
/// <inheritdoc /> | ||
public override string ToString() { | ||
if ((ForScripts == false) && (ForStyles == false)) | ||
{ | ||
return string.Empty; | ||
} | ||
|
||
List<string> requiredOn = new List<string>(2); | ||
if (ForScripts == true) | ||
{ | ||
requiredOn.Add("script"); | ||
} | ||
if (ForStyles == true) | ||
{ | ||
requiredOn.Add("style"); | ||
} | ||
|
||
return $"require-sri-for {string.Join(" ", requiredOn)}"; | ||
|
||
} | ||
} | ||
} |
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
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
52 changes: 52 additions & 0 deletions
52
test/Joonasw.AspNetCore.SecurityHeaders.Tests/CspRequireSriBuilderTests.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,52 @@ | ||
using Joonasw.AspNetCore.SecurityHeaders.Csp.Builder; | ||
using Xunit; | ||
|
||
namespace Joonasw.AspNetCore.SecurityHeaders.Tests | ||
{ | ||
public class CspRequireSriBuilderTests | ||
{ | ||
[Fact] | ||
public void NoOptions_DoesNotGenerateHeader() | ||
{ | ||
var builder = new CspRequireSriBuilder(); | ||
|
||
var options = builder.BuildOptions(); | ||
|
||
Assert.True(string.IsNullOrWhiteSpace(options.ToString())); | ||
} | ||
|
||
[Fact] | ||
public void RequireScript_GeneratesHeader() | ||
{ | ||
var builder = new CspRequireSriBuilder(); | ||
builder.ForScripts(); | ||
|
||
var options = builder.BuildOptions(); | ||
|
||
Assert.Equal("require-sri-for script", options.ToString()); | ||
} | ||
|
||
[Fact] | ||
public void RequireStyle_GeneratesHeader() | ||
{ | ||
var builder = new CspRequireSriBuilder(); | ||
builder.ForStyles(); | ||
|
||
var options = builder.BuildOptions(); | ||
|
||
Assert.Equal("require-sri-for style", options.ToString()); | ||
} | ||
|
||
[Fact] | ||
public void RequireScriptAndStyle_GeneratesHeader() | ||
{ | ||
var builder = new CspRequireSriBuilder(); | ||
builder.ForScripts(); | ||
builder.ForStyles(); | ||
|
||
var options = builder.BuildOptions(); | ||
|
||
Assert.Equal("require-sri-for script style", options.ToString()); | ||
} | ||
} | ||
} |