Merge "More authtypes validation checks"
This commit is contained in:
commit
a5eef7d4cb
@ -31,6 +31,7 @@ conditions:
|
||||
|
||||
import hashlib
|
||||
import os
|
||||
import string
|
||||
import sys
|
||||
|
||||
|
||||
@ -158,6 +159,13 @@ class Sha1(object):
|
||||
except ValueError:
|
||||
raise ValueError("Missing '$' in %s" % auth_rest)
|
||||
|
||||
if len(auth_salt) == 0:
|
||||
raise ValueError("Salt must have non-zero length!")
|
||||
if len(auth_hash) != 40:
|
||||
raise ValueError("Hash must have 40 chars!")
|
||||
if not all(c in string.hexdigits for c in auth_hash):
|
||||
raise ValueError("Hash must be hexadecimal!")
|
||||
|
||||
return dict(salt=auth_salt, hash=auth_hash)
|
||||
|
||||
|
||||
@ -219,4 +227,12 @@ class Sha512(object):
|
||||
auth_salt, auth_hash = auth_rest.split('$')
|
||||
except ValueError:
|
||||
raise ValueError("Missing '$' in %s" % auth_rest)
|
||||
|
||||
if len(auth_salt) == 0:
|
||||
raise ValueError("Salt must have non-zero length!")
|
||||
if len(auth_hash) != 128:
|
||||
raise ValueError("Hash must have 128 chars!")
|
||||
if not all(c in string.hexdigits for c in auth_hash):
|
||||
raise ValueError("Hash must be hexadecimal!")
|
||||
|
||||
return dict(salt=auth_salt, hash=auth_hash)
|
||||
|
@ -62,10 +62,42 @@ class TestValidation(unittest.TestCase):
|
||||
creds = 'sha1:saltkeystring'
|
||||
self.assertRaisesRegexp(ValueError, "Missing '\$' in .*",
|
||||
authtypes.validate_creds, creds)
|
||||
# wrong sha1 format, missing salt
|
||||
creds = 'sha1:$hash'
|
||||
self.assertRaisesRegexp(ValueError, "Salt must have non-zero length!",
|
||||
authtypes.validate_creds, creds)
|
||||
# wrong sha1 format, missing hash
|
||||
creds = 'sha1:salt$'
|
||||
self.assertRaisesRegexp(ValueError, "Hash must have 40 chars!",
|
||||
authtypes.validate_creds, creds)
|
||||
# wrong sha1 format, short hash
|
||||
creds = 'sha1:salt$short_hash'
|
||||
self.assertRaisesRegexp(ValueError, "Hash must have 40 chars!",
|
||||
authtypes.validate_creds, creds)
|
||||
# wrong sha1 format, wrong format
|
||||
creds = 'sha1:salt$' + "z" * 40
|
||||
self.assertRaisesRegexp(ValueError, "Hash must be hexadecimal!",
|
||||
authtypes.validate_creds, creds)
|
||||
# wrong sha512 format, missing `$`
|
||||
creds = 'sha512:saltkeystring'
|
||||
self.assertRaisesRegexp(ValueError, "Missing '\$' in .*",
|
||||
authtypes.validate_creds, creds)
|
||||
# wrong sha512 format, missing salt
|
||||
creds = 'sha512:$hash'
|
||||
self.assertRaisesRegexp(ValueError, "Salt must have non-zero length!",
|
||||
authtypes.validate_creds, creds)
|
||||
# wrong sha512 format, missing hash
|
||||
creds = 'sha512:salt$'
|
||||
self.assertRaisesRegexp(ValueError, "Hash must have 128 chars!",
|
||||
authtypes.validate_creds, creds)
|
||||
# wrong sha512 format, short hash
|
||||
creds = 'sha512:salt$short_hash'
|
||||
self.assertRaisesRegexp(ValueError, "Hash must have 128 chars!",
|
||||
authtypes.validate_creds, creds)
|
||||
# wrong sha1 format, wrong format
|
||||
creds = 'sha512:salt$' + "z" * 128
|
||||
self.assertRaisesRegexp(ValueError, "Hash must be hexadecimal!",
|
||||
authtypes.validate_creds, creds)
|
||||
|
||||
|
||||
class TestPlaintext(unittest.TestCase):
|
||||
|
Loading…
x
Reference in New Issue
Block a user