Remove trap paths and known hosts: catch everything on the trap port
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b9f2b0443c
commit
9a56fc5b48
31 files changed
+335
-462
No files matched your search
@@ -1,13 +1,11 @@
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using Flypaper.Core.Settings;
|
||||
using Flypaper.Http.Matching;
|
||||
using Flypaper.Http.Middleware;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.HttpOverrides;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Flypaper.Http;
|
||||
@@ -16,20 +14,18 @@ public static class FlypaperHttpExtensions
|
||||
{
|
||||
private static readonly string[] DefaultTrustedProxies = ["10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "fc00::/7"];
|
||||
|
||||
private static readonly string[] RemovedSettings =
|
||||
[
|
||||
"Flypaper:Http:KnownHosts",
|
||||
"Flypaper:Http:TrapPaths",
|
||||
"Flypaper:Http:ExtraKnownHosts",
|
||||
"Flypaper:Http:TrapPathsOverride"
|
||||
];
|
||||
|
||||
public static IServiceCollection AddFlypaperHttp(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.Configure<HttpTrapOptions>(configuration.GetSection(HttpTrapOptions.SectionName));
|
||||
services.Configure<PortOptions>(configuration.GetSection(PortOptions.SectionName));
|
||||
services.PostConfigure<HttpTrapOptions>(options =>
|
||||
{
|
||||
if (options.TrapPathsOverride is not null)
|
||||
{
|
||||
options.TrapPaths = SettingsList.Split(options.TrapPathsOverride);
|
||||
}
|
||||
|
||||
options.KnownHosts = options.KnownHosts.Concat(SettingsList.Split(options.ExtraKnownHosts)).Distinct(StringComparer.OrdinalIgnoreCase).ToArray();
|
||||
});
|
||||
services.TryAddSingleton<TrapMatcher>();
|
||||
|
||||
services.AddOptions<ForwardedHeadersOptions>()
|
||||
.Configure<IOptions<HttpTrapOptions>>((forwarded, trap) =>
|
||||
@@ -55,6 +51,16 @@ public static class FlypaperHttpExtensions
|
||||
return app;
|
||||
}
|
||||
|
||||
public static void WarnAboutRemovedSettings(IConfiguration configuration, ILogger logger)
|
||||
{
|
||||
foreach (var key in RemovedSettings.Where(key => configuration.GetSection(key).Exists()))
|
||||
{
|
||||
logger.LogWarning(
|
||||
"Setting {Key} is set but no longer has any effect: every request on the trap port is caught",
|
||||
key.Replace(":", "__", StringComparison.Ordinal));
|
||||
}
|
||||
}
|
||||
|
||||
private static int HostBits(string address) =>
|
||||
IPAddress.Parse(address).AddressFamily == AddressFamily.InterNetworkV6 ? 128 : 32;
|
||||
}
|
||||
@@ -6,13 +6,5 @@ public sealed class HttpTrapOptions
|
||||
|
||||
public const string HealthPath = "/health";
|
||||
|
||||
public string[] KnownHosts { get; set; } = [];
|
||||
|
||||
public string[] TrapPaths { get; set; } = [];
|
||||
|
||||
public string[] TrustedProxies { get; set; } = [];
|
||||
|
||||
public string? TrapPathsOverride { get; set; }
|
||||
|
||||
public string? ExtraKnownHosts { get; set; }
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
namespace Flypaper.Http.Matching;
|
||||
|
||||
public enum TrapMatch
|
||||
{
|
||||
None,
|
||||
UnknownHost,
|
||||
TrapPath
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Flypaper.Http.Matching;
|
||||
|
||||
public sealed class TrapMatcher : IDisposable
|
||||
{
|
||||
private readonly IDisposable? _subscription;
|
||||
private volatile Snapshot _snapshot;
|
||||
|
||||
public TrapMatcher(IOptionsMonitor<HttpTrapOptions> options)
|
||||
{
|
||||
_snapshot = new Snapshot(options.CurrentValue);
|
||||
_subscription = options.OnChange(updated => _snapshot = new Snapshot(updated));
|
||||
}
|
||||
|
||||
public TrapMatch Match(string? host, string? path)
|
||||
{
|
||||
var snapshot = _snapshot;
|
||||
if (!snapshot.IsKnownHost(host))
|
||||
{
|
||||
return TrapMatch.UnknownHost;
|
||||
}
|
||||
|
||||
return snapshot.IsTrapPath(path ?? "/") ? TrapMatch.TrapPath : TrapMatch.None;
|
||||
}
|
||||
|
||||
public void Dispose() => _subscription?.Dispose();
|
||||
|
||||
private sealed class Snapshot
|
||||
{
|
||||
private readonly HashSet<string> _hosts = new(StringComparer.OrdinalIgnoreCase);
|
||||
private readonly List<string> _hostSuffixes = [];
|
||||
private readonly HashSet<string> _paths = new(StringComparer.OrdinalIgnoreCase);
|
||||
private readonly List<string> _pathPrefixes = [];
|
||||
|
||||
public Snapshot(HttpTrapOptions options)
|
||||
{
|
||||
foreach (var host in options.KnownHosts.Where(h => !string.IsNullOrWhiteSpace(h)))
|
||||
{
|
||||
if (host.StartsWith("*.", StringComparison.Ordinal))
|
||||
{
|
||||
_hostSuffixes.Add(host[1..]);
|
||||
}
|
||||
else
|
||||
{
|
||||
_hosts.Add(host);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var path in options.TrapPaths.Where(p => !string.IsNullOrWhiteSpace(p)))
|
||||
{
|
||||
if (path.EndsWith('*'))
|
||||
{
|
||||
_pathPrefixes.Add(path[..^1]);
|
||||
}
|
||||
else
|
||||
{
|
||||
_paths.Add(TrimTrailingSlash(path));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsKnownHost(string? host) =>
|
||||
!string.IsNullOrEmpty(host)
|
||||
&& (_hosts.Contains(host)
|
||||
|| _hostSuffixes.Any(suffix => host.EndsWith(suffix, StringComparison.OrdinalIgnoreCase)));
|
||||
|
||||
public bool IsTrapPath(string path) =>
|
||||
_paths.Contains(TrimTrailingSlash(path))
|
||||
|| _pathPrefixes.Any(prefix => path.StartsWith(prefix, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
private static string TrimTrailingSlash(string path) => path.Length > 1 ? path.TrimEnd('/') : path;
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@ using Flypaper.Core.Abstractions;
|
||||
using Flypaper.Core.Models;
|
||||
using Flypaper.Core.Networking;
|
||||
using Flypaper.Core.Runtime;
|
||||
using Flypaper.Http.Matching;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Http.Features;
|
||||
using Microsoft.AspNetCore.Server.Kestrel.Core.Features;
|
||||
@@ -13,7 +12,6 @@ namespace Flypaper.Http.Middleware;
|
||||
|
||||
internal sealed class FlypaperMiddleware(
|
||||
RequestDelegate next,
|
||||
TrapMatcher matcher,
|
||||
TrapRunner runner,
|
||||
IOptions<PortOptions> ports,
|
||||
TimeProvider timeProvider)
|
||||
@@ -27,11 +25,6 @@ internal sealed class FlypaperMiddleware(
|
||||
}
|
||||
|
||||
var request = context.Request;
|
||||
if (matcher.Match(request.Host.Host, request.Path.Value) == TrapMatch.None)
|
||||
{
|
||||
context.Response.StatusCode = StatusCodes.Status404NotFound;
|
||||
return;
|
||||
}
|
||||
|
||||
var session = new TrapSession(
|
||||
Guid.NewGuid(),
|
||||
|
||||
Reference in new issue
Block a user