Skip to content

Commit 26fd2c6

Browse files
committed
Add hostname validation
1 parent 18fbff4 commit 26fd2c6

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

locale/circuitpython.pot

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: PACKAGE VERSION\n"
1010
"Report-Msgid-Bugs-To: \n"
11-
"POT-Creation-Date: 2020-10-12 14:16+0530\n"
11+
"POT-Creation-Date: 2020-10-15 16:06+0530\n"
1212
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1313
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1414
"Language-Team: LANGUAGE <[email protected]>\n"
@@ -948,7 +948,7 @@ msgid "Hardware in use, try alternative pins"
948948
msgstr ""
949949

950950
#: shared-bindings/wifi/Radio.c
951-
msgid "Hostname must be between 1 and 63 characters"
951+
msgid "Hostname must be between 1 and 253 characters"
952952
msgstr ""
953953

954954
#: extmod/vfs_posix_file.c py/objstringio.c
@@ -2732,6 +2732,10 @@ msgstr ""
27322732
msgid "invalid format specifier"
27332733
msgstr ""
27342734

2735+
#: shared-bindings/wifi/Radio.c
2736+
msgid "invalid hostname"
2737+
msgstr ""
2738+
27352739
#: extmod/modussl_axtls.c
27362740
msgid "invalid key"
27372741
msgstr ""

shared-bindings/wifi/Radio.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#include "shared-bindings/wifi/__init__.h"
28+
29+
#include <regex.h>
2730
#include <string.h>
2831

29-
#include "py/objproperty.h"
3032
#include "py/runtime.h"
31-
#include "shared-bindings/wifi/__init__.h"
33+
#include "py/objproperty.h"
3234

3335
//| class Radio:
3436
//| """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)
115117
mp_buffer_info_t hostname;
116118
mp_get_buffer_raise(hostname_in, &hostname, MP_BUFFER_READ);
117119

118-
if (hostname.len < 1 || hostname.len > 63) {
119-
mp_raise_ValueError(translate("Hostname must be between 1 and 63 characters"));
120+
if (hostname.len < 1 || hostname.len > 253) {
121+
mp_raise_ValueError(translate("Hostname must be between 1 and 253 characters"));
122+
}
123+
124+
regex_t regex; //validate hostname according to RFC 1123
125+
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);
126+
if (regexec(&regex, hostname.buf, 0, NULL, 0)) {
127+
mp_raise_ValueError(translate("invalid hostname"));
120128
}
129+
regfree(&regex);
121130

122131
wifi_radio_obj_t *self = MP_OBJ_TO_PTR(self_in);
123132
common_hal_wifi_radio_set_hostname(self, hostname.buf);

0 commit comments

Comments
 (0)