Skip to content
This repository was archived by the owner on Jun 18, 2024. It is now read-only.

Commit fad46e8

Browse files
committed
crypto: removed check on ciphertext length to allow decryption of empty secrets
1 parent 0f8a2c1 commit fad46e8

5 files changed

Lines changed: 51 additions & 29 deletions

File tree

infisical/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "1.0.0"
1+
__version__ = "1.0.1"

infisical/utils/crypto.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
def encrypt_asymmetric(
1313
plaintext: Union[Buffer, str],
14-
public_key: Union[Buffer, Base64String],
15-
private_key: Union[Buffer, Base64String],
14+
public_key: Union[Buffer, Base64String, public.PublicKey],
15+
private_key: Union[Buffer, Base64String, public.PrivateKey],
1616
) -> Tuple[Base64String, Base64String]:
1717
"""Performs asymmetric encryption of the ``plaintext`` with x25519-xsalsa20-poly1305
1818
algorithm with the given parameters. Each of those params should be either the raw value in bytes
@@ -24,21 +24,33 @@ def encrypt_asymmetric(
2424
:raises ValueError: If ``plaintext``, ``public_key`` or ``private_key`` are empty
2525
:return: A tuple containing the ciphered text and the random nonce used for encryption
2626
"""
27-
if len(plaintext) == 0 or len(public_key) == 0 or len(private_key) == 0:
28-
raise ValueError()
27+
if (not isinstance(public_key, public.PublicKey) and len(public_key) == 0) or (
28+
not isinstance(private_key, public.PrivateKey) and len(private_key) == 0
29+
):
30+
raise ValueError("Public key and private key cannot be empty!")
2931

3032
m_plaintext = (
3133
str.encode(plaintext, "utf-8") if isinstance(plaintext, str) else plaintext
3234
)
3335
m_public_key = (
3436
b64decode(public_key) if isinstance(public_key, Base64String) else public_key
3537
)
38+
m_public_key = (
39+
public.PublicKey(m_public_key)
40+
if isinstance(m_public_key, (bytes, bytearray, memoryview))
41+
else m_public_key
42+
)
3643
m_private_key = (
3744
b64decode(private_key) if isinstance(private_key, Base64String) else private_key
3845
)
46+
m_private_key = (
47+
public.PrivateKey(m_private_key)
48+
if isinstance(m_private_key, (bytes, bytearray, memoryview))
49+
else m_private_key
50+
)
3951

4052
nonce = utils.random(24)
41-
box = public.Box(public.PrivateKey(m_private_key), public.PublicKey(m_public_key))
53+
box = public.Box(m_private_key, m_public_key)
4254
ciphertext = box.encrypt(m_plaintext, nonce).ciphertext
4355

4456
return (b64encode(ciphertext).decode("utf-8"), b64encode(nonce).decode("utf-8"))
@@ -67,7 +79,9 @@ def decrypt_asymmetric(
6779
or (not isinstance(public_key, public.PublicKey) and len(public_key) == 0)
6880
or (not isinstance(private_key, public.PrivateKey) and len(private_key) == 0)
6981
):
70-
raise ValueError()
82+
raise ValueError(
83+
"Public key, private key, ciphertext and nonce cannot be empty!"
84+
)
7185

