-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
secrets: add hide patters, make it default, add readme, add example
- Loading branch information
Andy Landy
committed
Feb 19, 2025
1 parent
8bd8d66
commit 8a02aa9
Showing
8 changed files
with
155 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
""" | ||
When you deal with secrets like passwords or tokens any debug report system | ||
puts you at risk of exposing those secrets. | ||
Why? Some secrets are stored securely in databases and are kept decrypted only | ||
for short time in memory while being used in code. So any traceback printout | ||
will save them explicitly for you to read later. | ||
Its not a huge problem if the secret is stored unencrypted, i.e. its your | ||
(not users') secret and the machine is secure. User passwords are a problem. | ||
How to prevent secret vars from being printed? You might | ||
* correctly guess how the secret variables in your code are called | ||
* hide the 3rd party well-established library code totally | ||
It will look like this: some of variables in your code will have | ||
"...hidden..." values, and all library files (where secret variables might go | ||
with unrelated names like "s", "part", "msg" etc.) will be skipped totally. | ||
Default settings include all frequent patters for your variables: | ||
PATTERN_TO_HIDE = '.*(?i:pass|secret|token|key|api_key|cred|pwd)/*' | ||
Which will hide a bit too much, names like "keyword" or "compassion". Address | ||
examples below to fit the hiding. | ||
To hide libraries address examples below, note that you can only allow code, | ||
not deny it, so all 3rd party libraries will be hidden if you use this | ||
setting. | ||
""" | ||
|
||
# simple tools usage, for more manual approach address format_customized.py | ||
|
||
|
||
# hide all libraries except couple | ||
|
||
from traceback_with_variables import fmt, hide | ||
fmt.brief_files_except = ['.*my_project.*', '.*some_library_1.*', '.*some_library_2.*'] | ||
|
||
|
||
# show all variables | ||
|
||
fmt.custom_var_printers = [] | ||
|
||
|
||
# hide variables differently | ||
|
||
fmt.custom_var_printers = [ | ||
('.*(precious|ring).*', hide), # by name | ||
(MySecret, hide), # by class | ||
] |
26 changes: 26 additions & 0 deletions
26
tests/dumps/test_default_global_hooks.patterns_to_hide.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
Traceback with variables (most recent call last): | ||
File "...omitted for tests only.../test_default_global_hooks.py", line...omitted for tests only..., in check | ||
f() | ||
tb_reg = <bound method Reg.match_tb_text of <tests.test_utils.Reg object at 0x...omitted for tests only...>> | ||
fmt = traceback_with_variables.core.Format(after=0, before=0, brief_files_except=[], color_scheme=None, custom_var_printers=[(<function _var_filter_to_should_print.<locals>.should_print at 0x...omitted for tests only...>, <function hide at 0x...omitted for tests only...>)], ellipsis_='...', ellipsis_rel_pos=0.7, max_exc_str_len=10000, max_value_str_len=1000, objects_details=1, skip_files_except=[]) | ||
File "...omitted for tests only.../test_default_global_hooks.py", line...omitted for tests only..., in f | ||
return f(n - 1) | ||
n = 1 | ||
money = '1000' | ||
password = ...hidden... | ||
secret_word = ...hidden... | ||
monkey_species = ...hidden... | ||
my_pAssWOrd_2 = ...hidden... | ||
mySecretN = ...hidden... | ||
card_number = '1234 5678 1234 5678' | ||
File "...omitted for tests only.../test_default_global_hooks.py", line...omitted for tests only..., in f | ||
raise ValueError('planned exception') | ||
n = 0 | ||
money = '1000' | ||
password = ...hidden... | ||
secret_word = ...hidden... | ||
monkey_species = ...hidden... | ||
my_pAssWOrd_2 = ...hidden... | ||
mySecretN = ...hidden... | ||
card_number = '1234 5678 1234 5678' | ||
builtins.ValueError: planned exception |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import pytest | ||
|
||
from traceback_with_variables import fmt, format_exc | ||
|
||
from tests.test_utils import tb_reg | ||
|
||
|
||
def f(n = 1): | ||
money = '1000' | ||
password = 'qwerty' | ||
secret_word = 'please' | ||
monkey_species = 'gorilla' | ||
my_pAssWOrd_2 = 'qwerty' | ||
mySecretN = 5 | ||
card_number = '1234 5678 1234 5678' | ||
|
||
if n > 0: | ||
return f(n - 1) | ||
|
||
raise ValueError('planned exception') | ||
|
||
|
||
def check(tb_reg, fmt): | ||
try: | ||
f() | ||
except Exception: # noqa | ||
tb_reg(format_exc(fmt=fmt)) | ||
|
||
|
||
def test_patterns_to_hide(tb_reg): | ||
check(tb_reg, fmt) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters