Skip to content

Commit 8fccacf

Browse files
committed
Fix bandit
1 parent 277ceb1 commit 8fccacf

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

mcpgateway/alembic/versions/a706a3320c56_use_argon2id_for_encryption_key.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# -*- coding: utf-8 -*-
12
"""Use Argon2id for encryption key
23
34
Revision ID: a706a3320c56
@@ -19,8 +20,8 @@
1920
from cryptography.fernet import Fernet
2021
from cryptography.hazmat.primitives import hashes
2122
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
22-
from sqlalchemy import text
2323
import sqlalchemy as sa
24+
from sqlalchemy import text
2425

2526
# First-Party
2627
from mcpgateway.config import settings
@@ -263,7 +264,7 @@ def _upgrade_json_client_secret(conn, table):
263264
continue
264265

265266
old = cfg.get("client_secret")
266-
new = _upgrade_value(old) # your helper
267+
new = _upgrade_value(old) # your helper
267268
if not new:
268269
continue
269270

@@ -295,7 +296,7 @@ def _downgrade_json_client_secret(conn, table):
295296
continue
296297

297298
old = cfg.get("client_secret")
298-
new = _downgrade_value(old) # your helper
299+
new = _downgrade_value(old) # your helper
299300
if not new:
300301
continue
301302

mcpgateway/services/encryption_service.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ class EncryptionService:
4545
False
4646
"""
4747

48-
def __init__(self, encryption_secret: Union[SecretStr, str], time_cost: Optional[int] = None, memory_cost: Optional[int] = None, parallelism: Optional[int] = None, hash_len: int = 32, salt_len: int = 16):
48+
def __init__(
49+
self, encryption_secret: Union[SecretStr, str], time_cost: Optional[int] = None, memory_cost: Optional[int] = None, parallelism: Optional[int] = None, hash_len: int = 32, salt_len: int = 16
50+
):
4951
"""Initialize the encryption handler.
5052
5153
Args:
@@ -151,9 +153,27 @@ def is_encrypted(self, text: str) -> bool:
151153
152154
Returns:
153155
True if the string appears to be encrypted
156+
157+
Note:
158+
Supports both legacy PBKDF2 (base64-wrapped Fernet) and new Argon2id
159+
(JSON bundle) formats. Checks JSON format first, then falls back to
160+
base64 check for legacy format.
154161
"""
162+
if not text:
163+
return False
164+
165+
# Check for new Argon2id JSON bundle format
166+
if text.startswith("{"):
167+
try:
168+
obj = json.loads(text)
169+
if isinstance(obj, dict) and obj.get("kdf") == "argon2id":
170+
return True
171+
except (json.JSONDecodeError, ValueError, KeyError):
172+
# Not valid JSON or missing expected structure - continue to legacy check
173+
pass
174+
175+
# Check for legacy PBKDF2 base64-wrapped Fernet format
155176
try:
156-
# Try to decode as base64 and check if it looks like encrypted data
157177
decoded = base64.urlsafe_b64decode(text.encode())
158178
# Encrypted data should be at least 32 bytes (Fernet minimum)
159179
return len(decoded) >= 32

0 commit comments

Comments
 (0)