Skip to content

Commit

Permalink
sanitize relative url
Browse files Browse the repository at this point in the history
  • Loading branch information
JerryImMouse committed Jun 15, 2024
1 parent 1db725a commit 9397c9a
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions Jerry.Utilities/HttpUtility/MainListener.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Net;
using System.Text.Encodings.Web;
using System.Text.RegularExpressions;
using Jerry.Utilities.Logging;
using Jerry.Utilities.Logging.LogStructs;

Expand All @@ -7,7 +9,7 @@ namespace Jerry.Utilities.HttpUtility;
/// <summary>
/// Wrapper around <see cref="HttpListener"/> for ease use, provides simple interface to "talk" with some program using HTTP or creating an api
/// </summary>
public class MainListener
public sealed class MainListener
{
private HttpListener _nativeListener = new();
public IHandlerGroup? HandlerGroup = null;
Expand All @@ -23,7 +25,7 @@ public async Task StartListenAsync()
{
var rawCtx = await _nativeListener.GetContextAsync();
var ctx = new HttpContext(rawCtx);
Logger.Info($"Processing incoming connections from {ctx.RemoteEndPoint.Address}:{ctx.RemoteEndPoint.Port} to {ctx.RelativeUrl}");
Logger.Info($"Processing incoming connections from {ctx.RemoteEndPoint.Address}:{ctx.RemoteEndPoint.Port} to {Sanitize(ctx.RelativeUrl)}");

// we are not waiting for task finishing
Task.Run(() => ProcessContextAsync(ctx));
Expand All @@ -50,4 +52,10 @@ public void RegisterHandler(Func<HttpContext, Task<bool>> handler)
{
_handlers.Add(handler);
}

private string Sanitize(string str)
{
var reg = new Regex(@"[^\w\.@-]");
return reg.Replace(str, "_");
}
}

0 comments on commit 9397c9a

Please sign in to comment.