[PATCH] Forbid "root" as a user or group name. (#968451)
David Shea
dshea at redhat.com
Wed Sep 18 20:15:41 UTC 2013
This also fixes the handling of single-character names, which were
mistakenly forbidden before.
---
pyanaconda/regexes.py | 7 +++++--
tests/regex_tests/username_test.py | 16 ++++++++++++----
2 files changed, 17 insertions(+), 6 deletions(-)
diff --git a/pyanaconda/regexes.py b/pyanaconda/regexes.py
index 44acc0b..5859e59 100644
--- a/pyanaconda/regexes.py
+++ b/pyanaconda/regexes.py
@@ -38,10 +38,13 @@ GECOS_VALID = re.compile(r'^[^:]*$')
# and UT_NAMESIZE for user names (which is defined as 32 bits/utmp.h). This
# expression captures all of that: the initial character, followed by either
# up to 30 portable characters and a dollar sign or up to 31 portable characters,
-# both for a maximum total of 32. The empty string is not allowed.
+# both for a maximum total of 32. The empty string is not allowed. "root" is not
+# allowed.
# a base expression without anchors, helpful for building other expressions
-_USERNAME_BASE = r'[a-zA-Z0-9._](([a-zA-Z0-9._-]{,30}\$)|([a-zA-Z0-9._-]{,31}))'
+# If the string is the right length to match "root", use a lookback expression
+# to make sure it isn't.
+_USERNAME_BASE = r'[a-zA-Z0-9._](([a-zA-Z0-9._-]{1,2})|([a-zA-Z0-9._-]{3}(?<!root))|([a-zA-Z0-9._-]{,30}\$)|([a-zA-Z0-9._-]{4,31}))?'
USERNAME_VALID = re.compile(r'^' + _USERNAME_BASE + '$')
GROUPNAME_VALID = USERNAME_VALID
diff --git a/tests/regex_tests/username_test.py b/tests/regex_tests/username_test.py
index 9877410..0c0d5d2 100644
--- a/tests/regex_tests/username_test.py
+++ b/tests/regex_tests/username_test.py
@@ -29,14 +29,14 @@ class UsernameRegexTestCase(unittest.TestCase):
for good in goodlist:
try:
self.assertIsNotNone(expression.match(good))
- except AssertionError as error:
+ except AssertionError:
got_error = True
print("Good string %s did not match expression" % good)
for bad in badlist:
try:
self.assertIsNone(expression.match(bad))
- except AssertionError as error:
+ except AssertionError:
got_error = True
print("Bad string %s matched expression" % bad)
@@ -70,7 +70,12 @@ class UsernameRegexTestCase(unittest.TestCase):
'g_burdell',
'_burdell',
'gggggggggggggggggggggggggburdell', # 32 characters
- 'ggggggggggggggggggggggggburdell$'
+ 'ggggggggggggggggggggggggburdell$',
+ '_',
+ 'r',
+ 'ro',
+ 'roo',
+ 'roota',
]
bad_tests = [
@@ -83,7 +88,10 @@ class UsernameRegexTestCase(unittest.TestCase):
'ggggggggggggggggggggggggggburdell', # 33 characters
'gggggggggggggggggggggggggburdell$',
' gburdell',
- ':gburdell'
+ ':gburdell',
+ 'root',
+ '$',
+ '-'
]
self._run_tests(USERNAME_VALID, good_tests, bad_tests)
--
1.8.3.1
More information about the anaconda-patches
mailing list