Files
openwaf/dotnet/OpenWaf.Tests.Integration/FakeUpstream.cs
T
Pavlo Ratushnyi a3d0777492 openwaf: edge WAF with one decision point and a streak ban
Initial implementation of the OPEN-2 epic: a .NET 10 YARP edge that
decides from what the upstream answered.

Design (docs/ARCHITECTURE.md, tracker authority 4c9de3f6):
- one short-circuit point, ordered: always-pass prefixes -> allowlist ->
  operator block (301) -> engine ban (403 + Retry-After) -> geo (451)
- a post-response observer feeds a consecutive-refusal streak; N refusals
  in M minutes ban for BanDuration; 5xx alerts and never bans
- provenance on the block row, so clearing engine bans cannot touch an
  operator's entries; the toggle is Off/Observe/Enforce and changes no rows
- inherited protections from the reference deployment: one durable write
  per source per 30s, LRU-bounded streak map, no DB on the request path

Verification: 36 unit + 14 integration tests, plus scripts/smoke.sh (19
assertions against a running edge and a real upstream, including the
distroless --healthcheck mode).

Findings worth keeping (documented in docs/ARCHITECTURE.md):
- ctx.RequestAborted silently cancelled the ban write, because the
  observer runs after the response is written
- SQLite cannot translate DateTimeOffset comparisons; timestamps are
  stored as integers there and stay timestamptz on PostgreSQL
- CIDR masking order silently widened prefixes (198.51.100.24/19 -> /16)
- an empty string in appsettings.json defeats the env fallback (?? on "")
- concurrent ban writes raced into the unique index and lost evidence

Also moves the unrelated 2026-09-30 inference benchmarks out of the
project root into research/.
2026-10-01 02:13:01 +02:00

77 lines
2.2 KiB
C#

using System.Net;
using System.Net.Sockets;
using Microsoft.AspNetCore.Http;
namespace OpenWaf.Tests.Integration;
/// <summary>
/// The application behind the edge. It is deliberately dumb: it answers a
/// configured status per path and counts what it was asked. The whole point of
/// the feature is the edge reacting to what the upstream answered, so the test
/// needs to control that answer and observe whether the edge noticed.
/// </summary>
internal sealed class FakeUpstream : IAsyncDisposable
{
private readonly HttpListener _listener = new();
private readonly CancellationTokenSource _cts = new();
public string Address { get; }
public int RequestCount;
public List<string> Paths = [];
/// <summary>What this upstream answers for a path.</summary>
public Func<string, int> StatusFor { get; set; } = _ => StatusCodes.Status404NotFound;
public static FakeUpstream Start()
{
var port = FreePort();
var upstream = new FakeUpstream($"http://127.0.0.1:{port}/");
upstream._listener.Start();
_ = upstream.LoopAsync();
return upstream;
}
private FakeUpstream(string address)
{
Address = address;
_listener.Prefixes.Add(address);
}
private async Task LoopAsync()
{
try
{
while (!_cts.IsCancellationRequested)
{
var context = await _listener.GetContextAsync();
var path = context.Request.Url?.AbsolutePath ?? "/";
Interlocked.Increment(ref RequestCount);
lock (Paths) Paths.Add(path);
context.Response.StatusCode = StatusFor(path);
context.Response.OutputStream.Close();
}
}
catch
{
// Listener closed at teardown — the expected way out.
}
}
private static int FreePort()
{
var probe = new TcpListener(IPAddress.Loopback, 0);
probe.Start();
var port = ((IPEndPoint)probe.LocalEndpoint).Port;
probe.Stop();
return port;
}
public async ValueTask DisposeAsync()
{
_cts.Cancel();
_listener.Close();
await Task.CompletedTask;
}
}