Skip to content

Commit 1964a34

Browse files
Merge pull request #67 from cantus-firmus/add_parameter_filtering
Added logic to exclude parameters present in the APU URL string
2 parents df97a40 + 39e2516 commit 1964a34

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

drf_api_logger/middleware/api_logger_middleware.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import time
3+
import re
34
from django.conf import settings
45
from django.urls import resolve
56
from django.utils import timezone
@@ -101,8 +102,11 @@ def __call__(self, request):
101102
if len(self.DRF_API_LOGGER_METHODS) > 0 and method not in self.DRF_API_LOGGER_METHODS:
102103
return response
103104

104-
if response.get('content-type') in ('application/json', 'application/vnd.api+json',):
105-
if getattr(response, 'streaming', False):
105+
if response.get('content-type') in ('application/json', 'application/vnd.api+json', 'application/gzip'):
106+
107+
if response.get('content-type') == 'application/gzip':
108+
response_body = '** GZIP Archive **'
109+
elif getattr(response, 'streaming', False):
106110
response_body = '** Streaming **'
107111
else:
108112
if type(response.content) == bytes:
@@ -119,7 +123,7 @@ def __call__(self, request):
119123
api = request.build_absolute_uri()
120124

121125
data = dict(
122-
api=api,
126+
api=mask_sensitive_data(api, mask_api_parameters=True),
123127
headers=mask_sensitive_data(headers),
124128
body=mask_sensitive_data(request_data),
125129
method=method,

drf_api_logger/utils.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,20 @@ def database_log_enabled():
4747
return drf_api_logger_database
4848

4949

50-
def mask_sensitive_data(data):
50+
def mask_sensitive_data(data, mask_api_parameters=False):
5151
"""
5252
Hides sensitive keys specified in sensitive_keys settings.
5353
Loops recursively over nested dictionaries.
54+
55+
When the mask_api_parameters parameter is set, the function will
56+
instead iterate over sensitive_keys and remove them from an api
57+
URL string.
5458
"""
5559

5660
if type(data) != dict:
61+
if mask_api_parameters and type(data) == str:
62+
for sensitive_key in SENSITIVE_KEYS:
63+
data = re.sub('({}=)(.*?)($|&)'.format(sensitive_key), '\g<1>***FILTERED***\g<3>'.format(sensitive_key.upper()), data)
5764
return data
5865

5966
for key, value in data.items():

0 commit comments

Comments
 (0)