using OpenWaf.Domain.Engine;
using OpenWaf.Domain.Policy;
using OpenWaf.Domain.Security;
namespace OpenWaf.Tests.Unit;
///
/// 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.
///
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,", 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 { [entry.SourceKey] = entry }
: new Dictionary(),
OperatorBlocks = engine
? new Dictionary()
: new Dictionary { [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);
}
}