Skip to content

Commit 02b8ac2

Browse files
authored
.NET: sanitize redirectUrl for logs (#2356)
* sanitize redirectUrl for logs * use basepath * sanitize both path and reddirect url
1 parent 7148718 commit 02b8ac2

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

dotnet/src/Microsoft.Agents.AI.DevUI/DevUIMiddleware.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.IO.Compression;
55
using System.Reflection;
66
using System.Security.Cryptography;
7+
using System.Text.RegularExpressions;
78
using Microsoft.AspNetCore.StaticFiles;
89
using Microsoft.Extensions.Primitives;
910
using Microsoft.Net.Http.Headers;
@@ -13,8 +14,11 @@ namespace Microsoft.Agents.AI.DevUI;
1314
/// <summary>
1415
/// Handler that serves embedded DevUI resource files from the 'resources' directory.
1516
/// </summary>
16-
internal sealed class DevUIMiddleware
17+
internal sealed partial class DevUIMiddleware
1718
{
19+
[GeneratedRegex(@"[\r\n]+")]
20+
private static partial Regex NewlineRegex();
21+
1822
private const string GZipEncodingValue = "gzip";
1923
private static readonly StringValues s_gzipEncodingHeader = new(GZipEncodingValue);
2024
private static readonly Assembly s_assembly = typeof(DevUIMiddleware).Assembly;
@@ -70,15 +74,16 @@ public async Task HandleRequestAsync(HttpContext context)
7074
// This ensures relative URLs in the HTML work correctly
7175
if (string.Equals(path, this._basePath, StringComparison.OrdinalIgnoreCase) && !path.EndsWith('/'))
7276
{
73-
var redirectUrl = $"{path}/";
77+
var redirectUrl = this._basePath + "/";
7478
if (context.Request.QueryString.HasValue)
7579
{
7680
redirectUrl += context.Request.QueryString.Value;
7781
}
7882

7983
context.Response.StatusCode = StatusCodes.Status301MovedPermanently;
8084
context.Response.Headers.Location = redirectUrl;
81-
this._logger.LogDebug("Redirecting {OriginalPath} to {RedirectUrl}", path, redirectUrl);
85+
86+
this._logger.LogDebug("Redirecting {OriginalPath} to {RedirectUrl}", NewlineRegex().Replace(path, ""), NewlineRegex().Replace(redirectUrl, ""));
8287
return;
8388
}
8489

0 commit comments

Comments
 (0)