Skip to content

Commit 8faa04a

Browse files
Validate short and long names so whitespace or empty names cannot be used (#6993)
* Say issue #6867 about adding validation for long_name and short_name. Firmware should expect at least 1 non-whitespace character for both long_name and short_name. added the USERPREFS_CONFIG_DEVICE_ROLE example to userPrefs.jsonc * Validation for user long_name and short_name implemented. No longer can use whitespace characters. Return BAD_REQUEST error responses when validation fails and warning logs when validation rejects invalid names. * Improve whitespace validation for user names with ctype.h, ensure logging works * Add whitespace validation to ham mode to prevent validation bypass and to match python cli command * punctuation change --------- Co-authored-by: Ben Meadors <benmmeadors@gmail.com>
1 parent fede1b8 commit 8faa04a

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

src/modules/AdminModule.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "SPILock.h"
88
#include "meshUtils.h"
99
#include <FSCommon.h>
10+
#include <ctype.h> // for better whitespace handling
1011
#if defined(ARCH_ESP32) && !MESHTASTIC_EXCLUDE_BLUETOOTH
1112
#include "BleOta.h"
1213
#endif
@@ -155,6 +156,28 @@ bool AdminModule::handleReceivedProtobuf(const meshtastic_MeshPacket &mp, meshta
155156
*/
156157
case meshtastic_AdminMessage_set_owner_tag:
157158
LOG_DEBUG("Client set owner");
159+
// Validate names
160+
if (*r->set_owner.long_name) {
161+
const char *start = r->set_owner.long_name;
162+
// Skip all whitespace (space, tab, newline, etc)
163+
while (*start && isspace((unsigned char)*start))
164+
start++;
165+
if (*start == '\0') {
166+
LOG_WARN("Rejected long_name: must contain at least 1 non-whitespace character");
167+
myReply = allocErrorResponse(meshtastic_Routing_Error_BAD_REQUEST, &mp);
168+
break;
169+
}
170+
}
171+
if (*r->set_owner.short_name) {
172+
const char *start = r->set_owner.short_name;
173+
while (*start && isspace((unsigned char)*start))
174+
start++;
175+
if (*start == '\0') {
176+
LOG_WARN("Rejected short_name: must contain at least 1 non-whitespace character");
177+
myReply = allocErrorResponse(meshtastic_Routing_Error_BAD_REQUEST, &mp);
178+
break;
179+
}
180+
}
158181
handleSetOwner(r->set_owner);
159182
break;
160183

@@ -1153,6 +1176,27 @@ void AdminModule::handleStoreDeviceUIConfig(const meshtastic_DeviceUIConfig &uic
11531176

11541177
void AdminModule::handleSetHamMode(const meshtastic_HamParameters &p)
11551178
{
1179+
// Validate ham parameters before setting since this would bypass validation in the owner struct
1180+
if (*p.call_sign) {
1181+
const char *start = p.call_sign;
1182+
// Skip all whitespace
1183+
while (*start && isspace((unsigned char)*start))
1184+
start++;
1185+
if (*start == '\0') {
1186+
LOG_WARN("Rejected ham call_sign: must contain at least 1 non-whitespace character");
1187+
return;
1188+
}
1189+
}
1190+
if (*p.short_name) {
1191+
const char *start = p.short_name;
1192+
while (*start && isspace((unsigned char)*start))
1193+
start++;
1194+
if (*start == '\0') {
1195+
LOG_WARN("Rejected ham short_name: must contain at least 1 non-whitespace character");
1196+
return;
1197+
}
1198+
}
1199+
11561200
// Set call sign and override lora limitations for licensed use
11571201
strncpy(owner.long_name, p.call_sign, sizeof(owner.long_name));
11581202
strncpy(owner.short_name, p.short_name, sizeof(owner.short_name));

userPrefs.jsonc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
// "USERPREFS_CONFIG_LORA_REGION": "meshtastic_Config_LoRaConfig_RegionCode_US",
2222
// "USERPREFS_CONFIG_OWNER_LONG_NAME": "My Long Name",
2323
// "USERPREFS_CONFIG_OWNER_SHORT_NAME": "MLN",
24+
// "USERPREFS_CONFIG_DEVICE_ROLE": "meshtastic_Config_DeviceConfig_Role_CLIENT", // Defaults to CLIENT. ROUTER*, LOST AND FOUND, and REPEATER roles are restricted.
2425
// "USERPREFS_EVENT_MODE": "1",
2526
// "USERPREFS_FIXED_BLUETOOTH": "121212",
2627
// "USERPREFS_FIXED_GPS": "",

0 commit comments

Comments
 (0)