From 05f0280d8e7a5eff4b8ec4580cd7c8bce632da79 Mon Sep 17 00:00:00 2001 From: Jason Huebert Date: Sat, 3 Oct 2026 16:53:21 -0500 Subject: [PATCH] fix(filter): time the content rules on the same monotonic clock checkContent() read a raw millis() value while checkPacket() folds millis() into a 64-bit accumulator. Both phases write the same rule's throttle state, so a millis() wrap made a 32-bit comparison read as 49.7 days: a throttled rule handed out a fresh pass milliseconds after the last one, and a rule that alternated between the packet and content phases never throttled at all. The content scan now takes its clock from uptimeMillis(), once per scan, so both phases agree on elapsed time across a wrap. --- examples/simple_repeater/PacketFilter.cpp | 8 ++-- .../test_packet_filter/test_packet_filter.cpp | 41 +++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/examples/simple_repeater/PacketFilter.cpp b/examples/simple_repeater/PacketFilter.cpp index deb89edf..755a317b 100644 --- a/examples/simple_repeater/PacketFilter.cpp +++ b/examples/simple_repeater/PacketFilter.cpp @@ -608,9 +608,11 @@ uint8_t FilterRules::checkContent(mesh::Packet* pkt, uint8_t type, const mesh::G uint8_t verdict = FILTER_ACT_ALLOW; PacketHashCache pkt_hash(pkt); - // one clock reading for the whole scan: the throttle gate stamps state that - // checkPacket() writes too, and both phases read the same millis() clock - const uint32_t now = millis(); + // one clock reading for the whole scan, folded into the same 64-bit + // accumulator checkPacket() reads: a raw millis() reading would compare as a + // 32-bit wrap and hand out a fresh throttle pass milliseconds after the last + // one, and both phases stamp the same rule's rate state + const uint64_t now = uptimeMillis(millis()); for (int i = 0; i < num_rules; i++) { FilterRule* r = &rules[i]; if (!r->enabled) continue; diff --git a/test/test_packet_filter/test_packet_filter.cpp b/test/test_packet_filter/test_packet_filter.cpp index 5ab9a262..0a0c8911 100644 --- a/test/test_packet_filter/test_packet_filter.cpp +++ b/test/test_packet_filter/test_packet_filter.cpp @@ -3062,6 +3062,47 @@ TEST_F(FilterTest, LongIdleBeyondOneMillisWrapIsStillTimedCorrectly) { EXPECT_EQ(forwardPacket(filter, soon, t + 1000), FILTER_ACT_DROP); } +// The content phase must read the same monotonic clock as the packet phase: a +// raw millis() reading compares as a 32-bit wrap, so two messages 32 ms apart +// would look 49.7 days apart and the throttled rule would hand out a free pass. +TEST_F(FilterTest, ContentThrottleTimesRealElapsedTimeAcrossTheWrap) { + expectOk(filter, "add text=hello throttle=60"); + auto body = makeGroupText("Alice", "hello"); + auto pkt = makePacket(ROUTE_TYPE_FLOOD, PAYLOAD_TYPE_GRP_TXT, body.len); + auto chan = channelFromStore(filter, 0); + + g_mock_millis = 0xfffffff0u; // just before the 32-bit millis() wrap + ASSERT_EQ(filter.checkContent(&pkt, PAYLOAD_TYPE_GRP_TXT, chan, body.data, + body.len, nullptr), FILTER_ACT_ALLOW); // free pass + + g_mock_millis = 16; // 32 ms later, across the wrap: the window is not spent + EXPECT_EQ(filter.checkContent(&pkt, PAYLOAD_TYPE_GRP_TXT, chan, body.data, + body.len, nullptr), FILTER_ACT_DROP); + EXPECT_EQ(filter.getRule(0)->hits, 1u); + EXPECT_EQ(filter.getRule(0)->throttle_pass, 1u); +} + +// checkContent() evaluates packet-level rules too, so one rule's window can be +// stamped by either phase. Across a wrap the two phases must still agree on how +// much real time has passed. +TEST_F(FilterTest, ThrottleWindowIsSharedByBothPhases) { + expectOk(filter, "add throttle=60"); + auto pkt = makePacket(ROUTE_TYPE_FLOOD, PAYLOAD_TYPE_GRP_TXT); + auto body = makeGroupText("Alice", "hello"); + auto chan = channelFromStore(filter, 0); + + g_mock_millis = 0xfffffff0u; + ASSERT_EQ(filter.checkPacket(&pkt, g_mock_millis, nullptr), FILTER_ACT_ALLOW); // free pass + g_mock_millis = 16; // 32 ms later, across the wrap + filter.uptimeMillis(g_mock_millis); // the loop keeps the clock moving + + // the content phase sees the window the packet phase started: 32 ms of real + // time has passed, so the rule still decides + EXPECT_EQ(filter.checkContent(&pkt, PAYLOAD_TYPE_GRP_TXT, chan, body.data, + body.len, nullptr), FILTER_ACT_DROP); + EXPECT_EQ(filter.getRule(0)->hits, 1u); +} + TEST_F(FilterTest, ThrottleOnPacketRulesViaCheckPacket) { // packet-level rules meter on the checkPacket path, and interleave with the // advert rate limiter: a rule drop pre-empts it, a fall-through still hits it