mirror of
https://github.com/jhuebert/MeshCore.git
synced 2026-12-03 10:54:13 +00:00
feat(filter): add action=cli rule model + validation
One new rule action, cli, joins drop and forward: a matching decrypted group-text message on a keyed channel is forwarded like action=forward AND queued as a CLI script for deferred execution (the runner lands in a following commit). The action rides the existing FilterRule::action byte, so the persisted v7 layout, its static_asserts and the load path are all untouched. Constraints enforced at filter add and re-checked in validRule(): chan= is required and must name at least one PSK-backed (non-#-named) channel, type is forced to txt, prob=/throttle= are rejected (a gated cli rule could fragment a fleet). A cli rule left with '#' channels only after a chan del is made inert by clearing its mask — MASK_SET with no bits stays the documented inert rule and keeps the record savable. Display: filter list shows C, filter get prints cli, add errors state what is accepted.
This commit is contained in:
1 parent
ffc2183464
commit
fc4b2e930b
3 files changed
+176
-8
No files matched your search
@@ -255,10 +255,24 @@ void FilterRules::delChannel(int idx) {
|
||||
// above it, and chan_mask >> 32 would be an undefined shift
|
||||
uint32_t hi = (idx + 1 < FILTER_MAX_CHANNELS) ? ((r->chan_mask >> (idx + 1)) << idx) : 0;
|
||||
r->chan_mask = lo | hi;
|
||||
confineCliRule(r); // a cli rule left with '#' channels only must go inert
|
||||
}
|
||||
markDirty();
|
||||
}
|
||||
|
||||
// A cli rule must never execute scripts on a '#'-named channel: such names
|
||||
// derive their key from the public channel name, which authenticates nobody.
|
||||
// When no surviving channel of the rule is PSK-backed (e.g. its private channel
|
||||
// was deleted), clear the mask — MASK_SET with no bits is the documented inert
|
||||
// rule, and the record stays savable.
|
||||
void FilterRules::confineCliRule(FilterRule* r) {
|
||||
if (r->action != FILTER_ACT_CLI) return;
|
||||
for (int c = 0; c < num_channels; c++) {
|
||||
if ((r->chan_mask & (1u << c)) && channels[c].name[0] != '#') return;
|
||||
}
|
||||
r->chan_mask = 0;
|
||||
}
|
||||
|
||||
int FilterRules::searchChannelsByHash(const uint8_t* hash, mesh::GroupChannel dest[], int max_matches) {
|
||||
if (!enabled) return 0; // filter off -> behave like stock (no channel DB)
|
||||
int n = 0;
|
||||
@@ -756,7 +770,8 @@ static bool oldStringsTerminate(uint8_t ver, const uint8_t* rec) {
|
||||
// because matching is first-match-wins that changes which rule decides a packet
|
||||
// — quietly losing a predicate is worse than not having the rule at all.
|
||||
static bool validRule(const FilterRule* r) {
|
||||
if (r->action != FILTER_ACT_DROP && r->action != FILTER_ACT_FORWARD) return false;
|
||||
if (r->action != FILTER_ACT_DROP && r->action != FILTER_ACT_FORWARD &&
|
||||
r->action != FILTER_ACT_CLI) return false;
|
||||
if (ruleTypeMask(r) & ~FILTER_TYPE_MASK_ALL) return false;
|
||||
if (r->route_mask & ~(FILTER_ROUTE_FLOOD | FILTER_ROUTE_DIRECT)) return false;
|
||||
|
||||
@@ -794,6 +809,14 @@ static bool validRule(const FilterRule* r) {
|
||||
if (memchr(r->regions, 0, sizeof(r->regions)) == NULL) return false;
|
||||
|
||||
if (r->prob > 100) return false; // 0 = unset = 100 %
|
||||
|
||||
// action=cli constraints, the same refusals `filter add` enforces: a record
|
||||
// breaking them was not written by this firmware
|
||||
if (r->action == FILTER_ACT_CLI) {
|
||||
if (ruleTypeMask(r) != FILTER_TYPE_GRP_TXT) return false; // group text only
|
||||
if (!(r->chan_flags & FILTER_CHANFLG_MASK_SET)) return false; // needs a named channel
|
||||
if (r->prob != 0 || r->throttle != 0) return false; // no gates
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -983,6 +1006,7 @@ void FilterRules::load(FILESYSTEM* fs) {
|
||||
uint32_t keep = 0;
|
||||
for (int c = 0; c < num_channels; c++) if (rules[i].chan_mask & (1u << c)) keep |= (1u << c);
|
||||
rules[i].chan_mask = keep;
|
||||
confineCliRule(&rules[i]); // a cli rule on '#' channels only is inert, not public
|
||||
}
|
||||
} else {
|
||||
file.close();
|
||||
@@ -1217,6 +1241,10 @@ static bool addRuleParam(FilterRules& filter, FilterRule* r, RegionMap* regions,
|
||||
}
|
||||
// a repeated type= replaces the earlier one. Alternatives inside ONE value
|
||||
// still OR, because that is how the list reads.
|
||||
if (r->action == FILTER_ACT_CLI && mask != FILTER_TYPE_GRP_TXT) {
|
||||
strcpy(reply, "Err - action=cli runs group text only");
|
||||
return false;
|
||||
}
|
||||
r->type_mask = any ? 0 : (uint8_t)(mask & 0xFF);
|
||||
r->type_mask_hi = any ? 0 : (uint8_t)(mask >> 8);
|
||||
return true;
|
||||
@@ -1292,7 +1320,19 @@ static bool addRuleParam(FilterRules& filter, FilterRule* r, RegionMap* regions,
|
||||
if (strcmp(key, "action") == 0) {
|
||||
if (strcmp(val, "drop") == 0) r->action = FILTER_ACT_DROP;
|
||||
else if (strcmp(val, "forward") == 0) r->action = FILTER_ACT_FORWARD;
|
||||
else { strcpy(reply, "Err - action must be drop|forward"); return false; }
|
||||
else if (strcmp(val, "cli") == 0) {
|
||||
// Scripts are group text: a type= given earlier must already be txt-only,
|
||||
// an omitted one is forced to txt (a later non-txt type= is rejected by
|
||||
// the type= branch while the action is cli).
|
||||
const uint16_t type_all = ruleTypeMask(r);
|
||||
if (type_all == 0) r->type_mask = FILTER_TYPE_GRP_TXT;
|
||||
else if (type_all != FILTER_TYPE_GRP_TXT) {
|
||||
strcpy(reply, "Err - action=cli runs group text only");
|
||||
return false;
|
||||
}
|
||||
r->action = FILTER_ACT_CLI;
|
||||
}
|
||||
else { strcpy(reply, "Err - action must be drop|forward|cli"); return false; }
|
||||
return true;
|
||||
}
|
||||
if (strcmp(key, "prob") == 0) {
|
||||
@@ -1460,6 +1500,29 @@ static void cliAdd(FilterRules& filter, RegionMap* regions, char* params, char*
|
||||
rollbackAdd(filter, idx, chans_before);
|
||||
return;
|
||||
}
|
||||
if (r->action == FILTER_ACT_CLI) {
|
||||
// Gates decide whether a rule decides: a throttled fleet would fragment
|
||||
// (each node has its own clock state) and prob= would be all-or-none
|
||||
// fleet-wide, so cli takes neither.
|
||||
if (r->prob != 0 || r->throttle != 0) {
|
||||
strcpy(reply, "Err - action=cli takes no prob/throttle");
|
||||
rollbackAdd(filter, idx, chans_before);
|
||||
return;
|
||||
}
|
||||
// The rule needs at least one named PSK-backed channel: '#' names derive
|
||||
// their key from the public channel name, which authenticates nobody.
|
||||
bool psk_chan = false;
|
||||
for (int c = 0; c < filter.getNumChannels(); c++) {
|
||||
if ((r->chan_mask & (1u << c)) && filter.getChannel(c)->name[0] != '#') { psk_chan = true; break; }
|
||||
}
|
||||
if (!(r->chan_flags & FILTER_CHANFLG_MASK_SET) || !psk_chan) {
|
||||
strcpy(reply, "Err - action=cli needs chan=<private channel>");
|
||||
rollbackAdd(filter, idx, chans_before);
|
||||
return;
|
||||
}
|
||||
snprintf(reply, CLI_REPLY_MAX, "OK - rule %d added (cli: PSK holder = admin)", idx);
|
||||
return;
|
||||
}
|
||||
snprintf(reply, CLI_REPLY_MAX, "OK - rule %d added", idx);
|
||||
}
|
||||
|
||||
@@ -1471,7 +1534,8 @@ static void cliList(FilterRules& filter, char* reply) {
|
||||
for (int i = 0; i < filter.getNumRules(); i++) {
|
||||
auto r = filter.getRule(i);
|
||||
radd(&out, &remain, " %d%c%c%03X", i, r->enabled ? 'e' : 'd',
|
||||
r->action == FILTER_ACT_DROP ? 'D' : 'F', ruleDigest(r) & 0xFFF);
|
||||
r->action == FILTER_ACT_DROP ? 'D' : r->action == FILTER_ACT_CLI ? 'C' : 'F',
|
||||
ruleDigest(r) & 0xFFF);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1481,7 +1545,7 @@ static void cliGet(FilterRules& filter, int idx, char* reply) {
|
||||
char* out = reply;
|
||||
int remain = CLI_REPLY_MAX;
|
||||
radd(&out, &remain, "r%d %s %s", idx, r->enabled ? "en" : "dis",
|
||||
r->action == FILTER_ACT_DROP ? "drop" : "forward");
|
||||
r->action == FILTER_ACT_DROP ? "drop" : r->action == FILTER_ACT_CLI ? "cli" : "forward");
|
||||
|
||||
const uint16_t type_all = ruleTypeMask(r);
|
||||
if (type_all) {
|
||||
|
||||
@@ -8,8 +8,10 @@
|
||||
// decides the packet's verdict (first match wins). Unspecified predicate =
|
||||
// wildcard. On top of the predicates, prob= and throttle= are match gates: a
|
||||
// rule whose predicates match still only decides a packet its gates admit
|
||||
// (see probDecides/throttleDecides). Actions: drop (enforce) and forward
|
||||
// (terminal: stop the list, count the hit, let the packet through).
|
||||
// (see probDecides/throttleDecides). Actions: drop (enforce), forward
|
||||
// (terminal: stop the list, count the hit, let the packet through) and cli
|
||||
// (forward plus queue the message text as a CLI script, executed deferred —
|
||||
// see CliScript.h).
|
||||
//
|
||||
// For a decrypted group packet the whole rule list — packet-level and content
|
||||
// predicates alike — is evaluated once in checkContent() (called from
|
||||
@@ -39,6 +41,9 @@
|
||||
#define FILTER_ACT_DROP 1
|
||||
#define FILTER_ACT_FORWARD 2 // value 2 = the old logonly byte; configs
|
||||
// load identically across the rename
|
||||
#define FILTER_ACT_CLI 3 // forward + enqueue the message text as a
|
||||
// deferred CLI script (CliScriptRunner); rides
|
||||
// the existing action byte, no record growth
|
||||
|
||||
// payload-type mask bits (indexed by mesh::Packet payload type, 4 bits).
|
||||
// All types a rule can name, low byte first: the filter sees every packet the
|
||||
@@ -108,7 +113,7 @@ struct Interval {
|
||||
|
||||
struct FilterRule {
|
||||
bool enabled;
|
||||
uint8_t action; // FILTER_ACT_DROP | FILTER_ACT_FORWARD
|
||||
uint8_t action; // FILTER_ACT_DROP | FILTER_ACT_FORWARD | FILTER_ACT_CLI
|
||||
uint8_t type_mask; // bits 0..7 by payload type (see FILTER_TYPE_*); 0 = any
|
||||
uint8_t route_mask; // FILTER_ROUTE_* bits; 0 = any
|
||||
Interval hops; // flood path length (getPathHashCount)
|
||||
@@ -348,6 +353,12 @@ private:
|
||||
void billSaved(uint32_t est_air_ms) { air_saved_ms += est_air_ms; }
|
||||
bool regexMatches(const char* pattern, const char* subject);
|
||||
bool channelMatchesStore(const FilterRule* r, const mesh::GroupChannel& channel) const;
|
||||
// A cli rule must never execute scripts on a '#'-named channel: such names
|
||||
// derive their key from the public channel name, which authenticates nobody.
|
||||
// When no surviving channel of the rule is PSK-backed (e.g. its private
|
||||
// channel was deleted), clear the mask — MASK_SET with no bits is the
|
||||
// documented inert rule, and the record stays savable.
|
||||
void confineCliRule(FilterRule* r);
|
||||
// match gates + commit, shared by checkPacket()/checkContent(): run the prob
|
||||
// roll and the throttle gate; when the rule decides, record the hit, bill
|
||||
// the saved airtime, and store its action in `out`. Returns false when a
|
||||
|
||||
@@ -4732,7 +4732,7 @@ TEST_F(FilterTest, StashClearedOnNewSequence) {
|
||||
// ---------------------------------------------------------------- rename (logonly -> forward)
|
||||
|
||||
TEST_F(FilterTest, LogonlyKeywordRejected) {
|
||||
EXPECT_EQ(cli(filter, "add sender=x action=logonly"), "Err - action must be drop|forward");
|
||||
EXPECT_EQ(cli(filter, "add sender=x action=logonly"), "Err - action must be drop|forward|cli");
|
||||
EXPECT_EQ(filter.getNumRules(), 0); // rolled back, no half rule
|
||||
}
|
||||
|
||||
@@ -4865,6 +4865,99 @@ TEST_F(FilterTest, MoveChangesVerdicts) {
|
||||
EXPECT_EQ(contentCheck(filter, pkt, "#foo", "Alice", "hi"), FILTER_ACT_DROP);
|
||||
}
|
||||
|
||||
// UNIT TESTS: action=cli rule model (validation, display, persistence)
|
||||
// ============================================================
|
||||
|
||||
// add a private (PSK-backed) channel and return its store index
|
||||
static int addPrivateChan(FilterRules& filter, const char* name) {
|
||||
std::string cmd = std::string("chan add ") + name + " 00112233445566778899aabbccddeeff";
|
||||
expectOk(filter, cmd.c_str());
|
||||
int idx = filter.indexOfChannel(name);
|
||||
EXPECT_GE(idx, 0);
|
||||
return idx;
|
||||
}
|
||||
|
||||
TEST_F(FilterTest, CliRuleRequiresChannel) {
|
||||
ASSERT_EQ(cli(filter, "add action=cli"), "Err - action=cli needs chan=<private channel>");
|
||||
ASSERT_EQ(cli(filter, "add chanhash=AB action=cli"),
|
||||
"Err - action=cli needs chan=<private channel>"); // bare air hash is not a keyed channel
|
||||
ASSERT_EQ(cli(filter, "add chan=#ops action=cli"),
|
||||
"Err - action=cli needs chan=<private channel>"); // '#'-names authenticate nobody
|
||||
EXPECT_EQ(filter.getNumChannels(), 1); // the auto-provisioned '#ops' was rolled back
|
||||
}
|
||||
|
||||
TEST_F(FilterTest, CliRuleAcceptsPskChannelAmongHashChannels) {
|
||||
addPrivateChan(filter, "ops");
|
||||
expectOk(filter, "add chan=ops,#ops2 action=cli");
|
||||
EXPECT_EQ(filter.getRule(0)->action, FILTER_ACT_CLI);
|
||||
}
|
||||
|
||||
TEST_F(FilterTest, CliRuleRejectsProbAndThrottle) {
|
||||
addPrivateChan(filter, "ops");
|
||||
ASSERT_EQ(cli(filter, "add chan=ops action=cli prob=50"),
|
||||
"Err - action=cli takes no prob/throttle");
|
||||
ASSERT_EQ(cli(filter, "add prob=50 chan=ops action=cli"),
|
||||
"Err - action=cli takes no prob/throttle"); // gate given before the action
|
||||
ASSERT_EQ(cli(filter, "add chan=ops action=cli throttle=60"),
|
||||
"Err - action=cli takes no prob/throttle");
|
||||
EXPECT_EQ(filter.getNumRules(), 0); // every rejected add was rolled back
|
||||
}
|
||||
|
||||
TEST_F(FilterTest, CliRuleRejectsNonTxtType) {
|
||||
addPrivateChan(filter, "ops");
|
||||
ASSERT_EQ(cli(filter, "add chan=ops type=data action=cli"),
|
||||
"Err - action=cli runs group text only");
|
||||
ASSERT_EQ(cli(filter, "add chan=ops action=cli type=advert"),
|
||||
"Err - action=cli runs group text only"); // type= after the action
|
||||
ASSERT_EQ(cli(filter, "add chan=ops type=txt,data action=cli"),
|
||||
"Err - action=cli runs group text only");
|
||||
EXPECT_EQ(filter.getNumRules(), 0);
|
||||
}
|
||||
|
||||
TEST_F(FilterTest, CliRuleForcesTxtTypeAndDisplaysCli) {
|
||||
addPrivateChan(filter, "ops");
|
||||
ASSERT_EQ(cli(filter, "add chan=ops action=cli"), "OK - rule 0 added (cli: PSK holder = admin)");
|
||||
auto r = filter.getRule(0);
|
||||
EXPECT_EQ(r->action, FILTER_ACT_CLI);
|
||||
EXPECT_EQ(r->type_mask, FILTER_TYPE_GRP_TXT); // omitted type= is forced to txt
|
||||
EXPECT_EQ(r->type_mask_hi, 0);
|
||||
std::string listing = cli(filter, "list");
|
||||
ASSERT_EQ(listing.substr(0, 9), "on 1/32: ");
|
||||
EXPECT_EQ(listing[11], 'C'); // action letter in the "0eC4A2" token
|
||||
std::string detail = cli(filter, "get 0");
|
||||
EXPECT_EQ(detail.substr(0, 9), "r0 en cli");
|
||||
EXPECT_NE(detail.find("type=txt"), std::string::npos);
|
||||
}
|
||||
|
||||
TEST_F(FilterTest, CliRuleRoundtripsThroughSaveLoad) {
|
||||
// validRule() must accept the new action value, or a saved cli rule would
|
||||
// truncate the rule list on the next boot
|
||||
addPrivateChan(filter, "ops");
|
||||
expectOk(filter, "add chan=ops action=cli");
|
||||
filter.save(&fs);
|
||||
filter.load(&fs);
|
||||
ASSERT_EQ(filter.getNumRules(), 1);
|
||||
EXPECT_EQ(filter.getRule(0)->action, FILTER_ACT_CLI);
|
||||
EXPECT_EQ(filter.getRule(0)->type_mask, FILTER_TYPE_GRP_TXT);
|
||||
}
|
||||
|
||||
TEST_F(FilterTest, CliRuleGoesInertWhenItsPskChannelIsDeleted) {
|
||||
// chan=ops,#ops2: deleting the PSK channel must not leave the rule executing
|
||||
// scripts on the '#'-named (derived-key, public) channel
|
||||
addPrivateChan(filter, "ops");
|
||||
expectOk(filter, "add chan=ops,#ops2 action=cli");
|
||||
ASSERT_EQ(cli(filter, "chan del ops"), "OK - chan ops deleted");
|
||||
mesh::Packet pkt;
|
||||
EXPECT_EQ(contentCheck(filter, pkt, "#ops2", "Alice", "!id k1\nreset"),
|
||||
FILTER_ACT_ALLOW); // inert: no PSK-backed channel left in the mask
|
||||
// the inert record is still savable (MASK_SET with no bits, not a wildcard)
|
||||
filter.save(&fs);
|
||||
filter.load(&fs);
|
||||
ASSERT_EQ(filter.getNumRules(), 1);
|
||||
EXPECT_EQ(filter.getRule(0)->action, FILTER_ACT_CLI);
|
||||
EXPECT_EQ(filter.getRule(0)->chan_mask, 0);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
|
||||
Reference in new issue
Block a user