[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [tor/master] Introducing helper function to validate DNS name strings.



commit 1ea9a6fd72b66ec634446cbd2119641a5ed1e703
Author: rl1987 <rl1987@xxxxxxxxxxxxxxxx>
Date:   Sun Oct 12 19:33:08 2014 +0300

    Introducing helper function to validate DNS name strings.
---
 src/common/util.c    |   36 ++++++++++++++++++++++++++++++++++++
 src/common/util.h    |    1 +
 src/test/test_util.c |   31 +++++++++++++++++++++++++++++++
 3 files changed, 68 insertions(+)

diff --git a/src/common/util.c b/src/common/util.c
index f4d293c..606b9e1 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -957,6 +957,42 @@ string_is_key_value(int severity, const char *string)
   return 1;
 }
 
+/** Return true iff <b>string</b> is valid DNS name, as defined in
+ * RFC 1035 Section 2.3.1.
+ */
+int
+string_is_valid_hostname(const char *string)
+{
+   int result = 1;
+   smartlist_t *components;
+
+   components = smartlist_new();
+
+   smartlist_split_string(components,string,".",0,0);
+
+   SMARTLIST_FOREACH_BEGIN(components, char *, c) {
+     if (c[0] == '-') {
+       result = 0;
+       break;
+     }
+
+     do {
+       if ((*c >= 'a' && *c <= 'z') ||
+           (*c >= 'A' && *c <= 'Z') ||
+           (*c >= '0' && *c <= '9') ||
+           (*c == '-'))
+         c++;
+       else
+         result = 0;
+     } while (result && *c);
+
+   } SMARTLIST_FOREACH_END(c);
+
+   smartlist_free(components);
+
+   return result;
+}
+
 /** Return true iff the DIGEST256_LEN bytes in digest are all zero. */
 int
 tor_digest256_is_zero(const char *digest)
diff --git a/src/common/util.h b/src/common/util.h
index 61bb05f..2634adc 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -227,6 +227,7 @@ const char *find_str_at_start_of_line(const char *haystack,
                                       const char *needle);
 int string_is_C_identifier(const char *string);
 int string_is_key_value(int severity, const char *string);
+int string_is_valid_hostname(const char *string);
 
 int tor_mem_is_zero(const char *mem, size_t len);
 int tor_digest_is_zero(const char *digest);
diff --git a/src/test/test_util.c b/src/test/test_util.c
index c67aa71..fb3ce7d 100644
--- a/src/test/test_util.c
+++ b/src/test/test_util.c
@@ -4122,6 +4122,36 @@ test_util_max_mem(void *arg)
   ;
 }
 
+static void
+test_util_hostname_validation(void *arg)
+{
+  (void)arg;
+
+  // Lets try valid hostnames first.
+  tt_assert(string_is_valid_hostname("torproject.org"));
+  tt_assert(string_is_valid_hostname("ocw.mit.edu"));
+  tt_assert(string_is_valid_hostname("i.4cdn.org"));
+  tt_assert(string_is_valid_hostname("stanford.edu"));
+  tt_assert(string_is_valid_hostname("multiple-words-with-hypens.jp"));
+
+  // Subdomain name cannot start with '-'.
+  tt_assert(!string_is_valid_hostname("-torproject.org"));
+  tt_assert(!string_is_valid_hostname("subdomain.-domain.org"));
+  tt_assert(!string_is_valid_hostname("-subdomain.domain.org"));
+  
+  // Hostnames cannot contain non-alphanumeric characters.
+  tt_assert(!string_is_valid_hostname("%%domain.\\org."));  
+  tt_assert(!string_is_valid_hostname("***x.net"));
+  tt_assert(!string_is_valid_hostname("___abc.org"));
+  tt_assert(!string_is_valid_hostname("\xff\xffxyz.org"));
+  tt_assert(!string_is_valid_hostname("word1 word2.net"));
+
+  // XXX: do we allow single-label DNS names?
+
+  done:
+  return;
+}
+
 struct testcase_t util_tests[] = {
   UTIL_LEGACY(time),
   UTIL_TEST(parse_http_time, 0),
@@ -4194,6 +4224,7 @@ struct testcase_t util_tests[] = {
   { "socketpair_ersatz", test_util_socketpair, TT_FORK,
     &socketpair_setup, (void*)"1" },
   UTIL_TEST(max_mem, 0),
+  UTIL_TEST(hostname_validation, 0),
   END_OF_TESTCASES
 };
 



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits