Skip to content

Commit 5b398f7

Browse files
author
liuhuiqi.7
committed
feat(e2ee): fix b64 check
1 parent ee41d32 commit 5b398f7

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

volcenginesdkarkruntime/_utils/_key_agreement.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,28 @@ def aes_gcm_decrypt_base64_list(key: bytes, nonce: bytes, ciphertext: str) -> st
100100
try:
101101
result.append(aes_gcm_decrypt_base64_string(key, nonce, b64))
102102
except Exception:
103+
print("decrypt base64 string failed", b64)
103104
for i in range(20, len(b64), 4):
104105
try:
105-
decrypted = aes_gcm_decrypt_base64_string(key, nonce, b64[:i+4])
106+
decrypted = aes_gcm_decrypt_base64_string(
107+
key, nonce, b64[:i+4])
106108
result.append(decrypted)
107-
decrypted = aes_gcm_decrypt_base64_string(key, nonce, b64[i+4:])
109+
decrypted = aes_gcm_decrypt_base64_string(
110+
key, nonce, b64[i+4:])
108111
result.append(decrypted)
109112
except Exception:
110-
result.append('')
113+
print("decrypt base64 string failed",
114+
b64, b64[:i+4], b64[i+4:])
115+
print('decrypt base64 list result', result)
111116
return ''.join(result)
112117

113118

119+
def decrypt_validate(ciphertext: str) -> bool:
120+
cipher_bytes = ciphertext.encode()
121+
cipher_b64_bytes = base64.decodebytes(cipher_bytes)
122+
return len(cipher_bytes)/4 >= len(cipher_b64_bytes)/3 >= len(cipher_bytes)/4 - 1
123+
124+
114125
def marshal_cryptography_pub_key(key) -> bytes:
115126
# python version of crypto/elliptic/elliptic.go Marshal
116127
# without point on curve check

volcenginesdkarkruntime/resources/chat/completions.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
from ..._types import Body, Query, Headers
3333
from ..._utils._utils import deepcopy_minimal, with_sts_token, async_with_sts_token
34-
from ..._utils._key_agreement import aes_gcm_decrypt_base64_string, aes_gcm_decrypt_base64_list
34+
from ..._utils._key_agreement import aes_gcm_decrypt_base64_string, aes_gcm_decrypt_base64_list, decrypt_validate
3535
from ..._base_client import make_request_options
3636
from ..._resource import SyncAPIResource, AsyncAPIResource
3737
from ..._compat import cached_property
@@ -149,7 +149,7 @@ def _decrypt(
149149
content = aes_gcm_decrypt_base64_string(
150150
key, nonce, choice.message.content
151151
)
152-
if content == '':
152+
if not decrypt_validate(choice.message.content):
153153
content = aes_gcm_decrypt_base64_list(
154154
key, nonce, choice.message.content
155155
)
@@ -312,7 +312,7 @@ async def _decrypt(
312312
content = aes_gcm_decrypt_base64_string(
313313
key, nonce, choice.message.content
314314
)
315-
if content == '':
315+
if not decrypt_validate(choice.message.content):
316316
content = aes_gcm_decrypt_base64_list(
317317
key, nonce, choice.message.content
318318
)

0 commit comments

Comments
 (0)