Repository navigation
make call more secure by verifying estate in header against claims #604
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
Merged
Merged
Changes from all commits
Commits
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
56 changes: 56 additions & 0 deletions
56
EstateReportingAPI.IntegrationTests/TenantContextEndpointTests.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,56 @@ | ||
| using System; | ||
| using System.Net; | ||
| using System.Net.Http; | ||
| using System.Net.Http.Headers; | ||
| using System.Threading.Tasks; | ||
| using Xunit; | ||
| using Shouldly; | ||
|
|
||
| namespace EstateReportingAPI.IntegrationTests; | ||
|
|
||
| public sealed class TenantContextEndpointTests : ControllerTestsBase | ||
| { | ||
| [Theory] | ||
| [InlineData("api/calendars/comparisondates")] | ||
| [InlineData("api/contracts")] | ||
| [InlineData("api/estates")] | ||
| [InlineData("api/fileimportlogs")] | ||
| [InlineData("api/fileprofiles")] | ||
| [InlineData("api/merchants")] | ||
| [InlineData("api/operators")] | ||
| [InlineData("api/settlements/todayssettlements")] | ||
| [InlineData("api/transactions/todayssales")] | ||
| public async Task ProtectedEndpoint_DifferentEstateHeader_ReturnsForbidden(string route) | ||
| { | ||
| using HttpRequestMessage request = new(HttpMethod.Get, route); | ||
| request.Headers.Authorization = new AuthenticationHeaderValue("Test"); | ||
| request.Headers.Add("estateId", Guid.NewGuid().ToString()); | ||
|
|
||
| using HttpResponseMessage response = await this.Client.SendAsync(request); | ||
|
|
||
| response.StatusCode.ShouldBe(HttpStatusCode.Forbidden); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("health")] | ||
| [InlineData("healthui")] | ||
| public async Task HealthEndpoint_DoesNotRequireEstateContext(string route) | ||
| { | ||
| using HttpRequestMessage request = new(HttpMethod.Get, route); | ||
|
|
||
| using HttpResponseMessage response = await this.Client.SendAsync(request); | ||
|
|
||
| ((int)response.StatusCode).ShouldNotBe((int)HttpStatusCode.Unauthorized); | ||
| ((int)response.StatusCode).ShouldNotBe((int)HttpStatusCode.Forbidden); | ||
| } | ||
|
|
||
| protected override Task ClearStandingData() | ||
| { | ||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| protected override Task SetupStandingData() | ||
| { | ||
| return Task.CompletedTask; | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| using Microsoft.AspNetCore.Authorization; | ||
|
|
||
| namespace EstateReportingAPI.Common | ||
| { | ||
| public interface IEstateContext | ||
| { | ||
| Guid EstateId { get; } | ||
| } | ||
|
|
||
| public sealed class EstateContext : IEstateContext | ||
| { | ||
| public Guid EstateId { get; private set; } | ||
|
|
||
| internal void SetEstate(Guid estateId) | ||
| { | ||
| if (estateId == Guid.Empty) | ||
| throw new ArgumentException("Estate ID cannot be empty.", nameof(estateId)); | ||
|
|
||
| EstateId = estateId; | ||
| } | ||
| } | ||
|
|
||
| public sealed class EstateContextMiddleware | ||
| { | ||
| private readonly RequestDelegate _next; | ||
|
|
||
| public EstateContextMiddleware(RequestDelegate next) | ||
| { | ||
| _next = next; | ||
| } | ||
|
|
||
| public async Task InvokeAsync(HttpContext httpContext,EstateContext estateContext) | ||
| { | ||
| // Health endpoints or anonymous endpoints do not need estate context. | ||
| if (httpContext.GetEndpoint()?.Metadata | ||
| .GetMetadata<IAllowAnonymous>() != null) | ||
| { | ||
| await _next(httpContext); | ||
| return; | ||
| } | ||
|
|
||
| if (httpContext.User.Identity?.IsAuthenticated != true) | ||
| { | ||
| httpContext.Response.StatusCode = StatusCodes.Status401Unauthorized; | ||
| return; | ||
| } | ||
|
|
||
| string? estateClaim = httpContext.User.FindFirst("estateId")?.Value; | ||
|
|
||
| if (!Guid.TryParse(estateClaim, out Guid authenticatedEstateId) || | ||
| authenticatedEstateId == Guid.Empty) | ||
| { | ||
| httpContext.Response.StatusCode = StatusCodes.Status403Forbidden; | ||
| return; | ||
| } | ||
|
|
||
| // Backwards compatibility: if the header is supplied, it must agree | ||
| // with the authenticated claim. | ||
| if (httpContext.Request.Headers.TryGetValue("estateId", out var headerValue)) | ||
| { | ||
| if (!Guid.TryParse(headerValue.SingleOrDefault(), out Guid headerEstateId) || | ||
| headerEstateId != authenticatedEstateId) | ||
|
StuartFerguson marked this conversation as resolved.
Resolved
|
||
| { | ||
| httpContext.Response.StatusCode = StatusCodes.Status403Forbidden; | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| estateContext.SetEstate(authenticatedEstateId); | ||
|
|
||
| await _next(httpContext); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.