1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Fixes #2473: Fix encoding of long (>127 character) secrets

This commit is contained in:
Jeremy Stretch
2018-11-07 13:44:16 -05:00
parent c1838104ae
commit 69d829ce8d
3 changed files with 20 additions and 7 deletions

View File

@@ -1,6 +1,7 @@
from __future__ import unicode_literals
import os
import sys
from Crypto.Cipher import AES, PKCS1_OAEP
from Crypto.PublicKey import RSA
@@ -392,6 +393,7 @@ class Secret(ChangeLoggedModel, CustomFieldModel):
s = s.encode('utf8')
if len(s) > 65535:
raise ValueError("Maximum plaintext size is 65535 bytes.")
# Minimum ciphertext size is 64 bytes to conceal the length of short secrets.
if len(s) <= 62:
pad_length = 62 - len(s)
@@ -399,12 +401,14 @@ class Secret(ChangeLoggedModel, CustomFieldModel):
pad_length = 16 - ((len(s) + 2) % 16)
else:
pad_length = 0
return (
chr(len(s) >> 8).encode() +
chr(len(s) % 256).encode() +
s +
os.urandom(pad_length)
)
# Python 2 compatibility
if sys.version_info[0] < 3:
header = chr(len(s) >> 8) + chr(len(s) % 256)
else:
header = bytes([len(s) >> 8]) + bytes([len(s) % 256])
return header + s + os.urandom(pad_length)
def _unpad(self, s):
"""