Files
Pavlo Ratushnyi 575ef88afe an operator can ask what the edge would answer, before it answers anyone
OPEN-59. Every other admin surface answers what the edge already did, so the only test for a path rule, an exemption or a country rule is a real visitor getting the real refusal. GET /admin-api/decide?ip=&path=&country= runs the same DecisionEngine over the same snapshot the wire reads — there is no second decision to drift — and narrates the order.

The trace is the product: a verdict says what would happen, the pillar list says which pillar to change, and it keeps four silences apart (did_not_match, not_reached, not_enforced_in_this_mode, not_evaluated). In Observe mode the engine line says whether a live ban for that source exists, because 'nothing was known about this address' and 'something was known and the mode chose not to act' are the two answers operators read as one. What a preview cannot honestly answer — the live rate counter and a streak that depends on responses the upstream has not produced — is named in the body rather than left out.

Three things the run caught. IPAddress.TryParse accepts abbreviated IPv4, so '203.0.113' parses as 203.0.113.0 and a missing octet was answered 'pass' for an address the operator did not mean; the guard now requires a full dotted quad. The preview read state.Current while the guard calls GetOrRefresh(), so it could answer from a snapshot the wire had already replaced — the whole claim of the feature is that the two agree. And a Pass carries no source key, so the Observe note claimed 'no live ban' for a source that had one; the key is now computed for the question rather than read off the answer.

Gates on this tree: build 0 warnings / 0 errors, unit 256/256, integration 245/245, smoke 65/0, compose 229/0 against the built image (261 MB), console 90/0.
2026-10-05 01:36:29 +02:00

133 lines
6.1 KiB
C#

