[tor-commits] [tor/release-0.3.3] Do not consider IP strings valid DNS names. Fixes #25055

nickm at torproject.org nickm at torproject.org
Wed Mar 28 11:50:56 UTC 2018


commit 1af016e96e133718546b55c8b7fafd3345aaeeb8
Author: rl1987 <rl1987 at sdf.lonestar.org>
Date:   Sun Feb 11 16:39:23 2018 +0100

    Do not consider IP strings valid DNS names. Fixes #25055
---
 src/common/util.c    | 23 +++++++++++++++--------
 src/test/test_util.c |  5 +++++
 2 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/src/common/util.c b/src/common/util.c
index 1818b4f19..7c715fb3c 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -100,6 +100,8 @@
 #undef MALLOC_ZERO_WORKS
 #endif
 
+#include <ctype.h>
+
 /* =====
  * Memory management
  * ===== */
@@ -1110,16 +1112,21 @@ string_is_valid_hostname(const char *string)
       continue;
     }
 
-    do {
-      if ((*c >= 'a' && *c <= 'z') ||
-          (*c >= 'A' && *c <= 'Z') ||
-          (*c >= '0' && *c <= '9') ||
-          (*c == '-') || (*c == '_'))
+    if (c_sl_idx == c_sl_len - 1) {
+      do {
+        result = isalpha(*c);
         c++;
-      else
-        result = 0;
-    } while (result && *c);
+      } while (result && *c);
+    } else {
+      do {
+        result = (isalnum(*c) || (*c == '-') || (*c == '_'));
+        c++;
+      } while (result > 0 && *c);
+    }
 
+    if (result == 0) {
+      break;
+    }
   } SMARTLIST_FOREACH_END(c);
 
   SMARTLIST_FOREACH_BEGIN(components, char *, c) {
diff --git a/src/test/test_util.c b/src/test/test_util.c
index b67fad58e..2fa03e5bc 100644
--- a/src/test/test_util.c
+++ b/src/test/test_util.c
@@ -5584,6 +5584,11 @@ test_util_hostname_validation(void *arg)
   tt_assert(!string_is_valid_hostname("."));
   tt_assert(!string_is_valid_hostname(".."));
 
+  // IP address strings are not hostnames.
+  tt_assert(!string_is_valid_hostname("8.8.8.8"));
+  tt_assert(!string_is_valid_hostname("[2a00:1450:401b:800::200e]"));
+  tt_assert(!string_is_valid_hostname("2a00:1450:401b:800::200e"));
+
   done:
   return;
 }





More information about the tor-commits mailing list