Skip to content

Commit

Permalink
Sanitize CorrelationId header before using it (#1067)
Browse files Browse the repository at this point in the history
* fix: remove leading and trailing whitespaces

* fix: remove line endings

* fix: limit length to 100

* chore: don't use IgnoreAntiforgeryToken for /connect/token endpoint
  • Loading branch information
tnotheis authored Mar 4, 2025
1 parent 04599ac commit 53a6a45
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public AuthorizationController(
}

[HttpPost("~/connect/token")]
[IgnoreAntiforgeryToken]
[Produces("application/json")]
[Consumes("application/x-www-form-urlencoded")]
[AllowAnonymous]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Backbone.BuildingBlocks.Infrastructure.CorrelationIds;
using Backbone.Tooling.Extensions;
using Microsoft.AspNetCore.Http;

namespace Backbone.BuildingBlocks.API.Mvc.Middleware;
Expand All @@ -20,6 +21,10 @@ public async Task InvokeAsync(HttpContext context)
{
correlationId = CustomLogContext.GenerateCorrelationId();
}
else
{
correlationId = correlationId.Trim().ReplaceLineEndings("").TruncateToXChars(100);
}

context.Response.Headers["X-Correlation-ID"] = correlationId;

Expand Down
5 changes: 5 additions & 0 deletions BuildingBlocks/src/Tooling/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public static bool MatchesRegex(this string text, string regexString)
return regex.IsMatch(text);
}

public static string TruncateToXChars(this string text, int maxLength)
{
return text[..Math.Min(text.Length, maxLength)];
}

public static byte[] GetBytes(this string text)
{
return Encoding.UTF8.GetBytes(text);
Expand Down

0 comments on commit 53a6a45

Please sign in to comment.