using OpenWaf.Domain.Engine;
using OpenWaf.Domain.Policy;
using OpenWaf.Domain.Security;
namespace OpenWaf.Tests.Unit;
/// <summary>
/// The narrated order (OPEN-59). The decision itself is covered by
/// <see cref="DecisionEngineTests"/>; what is covered here is the answer an
/// operator is given about that decision, and the two ways it can lie: calling a
/// pillar that was never asked a pillar that said no, and calling a pillar this
/// configuration does not run a pillar that was asked and agreed.
/// </summary>
public sealed class DecisionTraceTests
{
private static readonly DateTimeOffset Now = new(2026, 10, 5, 12, 0, 0, TimeSpan.Zero);
private static EdgeSnapshot Snapshot(
EdgePolicy? policy = null,
Dictionary<string, BlockEntry>? bans = null) => new()
{
GeneratedAt = Now,
Policy = policy ?? EdgePolicy.Default,
EngineBans = bans ?? new Dictionary<string, BlockEntry>(StringComparer.Ordinal)
};
private static string OutcomeOf(IReadOnlyList<PillarTrace> rows, string pillar)
=> PillarOutcomes.Label(rows.First(r => r.Pillar == pillar).Outcome);
private static string? NoteOf(IReadOnlyList<PillarTrace> rows, string pillar)
=> rows.First(r => r.Pillar == pillar).Note;
[Fact]
public void A_pass_with_no_address_names_the_source_pillars_as_never_asked()
{
var rows = DecisionTrace.For(Snapshot(), EdgeDecision.Pass, Now, hasAddress: false, hasCountry: false);
Assert.Equal(DecisionTrace.Pillars.Length, rows.Count);
foreach (var pillar in new[] { "alwaysPass", "allowlist", "requestRule" })
Assert.Equal("did_not_match", OutcomeOf(rows, pillar));
foreach (var pillar in new[] { "operatorBlock", "engineBan" })
{
Assert.Equal("not_evaluated", OutcomeOf(rows, pillar));
Assert.Contains("no address was given", NoteOf(rows, pillar)!);
}
// The geo pillar gets the more specific truth in this configuration: it is
// off, which is a different fact from the missing address.
Assert.Equal("not_enforced_in_this_mode", OutcomeOf(rows, "geo"));
Assert.Equal("not_evaluated", OutcomeOf(rows, "rate"));
}
[Fact]
public void A_pillar_after_the_answering_one_was_not_reached_rather_than_answered_no()
{
var decision = new EdgeDecision(EdgeVerdict.BlockRule, 403, Reason: "rule_7", RulePattern: "/x*");
var rows = DecisionTrace.For(
Snapshot(new EdgePolicy { Mode = EnforcementMode.Enforce }), decision, Now, hasAddress: true, hasCountry: false);
Assert.Equal("did_not_match", OutcomeOf(rows, "alwaysPass"));
Assert.Equal("did_not_match", OutcomeOf(rows, "allowlist"));
Assert.Equal("answered", OutcomeOf(rows, "requestRule"));
Assert.Contains("rule_7", NoteOf(rows, "requestRule")!);
Assert.Equal("not_reached", OutcomeOf(rows, "operatorBlock"));
Assert.Equal("not_reached", OutcomeOf(rows, "engineBan"));
}
[Fact]
public void Observe_mode_is_reported_as_a_pillar_this_configuration_does_not_run()
{
var observe = new EdgePolicy { Mode = EnforcementMode.Observe };
var rows = DecisionTrace.For(Snapshot(observe), EdgeDecision.Pass, Now, hasAddress: true, hasCountry: false);
Assert.Equal("not_enforced_in_this_mode", OutcomeOf(rows, "engineBan"));
// And in Enforce mode the same pillar was asked and said no, which is the
// difference an operator acts on.
var enforce = DecisionTrace.For(
Snapshot(new EdgePolicy { Mode = EnforcementMode.Enforce }),
EdgeDecision.Pass, Now, hasAddress: true, hasCountry: false);
Assert.Equal("did_not_match", OutcomeOf(enforce, "engineBan"));
Assert.Null(NoteOf(enforce, "engineBan"));
}
[Fact]
public void A_live_ban_the_mode_refused_to_enforce_is_named_as_existing()
{
var observe = new EdgePolicy { Mode = EnforcementMode.Observe };
var snapshot = Snapshot(observe, new Dictionary<string, BlockEntry>(StringComparer.Ordinal)
{
["203.0.113.9"] = new()
{
SourceKey = "203.0.113.9",
Source = BlockSource.Engine,
ExpiresAt = Now.AddMinutes(30),
Reason = "3 consecutive refusals"
}
});
var note = DecisionTrace.NoteForUnasked("engineBan", snapshot, "203.0.113.9", true, false, Now);
Assert.Contains("a live engine ban exists", note);
// The same line for a source nothing is known about says exactly that,
// because "nothing was known" and "known and not acted on" are the two
// answers an operator keeps confusing.
var quiet = DecisionTrace.NoteForUnasked("engineBan", snapshot, "198.51.100.1", true, false, Now);
Assert.Contains("Observe mode does not enforce engine bans", quiet);
Assert.DoesNotContain("exists", quiet);
}
[Fact]
public void A_geo_policy_that_is_on_but_has_no_country_is_not_the_same_as_one_that_is_off()
{
var on = new EdgePolicy { Geo = GeoMode.DenyList };
var rows = DecisionTrace.For(Snapshot(on), EdgeDecision.Pass, Now, hasAddress: true, hasCountry: false);
Assert.Equal("not_evaluated", OutcomeOf(rows, "geo"));
Assert.Contains("no country is known", NoteOf(rows, "geo")!);
var off = DecisionTrace.For(Snapshot(), EdgeDecision.Pass, Now, hasAddress: true, hasCountry: true);
Assert.Equal("not_enforced_in_this_mode", OutcomeOf(off, "geo"));
Assert.Contains("the geo pillar is off", NoteOf(off, "geo")!);
}
[Fact]
public void Every_verdict_has_its_own_spelling_because_blockrule_is_not_a_word_any_surface_prints()
{
var labels = Enum.GetValues<EdgeVerdict>().Select(EdgeVerdictLabel.For).ToArray();
Assert.Equal(Enum.GetValues<EdgeVerdict>().Length, labels.Distinct().Count());
Assert.Contains("block_rule", labels);
Assert.Contains("pass_rule_observed", labels);
Assert.DoesNotContain("blockrule", labels);
Assert.All(labels, l => Assert.Equal(l, l.ToLowerInvariant()));
}
}