using Flypaper.Core; using Flypaper.Core.Strategies; using Flypaper.Http; using Flypaper.Storage; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Options; using Shouldly; namespace Flypaper.Tests; public sealed class SettingsOverrideTests : IDisposable { private readonly string _dbPath = Path.Combine(Path.GetTempPath(), $"flypaper-test-{Guid.NewGuid():N}.db"); private IHost? _host; [Fact] public async Task Db_overrides_startup_values_for_whitelisted_keys_only() { await using (var db = new FlypaperDbContext(new DbContextOptionsBuilder().UseSqlite($"Data Source={_dbPath}").Options)) { await db.Database.MigrateAsync(); db.Settings.AddRange( new SettingEntity { Key = SettingKeys.MaxConnections, Value = "5" }, new SettingEntity { Key = SettingKeys.TrapPaths, Value = "/custom\n/other/*" }, new SettingEntity { Key = SettingKeys.ExtraKnownHosts, Value = "extra.example" }, new SettingEntity { Key = "Flypaper:Admin:Password", Value = "evil" }); await db.SaveChangesAsync(); } var host = BuildHost(); await host.Services.MigrateFlypaperDatabaseAsync(); var core = host.Services.GetRequiredService>().CurrentValue; var http = host.Services.GetRequiredService>().CurrentValue; var configuration = host.Services.GetRequiredService(); core.MaxConnections.ShouldBe(5); core.MaxConnectionsPerIp.ShouldBe(10); http.TrapPaths.ShouldBe(["/custom", "/other/*"]); http.KnownHosts.ShouldBe(["dashboard.lan", "extra.example"]); http.TrustedProxies.ShouldBe(["172.18.0.0/16"]); configuration["Flypaper:Admin:Password"].ShouldBe("secret"); } [Fact] public async Task Missing_db_overrides_leave_startup_values_untouched() { var host = BuildHost(); await host.Services.MigrateFlypaperDatabaseAsync(); host.Services.GetRequiredService>().CurrentValue.MaxConnections.ShouldBe(100); host.Services.GetRequiredService>().CurrentValue.TrapPaths.ShouldBe(["/.env"]); } [Fact] public async Task Saving_reloads_trap_matcher_and_reset_restores_startup_values() { var host = BuildHost(); await host.Services.MigrateFlypaperDatabaseAsync(); var matcher = host.Services.GetRequiredService(); var store = host.Services.GetRequiredService(); matcher.Match("dashboard.lan", "/custom").ShouldBe(TrapMatch.None); matcher.Match("dashboard.lan", "/.env").ShouldBe(TrapMatch.TrapPath); matcher.Match("extra.example", "/").ShouldBe(TrapMatch.UnknownHost); await store.SaveAsync(new RuntimeSettings( ["/custom"], TimeSpan.FromSeconds(3), 50, 4, TimeSpan.FromMinutes(10), ["extra.example"])); matcher.Match("dashboard.lan", "/custom").ShouldBe(TrapMatch.TrapPath); matcher.Match("dashboard.lan", "/.env").ShouldBe(TrapMatch.None); matcher.Match("extra.example", "/").ShouldBe(TrapMatch.None); host.Services.GetRequiredService>().CurrentValue.Interval.ShouldBe(TimeSpan.FromSeconds(3)); (await store.HasOverridesAsync()).ShouldBeTrue(); await store.ResetAsync(); matcher.Match("dashboard.lan", "/custom").ShouldBe(TrapMatch.None); matcher.Match("dashboard.lan", "/.env").ShouldBe(TrapMatch.TrapPath); (await store.HasOverridesAsync()).ShouldBeFalse(); } [Fact] public async Task Saved_empty_trap_path_list_disables_path_traps() { var host = BuildHost(); await host.Services.MigrateFlypaperDatabaseAsync(); var matcher = host.Services.GetRequiredService(); await host.Services.GetRequiredService().SaveAsync(new RuntimeSettings( [], TimeSpan.FromSeconds(3), 50, 4, TimeSpan.FromMinutes(10), [])); matcher.Match("dashboard.lan", "/.env").ShouldBe(TrapMatch.None); } private IHost BuildHost() { var builder = Host.CreateApplicationBuilder(new HostApplicationBuilderSettings { DisableDefaults = true }); builder.Configuration.AddInMemoryCollection(new Dictionary { ["ConnectionStrings:Flypaper"] = $"Data Source={_dbPath}", ["Flypaper:MaxConnections"] = "100", ["Flypaper:MaxConnectionsPerIp"] = "10", ["Flypaper:Http:TrapPaths:0"] = "/.env", ["Flypaper:Http:KnownHosts:0"] = "dashboard.lan", ["Flypaper:Http:TrustedProxies:0"] = "172.18.0.0/16", ["Flypaper:Admin:Password"] = "secret" }); builder.AddFlypaperStorage(); builder.Services.AddLogging(); builder.Services.AddFlypaperCore(builder.Configuration); builder.Services.AddFlypaperHttp(builder.Configuration); return _host = builder.Build(); } public void Dispose() { _host?.Dispose(); SqliteConnection.ClearAllPools(); foreach (var suffix in new[] { "", "-wal", "-shm" }) { File.Delete(_dbPath + suffix); } } }