7286
m_ciphertext = (
7387
b64decode(ciphertext) if isinstance(ciphertext, Base64String) else ciphertext
@@ -107,8 +121,8 @@ def encrypt_symmetric(
107121
:raises ValueError: If either ``plaintext`` or ``key`` is empty
108122
:return: Ciphered text
109123
"""
110-
if len(plaintext) == 0 or len(key) == 0:
111-
raise ValueError()
124+
if len(key) == 0:
125+
raise ValueError("The given key is empty!")
112126

113127
BLOCK_SIZE_BYTES = 16
114128

@@ -146,7 +160,7 @@ def decrypt_symmetric(
146160
:raises ValueError: If ``ciphertext``, ``iv``, ``tag`` or ``key`` are empty or tag/mac does not match
147161
:return: Deciphered text
148162
"""
149-
if len(ciphertext) == 0 or len(tag) == 0 or len(iv) == 0 or len(key) == 0:
163+
if len(tag) == 0 or len(iv) == 0 or len(key) == 0:
150164
raise ValueError("One of the given parameter is empty!")
151165

152166
m_key = b64decode(key) if isinstance(key, Base64String) else key

tests/test_utils/test_crypto_decrypt_symmetric.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,6 @@ def test_decrypt_symmetric_empty_param() -> None:
3333
iv="H/cRvADtxDa4XWzM2p1j0w==",
3434
)
3535

36-
with pytest.raises(ValueError):
37-
decrypt_symmetric(
38-
key="NDQxYThhNGFlOTdlMDQyNzBmOWI0MDkyZDgzYThmMGQ=",
39-
ciphertext="",
40-
tag="DlHIpSGeE7FIJQ3bxyqB7Q==",
41-
iv="H/cRvADtxDa4XWzM2p1j0w==",
42-
)
43-
4436
with pytest.raises(ValueError):
4537
decrypt_symmetric(
4638
key="NDQxYThhNGFlOTdlMDQyNzBmOWI0MDkyZDgzYThmMGQ=",
@@ -56,3 +48,10 @@ def test_decrypt_symmetric_empty_param() -> None:
5648
tag="DlHIpSGeE7FIJQ3bxyqB7Q==",
5749
iv="",
5850
)
51+
52+
decrypt_symmetric(
53+
key="C4AmL9liaUXm5tNVoHBTJw==",
54+
ciphertext="",
55+
tag="w+3JZYTW+YiKagCseraf4Q==",
56+
iv="zw8vhOL67bEhvRijTCA+vA==",
57+
)

tests/test_utils/test_crypto_encrypt_asymmetric.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,12 @@ def test_encrypt_asymmetric_bytes() -> None:
5151

5252

5353
def test_encrypt_asymmetric_empty_param() -> None:
54-
with pytest.raises(ValueError):
55-
encrypt_asymmetric(
56-
plaintext="", private_key=ALICE_PRIVATE_KEY, public_key=BOB_PUBLIC_KEY
57-
)
54+
cipher, nonce = encrypt_asymmetric(
55+
plaintext="", private_key=ALICE_PRIVATE_KEY, public_key=BOB_PUBLIC_KEY
56+
)
57+
58+
assert len(cipher) > 0
59+
assert len(nonce) > 0
5860

5961
with pytest.raises(ValueError):
6062
encrypt_asymmetric(

tests/test_utils/test_crypto_encrypt_symmetric.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
from Cryptodome.Random import get_random_bytes
23
from infisical.utils.crypto import decrypt_symmetric, encrypt_symmetric
34

45

@@ -34,15 +35,21 @@ def test_encrypt_symmetric_bytes() -> None:
3435
assert plaintext == "9c07298c06c6aaa762fcee342cf6bc34"
3536

3637

37-
def test_encrypt_symmetric_empty_param() -> None:
38-
with pytest.raises(ValueError):
39-
encrypt_symmetric(
40-
plaintext="",
41-
key="NDQxYThhNGFlOTdlMDQyNzBmOWI0MDkyZDgzYThmMGQ=",
42-
)
43-
38+
def test_encrypt_symmetric_empty_key() -> None:
4439
with pytest.raises(ValueError):
4540
encrypt_symmetric(
4641
plaintext="9c07298c06c6aaa762fcee342cf6bc34",
4742
key="",
4843
)
44+
45+
46+
def test_encrypt_symmetric_empty_plaintext() -> None:
47+
key = get_random_bytes(16)
48+
49+
cipher, iv, tag = encrypt_symmetric(
50+
plaintext="",
51+
key=key,
52+
)
53+
54+
assert len(cipher) == 0
55+
assert len(iv) > 0 and len(tag) > 0

0 commit comments

Comments
 (0)