tests: cover CLI prefix stripping, set-pin gate, paged reply bounds, and ACL write failures
Changelog: -
This commit is contained in:
1 parent
0dcc9c6e06
commit
5f31fc8e50
6 files changed
+192
-35
No files matched your search
@@ -2126,14 +2126,7 @@ void MyMesh::enterCLIRescue() {
|
||||
extern "C" void retainedClockInvalidate() __attribute__((weak)); // NRF52Board.cpp
|
||||
|
||||
bool MyMesh::handleCommand(const char* command, uint32_t sender_timestamp, char* reply) {
|
||||
while (*command == ' ') command++; // skip leading spaces
|
||||
|
||||
if (strlen(command) > 4 && command[2] == '|') { // optional prefix (for companion radio CLI)
|
||||
memcpy(reply, command, 3); // reflect the prefix back
|
||||
reply += 3;
|
||||
*reply = 0;
|
||||
command += 3;
|
||||
}
|
||||
cli_args::stripPrefix(command, reply);
|
||||
|
||||
if (_prefs.getRadioPrefs()->handleCommand(command, sender_timestamp, reply)) { // is radio CLI command?
|
||||
if (_prefs.getRadioPrefs()->isDirty()) { savePrefs(); }
|
||||
@@ -2207,18 +2200,15 @@ bool MyMesh::handleCommand(const char* command, uint32_t sender_timestamp, char*
|
||||
#endif
|
||||
|
||||
if (memcmp(command, "set pin ", 8) == 0) {
|
||||
const char* err = nullptr;
|
||||
#ifdef LOCKED_NODE
|
||||
if (_cli_rescue) {
|
||||
// CUSTOM (TeTeHacko): a known PIN = pair and read it all (checked after
|
||||
// leading-space and XX| stripping so " set pin ..." cannot bypass it)
|
||||
strcpy(reply, "Error: locked build");
|
||||
return true;
|
||||
}
|
||||
const bool locked = true;
|
||||
#else
|
||||
const bool locked = false;
|
||||
#endif
|
||||
if (sender_timestamp == 0 && !_cli_rescue && (!_serial || !_serial->isConnectionSecure())) {
|
||||
// CUSTOM (TeTeHacko): the same gate as CMD_SET_DEVICE_PIN -- through the
|
||||
// CLI, USB set the PIN it may not set directly, then paired over BLE
|
||||
strcpy(reply, "Error: set pin only over paired BLE");
|
||||
if (!cli_args::canSetPin(sender_timestamp, _cli_rescue, locked,
|
||||
_serial && _serial->isConnectionSecure(), &err)) {
|
||||
strcpy(reply, err);
|
||||
return true;
|
||||
}
|
||||
long pin; // atoi(): "abc" was PIN 0, i.e. a random one
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "MyMesh.h"
|
||||
#include <algorithm>
|
||||
#include <helpers/CliArgs.h>
|
||||
#include <helpers/sensors/LPPDataHelpers.h>
|
||||
#include <helpers/UTF8Helpers.h>
|
||||
|
||||
@@ -219,9 +220,7 @@ void MyMesh::formatNodesReply(char* reply, int max_len, uint16_t offset) {
|
||||
prefix, (unsigned)n->type, (long)n->lat, (long)n->lon,
|
||||
(int)n->snr, (int)n->rssi, (unsigned long)(now - n->heard_timestamp),
|
||||
(unsigned long)n->pkt_hash, pathhex, n->name);
|
||||
int ll = strlen(line);
|
||||
if ((dp - reply) + ll + 24 > max_len) break; // room for trailer; never overflow reply
|
||||
memcpy(dp, line, ll); dp += ll;
|
||||
if (!cli_args::appendLine(dp, reply, max_len, 24, line)) break; // room for trailer; never overflow reply
|
||||
}
|
||||
dp += sprintf(dp, "E,%d,%d", i, total);
|
||||
*dp = 0;
|
||||
@@ -276,9 +275,7 @@ void MyMesh::formatRxLogReply(char* reply, int max_len, uint32_t cursor) {
|
||||
(unsigned long)r->seq, (unsigned long)r->when,
|
||||
(unsigned long)r->pkt_hash, (unsigned)r->header,
|
||||
(int)r->snr, (int)r->rssi, pathhex);
|
||||
int ll = strlen(line);
|
||||
if ((dp - reply) + ll + 40 > max_len) break; // room for trailer; never overflow reply
|
||||
memcpy(dp, line, ll); dp += ll;
|
||||
if (!cli_args::appendLine(dp, reply, max_len, 40, line)) break; // room for trailer; never overflow reply
|
||||
last = r->seq;
|
||||
}
|
||||
dp += sprintf(dp, "E,%lu,%lu,%lu", (unsigned long)last, (unsigned long)oldest, (unsigned long)newest);
|
||||
@@ -1931,12 +1928,9 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, char *command, char *reply
|
||||
}
|
||||
#endif
|
||||
|
||||
if (strlen(command) > 4 && command[2] == '|') { // optional prefix (for companion radio CLI)
|
||||
memcpy(reply, command, 3); // reflect the prefix back
|
||||
reply += 3;
|
||||
reply_max = reply_max > 3 ? reply_max - 3 : 0;
|
||||
command += 3;
|
||||
}
|
||||
const char* cmd = command;
|
||||
cli_args::stripPrefix(cmd, reply, &reply_max);
|
||||
command = const_cast<char*>(cmd);
|
||||
|
||||
// handle ACL related commands
|
||||
if (memcmp(command, "setperm ", 8) == 0) { // format: setperm {pubkey-hex} {permissions-int8}
|
||||
|
||||
@@ -11,7 +11,9 @@
|
||||
// float a '.' and fraction. No exponent, no hex, no trailing text.
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
namespace cli_args {
|
||||
|
||||
@@ -53,4 +55,45 @@ inline bool parseFloat(const char* s, float lo, float hi, float* out) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Strips leading spaces and an optional 3-byte "XX|" prefix (reflected into
|
||||
// `reply`, deducting 3 from `*reply_max` when provided).
|
||||
inline void stripPrefix(const char*& command, char*& reply, int* reply_max = nullptr) {
|
||||
while (*command == ' ') command++;
|
||||
if (strlen(command) > 4 && command[2] == '|') {
|
||||
if (!reply_max || *reply_max >= 3) {
|
||||
memcpy(reply, command, 3);
|
||||
reply += 3;
|
||||
*reply = 0;
|
||||
if (reply_max) *reply_max -= 3;
|
||||
}
|
||||
command += 3;
|
||||
while (*command == ' ') command++;
|
||||
}
|
||||
}
|
||||
|
||||
// Security gate for companion `set pin`: blocked in locked CLI rescue and over
|
||||
// unencrypted local transports (USB), allowed over paired BLE or remote ACL.
|
||||
inline bool canSetPin(uint32_t sender_timestamp, bool cli_rescue, bool locked_node,
|
||||
bool serial_secure, const char** err_out = nullptr) {
|
||||
if (locked_node && cli_rescue) {
|
||||
if (err_out) *err_out = "Error: locked build";
|
||||
return false;
|
||||
}
|
||||
if (sender_timestamp == 0 && !cli_rescue && !serial_secure) {
|
||||
if (err_out) *err_out = "Error: set pin only over paired BLE";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Appends `line` to a paged CLI reply only if it fits with `trailer_reserve`
|
||||
// bytes left for the end marker; returns false when the page is full.
|
||||
inline bool appendLine(char*& dp, const char* reply, int max_len, int trailer_reserve, const char* line) {
|
||||
int ll = (int)strlen(line);
|
||||
if ((dp - reply) + ll + trailer_reserve > max_len) return false;
|
||||
memcpy(dp, line, ll);
|
||||
dp += ll;
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace cli_args
|
||||
@@ -18,11 +18,12 @@ class File {
|
||||
std::string _name;
|
||||
size_t _pos = 0;
|
||||
bool _write = false;
|
||||
size_t _short_write = 0;
|
||||
|
||||
public:
|
||||
File() = default;
|
||||
File(std::map<std::string, std::string>* files, const std::string& name, bool write)
|
||||
: _files(files), _name(name), _write(write) {
|
||||
File(std::map<std::string, std::string>* files, const std::string& name, bool write, size_t short_write = 0)
|
||||
: _files(files), _name(name), _write(write), _short_write(short_write) {
|
||||
if (write) (*_files)[name] = "";
|
||||
}
|
||||
explicit operator bool() const { return _files != nullptr; }
|
||||
@@ -34,8 +35,9 @@ public:
|
||||
return (int)n;
|
||||
}
|
||||
size_t write(const uint8_t* buf, size_t len) {
|
||||
(*_files)[_name].append((const char*)buf, len);
|
||||
return len;
|
||||
size_t n = (_short_write > 0 && _short_write < len) ? _short_write : len;
|
||||
(*_files)[_name].append((const char*)buf, n);
|
||||
return n;
|
||||
}
|
||||
void close() {}
|
||||
};
|
||||
@@ -46,14 +48,22 @@ class Adafruit_LittleFS {
|
||||
public:
|
||||
std::map<std::string, std::string> files;
|
||||
int writes = 0;
|
||||
bool fail_next_write_open = false;
|
||||
size_t short_next_write = 0;
|
||||
|
||||
bool exists(const char* name) { return files.count(name) > 0; }
|
||||
bool remove(const char* name) { return files.erase(name) > 0; }
|
||||
bool mkdir(const char*) { return true; }
|
||||
Adafruit_LittleFS_Namespace::File open(const char* name, uint8_t mode = FILE_O_READ) {
|
||||
if (mode == FILE_O_WRITE) {
|
||||
if (fail_next_write_open) {
|
||||
fail_next_write_open = false;
|
||||
return Adafruit_LittleFS_Namespace::File();
|
||||
}
|
||||
writes++;
|
||||
return Adafruit_LittleFS_Namespace::File(&files, name, true);
|
||||
size_t sw = short_next_write;
|
||||
short_next_write = 0;
|
||||
return Adafruit_LittleFS_Namespace::File(&files, name, true, sw);
|
||||
}
|
||||
if (!exists(name)) return Adafruit_LittleFS_Namespace::File();
|
||||
return Adafruit_LittleFS_Namespace::File(&files, name, false);
|
||||
|
||||
@@ -187,6 +187,44 @@ TEST(AclReplayFloor, AlternatesSlotsAcrossReboots) {
|
||||
}
|
||||
}
|
||||
|
||||
TEST(AclReplayFloor, FailedOpenDoesNotRotateSlot) {
|
||||
Node n;
|
||||
ClientInfo* c = n.admin(1);
|
||||
ASSERT_TRUE(n.accept(c, NOW - 30)); // slot 0 -> next slot 1
|
||||
ASSERT_TRUE(n.accept(c, NOW - 20)); // slot 1 -> next slot 0 (oldest)
|
||||
n.acl->save(&n.fs);
|
||||
|
||||
// Open fails on slot 0: must NOT advance to slot 1 (which holds NOW - 20).
|
||||
n.fs.fail_next_write_open = true;
|
||||
ASSERT_TRUE(n.accept(c, NOW - 15));
|
||||
|
||||
// Next write is torn; because slot stayed 0, slot 1 (NOW - 20) stays intact.
|
||||
n.fs.short_next_write = 5;
|
||||
ASSERT_TRUE(n.accept(c, NOW - 10));
|
||||
|
||||
n.boot();
|
||||
c = n.acl->getClient(idFor(1).pub_key, PUB_KEY_SIZE);
|
||||
EXPECT_EQ(c->last_timestamp, NOW - 20);
|
||||
}
|
||||
|
||||
TEST(AclReplayFloor, ShortWriteDoesNotRotateSlot) {
|
||||
Node n;
|
||||
ClientInfo* c = n.admin(1);
|
||||
ASSERT_TRUE(n.accept(c, NOW - 30)); // slot 0
|
||||
ASSERT_TRUE(n.accept(c, NOW - 20)); // slot 1
|
||||
n.acl->save(&n.fs);
|
||||
|
||||
// Two consecutive short writes must both target slot 0 and leave slot 1 intact.
|
||||
n.fs.short_next_write = 4;
|
||||
ASSERT_TRUE(n.accept(c, NOW - 15));
|
||||
n.fs.short_next_write = 4;
|
||||
ASSERT_TRUE(n.accept(c, NOW - 10));
|
||||
|
||||
n.boot();
|
||||
c = n.acl->getClient(idFor(1).pub_key, PUB_KEY_SIZE);
|
||||
EXPECT_EQ(c->last_timestamp, NOW - 20);
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
|
||||
@@ -49,6 +49,88 @@ TEST(CliArgsFloat, RefusesNanInfExponentAndGarbage) {
|
||||
EXPECT_FALSE(parseFloat("91", -90, 90, &f));
|
||||
EXPECT_EQ(f, 1);
|
||||
}
|
||||
|
||||
TEST(CliPrefix, StripsSpacesAndReflectsPrefixWithBudget) {
|
||||
char buf[32] = {};
|
||||
char* reply = buf;
|
||||
int max_len = 150;
|
||||
const char* cmd = " 01| set pin 123456";
|
||||
cli_args::stripPrefix(cmd, reply, &max_len);
|
||||
EXPECT_STREQ(cmd, "set pin 123456");
|
||||
EXPECT_STREQ(buf, "01|");
|
||||
EXPECT_EQ(reply, buf + 3);
|
||||
EXPECT_EQ(max_len, 147);
|
||||
}
|
||||
|
||||
TEST(CliPrefix, LeadingSpacesWithoutPrefix) {
|
||||
char buf[16] = {};
|
||||
char* reply = buf;
|
||||
int max_len = 150;
|
||||
const char* cmd = " set pin 123456";
|
||||
cli_args::stripPrefix(cmd, reply, &max_len);
|
||||
EXPECT_STREQ(cmd, "set pin 123456");
|
||||
EXPECT_STREQ(buf, "");
|
||||
EXPECT_EQ(reply, buf);
|
||||
EXPECT_EQ(max_len, 150);
|
||||
}
|
||||
|
||||
TEST(CliPrefix, TinyBudgetDoesNotOverflowOrGoNegative) {
|
||||
char buf[4] = {'X', 'X', 'X', 0};
|
||||
char* reply = buf;
|
||||
int max_len = 2;
|
||||
const char* cmd = "01|nodes";
|
||||
cli_args::stripPrefix(cmd, reply, &max_len);
|
||||
EXPECT_STREQ(cmd, "nodes");
|
||||
EXPECT_EQ(reply, buf);
|
||||
EXPECT_EQ(max_len, 2);
|
||||
}
|
||||
|
||||
TEST(CliSetPinGate, EnforcesTransportAndLockedBuild) {
|
||||
const char* err = nullptr;
|
||||
// Local USB (unsecured, not rescue) -> blocked
|
||||
EXPECT_FALSE(cli_args::canSetPin(0, false, false, false, &err));
|
||||
EXPECT_STREQ(err, "Error: set pin only over paired BLE");
|
||||
|
||||
// Locked CLI rescue -> blocked
|
||||
EXPECT_FALSE(cli_args::canSetPin(0, true, true, false, &err));
|
||||
EXPECT_STREQ(err, "Error: locked build");
|
||||
|
||||
// Paired BLE -> allowed
|
||||
EXPECT_TRUE(cli_args::canSetPin(0, false, true, true, &err));
|
||||
|
||||
// Unlocked CLI rescue -> allowed
|
||||
EXPECT_TRUE(cli_args::canSetPin(0, true, false, false, &err));
|
||||
|
||||
// Remote ACL admin (sender_timestamp != 0) -> allowed
|
||||
EXPECT_TRUE(cli_args::canSetPin(1763400000u, false, true, false, &err));
|
||||
}
|
||||
|
||||
TEST(CliPagedReply, NeverOverflowsSmallMeshBufferWithPrefix) {
|
||||
// Exact mesh CLI size: 150 B reply buffer, 3 B taken by "01|", leaving 147 B.
|
||||
char buf[150] = {};
|
||||
char* reply = buf;
|
||||
int max_len = (int)sizeof(buf);
|
||||
const char* cmd = "01|nodes";
|
||||
cli_args::stripPrefix(cmd, reply, &max_len);
|
||||
|
||||
char* dp = reply;
|
||||
std::string long_line(140, 'A');
|
||||
long_line.push_back('\n');
|
||||
// 141 B line + 24 B trailer reserve > 147 B -> must refuse even as first line
|
||||
EXPECT_FALSE(cli_args::appendLine(dp, reply, max_len, 24, long_line.c_str()));
|
||||
EXPECT_EQ(dp, reply);
|
||||
|
||||
std::string short_line(50, 'B');
|
||||
short_line.push_back('\n');
|
||||
EXPECT_TRUE(cli_args::appendLine(dp, reply, max_len, 24, short_line.c_str()));
|
||||
EXPECT_TRUE(cli_args::appendLine(dp, reply, max_len, 24, short_line.c_str()));
|
||||
EXPECT_FALSE(cli_args::appendLine(dp, reply, max_len, 24, short_line.c_str()));
|
||||
|
||||
dp += sprintf(dp, "E,2,3");
|
||||
*dp = 0;
|
||||
EXPECT_LT((size_t)(dp - buf), sizeof(buf));
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
|
||||
Reference in new issue
Block a user