hostname-util: don't allow machine tags to begin/end with '-' or '.'

This commit is contained in:
Lennart Poettering
2026-06-01 12:16:28 +01:00
committed by Luca Boccassi
parent 7602d82407
commit 95c7dc3308
2 changed files with 10 additions and 0 deletions
+6
View File
@@ -259,6 +259,12 @@ bool machine_tag_is_valid(const char *s) {
if (n <= 0 || n >= 256)
return false;
/* Don't allow "-" and "." as first or last char. (This is load-bearing, we want that "+"/"-" can be
* used as prefix for adding/removing tags from the list). */
if (strchr("-.", s[0]) ||
strchr("-.", s[n-1]))
return false;
return in_charset(s, ALPHANUMERICAL "-.");
}
+4
View File
@@ -132,6 +132,10 @@ TEST(machine_tag_is_valid) {
assert_se(!machine_tag_is_valid("fööbar")); /* non-ASCII */
assert_se(!machine_tag_is_valid("foo/bar"));
assert_se(!machine_tag_is_valid("foo_bar"));
assert_se(!machine_tag_is_valid("-foo"));
assert_se(!machine_tag_is_valid("foo-"));
assert_se(!machine_tag_is_valid(".foo"));
assert_se(!machine_tag_is_valid("foo."));
/* Length boundary: 255 characters is fine, 256 is too long */
_cleanup_free_ char *max = strrep("a", 255), *over = strrep("a", 256);