Skip to content

Commit 53a6a45

Browse files
authored
Sanitize CorrelationId header before using it (#1067)
* fix: remove leading and trailing whitespaces * fix: remove line endings * fix: limit length to 100 * chore: don't use IgnoreAntiforgeryToken for /connect/token endpoint
1 parent 04599ac commit 53a6a45

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

Applications/ConsumerApi/src/Controllers/AuthorizationController.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public AuthorizationController(
3434
}
3535

3636
[HttpPost("~/connect/token")]
37-
[IgnoreAntiforgeryToken]
3837
[Produces("application/json")]
3938
[Consumes("application/x-www-form-urlencoded")]
4039
[AllowAnonymous]

BuildingBlocks/src/BuildingBlocks.API/Mvc/Middleware/CorrelationIdMiddleware.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Backbone.BuildingBlocks.Infrastructure.CorrelationIds;
2+
using Backbone.Tooling.Extensions;
23
using Microsoft.AspNetCore.Http;
34

45
namespace Backbone.BuildingBlocks.API.Mvc.Middleware;
@@ -20,6 +21,10 @@ public async Task InvokeAsync(HttpContext context)
2021
{
2122
correlationId = CustomLogContext.GenerateCorrelationId();
2223
}
24+
else
25+
{
26+
correlationId = correlationId.Trim().ReplaceLineEndings("").TruncateToXChars(100);
27+
}
2328

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

BuildingBlocks/src/Tooling/Extensions/StringExtensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ public static bool MatchesRegex(this string text, string regexString)
2222
return regex.IsMatch(text);
2323
}
2424

25+
public static string TruncateToXChars(this string text, int maxLength)
26+
{
27+
return text[..Math.Min(text.Length, maxLength)];
28+
}
29+
2530
public static byte[] GetBytes(this string text)
2631
{
2732
return Encoding.UTF8.GetBytes(text);

0 commit comments

Comments
 (0)