]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Introducing helper function to validate DNS name strings.
authorrl1987 <rl1987@sdf.lonestar.org>
Sun, 12 Oct 2014 16:33:08 +0000 (19:33 +0300)
committerNick Mathewson <nickm@torproject.org>
Tue, 4 Nov 2014 05:35:43 +0000 (00:35 -0500)
src/common/util.c
src/common/util.h
src/test/test_util.c

index f4d293c8383b0955bd5c1cc594efcb779954a1f6..606b9e1ea0161087e7250f5967de71a2905b6105 100644 (file)
@@ -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)
index 61bb05f01610d63c50cb5c8d8c7f365337b3fc26..2634adc62bc28cfe705b033f30da680c6e84e8fd 100644 (file)
@@ -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);
index c67aa71b029c3f6511f2843dc915b9109350a19e..fb3ce7d941730155a0ac29b5c663fe83e4ccc34f 100644 (file)
@@ -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
 };