summaryrefslogtreecommitdiff
path: root/shared-bindings
diff options
context:
space:
mode:
authormicroDev <70126934+microDev1@users.noreply.github.com>2020-10-15 16:08:01 +0530
committermicroDev <70126934+microDev1@users.noreply.github.com>2020-10-15 16:08:01 +0530
commit26fd2c62238bb2fad5c0b727e279157f3f6c02cb (patch)
tree1035052468f01dd1a7de2dfe50e26aa33138cdd2 /shared-bindings
parent18fbff4f57e0c0b25ecaa3e1064c71b7a09f320b (diff)
Add hostname validation
Diffstat (limited to 'shared-bindings')
-rw-r--r--shared-bindings/wifi/Radio.c17
1 files changed, 13 insertions, 4 deletions
diff --git a/shared-bindings/wifi/Radio.c b/shared-bindings/wifi/Radio.c
index 481294463..b548d66f6 100644
--- a/shared-bindings/wifi/Radio.c
+++ b/shared-bindings/wifi/Radio.c
@@ -24,11 +24,13 @@
* THE SOFTWARE.
*/
+#include "shared-bindings/wifi/__init__.h"
+
+#include <regex.h>
#include <string.h>
-#include "py/objproperty.h"
#include "py/runtime.h"
-#include "shared-bindings/wifi/__init__.h"
+#include "py/objproperty.h"
//| class Radio:
//| """Native wifi radio.
@@ -115,9 +117,16 @@ STATIC mp_obj_t wifi_radio_set_hostname(mp_obj_t self_in, mp_obj_t hostname_in)
mp_buffer_info_t hostname;
mp_get_buffer_raise(hostname_in, &hostname, MP_BUFFER_READ);
- if (hostname.len < 1 || hostname.len > 63) {
- mp_raise_ValueError(translate("Hostname must be between 1 and 63 characters"));
+ if (hostname.len < 1 || hostname.len > 253) {
+ mp_raise_ValueError(translate("Hostname must be between 1 and 253 characters"));
+ }
+
+ regex_t regex; //validate hostname according to RFC 1123
+ regcomp(&regex,"^(([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(&regex, hostname.buf, 0, NULL, 0)) {
+ mp_raise_ValueError(translate("invalid hostname"));
}
+ regfree(&regex);
wifi_radio_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_wifi_radio_set_hostname(self, hostname.buf);