Skip to content

Commit

Permalink
sprintfix: 修复非法 b64 导致脱敏模式串被误转为空串的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhuoZhuoCrayon committed Sep 5, 2023
1 parent 4f77fd0 commit f0c1e9d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
6 changes: 6 additions & 0 deletions gcloud/utils/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"""
import json
import typing
import base64

from bkcrypto import constants as crypto_constants
from bkcrypto.asymmetric.configs import KeyConfig as AsymmetricKeyConfig
Expand Down Expand Up @@ -60,6 +61,11 @@ def decrypt(ciphertext: str, using: typing.Optional[str] = None) -> str:

# 2. 尝试对明文二次 RSA 解密,用于兼容原逻辑
try:
# 该校验用于避免非 b64 串 decode 返回空的场景
# 尝试 base64 解密,如果解密结果是空串,说明 plaintext 为空或者字符非法,说明非密文,直接返回;
# 如果抛出异常,在外层被 catch 后返回
if not base64.b64decode(plaintext.encode(encoding="utf-8")):
return plaintext
plaintext = asymmetric_cipher_manager.cipher(using=using, cipher_type=AsymmetricCipherType.RSA.value).decrypt(
plaintext
)
Expand Down
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ django-nose==1.4.5
coverage==4.5.1
python-magic==0.4.15
cryptography==3.3.2
bk-crypto-python-sdk==1.1.1
gitdb2==2.0.6
GitPython==2.1.11
PyJWT==1.7.1
Expand Down Expand Up @@ -62,7 +61,7 @@ importlib-metadata==3.7.3
protobuf==3.19.4

# blueapps
blueapps[opentelemetry, bkcrypto]==4.8.0
blueapps[opentelemetry,bkcrypto]==4.8.0
whitenoise==5.2.0
raven==6.5.0
python-json-logger==2.0.1
Expand Down

0 comments on commit f0c1e9d

Please sign in to comment.