util: be more picky when validating hostnames
No longer allow dots at the beginning or end of host names, Or double dots. https://bugs.launchpad.net/ubuntu/+source/systemd/+bug/1152187/comments/14
This commit is contained in:
+17
-3
@@ -3773,13 +3773,27 @@ static bool hostname_valid_char(char c) {
|
||||
|
||||
bool hostname_is_valid(const char *s) {
|
||||
const char *p;
|
||||
bool dot;
|
||||
|
||||
if (isempty(s))
|
||||
return false;
|
||||
|
||||
for (p = s; *p; p++)
|
||||
if (!hostname_valid_char(*p))
|
||||
return false;
|
||||
for (p = s, dot = true; *p; p++) {
|
||||
if (*p == '.') {
|
||||
if (dot)
|
||||
return false;
|
||||
|
||||
dot = true;
|
||||
} else {
|
||||
if (!hostname_valid_char(*p))
|
||||
return false;
|
||||
|
||||
dot = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (dot)
|
||||
return false;
|
||||
|
||||
if (p-s > HOST_NAME_MAX)
|
||||
return false;
|
||||
|
||||
@@ -325,6 +325,19 @@ static void test_bus_path_escape(void) {
|
||||
test_bus_path_escape_one(":1", "_3a1");
|
||||
}
|
||||
|
||||
static void test_hostname_is_valid(void) {
|
||||
assert(hostname_is_valid("foobar"));
|
||||
assert(hostname_is_valid("foobar.com"));
|
||||
assert(!hostname_is_valid("fööbar"));
|
||||
assert(!hostname_is_valid(""));
|
||||
assert(!hostname_is_valid("."));
|
||||
assert(!hostname_is_valid(".."));
|
||||
assert(!hostname_is_valid("foobar."));
|
||||
assert(!hostname_is_valid(".foobar"));
|
||||
assert(!hostname_is_valid("foo..bar"));
|
||||
assert(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"));
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
test_streq_ptr();
|
||||
test_first_word();
|
||||
@@ -349,6 +362,7 @@ int main(int argc, char *argv[]) {
|
||||
test_default_term_for_tty();
|
||||
test_memdup_multiply();
|
||||
test_bus_path_escape();
|
||||
test_hostname_is_valid();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user