-
Notifications
You must be signed in to change notification settings - Fork 44
Jpk/report to #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jpknoll
wants to merge
6
commits into
juunas11:master
Choose a base branch
from
jpknoll:jpk/report-to
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Jpk/report to #41
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a44ba07
Add new report-to properties
jpknoll 7904b6c
Create Middleware and Options
jpknoll 8c4bd2a
Simple test
jpknoll 1dcd44e
Support multiple groups
jpknoll 03f89e9
init values array
jpknoll ffbb2a0
don't add this property
jpknoll File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
37 changes: 37 additions & 0 deletions
37
src/Joonasw.AspNetCore.SecurityHeaders/ReportTo/ReportToMiddleware.cs
This file contains hidden or 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,37 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Joonasw.AspNetCore.SecurityHeaders.ReportTo | ||
{ | ||
public class ReportToMiddleware | ||
{ | ||
private const string HeaderName = "Report-To"; | ||
private readonly RequestDelegate _next; | ||
private readonly string _headerValue; | ||
|
||
public ReportToMiddleware(RequestDelegate next, IOptions<ReportToOptions> options) | ||
{ | ||
_next = next; | ||
_headerValue = options.Value.BuildHeaderValue(); | ||
} | ||
|
||
public async Task Invoke(HttpContext context) | ||
{ | ||
// Check if a CSP header has already been added to the response | ||
// This can happen for example if a middleware re-executes the pipeline | ||
if (!ContainsReportToHeader(context.Response)) | ||
{ | ||
context.Response.Headers.Add(HeaderName, _headerValue); | ||
} | ||
await _next(context); | ||
} | ||
|
||
private bool ContainsReportToHeader(HttpResponse response) | ||
{ | ||
return response.Headers.Any(h => h.Key.Equals(HeaderName, StringComparison.OrdinalIgnoreCase)); | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/Joonasw.AspNetCore.SecurityHeaders/ReportTo/ReportToOptionsExtensions.cs
This file contains hidden or 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,61 @@ | ||
using Newtonsoft.Json; | ||
using System; | ||
|
||
namespace Joonasw.AspNetCore.SecurityHeaders.ReportTo | ||
{ | ||
internal static class ReportToOptionsExtensions | ||
{ | ||
public static string BuildHeaderValue(this ReportToOptions options) | ||
{ | ||
if (options == null) | ||
{ | ||
throw new ArgumentNullException(nameof(options)); | ||
} | ||
|
||
if (options.Groups == null || options.Groups.Count <= 0) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(options.Groups), "ReportToOptions must have at least one group"); | ||
} | ||
|
||
var values = new string[options.Groups.Count]; | ||
for (var i = 0; i < options.Groups.Count; i++) | ||
{ | ||
var group = options.Groups[i]; | ||
|
||
if (group.MaxAgeSeconds <= 0) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(group.MaxAgeSeconds), "ReportTo max age must be positive"); | ||
} | ||
|
||
if (group.Endpoints == null || group.Endpoints.Count <= 0) | ||
{ | ||
throw new ArgumentNullException(nameof(group.Endpoints), "At least one endpoint required"); | ||
} | ||
|
||
for (var j = 0; j < group.Endpoints.Count; j++) | ||
{ | ||
var e = group.Endpoints[j]; | ||
|
||
if (string.IsNullOrWhiteSpace(e.Url)) | ||
{ | ||
throw new ArgumentException($"{nameof(group.Endpoints)}[{j}].Url", "Url for endpoint required"); | ||
} | ||
|
||
if (e.Priority.HasValue && e.Priority <= 0) | ||
{ | ||
throw new ArgumentException($"{nameof(group.Endpoints)}[{j}].Priority", "Priority must be positive if present"); | ||
} | ||
|
||
if (e.Weight.HasValue && e.Weight <= 0) | ||
{ | ||
throw new ArgumentException($"{nameof(group.Endpoints)}[{j}].Weight", "Weight must be positive if present"); | ||
} | ||
} | ||
|
||
values[i] = JsonConvert.SerializeObject(group); | ||
} | ||
|
||
return string.Join(",\r\n", values); | ||
} | ||
} | ||
} |
This file contains hidden or 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,37 @@ | ||
using Newtonsoft.Json; | ||
using System.Collections.Generic; | ||
|
||
namespace Joonasw.AspNetCore.SecurityHeaders | ||
{ | ||
public class ReportToOptions | ||
{ | ||
public IList<Group> Groups { get; set; } = new List<Group>(); | ||
|
||
public class Group | ||
{ | ||
[JsonProperty("group")] | ||
public string GroupName { get; set; } | ||
|
||
[JsonProperty("include_subdomains")] | ||
public bool IncludeSubdomains { get; set; } | ||
|
||
[JsonProperty("max_age")] | ||
public int MaxAgeSeconds { get; set; } | ||
|
||
[JsonProperty("endpoints")] | ||
public IList<Endpoint> Endpoints { get; set; } = new List<Endpoint>(); | ||
|
||
public class Endpoint | ||
{ | ||
[JsonProperty("url")] | ||
public string Url { get; set; } | ||
|
||
[JsonProperty("priority")] | ||
public int? Priority { get; set; } | ||
|
||
[JsonProperty("weight")] | ||
public int? Weight { get; set; } | ||
} | ||
} | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
test/Joonasw.AspNetCore.SecurityHeaders.Tests/ReportToMiddlewareTests.cs
This file contains hidden or 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,86 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Joonasw.AspNetCore.SecurityHeaders.ReportTo; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Options; | ||
using Xunit; | ||
|
||
namespace Joonasw.AspNetCore.SecurityHeaders.Tests | ||
{ | ||
public class ReportToMiddlewareTests | ||
{ | ||
[Fact] | ||
public async Task ReportToHeaderSetCorrectly() | ||
{ | ||
bool reportToHeaderExists = false; | ||
RequestDelegate mockNext = (HttpContext ctx) => | ||
{ | ||
reportToHeaderExists = ctx.Response.Headers.ContainsKey("Report-To"); | ||
return Task.CompletedTask; | ||
}; | ||
var options = new ReportToOptions() | ||
{ | ||
Groups = new[] | ||
{ | ||
new ReportToOptions.Group { | ||
GroupName = "a", | ||
MaxAgeSeconds = 60, | ||
Endpoints = new [] | ||
{ | ||
new ReportToOptions.Group.Endpoint() { Url = "a" }, | ||
} | ||
}, | ||
}, | ||
}; | ||
|
||
var sut = new ReportToMiddleware(mockNext, Options.Create(options)); | ||
var mockContext = new DefaultHttpContext(); | ||
|
||
await sut.Invoke(mockContext); | ||
|
||
Assert.True(reportToHeaderExists); | ||
} | ||
|
||
[Fact] | ||
public async Task ReportToHeaderSupportsMultiple() | ||
{ | ||
bool reportToHeaderExists = false; | ||
RequestDelegate mockNext = (HttpContext ctx) => | ||
{ | ||
var header = ctx.Response.Headers["Report-To"]; | ||
reportToHeaderExists = !string.IsNullOrEmpty(header); | ||
return Task.CompletedTask; | ||
}; | ||
var options = new ReportToOptions() | ||
{ | ||
Groups = new[] | ||
{ | ||
new ReportToOptions.Group { | ||
GroupName = "a", | ||
MaxAgeSeconds = 60, | ||
Endpoints = new [] | ||
{ | ||
new ReportToOptions.Group.Endpoint() { Url = "a" }, | ||
} | ||
}, | ||
new ReportToOptions.Group { | ||
GroupName = "b", | ||
MaxAgeSeconds = 60, | ||
Endpoints = new [] | ||
{ | ||
new ReportToOptions.Group.Endpoint() { Url = "b" }, | ||
} | ||
}, | ||
}, | ||
}; | ||
|
||
var sut = new ReportToMiddleware(mockNext, Options.Create(options)); | ||
var mockContext = new DefaultHttpContext(); | ||
|
||
await sut.Invoke(mockContext); | ||
|
||
Assert.True(reportToHeaderExists); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.