using System.Net; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using OpenWaf.Domain.Security; using OpenWaf.Infrastructure.Persistence; namespace OpenWaf.Tests.Integration; /// /// The per-entry redirect is the one field on a block that ends up in a /// response header, and the shipped image accepted any string in it. /// /// Measured on openwaf:ci before the fix — POST /admin-api/blocks answered 204 /// for all of these, and the next request from that source then answered: /// "https://example.com/ok" 301 Location: https://example.com/ok /// "http://bad target" 301 Location: http://bad target /// "http://x.invalid/\r\nX-Injected: 1" 500 Internal Server Error, Content-Length: 0 /// "javascript:alert(1)" 301 Location: javascript:alert(1) /// "not-a-url" 301 Location: not-a-url /// The CRLF row is the one that makes this a defect rather than a wart: the /// operator's own block, written with a 204, turned that visitor's request into /// a server error. No header was injected — Kestrel refused the write — which /// is the only part of the old behaviour that was safe. /// public sealed class RedirectTargetTests { private const string Blocked = "203.0.113.66"; private const string Bystander = "198.51.100.7"; private static async Task<(HttpStatusCode Status, string Body)> PostBlockAsync(EdgeFixture f, string json) { using var request = new HttpRequestMessage(new HttpMethod("POST"), "/admin-api/blocks") { Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json") }; request.Headers.TryAddWithoutValidation("X-Admin-Token", EdgeFixture.AdminToken); var response = await f.Client.SendAsync(request); return (response.StatusCode, await response.Content.ReadAsStringAsync()); } private static async Task<(HttpStatusCode Status, string? Location)> GetAsync(EdgeFixture f, string ip) { using var request = new HttpRequestMessage(HttpMethod.Get, "/nope"); request.Headers.TryAddWithoutValidation("X-Forwarded-For", ip); var response = await f.Client.SendAsync(request); return (response.StatusCode, response.Headers.Location?.ToString()); } [Theory] [InlineData("http://bad target", "http://bad target")] [InlineData("http://x.invalid/\r\nX-Injected: 1", "X-Injected")] [InlineData("javascript:alert(1)", "javascript:alert(1)")] [InlineData("data:text/html,x", "data:text/html,x")] [InlineData("not-a-url", "not-a-url")] public async Task A_block_target_the_wire_cannot_carry_is_refused_at_the_api(string target, string echoFragment) { await using var f = new EdgeFixture(); var payload = System.Text.Json.JsonSerializer.Serialize(new { ip = Blocked, reason = "probe", redirectTo = target }); var (status, body) = await PostBlockAsync(f, payload); Assert.Equal(HttpStatusCode.BadRequest, status); Assert.Contains("redirectTo", body); Assert.Contains("absolute http(s) URL", body); Assert.Contains(echoFragment, body); using var scope = f.Factory.Services.CreateScope(); var db = scope.ServiceProvider.GetRequiredService(); Assert.Empty(await db.Blocks.Where(b => b.SourceKey == Blocked).ToListAsync()); } [Fact] public async Task A_well_formed_target_is_stored_and_answered_as_a_redirect() { await using var f = new EdgeFixture(); var (status, _) = await PostBlockAsync(f, "{\"ip\":\"" + Blocked + "\",\"reason\":\"confirmed scraper\",\"redirectTo\":\"https://honeypot.example.org/\"}"); Assert.Equal(HttpStatusCode.NoContent, status); await f.Service().InvalidateAsync(); var (code, location) = await GetAsync(f, Blocked); Assert.Equal(HttpStatusCode.MovedPermanently, code); Assert.Equal("https://honeypot.example.org/", location); var (bystanderCode, _) = await GetAsync(f, Bystander); Assert.Equal(HttpStatusCode.NotFound, bystanderCode); } [Fact] public async Task A_row_written_without_the_api_still_cannot_break_the_response() { await using var f = new EdgeFixture(); // Not through the API: a restored backup or a hand edit. The API guard // cannot see this, so the decision point has to. using (var scope = f.Factory.Services.CreateScope()) { var db = scope.ServiceProvider.GetRequiredService(); db.Blocks.Add(new BlockEntry { SourceKey = Blocked, Source = BlockSource.Operator, CreatedAt = DateTimeOffset.UtcNow.AddMinutes(-1), Reason = "operator: restored row with a CRLF target", RedirectTo = "http://x.invalid/\r\nX-Injected: 1" }); await db.SaveChangesAsync(); } await f.Service().InvalidateAsync(); var (code, location) = await GetAsync(f, Blocked); // Before the guard this was 500 with no body. Now it is the operator // block's own defined answer: 301 to the honey-pot default. Assert.Equal(HttpStatusCode.MovedPermanently, code); Assert.Equal("https://www.iana.org/help/example-domains", location); Assert.DoesNotContain("X-Injected", location ?? ""); } }