-
Notifications
You must be signed in to change notification settings - Fork 527
Expand file tree
/
Copy path_redaction.py
More file actions
171 lines (144 loc) · 4.5 KB
/
Copy path_redaction.py
File metadata and controls
171 lines (144 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import typing as t
from ddtrace.debugging._expressions import DDCompiler
from ddtrace.debugging._expressions import DDExpression
from ddtrace.internal.logger import get_logger
from ddtrace.internal.settings.dynamic_instrumentation import config
from ddtrace.internal.settings.dynamic_instrumentation import normalize_ident
from ddtrace.internal.utils.cache import cached
log = get_logger(__name__)
# The following identifier represent function argument/local variable/object
# attribute names that should be redacted from the payload.
REDACTED_IDENTIFIERS = (
frozenset(
{
"2fa",
"accesstoken",
"aiohttpsession",
"apikey",
"apisecret",
"apisignature",
"appkey",
"applicationkey",
"auth",
"authorization",
"authtoken",
"ccnumber",
"certificatepin",
"cipher",
"clientid",
"clientsecret",
"connectionstring",
"connectsid",
"cookie",
"credentials",
"creditcard",
"csrf",
"csrftoken",
"cvv",
"databaseurl",
"dburl",
"encryptionkey",
"encryptionkeyid",
"geolocation",
"gpgkey",
"ipaddress",
"jti",
"jwt",
"licensekey",
"masterkey",
"mysqlpwd",
"nonce",
"oauth",
"oauthtoken",
"otp",
"passhash",
"passwd",
"password",
"passwordb",
"pemfile",
"pgpkey",
"phpsessid",
"pin",
"pincode",
"pkcs8",
"privatekey",
"publickey",
"pwd",
"recaptchakey",
"refreshtoken",
"routingnumber",
"salt",
"secret",
"secretkey",
"secrettoken",
"securityanswer",
"securitycode",
"securityquestion",
"serviceaccountcredentials",
"session",
"sessionid",
"sessionkey",
"setcookie",
"signature",
"signaturekey",
"sshkey",
"ssn",
"symfony",
"token",
"transactionid",
"twiliotoken",
"usersession",
"voterid",
"xapikey",
"xauthtoken",
"xcsrftoken",
"xforwardedfor",
"xrealip",
"xsrf",
"xsrftoken",
}
)
| config.redacted_identifiers
)
REDACTED_PLACEHOLDER = r"{redacted}"
@cached()
def redact(ident: t.Union[str, bytes]) -> bool:
normalized = normalize_ident(ident if isinstance(ident, str) else ident.decode("utf-8", errors="replace"))
return normalized in REDACTED_IDENTIFIERS and normalized not in config.redaction_excluded_identifiers
@cached()
def redact_type(_type: str) -> bool:
_re = config.redacted_types_re
if _re is None:
return False
return _re.search(_type) is not None
class DDRedactedExpressionError(Exception):
pass
class DDRedactedCompiler(DDCompiler):
@classmethod
def __getmember__(cls, s: t.Any, a: str) -> t.Any:
if redact(a):
raise DDRedactedExpressionError(f"Access to attribute {a!r} is not allowed")
return super().__getmember__(s, a)
@classmethod
def __index__(cls, o: t.Any, i: t.Any) -> t.Any:
if isinstance(i, (str, bytes)) and redact(i):
raise DDRedactedExpressionError(f"Access to entry {i!r} is not allowed")
return super().__index__(o, i)
@classmethod
def __ref__(cls, s: str) -> str:
if redact(s):
raise DDRedactedExpressionError(f"Access to local {s!r} is not allowed")
return s
dd_compile_redacted = DDRedactedCompiler().compile
def _redacted_expr(exc: Exception) -> t.Callable[[t.Any], t.Any]:
def _(_: t.Any) -> t.Any:
raise exc
return _
class DDRedactedExpression(DDExpression):
__compiler__ = dd_compile_redacted
@classmethod
def on_compiler_error(cls, dsl: str, exc: Exception) -> t.Callable[[t.Any], t.Any]:
if isinstance(exc, DDRedactedExpressionError):
log.error("Cannot compile expression that references potential PII: %s", dsl, exc_info=True)
return _redacted_expr(exc)
return super().on_compiler_error(dsl, exc)