Files
Pavlo Ratushnyi 6664f94b22 an operator's decisions move to another edge in one call
An operator running several edges types every rule several times: blocks, allowlist, request rules, country rules and the policy overrides have their own endpoints one row at a time, and nothing moved them as a set. A new deployment starts empty, and a change that turns out wrong has nothing to be restored from. This is the multi-instance finding one level up from the shared streak store: SharedStreaks shares the counters, nothing shared the decisions.

GET /admin-api/export answers one document — version, exportedAt, edgeId, per-section counts, the policy section, and blocks / allowlist / rules / country rules. Only live rows; rules keep their evaluation order; and the document names how many engine bans it left behind, because "0 blocks" and "0 operator blocks, 7 engine bans left behind" are different sentences. A ban the engine earned is a claim about this edge's own traffic, so enforcing it where that traffic was never seen is a decision nobody made.

POST /admin-api/import is additive and idempotent: it never deletes a row the document does not mention — a transfer that silently clears a fleet's exemptions is worse than one that duplicates a block — a live row for the same source is reported as already there rather than rewritten, and expiry travels as an instant, so a block that expires in ten minutes where it came from expires in ten minutes here. Per-section counts (added / alreadyThere / skippedExpired / rejected) and a rejected[] list named by section and index; an unknown version, an oversize body or a malformed document writes nothing at all; a storage failure answers 503 with the report, because "it failed" and "it added 41 of 60" must not collapse into one sentence.

What the run caught, in order: (1) a unit assert used "203.0.113" as the invalid address and passed for the wrong reason — IPAddress.TryParse accepts it as 203.0.113.0, so the assert now names 203.0.113.999. (2) Two smoke asserts were wrong rather than the product: a helper printed a Python boolean where the gate compares text, and a blocks delete was asserted as 204 when the endpoint answers 200 with {"deleted":n}. (3) The metrics-as-a-number test could pass its wait on a cache refreshed between an enqueue and its first attempt — pending > 0 was true a moment before attempts > 0 could be — so the wait now requires both numbers. (4) A real defect, not a test one: the alert notifier left the rows it had claimed claimed when the process stopped, so the next process waited out the five-minute reclaim window to deliver an alert that was already owed. It now releases what it holds on the way out, and the reclaim window is back to being what it is for — a process that was killed and cannot say anything.

Gates on this tree: build 0 warnings / 0 errors, unit 285/285, integration 257/257 in 23s, smoke 82 passed / 0 failed (14 new), image 262 MB, compose-smoke 243 passed / 0 failed with 0 containers left behind (11 new, against the shipped image).

Refs OPEN-61.
2026-10-05 04:16:05 +02:00

109 lines
5.6 KiB
C#

