Просмотр исходного кода

Validate GPIO config fields and reject malformed HTTP pin numbers

SybexXx 3 дней назад
Родитель
Сommit
0aaa35439d
1 измененных файлов с 34 добавлено и 12 удалено
  1. 34 12
      code/components/jomjol_controlGPIO/server_GPIO.cpp

+ 34 - 12
code/components/jomjol_controlGPIO/server_GPIO.cpp

@@ -516,6 +516,9 @@ bool GpioHandler::readConfig()
     while (configFile.getNextLine(&line, disabledLine, eof) && !configFile.isNewParagraph(line))
     {
         splitted = ZerlegeZeile(line);
+        if (splitted.empty() || splitted[0].empty()) {
+            continue;
+        }
         // const std::regex pieces_regex("IO([0-9]{1,2})");
         // std::smatch pieces_match;
         // if (std::regex_match(splitted[0], pieces_match, pieces_regex) && (pieces_match.size() == 2))
@@ -588,27 +591,36 @@ bool GpioHandler::readConfig()
         }
         if (toUpper(splitted[0]) == "LEDNUMBERS")
         {
-            LEDNumbers = stoi(splitted[1]);
+            if (splitted.size() < 2) {
+                ESP_LOGW(TAG, "Ignoring incomplete LEDNUMBERS configuration");
+            } else {
+                LEDNumbers = atoi(splitted[1].c_str());
+            }
         }
         if (toUpper(splitted[0]) == "LEDCOLOR")
         {
-            uint8_t _r, _g, _b;
-            _r = stoi(splitted[1]);
-            _g = stoi(splitted[2]);
-            _b = stoi(splitted[3]);
-
-            LEDColor = Rgb{_r, _g, _b};
+            if (splitted.size() < 4) {
+                ESP_LOGW(TAG, "Ignoring incomplete LEDCOLOR configuration");
+            } else {
+                uint8_t _r = static_cast<uint8_t>(atoi(splitted[1].c_str()));
+                uint8_t _g = static_cast<uint8_t>(atoi(splitted[2].c_str()));
+                uint8_t _b = static_cast<uint8_t>(atoi(splitted[3].c_str()));
+                LEDColor = Rgb{_r, _g, _b};
+            }
         }
         if (toUpper(splitted[0]) == "LEDTYPE")
         {
-            if (splitted[1] == "WS2812")
+            if (splitted.size() < 2) {
+                ESP_LOGW(TAG, "Ignoring incomplete LEDTYPE configuration");
+            } else if (splitted[1] == "WS2812") {
                 LEDType = LED_WS2812;
-            if (splitted[1] == "WS2812B")
+            } else if (splitted[1] == "WS2812B") {
                 LEDType = LED_WS2812B;
-            if (splitted[1] == "SK6812")
+            } else if (splitted[1] == "SK6812") {
                 LEDType = LED_SK6812;
-            if (splitted[1] == "WS2813")
+            } else if (splitted[1] == "WS2813") {
                 LEDType = LED_WS2813;
+            }
         }
     }
 
@@ -712,8 +724,18 @@ esp_err_t GpioHandler::handleHttpRequest(httpd_req_t *req)
         return ESP_OK;    
     }
 
-    int gpionum = stoi(gpio);
+    if (gpio.empty()) {
+        httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "GPIO number is missing");
+        return ESP_OK;
+    }
+    for (size_t i = 0; i < gpio.length(); ++i) {
+        if (gpio[i] < '0' || gpio[i] > '9') {
+            httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "GPIO number must be numeric");
+            return ESP_OK;
+        }
+    }
 
+    const int gpionum = atoi(gpio.c_str());
     gpio_num_t gpio_num = static_cast<gpio_num_t>(gpionum);
     if (gpioBoardFindUserPin(gpio_num) == NULL)
     {