Skip to content
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

Sanitize CorrelationId header before using it #1067

Merged
merged 7 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading