Add hostname validation

This commit is contained in:
microDev 2020-10-15 16:08:01 +05:30
parent 18fbff4f57
commit 26fd2c6223
No known key found for this signature in database
GPG Key ID: 2C0867BE60967730
2 changed files with 19 additions and 6 deletions

View File

@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2020-10-12 14:16+0530\n"
"POT-Creation-Date: 2020-10-15 16:06+0530\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
@ -948,7 +948,7 @@ msgid "Hardware in use, try alternative pins"
msgstr ""
#: shared-bindings/wifi/Radio.c
msgid "Hostname must be between 1 and 63 characters"
msgid "Hostname must be between 1 and 253 characters"
msgstr ""
#: extmod/vfs_posix_file.c py/objstringio.c
@ -2732,6 +2732,10 @@ msgstr ""
msgid "invalid format specifier"
msgstr ""
#: shared-bindings/wifi/Radio.c
msgid "invalid hostname"
msgstr ""
#: extmod/modussl_axtls.c
msgid "invalid key"
msgstr ""

View File

@ -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,10 +117,17 @@ 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);