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.
112 lines
4.6 KiB
C#
112 lines
4.6 KiB
C#
using OpenWaf.Domain.Engine;
|
|
using OpenWaf.Domain.Policy;
|
|
using OpenWaf.Domain.Security;
|
|
|
|
namespace OpenWaf.Tests.Unit;
|
|
|
|
/// <summary>
|
|
/// A redirect target is written into a Location header, so "any string" is not
|
|
/// a safe answer for it.
|
|
///
|
|
/// Measured on the shipped image before this existed: POST /admin-api/blocks
|
|
/// answered 204 for every target below, and the next request from that source
|
|
/// paid — "http://x.invalid/\r\nX-Injected: 1" came back as
|
|
/// "500 Internal Server Error, Content-Length: 0" (Kestrel refused the header,
|
|
/// so the operator's own block became a server error for that visitor),
|
|
/// "javascript:alert(1)" was shipped as a Location, and "not-a-url" was a 301
|
|
/// no client can follow.
|
|
/// </summary>
|
|
public sealed class RedirectTargetTests
|
|
{
|
|
private static readonly DateTimeOffset Now = new(2026, 10, 1, 12, 0, 0, TimeSpan.Zero);
|
|
|
|
[Theory]
|
|
[InlineData("https://example.com/ok", true)]
|
|
[InlineData("http://example.com/a?b=c#d", true)]
|
|
[InlineData("https://www.iana.org/help/example-domains", true)]
|
|
[InlineData("http://bad target", false)]
|
|
[InlineData("http://x.invalid/\r\nX-Injected: 1", false)]
|
|
[InlineData("http://x.invalid/\nX-Injected: 1", false)]
|
|
[InlineData("javascript:alert(1)", false)]
|
|
[InlineData("data:text/html,<script>alert(1)</script>", false)]
|
|
[InlineData("not-a-url", false)]
|
|
[InlineData("/relative/path", false)]
|
|
[InlineData("", false)]
|
|
[InlineData(" ", false)]
|
|
[InlineData(" https://example.com/", false)]
|
|
public void Only_an_absolute_http_or_https_URL_can_be_sent_as_a_Location(string target, bool sendable)
|
|
=> Assert.Equal(sendable, RedirectTarget.IsSendable(target));
|
|
|
|
[Fact]
|
|
public void A_null_target_is_not_a_redirect_at_all()
|
|
=> Assert.False(RedirectTarget.IsSendable(null));
|
|
|
|
private static EdgeSnapshot Snapshot(EdgePolicy policy, BlockEntry entry, bool engine) => new()
|
|
{
|
|
GeneratedAt = Now,
|
|
Policy = policy,
|
|
EngineBans = engine
|
|
? new Dictionary<string, BlockEntry> { [entry.SourceKey] = entry }
|
|
: new Dictionary<string, BlockEntry>(),
|
|
OperatorBlocks = engine
|
|
? new Dictionary<string, BlockEntry>()
|
|
: new Dictionary<string, BlockEntry> { [entry.SourceKey] = entry }
|
|
};
|
|
|
|
private static BlockEntry Entry(string key, BlockSource source, string? redirect) => new()
|
|
{
|
|
SourceKey = key,
|
|
Source = source,
|
|
CreatedAt = Now.AddMinutes(-1),
|
|
ExpiresAt = Now.AddMinutes(30),
|
|
RedirectTo = redirect,
|
|
Reason = "3 consecutive refusals (400-499) within 5 min; last 404 /nope"
|
|
};
|
|
|
|
[Fact]
|
|
public void A_well_formed_target_is_the_redirect_the_operator_wrote()
|
|
{
|
|
var entry = Entry("203.0.113.7", BlockSource.Operator, "https://honeypot.example.org/");
|
|
var decision = DecisionEngine.Decide(
|
|
Snapshot(new EdgePolicy { Mode = EnforcementMode.Enforce }, entry, engine: false),
|
|
"203.0.113.7", "/nope", Now);
|
|
|
|
Assert.Equal(EdgeVerdict.BlockOperator, decision.Verdict);
|
|
Assert.Equal(301, decision.StatusCode);
|
|
Assert.Equal("https://honeypot.example.org/", decision.Location);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("http://x.invalid/\r\nX-Injected: 1")]
|
|
[InlineData("javascript:alert(1)")]
|
|
[InlineData("not-a-url")]
|
|
[InlineData("http://bad target")]
|
|
public void An_unsendable_target_never_reaches_the_wire_and_the_block_still_answers_its_own_kind(string target)
|
|
{
|
|
var entry = Entry("203.0.113.7", BlockSource.Operator, target);
|
|
var decision = DecisionEngine.Decide(
|
|
Snapshot(new EdgePolicy { Mode = EnforcementMode.Enforce }, entry, engine: false),
|
|
"203.0.113.7", "/nope", Now);
|
|
|
|
Assert.Equal(EdgeVerdict.BlockOperator, decision.Verdict);
|
|
Assert.Equal(301, decision.StatusCode);
|
|
Assert.Equal("https://www.iana.org/help/example-domains", decision.Location);
|
|
Assert.NotEqual(target, decision.Location);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("http://x.invalid/\r\nX-Injected: 1")]
|
|
[InlineData("javascript:alert(1)")]
|
|
public void An_engine_ban_with_a_broken_target_answers_the_engine_status_not_a_broken_redirect(string target)
|
|
{
|
|
var entry = Entry("203.0.113.7", BlockSource.Engine, target);
|
|
var decision = DecisionEngine.Decide(
|
|
Snapshot(new EdgePolicy { Mode = EnforcementMode.Enforce }, entry, engine: true),
|
|
"203.0.113.7", "/nope", Now);
|
|
|
|
Assert.Equal(EdgeVerdict.BlockEngine, decision.Verdict);
|
|
Assert.Equal(403, decision.StatusCode);
|
|
Assert.Null(decision.Location);
|
|
}
|
|
}
|