using System.Net; using System.Text.Json; using System.Text.RegularExpressions; using Flypaper.Core.Models; using Flypaper.Storage; using Flypaper.Storage.Entities; using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Shouldly; namespace Flypaper.Tests.Web; public sealed partial class RequestViewTests : IDisposable { private const string Password = "test-only-password"; private readonly string _dbPath = Path.Combine(Path.GetTempPath(), $"flypaper-request-{Guid.NewGuid():N}.db"); private readonly WebApplicationFactory _factory; public RequestViewTests() => _factory = new WebApplicationFactory().WithWebHostBuilder(builder => { builder.UseSetting("ConnectionStrings:Flypaper", $"Data Source={_dbPath}"); builder.UseSetting("Flypaper:Ports:Dashboard", "0"); builder.UseSetting("Flypaper:Admin:Password", Password); }); [Fact] public async Task Signed_in_users_get_the_request_and_attacker_markup_stays_out_of_the_page() { using var client = _factory.CreateClient(new WebApplicationFactoryClientOptions { AllowAutoRedirect = false }); var (withRequest, without) = await SeedAsync(); (await client.GetAsync($"/catches/{withRequest}/request")).StatusCode.ShouldBe(HttpStatusCode.Redirect); var token = TokenPattern().Match(await client.GetStringAsync("/login")).Groups[1].Value; await client.PostAsync("/auth/login", new FormUrlEncodedContent( [KeyValuePair.Create("password", Password), KeyValuePair.Create("__RequestVerificationToken", token)])); var response = await client.GetAsync($"/catches/{withRequest}/request"); response.Headers.CacheControl!.NoStore.ShouldBeTrue(); using var json = JsonDocument.Parse(await response.Content.ReadAsStringAsync()); json.RootElement.GetProperty("body").GetString().ShouldBe(""); json.RootElement.GetProperty("headers").GetString().ShouldNotBeNull().ShouldContain("Host: x"); var page = await client.GetStringAsync("/"); page.ShouldContain($"data-request=\"catches/{withRequest}/request\""); page.ShouldNotContain($"data-request=\"catches/{without}/request\""); page.ShouldNotContain(""); } private async Task<(long WithRequest, long Without)> SeedAsync() { await using var db = await _factory.Services.GetRequiredService>().CreateDbContextAsync(); var first = Entity(new RequestDetails("Host: x\n", "", 25, false)); var second = Entity(null); db.TrapEvents.AddRange(first, second); await db.SaveChangesAsync(); return (first.Id, second.Id); } private static TrapEventEntity Entity(RequestDetails? request) { var started = DateTimeOffset.UtcNow.AddMinutes(-1); return TrapEventEntity.From(new TrapEvent( Guid.NewGuid(), TrapProtocol.Http, IPAddress.Parse("203.0.113.70"), "maze-drip", started, started.AddSeconds(5), 10, TrapOutcome.ClientAborted, "POST /", null, request)); } [GeneratedRegex("name=\"__RequestVerificationToken\"[^>]*value=\"([^\"]+)\"")] private static partial Regex TokenPattern(); public void Dispose() { _factory.Dispose(); SqliteConnection.ClearAllPools(); foreach (var suffix in new[] { "", "-wal", "-shm" }) { File.Delete(_dbPath + suffix); } } }