Skip to content

Commit 5d94f8d

Browse files
committed
Make redaction recursive in local_vars
Change-Id: I5e9050116d4790ba693e0d4379a1d05df0306c40
1 parent 79b5e6a commit 5d94f8d

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

packages/cmk-ccc/cmk/ccc/crash_reporting.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,13 @@ def _get_generic_crash_info(
283283

284284

285285
def _get_local_vars_of_last_exception() -> str:
286+
local_vars = {}
287+
286288
# Suppressing to handle case where sys.exc_info has no crash information
287289
# (https://docs.python.org/2/library/sys.html#sys.exc_info)
288290
with suppress(IndexError):
289-
local_vars = _sanitize_variables(inspect.trace()[-1][0].f_locals)
291+
for key, val in inspect.trace()[-1][0].f_locals.items():
292+
local_vars[key] = _format_var_for_export(val)
290293
# This needs to be encoded as the local vars might contain binary data which can not be
291294
# transported using JSON.
292295
return base64.b64encode(
@@ -310,7 +313,12 @@ def _format_var_for_export(val: Any, maxdepth: int = 4, maxsize: int = 1024 * 10
310313
if isinstance(val, dict):
311314
val = val.copy()
312315
for item_key, item_val in val.items():
313-
val[item_key] = _format_var_for_export(item_val, maxdepth - 1)
316+
if any(
317+
sensitive_keyword in item_key.lower() for sensitive_keyword in SENSITIVE_KEYWORDS
318+
):
319+
val[item_key] = REDACTED_STRING
320+
else:
321+
val[item_key] = _format_var_for_export(item_val, maxdepth - 1)
314322

315323
elif isinstance(val, list):
316324
val = val[:]

tests/unit/cmk/utils/test_crash_reporting.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
44
# conditions defined in the file COPYING, which is part of this source code package.
55

6-
6+
import base64
77
import copy
88
import itertools
99
import json
@@ -38,6 +38,8 @@ def crashdir(tmp_path: Path) -> Path:
3838
@pytest.fixture()
3939
def crash(crashdir: Path) -> UnitTestCrashReport:
4040
try:
41+
# We need some var so the local_vars are part of the crash report
42+
some_local_var = [{"foo": {"deep": True, "password": "verysecret", "foo": "notsecret"}}] # noqa: F841
4143
raise ValueError("XYZ")
4244
except ValueError:
4345
return UnitTestCrashReport(
@@ -71,6 +73,12 @@ def test_crash_report_sanitization(crash: ABCCrashReport) -> None:
7173
}
7274

7375

76+
def test_crash_report_sanitization_local_vars(crash: ABCCrashReport) -> None:
77+
decoded_local_vars = base64.b64decode(crash.crash_info["local_vars"])
78+
assert b"verysecret" not in decoded_local_vars
79+
assert b"notsecret" in decoded_local_vars
80+
81+
7482
def test_crash_report_ident(crash: ABCCrashReport) -> None:
7583
assert crash.ident() == (crash.crash_info["id"],)
7684

0 commit comments

Comments
 (0)