mirror of
https://github.com/r4sas/py-i2phosts
synced 2025-01-29 16:04:15 +00:00
web/lib/validation.py: add base64-validity check
Base 64 hash shouldn't require more than 2 pad chars to be divided to 4 without leftover.
This commit is contained in:
parent
2b535dbf2b
commit
bb2582c62c
@ -53,6 +53,20 @@ def validate_b64hash(data, check_uniq=True):
|
||||
# keys with cert may ends with anything, so check is relaxed
|
||||
if length > 516 and re.match(r'[a-zA-Z0-9\-~]+$', data) == None:
|
||||
raise forms.ValidationError('Invalid characters in base64 hash')
|
||||
# base64-validity test
|
||||
if length > 516:
|
||||
# we need temporary variable here to avoid modifying main "data"
|
||||
test_data = data
|
||||
# add pad-characters needed for proper decoding cos i2p does not
|
||||
for i in range(4):
|
||||
quanta, leftover = divmod(len(test_data), 4)
|
||||
if leftover:
|
||||
test_data += '='
|
||||
else:
|
||||
break
|
||||
# if more than 2 pad chars were added, raise an error
|
||||
if i > 2:
|
||||
raise forms.ValidationError('Corrupted base64 hash')
|
||||
# base64-i2p
|
||||
if length == 516 and re.match(r'[a-zA-Z0-9\-~]+AA$', data) == None:
|
||||
raise forms.ValidationError('Invalid base64 hash')
|
||||
|
Loading…
x
Reference in New Issue
Block a user