Headers and the first 8 KB of the body are stored unmasked for 30 days, read alongside the response so the trap answers as before. MCP get_request and the expanded catch row show them. Changelog prepared for 0.10.0. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
84 lines
3.6 KiB
C#
84 lines
3.6 KiB
C#
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<Program> _factory;
|
|
|
|
public RequestViewTests() =>
|
|
_factory = new WebApplicationFactory<Program>().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("<script>alert(1)</script>");
|
|
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("<script>alert(1)</script>");
|
|
}
|
|
|
|
private async Task<(long WithRequest, long Without)> SeedAsync()
|
|
{
|
|
await using var db = await _factory.Services.GetRequiredService<IDbContextFactory<FlypaperDbContext>>().CreateDbContextAsync();
|
|
var first = Entity(new RequestDetails("Host: x\n", "<script>alert(1)</script>", 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);
|
|
}
|
|
}
|
|
}
|