Skip to content

Mask JWT tokens in logs in case of post-auth errors #1457

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

Merged
merged 2 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
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
12 changes: 4 additions & 8 deletions src/snowflake/connector/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import OpenSSL.SSL

from snowflake.connector.secret_detector import SecretDetector
from snowflake.connector.vendored.requests.models import PreparedRequest
from snowflake.connector.vendored.urllib3.connectionpool import (
HTTPConnectionPool,
Expand Down Expand Up @@ -980,14 +981,9 @@ def handle_invalid_certificate_error(self, conn, full_url, cause) -> None:
def _handle_unknown_error(self, method, full_url, headers, data, conn) -> None:
"""Handles unknown errors."""
if data:
try:
decoded_data = json.loads(data)
if decoded_data.get("data") and decoded_data["data"].get("PASSWORD"):
# masking the password
decoded_data["data"]["PASSWORD"] = "********"
data = json.dumps(decoded_data)
except Exception:
logger.info("data is not JSON")
_, masked_data, err_str = SecretDetector.mask_secrets(data)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we also check the first returned value to see if the data is masked? Or is this more like a best effort?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

if data is not masked, then original data is returned. It basically means that no secrets were detected. I'm not sure how we would use the masked value

Copy link
Contributor

Choose a reason for hiding this comment

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

I see. Thanks for the explanation :)

if err_str is None:
data = masked_data
logger.error(
f"Failed to get the response. Hanging? "
f"method: {method}, url: {full_url}, headers:{headers}, "
Expand Down
38 changes: 38 additions & 0 deletions test/unit/test_retry_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from __future__ import annotations

import errno
import logging
import os
import time
from unittest.mock import MagicMock, Mock, PropertyMock
Expand Down Expand Up @@ -217,3 +218,40 @@ def fake_request_exec(**kwargs):
assert ret == {}
assert cnt.c == 1 # failed on first call - did not retry
assert rest._connection.errorhandler.called # error


def test_secret_masking(caplog):
connection = MagicMock()
connection.errorhandler = Mock(return_value=None)

rest = SnowflakeRestful(
host="testaccount.snowflakecomputing.com", port=443, connection=connection
)

data = (
'{"code": 12345,'
' "data": {"TOKEN": "_Y1ZNETTn5/qfUWj3Jedb", "PASSWORD": "dummy_pass"}'
"}"
)
default_parameters = {
"method": "POST",
"full_url": "https://testaccount.snowflakecomputing.com/",
"headers": {},
"data": data,
}

class NotRetryableException(Exception):
pass

def fake_request_exec(**kwargs):
return None

# inject a fake method
rest._request_exec = fake_request_exec

# first two attempts will fail but third will success
with caplog.at_level(logging.ERROR):
ret = rest.fetch(timeout=10, **default_parameters)
assert '"TOKEN": "****' in caplog.text
assert '"PASSWORD": "****' in caplog.text
assert ret == {}