Measured on the shipped image (openwaf:ci). POST /admin-api/blocks answered 204 for every one of these, and the next request from that source paid for it: "https://example.com/ok" 301 Location: https://example.com/ok "http://bad target" 301 Location: http://bad target "http://x.invalid/<CRLF>X-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 defect rather than the wart: Kestrel refuses to write the header, so the operator's own block — written with a 204 — became a server error for that visitor. Nothing was injected; the server validated the header, which is the only part of the old behaviour that was safe. javascript: and data: are absolute URIs too, and a redirect is a navigation, so those are an execution request made on the operator's behalf by a string somebody typed into a form. RedirectTarget.IsSendable is now the one judge: absolute, http(s), no control characters or surrounding whitespace. The API answers 400 naming the field, and DecisionEngine ignores a target it cannot send and answers the block's own default instead — for a row that arrived by a restored backup or a hand edit, where the API guard never ran. The console gate also asserts the other half of the same boundary: every value that came from the API is rendered through esc(), and two of those values are not authored by the person reading the page — the path in an observation row is whatever an anonymous visitor asked for, and a block reason is a form field. The seed now sends a percent-encoded path carrying <img onerror=...> and an operator block whose reason carries <svg onload=...>, and the page must show both as text and create no elements from them. unit 105 -> 126 (+21: the target table, and the decision point's fallback) integration 55 -> 62 (+7: 400 by name for five targets, a valid target reaching the wire as 301, and a hand-written row with CRLF answering 301 to the default target instead of the 500 it used to answer) console 33 -> 37 assertions Teeth, each measured by removing the thing: esc() reduced to identity in the shipped page makes the console gate go red with 6 failures, and the page title becomes "XSS" — the anonymous visitor's path executed script in the operator's browser. Dropping the API guard fails exactly 5 integration tests; dropping the decision-point guard fails exactly 6 unit tests and the 1 hand-written-row test.
121 lines
5.4 KiB
C#
121 lines
5.4 KiB
C#
using System.Net;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using OpenWaf.Domain.Security;
|
|
using OpenWaf.Infrastructure.Persistence;
|
|
|
|
namespace OpenWaf.Tests.Integration;
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
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<OpenWafDbContext>();
|
|
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<EdgeStateStore>().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<OpenWafDbContext>();
|
|
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<EdgeStateStore>().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 ?? "");
|
|
}
|
|
}
|