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

Closes #2371 #2372

Merged
merged 5 commits into from
Jul 12, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
26 changes: 21 additions & 5 deletions ArchiSteamFarm/IPC/Integration/ApiAuthenticationMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,32 @@ private static async Task<HttpStatusCode> GetAuthenticationStatus(HttpContext co
throw new InvalidOperationException(nameof(ClearFailedAuthorizationsTimer));
}

IPAddress? clientIP = context.Connection.RemoteIpAddress;

if (clientIP == null) {
throw new InvalidOperationException(nameof(clientIP));
}

string? ipcPassword = ASF.GlobalConfig != null ? ASF.GlobalConfig.IPCPassword : GlobalConfig.DefaultIPCPassword;
JustArchi marked this conversation as resolved.
Show resolved Hide resolved

if (string.IsNullOrEmpty(ipcPassword)) {
return HttpStatusCode.OK;
}
if (IPAddress.IsLoopback(clientIP)) {
return HttpStatusCode.OK;
}

IPAddress? clientIP = context.Connection.RemoteIpAddress;
if (Startup.KnownNetworks.IsEmpty) {
JustArchi marked this conversation as resolved.
Show resolved Hide resolved
return HttpStatusCode.Forbidden;
}

if (clientIP == null) {
throw new InvalidOperationException(nameof(clientIP));
if (clientIP.IsIPv4MappedToIPv6) {
IPAddress mappedClientIP = clientIP.MapToIPv4();

if (Startup.KnownNetworks.Any(network => network.Contains(mappedClientIP))) {
return HttpStatusCode.OK;
}
}

return Startup.KnownNetworks.Any(network => network.Contains(clientIP)) ? HttpStatusCode.OK : HttpStatusCode.Forbidden;
}

if (FailedAuthorizations.TryGetValue(clientIP, out byte attempts)) {
Expand Down
22 changes: 12 additions & 10 deletions ArchiSteamFarm/IPC/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#endif
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Globalization;
using System.Net;
using System.Reflection;
Expand All @@ -53,6 +54,8 @@

namespace ArchiSteamFarm.IPC {
internal sealed class Startup {
internal static ImmutableHashSet<IPNetwork> KnownNetworks { get; private set; } = ImmutableHashSet<IPNetwork>.Empty;

private readonly IConfiguration Configuration;

public Startup(IConfiguration configuration) => Configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
Expand Down Expand Up @@ -148,12 +151,12 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
app.UseRouting();
#endif

// We want to protect our API with IPCPassword and additional security, this should be called after routing, so the middleware won't have to deal with API endpoints that do not exist
app.UseWhen(context => context.Request.Path.StartsWithSegments("/Api", StringComparison.OrdinalIgnoreCase), appBuilder => appBuilder.UseMiddleware<ApiAuthenticationMiddleware>());

string? ipcPassword = ASF.GlobalConfig != null ? ASF.GlobalConfig.IPCPassword : GlobalConfig.DefaultIPCPassword;
JustArchi marked this conversation as resolved.
Show resolved Hide resolved

if (!string.IsNullOrEmpty(ipcPassword)) {
// We want to protect our API with IPCPassword, this should be called after routing, so the middleware won't have to deal with API endpoints that do not exist
app.UseWhen(context => context.Request.Path.StartsWithSegments("/Api", StringComparison.OrdinalIgnoreCase), appBuilder => appBuilder.UseMiddleware<ApiAuthenticationMiddleware>());

// We want to apply CORS policy in order to allow userscripts and other third-party integrations to communicate with ASF API, this should be called before response compression, but can't be due to how our flow works
// We apply CORS policy only with IPCPassword set as an extra authentication measure
app.UseCors();
Expand Down Expand Up @@ -194,10 +197,9 @@ public void ConfigureServices(IServiceCollection services) {
// Prepare knownNetworks that we'll use in a second
HashSet<string>? knownNetworksTexts = Configuration.GetSection("Kestrel:KnownNetworks").Get<HashSet<string>>();

HashSet<IPNetwork>? knownNetworks = null;

if (knownNetworksTexts?.Count > 0) {
knownNetworks = new HashSet<IPNetwork>(knownNetworksTexts.Count);
// Use specified known networks
HashSet<IPNetwork> knownNetworks = new();

foreach (string knownNetworkText in knownNetworksTexts) {
string[] addressParts = knownNetworkText.Split('/', StringSplitOptions.RemoveEmptyEntries);
Expand All @@ -211,17 +213,17 @@ public void ConfigureServices(IServiceCollection services) {

knownNetworks.Add(new IPNetwork(ipAddress, prefixLength));
}

KnownNetworks = knownNetworks.ToImmutableHashSet();
}

// Add support for proxies
services.Configure<ForwardedHeadersOptions>(
options => {
options.ForwardedHeaders = ForwardedHeaders.All;

if (knownNetworks != null) {
foreach (IPNetwork knownNetwork in knownNetworks) {
options.KnownNetworks.Add(knownNetwork);
}
foreach (IPNetwork knownNetwork in KnownNetworks) {
options.KnownNetworks.Add(knownNetwork);
JustArchi marked this conversation as resolved.
Show resolved Hide resolved
}
}
);
Expand Down