-
Notifications
You must be signed in to change notification settings - Fork 3
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
feat: Add middleware to log all post requests. #651
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import logging | ||
import json | ||
import traceback | ||
import os | ||
import random | ||
|
@@ -56,3 +57,21 @@ def filter(self, record): | |
worker_id = getattr(record, 'worker_id', '') | ||
record.worker_id = worker_id | ||
return True | ||
|
||
class LogPostRequestMiddleware: | ||
def __init__(self, get_response): | ||
self.get_response = get_response | ||
|
||
def __call__(self, request): | ||
if request.method in ['POST', 'PUT'] : | ||
try: | ||
body_unicode = request.body.decode('utf-8') | ||
request_body = json.loads(body_unicode) | ||
logger.info("POST request to %s: %s", request.path, request_body) | ||
except (json.JSONDecodeError, UnicodeDecodeError): | ||
logger.warning("Failed to decode POST request body for %s", request.path) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's also do 1 general exception to be safe, just do logger.info there |
||
except Exception as e: | ||
logger.info('Something went wrong when logging post call - %s', e) | ||
|
||
response = self.get_response(request) | ||
return response | ||
Comment on lines
+61
to
+77
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Codebase verification 🛠️ Refactor suggestion Implement Data Masking or Filtering for Sensitive Information in Logs The current implementation logs all POST and PUT request bodies without masking or filtering sensitive information. This can lead to exposure of sensitive data such as PII. Recommended Actions:
🔗 Analysis chainImplement custom logger and review exception handling
Here's a suggested implementation addressing points 1 and 2: class LogPostRequestMiddleware:
def __init__(self, get_response):
self.get_response = get_response
self.logger = get_logger()
def __call__(self, request):
if request.method in ['POST', 'PUT']:
try:
body_unicode = request.body.decode('utf-8')
request_body = json.loads(body_unicode)
self.logger.info("POST request to %s: %s", request.path, request_body)
except (json.JSONDecodeError, UnicodeDecodeError):
self.logger.warning("Failed to decode POST request body for %s", request.path)
except Exception as e:
self.logger.error('Unexpected error when logging post call - %s', str(e), exc_info=True)
response = self.get_response(request)
return response To address point 3, let's check if there are any existing mechanisms for handling sensitive data in the project: 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Search for existing data masking or filtering mechanisms
rg -i "mask|filter|sensitive|pii" --type python
Length of output: 152 Script: #!/bin/bash
# Search for existing data masking or filtering mechanisms
rg -i "mask|filter|sensitive|pii" --type py
Length of output: 101832 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Consider using the custom logger for consistency
The new middleware is using the root logger, while there's a custom
get_logger()
function available in this file. For consistency and to benefit from the worker ID logging, consider using the custom logger.Replace the use of
logger
with the custom logger:This change will ensure that the worker ID is included in the log messages from the new middleware, maintaining consistency with other parts of the application.
Also applies to: 72-72