using OpenWaf.Domain.Security;
namespace OpenWaf.Tests.Unit;
/// <summary>
/// The rulings of a config transfer, away from an edge (OPEN-61).
///
/// The endpoint decides what to write; these functions decide whether a row is
/// worth writing at all, and every one of them is a ruling that could be
/// silently inverted in the handler — an expired row imported as live, a version
/// accepted that this build cannot read, a section size that turns one request
/// into a data load. Checked here, where they are arithmetic.
/// </summary>
public sealed class ConfigTransferTests
{
private static readonly DateTimeOffset Now = new(2026, 10, 5, 12, 0, 0, TimeSpan.Zero);
[Fact]
public void The_version_this_build_writes_is_the_only_one_it_reads()
{
Assert.Null(ConfigTransfer.ExplainVersion(ConfigTransfer.CurrentVersion));
var problem = ConfigTransfer.ExplainVersion(ConfigTransfer.CurrentVersion + 1)!;
Assert.Contains((ConfigTransfer.CurrentVersion + 1).ToString(), problem);
Assert.Contains(ConfigTransfer.CurrentVersion.ToString(), problem);
// A document that never named a version arrives as 0, and 0 is not a
// version this build can honour.
Assert.NotNull(ConfigTransfer.ExplainVersion(0));
}
[Fact]
public void A_section_over_the_cap_is_refused_naming_the_cap()
{
Assert.Null(ConfigTransfer.ExplainSectionSize("blocks", ConfigTransfer.MaxItemsPerSection));
var problem = ConfigTransfer.ExplainSectionSize("blocks", ConfigTransfer.MaxItemsPerSection + 1)!;
Assert.Contains("blocks", problem);
Assert.Contains(ConfigTransfer.MaxItemsPerSection.ToString(), problem);
}
[Fact]
public void A_row_without_an_expiry_never_expires_and_a_past_instant_always_has()
{
Assert.False(ConfigTransfer.IsExpired(null, Now));
Assert.False(ConfigTransfer.IsExpired(Now.AddSeconds(1), Now));
Assert.True(ConfigTransfer.IsExpired(Now.AddSeconds(-1), Now));
// The instant itself is already past: "expires at 12:00" is not enforced
// at 12:00, which is the same rule the engine's IsLive applies.
Assert.True(ConfigTransfer.IsExpired(Now, Now));
}
[Fact]
public void A_block_is_checked_by_the_same_guards_the_write_endpoint_uses()
{
Assert.Null(ConfigTransfer.ExplainBlock(new ExportedBlock("203.0.113.9", "incident", null, null)));
Assert.Null(ConfigTransfer.ExplainBlock(new ExportedBlock(
"2001:db8::1", null, "https://example.org/blocked", Now.AddDays(1))));
Assert.Contains("ip is required", ConfigTransfer.ExplainBlock(new ExportedBlock(" ", null, null, null))!);
// Not "203.0.113": IPAddress.TryParse reads the abbreviated form as
// 203.0.113.0, so that string is an address as far as this guard goes.
Assert.Contains("not an IP address", ConfigTransfer.ExplainBlock(new ExportedBlock("203.0.113.999", null, null, null))!);
Assert.Contains("reason must be at most",
ConfigTransfer.ExplainBlock(new ExportedBlock("203.0.113.9", new string('x', EntryLimits.NoteMaxLength + 1), null, null))!);
Assert.Contains("redirectTo must be an absolute http(s) URL",
ConfigTransfer.ExplainBlock(new ExportedBlock("203.0.113.9", null, "javascript:alert(1)", null))!);
}
[Fact]
public void An_allow_row_takes_an_address_or_a_cidr_and_nothing_else()
{
Assert.Null(ConfigTransfer.ExplainAllow(new ExportedAllow("198.51.100.0/24", "office", null)));
Assert.Null(ConfigTransfer.ExplainAllow(new ExportedAllow("198.51.100.7", null, null)));
Assert.Contains("address or CIDR", ConfigTransfer.ExplainAllow(new ExportedAllow("office.example", null, null))!);
Assert.Contains("address or CIDR", ConfigTransfer.ExplainAllow(new ExportedAllow("", null, null))!);
Assert.Contains("note must be at most",
ConfigTransfer.ExplainAllow(new ExportedAllow("198.51.100.7", new string('x', EntryLimits.NoteMaxLength + 1), null))!);
}
[Fact]
public void A_rule_pattern_that_can_never_match_is_refused_before_it_is_stored()
{
Assert.Null(ConfigTransfer.ExplainRule(new ExportedRule("/wp-admin*", "block", "cms", null)));
// Absent is observe, exactly as POST /admin-api/rules reads a missing action.
Assert.Null(ConfigTransfer.ExplainRule(new ExportedRule("/anything", null, null, null)));
Assert.Contains("action must be observe|block",
ConfigTransfer.ExplainRule(new ExportedRule("/anything", "deny", null, null))!);
Assert.NotNull(ConfigTransfer.ExplainRule(new ExportedRule("wp-admin*", "block", null, null)));
Assert.NotNull(ConfigTransfer.ExplainRule(new ExportedRule("", "block", null, null)));
}
[Fact]
public void A_country_code_is_the_two_letter_code_the_engine_matches()
{
Assert.Null(ConfigTransfer.ExplainCountry(new ExportedCountry("de", false)));
Assert.Equal("DE", ConfigTransfer.CountryCode(" de "));
Assert.Null(ConfigTransfer.CountryCode("12"));
Assert.Null(ConfigTransfer.CountryCode("DEU"));
Assert.Null(ConfigTransfer.CountryCode(null));
Assert.Contains("two-letter", ConfigTransfer.ExplainCountry(new ExportedCountry("12", false))!);
}
[Fact]
public void The_action_travels_in_the_spelling_the_table_stores()
{
Assert.Equal("observe", ConfigTransfer.RuleAction(null));
Assert.Equal("observe", ConfigTransfer.RuleAction(" "));
Assert.Equal("block", ConfigTransfer.RuleAction(" BLOCK "));
Assert.Null(ConfigTransfer.RuleAction("deny"));
}
}