2929# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3030
3131
32- import re
3332import warnings
3433from collections import defaultdict
3534
3635from elasticapm .conf .constants import BASE_SANITIZE_FIELD_NAMES , ERROR , MASK , SPAN , TRANSACTION
3736from elasticapm .utils import compat , varmap
38- from elasticapm .utils .encoding import force_text , keyword_field
37+ from elasticapm .utils .encoding import force_text
3938from elasticapm .utils .stacks import get_lines_from_file
4039
41- SANITIZE_VALUE_PATTERNS = [re .compile (r"^[- \d]{16,19}$" )] # credit card numbers, with or without spacers
42-
4340
4441def for_events (* events ):
4542 """
@@ -116,7 +113,7 @@ def sanitize_http_request_cookies(client, event):
116113
117114 # sanitize request.header.cookie string
118115 try :
119- cookie_string = event ["context" ]["request" ]["headers" ]["cookie" ]
116+ cookie_string = force_text ( event ["context" ]["request" ]["headers" ]["cookie" ], errors = "replace" )
120117 event ["context" ]["request" ]["headers" ]["cookie" ] = _sanitize_string (
121118 cookie_string , "; " , "=" , sanitize_field_names = client .config .sanitize_field_names
122119 )
@@ -134,7 +131,7 @@ def sanitize_http_response_cookies(client, event):
134131 :return: The modified event
135132 """
136133 try :
137- cookie_string = event ["context" ]["response" ]["headers" ]["set-cookie" ]
134+ cookie_string = force_text ( event ["context" ]["response" ]["headers" ]["set-cookie" ], errors = "replace" )
138135 event ["context" ]["response" ]["headers" ]["set-cookie" ] = _sanitize_string (
139136 cookie_string , ";" , "=" , sanitize_field_names = client .config .sanitize_field_names
140137 )
@@ -190,32 +187,6 @@ def sanitize_http_wsgi_env(client, event):
190187 return event
191188
192189
193- @for_events (ERROR , TRANSACTION )
194- def sanitize_http_request_querystring (client , event ):
195- """
196- Sanitizes http request query string
197- :param client: an ElasticAPM client
198- :param event: a transaction or error event
199- :return: The modified event
200- """
201- try :
202- query_string = force_text (event ["context" ]["request" ]["url" ]["search" ], errors = "replace" )
203- except (KeyError , TypeError ):
204- return event
205- if "=" in query_string :
206- sanitized_query_string = _sanitize_string (
207- query_string , "&" , "=" , sanitize_field_names = client .config .sanitize_field_names
208- )
209- full_url = event ["context" ]["request" ]["url" ]["full" ]
210- # we need to pipe the sanitized string through encoding.keyword_field to ensure that the maximum
211- # length of keyword fields is still ensured.
212- event ["context" ]["request" ]["url" ]["search" ] = keyword_field (sanitized_query_string )
213- event ["context" ]["request" ]["url" ]["full" ] = keyword_field (
214- full_url .replace (query_string , sanitized_query_string )
215- )
216- return event
217-
218-
219190@for_events (ERROR , TRANSACTION )
220191def sanitize_http_request_body (client , event ):
221192 """
@@ -276,16 +247,13 @@ def mark_in_app_frames(client, event):
276247
277248def _sanitize (key , value , ** kwargs ):
278249 if "sanitize_field_names" in kwargs :
279- sanitize_field_names = frozenset ( kwargs ["sanitize_field_names" ])
250+ sanitize_field_names = kwargs ["sanitize_field_names" ]
280251 else :
281- sanitize_field_names = frozenset ( BASE_SANITIZE_FIELD_NAMES )
252+ sanitize_field_names = BASE_SANITIZE_FIELD_NAMES
282253
283254 if value is None :
284255 return
285256
286- if isinstance (value , compat .string_types ) and any (pattern .match (value ) for pattern in SANITIZE_VALUE_PATTERNS ):
287- return MASK
288-
289257 if isinstance (value , dict ):
290258 # varmap will call _sanitize on each k:v pair of the dict, so we don't
291259 # have to do anything with dicts here
@@ -296,7 +264,7 @@ def _sanitize(key, value, **kwargs):
296264
297265 key = key .lower ()
298266 for field in sanitize_field_names :
299- if field in key :
267+ if field . match ( key . strip ()) :
300268 # store mask as a fixed length for security
301269 return MASK
302270 return value
0 commit comments