Skip to content

fix: Redact tokens in query and response logs (#545)#694

Closed
ad-claw000 wants to merge 1 commit into
developfrom
fix/545-redact-token-logs
Closed

fix: Redact tokens in query and response logs (#545)#694
ad-claw000 wants to merge 1 commit into
developfrom
fix/545-redact-token-logs

Conversation

@ad-claw000
Copy link
Copy Markdown
Contributor

Summary

When an Authenticate query fails or a subsequent query fails and the last response was from authentication, the error logs were exposing refresh_token, session_token, and passwords. This change redacts them.

Changes

  • aperturedb/Connector.py: Modified get_last_response_str to deepcopy and scrub session_token and refresh_token inside Authenticate.
  • aperturedb/CommonLibrary.py: Modified error logging in execute_query to also redact tokens/passwords in the query and response JSON before logging.

Fixes #545

Copilot AI review requested due to automatic review settings May 19, 2026 22:05
@ad-claw000 ad-claw000 self-assigned this May 19, 2026
@ad-claw000
Copy link
Copy Markdown
Contributor Author

Closing in favor of older PR #672

@ad-claw000 ad-claw000 closed this May 19, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses issue #545 by preventing authentication secrets (passwords and tokens) from being exposed in SDK logs when Authenticate (or subsequent queries around authentication) fail, by scrubbing sensitive fields before formatting them for logging.

Changes:

  • Redacts session_token/refresh_token when stringifying Connector.last_response via Connector.get_last_response_str().
  • Redacts password/token/session_token/refresh_token in execute_query() error logs by deep-copying and scrubbing the query/response objects before logging.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
aperturedb/Connector.py Scrubs auth tokens from the “last response” string representation used in error/diagnostic logging.
aperturedb/CommonLibrary.py Scrubs auth secrets from failed-transaction error logs to avoid leaking credentials/tokens.
Comments suppressed due to low confidence (2)

aperturedb/Connector.py:656

  • Redaction here only looks for Authenticate responses. last_response can also be a RefreshToken response (see _refresh_token() which reads session_token/refresh_token from response[0]["RefreshToken"]), and get_last_response_str() would currently serialize those tokens unredacted. Expanding the scrub to cover RefreshToken would prevent token leakage when a refresh occurs before a subsequent failing query.
            if isinstance(safe_response, list):
                for item in safe_response:
                    if isinstance(item, dict) and "Authenticate" in item:
                        auth_dict = item["Authenticate"]
                        if isinstance(auth_dict, dict):
                            if "session_token" in auth_dict:
                                auth_dict["session_token"] = "***"
                            if "refresh_token" in auth_dict:
                                auth_dict["refresh_token"] = "***"

aperturedb/CommonLibrary.py:333

  • This redaction logic only handles Authenticate objects; queries/responses involving RefreshToken can also contain refresh_token/session_token fields (Connector._refresh_token expects them). As a result, execute_query can still log sensitive tokens when a refresh-token transaction fails. Consider scrubbing the same fields under RefreshToken as well (and ideally using a small shared scrubber to avoid missing future auth-related commands).
        for obj in (safe_query, safe_r):
            if isinstance(obj, list):
                for item in obj:
                    if isinstance(item, dict) and "Authenticate" in item:
                        auth_dict = item["Authenticate"]
                        if isinstance(auth_dict, dict):
                            if "password" in auth_dict:
                                auth_dict["password"] = "***"
                            if "token" in auth_dict:
                                auth_dict["token"] = "***"
                            if "session_token" in auth_dict:
                                auth_dict["session_token"] = "***"
                            if "refresh_token" in auth_dict:
                                auth_dict["refresh_token"] = "***"


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread aperturedb/Connector.py
Comment on lines +646 to +659
try:
safe_response = copy.deepcopy(self.last_response)
if isinstance(safe_response, list):
for item in safe_response:
if isinstance(item, dict) and "Authenticate" in item:
auth_dict = item["Authenticate"]
if isinstance(auth_dict, dict):
if "session_token" in auth_dict:
auth_dict["session_token"] = "***"
if "refresh_token" in auth_dict:
auth_dict["refresh_token"] = "***"
return json.dumps(safe_response, indent=4, sort_keys=False)
except Exception:
return json.dumps(self.last_response, indent=4, sort_keys=False)
Comment on lines +316 to +318
import copy
safe_query = copy.deepcopy(query)
safe_r = copy.deepcopy(r)
Comment thread aperturedb/Connector.py
Comment on lines 641 to +657
def get_last_response_str(self):
import copy
if not self.last_response:
return "[]"

return json.dumps(self.last_response, indent=4, sort_keys=False)
try:
safe_response = copy.deepcopy(self.last_response)
if isinstance(safe_response, list):
for item in safe_response:
if isinstance(item, dict) and "Authenticate" in item:
auth_dict = item["Authenticate"]
if isinstance(auth_dict, dict):
if "session_token" in auth_dict:
auth_dict["session_token"] = "***"
if "refresh_token" in auth_dict:
auth_dict["refresh_token"] = "***"
return json.dumps(safe_response, indent=4, sort_keys=False)
@luisremis luisremis deleted the fix/545-redact-token-logs branch May 19, 2026 22:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Logging exposes token information

2 participants