Do not consider IP strings valid DNS names. Fixes #25055

This commit is contained in:
rl1987 2018-02-11 16:39:23 +01:00 committed by Nick Mathewson
parent 0e453929d2
commit 1af016e96e
2 changed files with 20 additions and 8 deletions

View File

@ -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;
}
if (c_sl_idx == c_sl_len - 1) {
do {
if ((*c >= 'a' && *c <= 'z') ||
(*c >= 'A' && *c <= 'Z') ||
(*c >= '0' && *c <= '9') ||
(*c == '-') || (*c == '_'))
result = isalpha(*c);
c++;
else
result = 0;
} 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) {

View File

@ -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;
}