Files
Pavlo Ratushnyi f1715e4b7c a refused visitor gets the answer in sentences, and the status never moves
Every refusal this edge makes was written for the operator and read by a person. The audience that can actually act on a refusal — the office whose address tripped a streak, the visitor who followed a link into a path a rule refuses, the customer a geo policy misattributed — cannot do anything with {"error":"blocked_by_policy","reason":"3 consecutive refusals…"}. The guard now negotiates the body by Accept: a client that names text/html or application/xhtml+xml with q>0 gets a page, everything else gets the JSON it has always got. Accept: */* is deliberately not HTML: it means "I did not think about it", and it is what curl, monitors and SDKs send, which are exactly the clients that parse the body.

BlockPage is a pure function in the domain (no HttpContext, no I/O) so the wording is testable without a pipeline; it landed there rather than in Edge/Pipeline because the unit project references Domain and Infrastructure only. Three rules it exists to keep: the status never changes (a 429 that became 200 because a browser looked at it would be a defence that switches itself off when observed); nothing is fetched (inline CSS, no script/font/image/link, robots=noindex — the page goes to anonymous visitors and the runtime image is distroless); and the operator's own note is never rendered, because an operator block's reason is free text typed during an incident about a source, not a message to this visitor, so that branch shows the fact and no reference. What the page does carry is the machine reference the JSON already carried — rule_12, country_denied_DE, rate_3_of_2 — plus Retry-After and the expiry in UTC: the string the visitor quotes and the string the operator searches the listings for.

EdgeVerdict gained BlockRate so the rate branch logs the same verdict line the other four do instead of being the one refusal with no log, and the same helper now writes all five.

Gates on this tree: build 0 warnings / 0 errors, unit 227/227 (+20 BlockPage cases), integration 225/225 in 20 s (+6 wire cases asking what a browser and what curl get for the same refusal), smoke passed 52 failed 0 (+4), compose 209 passed 0 failed against the image at 261 MB, console 90 assertions 0 failed — unchanged, because this feature changes what a visitor sees and nothing the console shows.

The smoke count also had to be re-derived rather than trusted: a python replace whose replacement omitted its anchor deleted the line check 'the path is refused rather than proxied' 403 and left its command dangling as a continuation of the rule-add check above it, so the gate reported 51 and one assertion had silently vanished while still looking green. Same failure mode as the PolicyBounds edit earlier in this session — replace(a, b) where b does not contain a is a delete, not an insert.
2026-10-04 21:53:06 +02:00

117 lines
4.7 KiB
C#

using OpenWaf.Domain.Engine;
using OpenWaf.Domain.Responses;
using Xunit;
namespace OpenWaf.Tests.Unit;
/// <summary>
/// The refusal as a page (OPEN-57). Two things are worth pinning here: which
/// clients get the page, and what the page is allowed to contain. Both are
/// rules about behaviour, not about looks — a page that leaks the operator's
/// note, quotes a rule pattern, or fetches a stylesheet is a different product
/// from the one this file describes.
/// </summary>
public sealed class BlockPageTests
{
private static readonly DateTimeOffset T0 = new(2026, 10, 4, 12, 0, 0, TimeSpan.Zero);
[Theory]
// A browser: text/html named explicitly.
[InlineData("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", true)]
[InlineData("text/html", true)]
[InlineData("application/xhtml+xml;q=0.9", true)]
[InlineData("TEXT/HTML", true)]
[InlineData("text/plain;q=1, text/html;q=0.2", true)]
// Not a browser. `*/*` is "I did not think about it", and it is what curl,
// monitors, SDKs and scrapers send — the clients that parse the body.
[InlineData("*/*", false)]
[InlineData("", false)]
[InlineData(null, false)]
[InlineData("application/json", false)]
[InlineData("application/json;q=1,*/*;q=0.5", false)]
// Named, then refused.
[InlineData("text/html;q=0", false)]
[InlineData("text/html;q=0.0", false)]
public void Only_a_client_that_named_html_gets_the_page(string? accept, bool wantsHtml)
=> Assert.Equal(wantsHtml, BlockPage.PrefersHtml(accept));
[Fact]
public void Each_refusal_says_what_it_is_in_words()
{
Assert.Contains("You are asking too often.",
BlockPage.Render(EdgeVerdict.BlockRate, "rate_3_of_2", null, 12, T0));
Assert.Contains("This site blocked your address.",
BlockPage.Render(EdgeVerdict.BlockEngine, "3 consecutive refusals", null, 0, T0));
Assert.Contains("This path is refused on this site.",
BlockPage.Render(EdgeVerdict.BlockRule, "rule_12", null, 0, T0));
Assert.Contains("not available from your region",
BlockPage.Render(EdgeVerdict.GeoReject, "country_denied_DE", null, 0, T0));
Assert.Contains("added your address to a list",
BlockPage.Render(EdgeVerdict.BlockOperator, null, null, 0, T0));
}
[Fact]
public void The_reference_the_json_carries_is_the_reference_the_page_shows()
{
var page = BlockPage.Render(EdgeVerdict.BlockRule, "rule_12", null, 0, T0);
Assert.Contains("rule_12", page);
Assert.Contains("Reference:", page);
}
[Fact]
public void A_reference_is_text_and_never_becomes_an_element()
{
var page = BlockPage.Render(EdgeVerdict.BlockEngine,
"<svg onload=document.title='XSS'>", null, 0, T0);
Assert.DoesNotContain("<svg", page);
Assert.Contains("&lt;svg", page);
}
[Fact]
public void An_expiry_is_shown_as_an_instant_and_a_wait()
{
var page = BlockPage.Render(EdgeVerdict.BlockEngine, "reason", T0.AddMinutes(25), 0, T0);
Assert.Contains("2026-10-04 12:25 UTC", page);
Assert.Contains("about 25 minutes from now", page);
}
[Fact]
public void A_pause_far_off_shows_the_instant_without_a_false_soon()
{
var page = BlockPage.Render(EdgeVerdict.BlockEngine, "reason", T0.AddHours(30), 0, T0);
Assert.Contains("2026-10-05 18:00 UTC", page);
Assert.DoesNotContain("from now", page);
}
[Fact]
public void A_retry_after_becomes_a_sentence_about_time()
{
Assert.Contains("Try again in 1 second.",
BlockPage.Render(EdgeVerdict.BlockRate, "rate_3_of_2", null, 1, T0));
Assert.Contains("Try again in 42 seconds.",
BlockPage.Render(EdgeVerdict.BlockRate, "rate_3_of_2", null, 42, T0));
Assert.DoesNotContain("Try again",
BlockPage.Render(EdgeVerdict.BlockRule, "rule_1", null, 0, T0));
}
[Fact]
public void The_page_fetches_nothing_and_runs_no_script()
{
var page = BlockPage.Render(EdgeVerdict.GeoReject, "country_denied_DE", T0.AddMinutes(5), 0, T0);
Assert.DoesNotContain("<script", page);
Assert.DoesNotContain("src=", page);
Assert.DoesNotContain("<link", page);
Assert.DoesNotContain("http://", page);
Assert.DoesNotContain("https://", page);
Assert.Contains("<style>", page);
Assert.Contains("<meta name=\"viewport\"", page);
}
[Fact]
public void An_operator_block_shows_no_reference_because_its_reason_is_the_operators_note()
{
var page = BlockPage.Render(EdgeVerdict.BlockOperator, null, null, 0, T0);
Assert.DoesNotContain("Reference:", page);
}
}