-
Notifications
You must be signed in to change notification settings - Fork 67
fix: add debug logging for AES key mismatch troubleshooting #842
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,7 +31,14 @@ def _get_encryption_key(): | |
| aes_iv = os.environ.get('GIT_TOKEN_AES_IV', '1234567890123456') | ||
| _aes_key = aes_key.encode('utf-8') | ||
| _aes_iv = aes_iv.encode('utf-8') | ||
| logger.info("Loaded encryption keys from environment variables") | ||
| # Debug logging: print key info for troubleshooting encryption mismatch | ||
| logger.info( | ||
| f"Loaded encryption keys from environment variables - " | ||
| f"GIT_TOKEN_AES_KEY: first 4 chars='{aes_key[:4]}', last 4 chars='{aes_key[-4:]}', length={len(aes_key)}" | ||
| ) | ||
| logger.info( | ||
| f"GIT_TOKEN_AES_IV: first 4 chars='{aes_iv[:4]}', last 4 chars='{aes_iv[-4:]}', length={len(aes_iv)}" | ||
| ) | ||
| return _aes_key, _aes_iv | ||
|
|
||
|
|
||
|
|
@@ -42,7 +49,7 @@ def _get_encryption_key(): | |
| def encrypt_sensitive_data(plain_text: str) -> str: | ||
| """ | ||
| Encrypt sensitive data using AES-256-CBC encryption | ||
|
|
||
| This is the core encryption function used by all sensitive data encryption. | ||
|
|
||
| Args: | ||
|
|
@@ -57,6 +64,12 @@ def encrypt_sensitive_data(plain_text: str) -> str: | |
| if plain_text == "***": | ||
| return "***" | ||
|
|
||
| # Debug logging: print plain text info | ||
| logger.info( | ||
| f"encrypt_sensitive_data called - plain_text length={len(plain_text)}, " | ||
| f"first 4 chars='{plain_text[:4] if len(plain_text) >= 4 else plain_text}'" | ||
| ) | ||
|
Comment on lines
+67
to
+71
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CRITICAL: Never log sensitive plaintext data (git tokens, API keys). This function encrypts git tokens and API keys—logging the first 4 characters of plaintext secrets is a severe security vulnerability:
For debugging encryption issues, log only non-sensitive metadata:
Never log actual secret content, even partially. 🔎 Safer logging approach- # Debug logging: print plain text info
- logger.info(
- f"encrypt_sensitive_data called - plain_text length={len(plain_text)}, "
- f"first 4 chars='{plain_text[:4] if len(plain_text) >= 4 else plain_text}'"
+ # Log encryption attempt with non-sensitive metadata only
+ logger.debug(
+ f"encrypt_sensitive_data called - input length={len(plain_text)}"
)🤖 Prompt for AI Agents |
||
|
|
||
| try: | ||
| aes_key, aes_iv = _get_encryption_key() | ||
|
|
||
|
|
@@ -76,7 +89,13 @@ def encrypt_sensitive_data(plain_text: str) -> str: | |
| encrypted_bytes = encryptor.update(padded_data) + encryptor.finalize() | ||
|
|
||
| # Return base64 encoded encrypted data | ||
| return base64.b64encode(encrypted_bytes).decode('utf-8') | ||
| encrypted_str = base64.b64encode(encrypted_bytes).decode('utf-8') | ||
| # Debug logging: print encryption success info | ||
| logger.info( | ||
| f"encrypt_sensitive_data success - encrypted length={len(encrypted_str)}, " | ||
| f"first 10 chars='{encrypted_str[:10]}'" | ||
| ) | ||
| return encrypted_str | ||
|
Comment on lines
+92
to
+98
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major Use Debug information should use Additionally, logging partial ciphertext samples, while less critical than logging plaintext, can still provide information useful for cryptanalysis. Consider logging only length and success status. 🔎 Recommended changes # Return base64 encoded encrypted data
encrypted_str = base64.b64encode(encrypted_bytes).decode('utf-8')
- # Debug logging: print encryption success info
- logger.info(
- f"encrypt_sensitive_data success - encrypted length={len(encrypted_str)}, "
- f"first 10 chars='{encrypted_str[:10]}'"
+ # Log encryption success with metadata only
+ logger.debug(
+ f"encrypt_sensitive_data success - output length={len(encrypted_str)}"
)
return encrypted_str
-except Exception as e:
+except Exception as e:
logger.error(f"Failed to encrypt sensitive data: {str(e)}")
raiseNote: The static analysis tool (Ruff TRY300) suggests moving the return to an
🧰 Tools🪛 Ruff (0.14.10)98-98: Consider moving this statement to an (TRY300) 🤖 Prompt for AI Agents |
||
| except Exception as e: | ||
| logger.error(f"Failed to encrypt sensitive data: {str(e)}") | ||
| raise | ||
|
|
@@ -85,7 +104,7 @@ def encrypt_sensitive_data(plain_text: str) -> str: | |
| def decrypt_sensitive_data(encrypted_text: str) -> Optional[str]: | ||
| """ | ||
| Decrypt sensitive data using AES-256-CBC decryption | ||
|
|
||
| This is the core decryption function used by all sensitive data decryption. | ||
|
|
||
| Args: | ||
|
|
@@ -100,6 +119,12 @@ def decrypt_sensitive_data(encrypted_text: str) -> Optional[str]: | |
| if encrypted_text == "***": | ||
| return "***" | ||
|
|
||
| # Debug logging: print encrypted text info | ||
| logger.info( | ||
| f"decrypt_sensitive_data called - encrypted_text length={len(encrypted_text)}, " | ||
| f"first 10 chars='{encrypted_text[:10] if len(encrypted_text) >= 10 else encrypted_text}'" | ||
| ) | ||
|
Comment on lines
+122
to
+126
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major Use Same issues as the encryption logging:
🔎 Recommended changes- # Debug logging: print encrypted text info
- logger.info(
- f"decrypt_sensitive_data called - encrypted_text length={len(encrypted_text)}, "
- f"first 10 chars='{encrypted_text[:10] if len(encrypted_text) >= 10 else encrypted_text}'"
+ # Log decryption attempt with metadata only
+ logger.debug(
+ f"decrypt_sensitive_data called - input length={len(encrypted_text)}"
)🤖 Prompt for AI Agents |
||
|
|
||
| try: | ||
| aes_key, aes_iv = _get_encryption_key() | ||
|
|
||
|
|
@@ -122,7 +147,13 @@ def decrypt_sensitive_data(encrypted_text: str) -> Optional[str]: | |
| decrypted_bytes = unpadder.update(decrypted_padded_bytes) + unpadder.finalize() | ||
|
|
||
| # Return decrypted string | ||
| return decrypted_bytes.decode('utf-8') | ||
| decrypted_str = decrypted_bytes.decode('utf-8') | ||
| # Debug logging: print decryption success info | ||
| logger.info( | ||
| f"decrypt_sensitive_data success - decrypted length={len(decrypted_str)}, " | ||
| f"first 4 chars='{decrypted_str[:4] if len(decrypted_str) >= 4 else decrypted_str}'" | ||
| ) | ||
| return decrypted_str | ||
|
Comment on lines
+150
to
+156
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CRITICAL: Never log decrypted sensitive data. This is identical to the pre-encryption logging issue—logging the first 4 characters of decrypted plaintext exposes git tokens and API keys in logs. This completely undermines the encryption security model. After decryption, log only success status and metadata, never the actual decrypted content. 🔎 Safer logging approach # Return decrypted string
decrypted_str = decrypted_bytes.decode('utf-8')
- # Debug logging: print decryption success info
- logger.info(
- f"decrypt_sensitive_data success - decrypted length={len(decrypted_str)}, "
- f"first 4 chars='{decrypted_str[:4] if len(decrypted_str) >= 4 else decrypted_str}'"
+ # Log decryption success with non-sensitive metadata only
+ logger.debug(
+ f"decrypt_sensitive_data success - output length={len(decrypted_str)}"
)
return decrypted_str
-except Exception as e:
+except Exception as e:
logger.warning(f"Failed to decrypt sensitive data: {str(e)}")
# Return the original text as fallback for backward compatibility
return encrypted_text🧰 Tools🪛 Ruff (0.14.10)156-156: Consider moving this statement to an (TRY300) 🤖 Prompt for AI Agents |
||
| except Exception as e: | ||
| logger.warning(f"Failed to decrypt sensitive data: {str(e)}") | ||
| # Return the original text as fallback for backward compatibility | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CRITICAL: Never log encryption key material, even partially.
Logging any portion of encryption keys (first/last characters) is a severe security vulnerability that:
For troubleshooting key mismatches between services, use these safer alternatives:
sha256(key)instead of partial key content—services can verify matching hashes without exposing key material🔎 Safer alternative using hash-based verification
🤖 Prompt for AI Agents