From 97733cbf43c3a6d722f3850f645457ffed694159 Mon Sep 17 00:00:00 2001
From: Simone Margaritelli <evilsocket@gmail.com>
Date: Thu, 24 Oct 2019 18:12:10 +0200
Subject: [PATCH] fix: hostname validation when provided by config

---
 pwnagotchi/__init__.py  | 5 +++++
 pwnagotchi/defaults.yml | 2 +-
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/pwnagotchi/__init__.py b/pwnagotchi/__init__.py
index f79ecee..c4dea27 100644
--- a/pwnagotchi/__init__.py
+++ b/pwnagotchi/__init__.py
@@ -2,6 +2,7 @@ import subprocess
 import os
 import logging
 import time
+import re
 import pwnagotchi.ui.view as view
 
 version = '1.1.0b'
@@ -17,6 +18,10 @@ def set_name(new_name):
     if new_name == '':
         return
 
+    if not re.match(r'^[a-zA-Z0-9\-]{2,25}$', new_name):
+        logging.warning("name '%s' is invalid: min length is 2, max length 25, only a-zA-Z0-9- allowed", new_name)
+        return
+
     current = name()
     if new_name != current:
         global _name
diff --git a/pwnagotchi/defaults.yml b/pwnagotchi/defaults.yml
index 33b1267..f7634f1 100644
--- a/pwnagotchi/defaults.yml
+++ b/pwnagotchi/defaults.yml
@@ -6,7 +6,7 @@
 #
 # main algorithm configuration
 main:
-    # if set this will set the hostname of the unit
+    # if set this will set the hostname of the unit. min length is 2, max length 25, only a-zA-Z0-9- allowed
     name: ''
     # currently implemented: en (default), de, el, fr, it, mk, nl, ru, se, pt-BR, es, pt
     lang: en