Explorar el Código

Reject malformed GPIO keys and invalid interrupt configurations

SybexXx hace 3 días
padre
commit
5fd9afa12c
Se han modificado 1 ficheros con 68 adiciones y 6 borrados
  1. 68 6
      code/components/jomjol_controlGPIO/server_GPIO.cpp

+ 68 - 6
code/components/jomjol_controlGPIO/server_GPIO.cpp

@@ -32,6 +32,48 @@
 
 static const char *TAG = "GPIO";
 
+static bool parseUserGpioKey(const std::string &key, gpio_num_t *gpio)
+{
+    if (gpio == NULL || key.size() < 3U || key.size() > 4U ||
+        key.compare(0, 2, "IO") != 0) {
+        return false;
+    }
+
+    int value = 0;
+    for (size_t i = 2U; i < key.size(); ++i) {
+        const char ch = key[i];
+        if (ch < '0' || ch > '9') {
+            return false;
+        }
+        value = (value * 10) + (ch - '0');
+    }
+
+    /* GPIO0..GPIO48 is the supported configuration namespace. */
+    if (value < 0 || value > 48) {
+        return false;
+    }
+
+    *gpio = static_cast<gpio_num_t>(value);
+    return true;
+}
+
+static bool isSupportedInterruptName(const std::string &name)
+{
+    return name == "disabled" ||
+           name == "rising-edge" ||
+           name == "falling-edge" ||
+           name == "rising-and-falling" ||
+           name == "low-level-trigger" ||
+           name == "high-level-trigger";
+}
+
+static bool isInputPinMode(gpio_pin_mode_t mode)
+{
+    return mode == GPIO_PIN_MODE_INPUT ||
+           mode == GPIO_PIN_MODE_INPUT_PULLUP ||
+           mode == GPIO_PIN_MODE_INPUT_PULLDOWN;
+}
+
 static uint8_t requiredCapabilitiesForPinMode(gpio_pin_mode_t mode)
 {
     switch (mode) {
@@ -528,14 +570,34 @@ bool GpioHandler::readConfig()
         if (toUpper(splitted[0]) == "MAINTOPICMQTT") {
 //            ESP_LOGD(TAG, "MAINTOPICMQTT found");
 //            mainTopicMQTT = splitted[1];
-        } else if ((splitted[0].rfind("IO", 0) == 0) && (splitted.size() >= 6))
+        } else if (toUpper(splitted[0]).rfind("IO", 0) == 0)
         {
-            ESP_LOGI(TAG,"Enable GP%s in %s mode", splitted[0].c_str(), splitted[1].c_str());
-            std::string gpioStr = splitted[0].substr(2, 2);
-            gpio_num_t gpioNr = (gpio_num_t)atoi(gpioStr.c_str());
+            gpio_num_t gpioNr = GPIO_NUM_NC;
+            const std::string gpioKey = toUpper(splitted[0]);
+            if (!parseUserGpioKey(gpioKey, &gpioNr)) {
+                ESP_LOGW(TAG, "Ignoring malformed GPIO configuration key '%s'", splitted[0].c_str());
+                continue;
+            }
+            if (splitted.size() < 6U) {
+                ESP_LOGW(TAG, "Ignoring incomplete GPIO configuration for %s", gpioKey.c_str());
+                continue;
+            }
+
+            ESP_LOGI(TAG,"Enable %s in %s mode", gpioKey.c_str(), splitted[1].c_str());
             gpio_pin_mode_t pinMode = resolvePinMode(toLower(splitted[1]));
-            gpio_int_type_t intType = resolveIntType(toLower(splitted[2]));
-            uint16_t dutyResolution = (uint8_t)atoi(splitted[3].c_str());
+            const std::string interruptName = toLower(splitted[2]);
+            if (!isSupportedInterruptName(interruptName)) {
+                ESP_LOGW(TAG, "Ignoring %s: unsupported interrupt setting '%s'",
+                         gpioKey.c_str(), interruptName.c_str());
+                continue;
+            }
+            gpio_int_type_t intType = resolveIntType(interruptName);
+            uint16_t dutyResolution = static_cast<uint8_t>(atoi(splitted[3].c_str()));
+
+            if (intType != GPIO_INTR_DISABLE && !isInputPinMode(pinMode)) {
+                ESP_LOGW(TAG, "Ignoring %s: interrupts are only valid for input modes", gpioKey.c_str());
+                continue;
+            }
 
             if (pinMode == GPIO_PIN_MODE_DISABLED) {
                 ESP_LOGW(TAG, "Ignoring disabled/unknown GPIO mode in %s", splitted[0].c_str());