Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Commit

Permalink
Added a check for an incomping corellation id header being an empty g…
Browse files Browse the repository at this point in the history
…uid, if detected then a bad request will be returned. (#5)
  • Loading branch information
chrisdexnimble authored Jul 11, 2023
1 parent d4a6b12 commit 13d2e5b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace Dfe.Academisation.CorrelationIdMiddleware;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System;
using System.Net;

/// <summary>
/// Middleware that checks incoming requests for a correlation and causation id header. If not found then default values will be created.
Expand Down Expand Up @@ -44,6 +46,21 @@ public Task Invoke(HttpContext httpContext, ICorrelationContext correlationConte
_logger.LogWarning("CorrelationIdMiddleware:Invoke - x-correlationId not detected in request headers. Generated a new one: {correlationId}", thisCorrelationId);
}

if (thisCorrelationId == Guid.Empty)
{
var result = new
{
StatusCode = (int)HttpStatusCode.BadRequest,
Message = $"Bad Request. {Keys.HeaderKey} header cannot be an empty GUID"
};


httpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
httpContext.Response.ContentType = "text/json";
return httpContext.Response.WriteAsync(result.ToString());
}


httpContext.Request.Headers[Keys.HeaderKey] = thisCorrelationId.ToString();

correlationContext.SetContext(thisCorrelationId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageTags>dfe;academisation;correlation;</PackageTags>
<UserSecretsId>4ac4e7ef-aaff-48a4-9e4d-44371c231191</UserSecretsId>
<PackageReadmeFile>README.md</PackageReadmeFile>
<Version>2.0.1</Version>
<Version>2.0.2</Version>
<Authors>DFE-Digital</Authors>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 2.0.2
Fixed passing in an empty GUID in the x-correlationId header causing an exception. Now if an empty GUID is detected a bad request will be returned.
If an empty guid is returned, the content of the response returned (along with the 400 status code) will be

`{ StatusCode = 400, Message = Bad Request. x-correlationId header cannot be an empty GUID }`

# 2.0.1
Fix to the log output so that `x-correlationId [guid]` is now `x-correlationId: [guid]

Expand Down

0 comments on commit 13d2e5b

Please sign in to comment.