From 7479ab47e07efaded3d0aeb3bd169cbf27dd8f7c Mon Sep 17 00:00:00 2001 From: Abhijit Menon-Sen Date: Thu, 3 Sep 2015 16:32:06 +0530 Subject: [PATCH] Be stricter about parsing hostname labels MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Labels must start with an alphanumeric character, may contain alphanumeric characters or hyphens, but must not end with a hyphen. We enforce those rules, but allow underscores wherever hyphens are accepted, and allow alphanumeric ranges anywhere. We relax the definition of "alphanumeric" to include Unicode characters even though such inventory hostnames cannot be used in practice unless an ansible_ssh_host is set for each of them. We still don't enforce length restrictions—the fact that we have to accept ranges makes it more complex, and it doesn't seem especially worthwhile. --- lib/ansible/parsing/utils/addresses.py | 22 +++++++++++++++------- test/units/parsing/test_addresses.py | 13 +++++++++++++ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/lib/ansible/parsing/utils/addresses.py b/lib/ansible/parsing/utils/addresses.py index 3a128a3524..71675769d2 100644 --- a/lib/ansible/parsing/utils/addresses.py +++ b/lib/ansible/parsing/utils/addresses.py @@ -61,6 +61,17 @@ ipv4_component = r''' ) '''.format(range=numeric_range) +# A hostname label, e.g. 'foo' in 'foo.example.com'. Consists of alphanumeric +# characters plus dashes (and underscores) or valid ranges. The label may not +# start or end with a hyphen or an underscore. This is interpolated into the +# hostname pattern below. We don't try to enforce the 63-char length limit. + +label = r''' + (?:[\w]|{range}) # Starts with an alphanumeric or a range + (?:[\w_-]|{range})* # Then zero or more of the same or [_-] + (?