mirror of
https://github.com/jhuebert/MeshCore.git
synced 2026-12-03 10:54:13 +00:00
fix(filter): refuse a trailing word on chan add and chan del
Both channel commands consumed only the tokens they needed and ignored the rest of the line, so `filter chan del Public typo` reported OK and deleted the channel — the one failure mode this branch refuses everywhere else, where a mistake that still changes the config can lock an admin out. Each now rejects an unexpected extra word with its own usage line before touching the store.
This commit is contained in:
1 parent
7849c1e200
commit
4b690bbc4f
2 files changed
+22
No files matched your search
@@ -1263,6 +1263,7 @@ static void cliChanAdd(FilterRules& filter, char* params, char* reply) {
|
||||
char* name = nextToken(&p);
|
||||
char* psk = nextToken(&p);
|
||||
if (name == NULL) { strcpy(reply, "Err - usage: filter chan add <name> [<psk-hex>]"); return; }
|
||||
if (!cliNoExtra(p, reply, "Err - usage: filter chan add <name> [<psk-hex>]")) return;
|
||||
if (strlen(name) >= FILTER_CHAN_NAME_LEN) { strcpy(reply, "Err - name too long"); return; }
|
||||
if (name[0] == '#' && name[1] == 0) { strcpy(reply, "Err - empty chan name"); return; }
|
||||
if (!validChannelName(name)) {
|
||||
@@ -1295,6 +1296,7 @@ static void cliChanDel(FilterRules& filter, char* params, char* reply) {
|
||||
char* p = params;
|
||||
char* name = nextToken(&p);
|
||||
if (name == NULL) { strcpy(reply, "Err - usage: filter chan del <name>"); return; }
|
||||
if (!cliNoExtra(p, reply, "Err - usage: filter chan del <name>")) return;
|
||||
int idx = filter.indexOfChannel(name);
|
||||
if (idx < 0) { strcpy(reply, "Err - unknown channel"); return; }
|
||||
filter.delChannel(idx);
|
||||
|
||||
@@ -1496,6 +1496,8 @@ TEST_F(FilterTest, ExtraTokensAreRejectedBeforeAnythingChanges) {
|
||||
{ "ratelimit clear junk", "Err - usage: ratelimit clear" },
|
||||
{ "stats junk", "Err - usage: stats [reset]" },
|
||||
{ "chan list 0 junk", "Err - usage: chan list [<start-idx>]" },
|
||||
{ "chan add #x 00112233445566778899aabbccddeeff junk", "Err - usage: filter chan add <name> [<psk-hex>]" },
|
||||
{ "chan del Public junk", "Err - usage: filter chan del <name>" },
|
||||
};
|
||||
for (const auto& c : cases) {
|
||||
EXPECT_EQ(cli(filter, c.cmd), c.usage) << c.cmd;
|
||||
@@ -1520,6 +1522,24 @@ TEST_F(FilterTest, RejectedTailsLeaveTheConfigUntouched) {
|
||||
EXPECT_EQ(filter.getAdvertRatelimit(), 48);
|
||||
}
|
||||
|
||||
TEST_F(FilterTest, RejectedChanTailsLeaveTheStoreUntouched) {
|
||||
// a trailing word on a channel command is a typo, not something to ignore: the
|
||||
// channel must neither be added nor deleted
|
||||
EXPECT_EQ(cli(filter, "chan add #new 00112233445566778899aabbccddeeff junk"),
|
||||
"Err - usage: filter chan add <name> [<psk-hex>]");
|
||||
EXPECT_EQ(filter.findChannel("#new"), nullptr);
|
||||
ASSERT_NE(filter.findChannel("Public"), nullptr);
|
||||
|
||||
EXPECT_EQ(cli(filter, "chan del Public junk"), "Err - usage: filter chan del <name>");
|
||||
ASSERT_NE(filter.findChannel("Public"), nullptr);
|
||||
EXPECT_EQ(filter.getNumChannels(), 1);
|
||||
|
||||
// the same commands without the tail still work
|
||||
ASSERT_EQ(cli(filter, "chan add #new 00112233445566778899aabbccddeeff").substr(0, 3), "OK ");
|
||||
EXPECT_EQ(cli(filter, "chan del Public"), "OK - chan Public deleted");
|
||||
EXPECT_EQ(filter.getNumChannels(), 1);
|
||||
}
|
||||
|
||||
TEST_F(FilterTest, OversizedAndUnbalancedCommandsAreRefusedWhole) {
|
||||
// a valid prefix plus an invalid tail must not be applied as the prefix
|
||||
std::string long_cmd = "add sender=";
|
||||
|
||||
Reference in new issue
Block a user