Reject invalid configuration for the default MQTT server (#6066)

* Sanity check configuration for the default MQTT server

* Skip for MESHTASTIC_EXCLUDE_MQTT

---------

Co-authored-by: Ben Meadors <benmmeadors@gmail.com>
This commit is contained in:
Eric Severance
2025-02-16 05:15:30 -08:00
committed by GitHub
parent 4407d9e040
commit 7648391f91
5 changed files with 78 additions and 6 deletions

View File

@@ -41,6 +41,7 @@ MQTT *mqtt;
namespace
{
constexpr int reconnectMax = 5;
constexpr uint16_t mqttPort = 1883;
// FIXME - this size calculation is super sloppy, but it will go away once we dynamically alloc meshpackets
static uint8_t bytes[meshtastic_MqttClientProxyMessage_size + 30]; // 12 for channel name and 16 for nodeid
@@ -245,6 +246,11 @@ std::pair<String, uint16_t> parseHostAndPort(String server, uint16_t port = 0)
}
return std::make_pair(std::move(server), port);
}
bool isDefaultServer(const String &host)
{
return host.length() == 0 || host == default_mqtt_address;
}
} // namespace
void MQTT::mqttCallback(char *topic, byte *payload, unsigned int length)
@@ -324,7 +330,7 @@ MQTT::MQTT() : concurrency::OSThread("mqtt"), mqttQueue(MAX_MQTT_QUEUE)
}
String host = parseHostAndPort(moduleConfig.mqtt.address).first;
isConfiguredForDefaultServer = host.length() == 0 || host == default_mqtt_address;
isConfiguredForDefaultServer = isDefaultServer(host);
IPAddress ip;
isMqttServerAddressPrivate = ip.fromString(host.c_str()) && isPrivateIpAddress(ip);
@@ -408,7 +414,7 @@ void MQTT::reconnect()
}
#if HAS_NETWORKING
// Defaults
int serverPort = 1883;
int serverPort = mqttPort;
const char *serverAddr = default_mqtt_address;
const char *mqttUsername = default_mqtt_username;
const char *mqttPassword = default_mqtt_password;
@@ -561,6 +567,23 @@ int32_t MQTT::runOnce()
return 30000;
}
bool MQTT::isValidConfig(const meshtastic_ModuleConfig_MQTTConfig &config)
{
String host;
uint16_t port;
std::tie(host, port) = parseHostAndPort(config.address, mqttPort);
const bool defaultServer = isDefaultServer(host);
if (defaultServer && config.tls_enabled) {
LOG_ERROR("Invalid MQTT config: TLS was enabled, but the default server does not support TLS");
return false;
}
if (defaultServer && port != mqttPort) {
LOG_ERROR("Invalid MQTT config: Unsupported port '%d' for the default MQTT server", port);
return false;
}
return true;
}
void MQTT::publishNodeInfo()
{
// TODO: NodeInfo broadcast over MQTT only (NODENUM_BROADCAST_NO_LORA)