Skip to content
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 masking to python middleware #955

Merged
merged 15 commits into from
Sep 18, 2024
1 change: 1 addition & 0 deletions packages/python/readme_metrics/Metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def process(self, request, response: ResponseInfoWrapper) -> None:
)
if self.config.IS_DEVELOPMENT_MODE:
print(traceback.format_exc())
return

self.queue.put(payload)
if self.queue.qsize() >= self.config.BUFFER_LENGTH:
Expand Down
7 changes: 3 additions & 4 deletions packages/python/readme_metrics/PayloadBuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,6 @@ def _build_request_payload(self, request) -> dict:
"""
headers = self.redact_dict(request.headers)

# Mask the auth header
if "Authorization" in headers:
headers["Authorization"] = mask(headers["Authorization"])

queryString = parse.parse_qsl(self._get_query_string(request))

content_type = self._get_content_type(headers)
Expand Down Expand Up @@ -224,6 +220,9 @@ def _build_request_payload(self, request) -> dict:
"bodySize": -1,
}

if "Authorization" in headers:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

at this point, if there is an Authorization header, won't there already be an unmasked Authorization header in payload["headers"]?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be my expectation too - does .append overwrite the existing header here, or do we need to forcefully delete it?

payload["headers"].append({"name": "Authorization", "value": mask(headers["Authorization"])})

if not post_data is False:
payload["postData"] = post_data

Expand Down
2 changes: 2 additions & 0 deletions packages/python/readme_metrics/tests/PayloadBuilder_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,8 @@ def test_auth_header_masked(self):
payload = self.createPayload(config)
data = payload(metrics.req, metrics.res, str(uuid.uuid4()))

auth_header = None

# Pull out the auth header
for header in data["request"]["log"]["entries"][0]["request"]["headers"]:
if header["name"] == "Authorization":
Expand Down
Loading