Skip to content

Commit

Permalink
fixed cross-origin security issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Zergatul committed Jul 13, 2022
1 parent 99a7518 commit b14fe29
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
25 changes: 23 additions & 2 deletions src/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Linq;
using System.Net;
using Zergatul.Obs.InputOverlay.RawInput;
using Zergatul.Obs.InputOverlay.RawInput.Device;
using Zergatul.Obs.InputOverlay.XInput;
Expand All @@ -26,6 +30,8 @@ public void ConfigureServices(IServiceCollection services)

public void Configure(IApplicationBuilder app, IHostApplicationLifetime hostAppLifetime, IWebSocketHandler handler)
{
string[] addresses = app.ServerFeatures.Get<IServerAddressesFeature>().Addresses.ToArray();

app.UseDefaultFiles();
app.UseStaticFiles();
app.UseWebSockets();
Expand All @@ -39,9 +45,17 @@ public void Configure(IApplicationBuilder app, IHostApplicationLifetime hostAppL
{
if (context.Request.Path == "/ws" && context.WebSockets.IsWebSocketRequest)
{
using (var ws = await context.WebSockets.AcceptWebSocketAsync())
if (addresses.Any(a1 => context.Request.Headers.Origin.Any(a2 => OriginMatch(a1, a2))))
{
using (var ws = await context.WebSockets.AcceptWebSocketAsync())
{
await handler.HandleWebSocket(ws);
}
}
else
{
await handler.HandleWebSocket(ws);
// deny requests from other origins
context.Response.StatusCode = (int)HttpStatusCode.Forbidden;
}
}
else
Expand All @@ -50,5 +64,12 @@ public void Configure(IApplicationBuilder app, IHostApplicationLifetime hostAppL
}
});
}

private bool OriginMatch(string origin1, string origin2)
{
Uri uri1 = new Uri(origin1);
Uri uri2 = new Uri(origin2);
return uri1.Scheme == uri2.Scheme && string.Equals(uri1.Host, uri2.Host, StringComparison.OrdinalIgnoreCase) && uri1.Port == uri2.Port;
}
}
}
2 changes: 1 addition & 1 deletion src/Zergatul.Obs.InputOverlay.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<RootNamespace>Zergatul.Obs.InputOverlay</RootNamespace>
<Authors>Zergatul</Authors>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<Version>2.2.2</Version>
<Version>2.2.3</Version>
<SupportedOSPlatformVersion>7.0</SupportedOSPlatformVersion>
<StartupObject>Zergatul.Obs.InputOverlay.Program</StartupObject>
<PackageProjectUrl>https://github.com/Zergatul/Zergatul.Obs.InputOverlay</PackageProjectUrl>
Expand Down

0 comments on commit b14fe29

Please sign in to comment.