Skip to content

Commit 56812f0

Browse files
author
Chris Rossi
authored
fix: do not disclose cache contents in stack traces (#485)
* fix: do not disclose cache contents in stack traces Fixes #482 * Blacken
1 parent 0064b18 commit 56812f0

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

packages/google-cloud-ndb/google/cloud/ndb/_cache.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ def get_and_validate(self, key):
4949
del self.data[key]
5050
raise KeyError(key)
5151

52+
def __repr__(self):
53+
return "ContextCache()"
54+
5255

5356
def _future_result(result):
5457
"""Returns a completed Future with the given result.

packages/google-cloud-ndb/tests/system/test_misc.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,18 @@
1717
"""
1818
import os
1919
import pickle
20+
import traceback
21+
22+
try:
23+
from unittest import mock
24+
except ImportError: # pragma: NO PY3 COVER
25+
import mock
2026

2127
import pytest
2228

2329
import test_utils.system
2430

31+
from google.api_core import exceptions as core_exceptions
2532
from google.cloud import ndb
2633

2734
from . import eventually, length_equals, KIND
@@ -315,3 +322,27 @@ class SomeKind(ndb.Model):
315322
entity = key.get()
316323

317324
assert entity.bar == 42
325+
326+
327+
@mock.patch("google.cloud.ndb._datastore_api.begin_transaction")
328+
def test_do_not_disclose_cache_contents(begin_transaction, client_context):
329+
"""Regression test for #482.
330+
331+
https://github.com/googleapis/python-ndb/issues/482
332+
"""
333+
begin_transaction.side_effect = core_exceptions.ServiceUnavailable(
334+
"Spurious Error"
335+
)
336+
337+
client_context.cache["hello dad"] = "i'm in jail"
338+
339+
@ndb.transactional()
340+
def callback():
341+
pass
342+
343+
with pytest.raises(Exception) as error_info:
344+
callback()
345+
346+
error = error_info.value
347+
message = "".join(traceback.format_exception_only(type(error), error))
348+
assert "hello dad" not in message

packages/google-cloud-ndb/tests/unit/test__cache.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ def test_get_and_validate_miss():
5858
with pytest.raises(KeyError):
5959
cache.get_and_validate("nonexistent_key")
6060

61+
@staticmethod
62+
def test___repr__():
63+
cache = _cache.ContextCache()
64+
cache["hello dad"] = "i'm in jail"
65+
assert repr(cache) == "ContextCache()"
66+
6167

6268
class Test_GlobalCacheBatch:
6369
@staticmethod

0 commit comments

Comments
 (0)