Skip to content
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

(KC-803)security-audit-report error-reporting improvement: #1277

Merged
merged 2 commits into from
Jul 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions keepercommander/commands/security_audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
from json import JSONDecodeError
from typing import Dict, List, Optional, Any
from charset_normalizer import detect

from cryptography.hazmat.primitives.asymmetric.rsa import RSAPrivateKey

Expand Down Expand Up @@ -346,14 +347,27 @@ def decrypt_security_data(sec_data, k): # type: (bytes, RSAPrivateKey) -> Dict[
decrypted = None
if sec_data:
try:
decrypted = crypto.decrypt_rsa(sec_data, k, apply_padding=True)
decrypted_bytes = crypto.decrypt_rsa(sec_data, k, apply_padding=True)
except Exception as e:
error = f'Decrypt fail (incremental data): {e}'
self.get_error_report_builder().update_report_data(error)
return decrypted
return

try:
decrypted = json.loads(decrypted.decode())
decoded = decrypted_bytes.decode()
except UnicodeDecodeError as ude:
detected_encoding = detect(decrypted_bytes).get('encoding')
error = f'Failed to decode incremental data, (possible) encoding: {detected_encoding}'
self.get_error_report_builder().update_report_data(error)
self.get_error_report_builder().update_report_data(decrypted_bytes)
return
except Exception as e:
error = f'Decode fail: {e}'
self.get_error_report_builder().update_report_data(error)
return

try:
decrypted = json.loads(decoded)
except Exception as e:
reason = f"Invalid JSON: {e.doc}" if isinstance(e, JSONDecodeError) else e
error = f'Load fail (incremental data). {reason}'
Expand Down
Loading