From d59774d7508e40f5ef73e6515b1ea9df0c7e8354 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 28 Sep 2022 15:11:55 -0500 Subject: [PATCH] don't use regcomp to check hostname validity --- shared-bindings/wifi/Radio.c | 45 ++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/shared-bindings/wifi/Radio.c b/shared-bindings/wifi/Radio.c index 3238025622..7463cdc77e 100644 --- a/shared-bindings/wifi/Radio.c +++ b/shared-bindings/wifi/Radio.c @@ -27,7 +27,6 @@ #include "shared-bindings/wifi/__init__.h" #include "shared-bindings/wifi/AuthMode.h" -#include #include #include "py/runtime.h" @@ -35,6 +34,43 @@ #define MAC_ADDRESS_LENGTH 6 +STATIC bool hostname_valid(const char *ptr, size_t len) { + #if 0 // validated by mp_arg_validate_length_range + if (len == 0 || len > 253) { + // at most 253 characters long + return false; + } + #endif + int partlen = 0; + while (len) { + char c = *ptr++; + len--; + if (c == '.') { + if (partlen == 0 || partlen > 63) { + return false; + } + partlen = 0; + continue; + } + partlen++; + if (c == '-') { + if (partlen == 1) { + return false; // part cannot begin with a dash + } + continue; + } else if ( + (c >= 'a' && c <= 'z') || + (c >= 'A' && c <= 'Z') || + (c >= '0' && c <= '9')) { + continue; + } + return false; + } + // check length of last part + return !(partlen > 63); +} + + //| class Radio: //| """Native wifi radio. //| @@ -89,14 +125,9 @@ STATIC mp_obj_t wifi_radio_set_hostname(mp_obj_t self_in, mp_obj_t hostname_in) mp_arg_validate_length_range(hostname.len, 1, 253, MP_QSTR_hostname); - #if !(defined(CONFIG_IDF_TARGET_ESP32C3) || defined(RASPBERRYPI)) - regex_t regex; // validate hostname according to RFC 1123 - regcomp(®ex,"^(([a-z0-9]|[a-z0-9][a-z0-9\\-]{0,61}[a-z0-9])\\.)*([a-z0-9]|[a-z0-9][a-z0-9\\-]{0,61}[a-z0-9])$", REG_EXTENDED | REG_ICASE | REG_NOSUB); - if (regexec(®ex, hostname.buf, 0, NULL, 0)) { + if (!hostname_valid(hostname.buf, hostname.len)) { mp_raise_ValueError(translate("invalid hostname")); } - regfree(®ex); - #endif wifi_radio_obj_t *self = MP_OBJ_TO_PTR(self_in); common_hal_wifi_radio_set_hostname(self, hostname.buf);