Skip to content

Added logic to exclude parameters present in the APU URL string #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions drf_api_logger/middleware/api_logger_middleware.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import time
import re
from django.conf import settings
from django.urls import resolve
from django.utils import timezone
Expand Down Expand Up @@ -101,8 +102,11 @@ def __call__(self, request):
if len(self.DRF_API_LOGGER_METHODS) > 0 and method not in self.DRF_API_LOGGER_METHODS:
return response

if response.get('content-type') in ('application/json', 'application/vnd.api+json',):
if getattr(response, 'streaming', False):
if response.get('content-type') in ('application/json', 'application/vnd.api+json', 'application/gzip'):

if response.get('content-type') == 'application/gzip':
response_body = '** GZIP Archive **'
elif getattr(response, 'streaming', False):
response_body = '** Streaming **'
else:
if type(response.content) == bytes:
Expand All @@ -119,7 +123,7 @@ def __call__(self, request):
api = request.build_absolute_uri()

data = dict(
api=api,
api=mask_sensitive_data(api, mask_api_parameters=True),
headers=mask_sensitive_data(headers),
body=mask_sensitive_data(request_data),
method=method,
Expand Down
9 changes: 8 additions & 1 deletion drf_api_logger/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,20 @@ def database_log_enabled():
return drf_api_logger_database


def mask_sensitive_data(data):
def mask_sensitive_data(data, mask_api_parameters=False):
"""
Hides sensitive keys specified in sensitive_keys settings.
Loops recursively over nested dictionaries.

When the mask_api_parameters parameter is set, the function will
instead iterate over sensitive_keys and remove them from an api
URL string.
"""

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

for key, value in data.items():
Expand Down