Skip to content

Commit

Permalink
Fix user agent pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
itssimon committed Jan 21, 2025
1 parent a4820a7 commit 4980efc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
12 changes: 5 additions & 7 deletions apitally/client/request_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import threading
import time
from collections import deque
from contextlib import suppress
from dataclasses import dataclass, field
from functools import lru_cache
from io import BufferedReader
Expand All @@ -29,14 +28,14 @@
ALLOWED_CONTENT_TYPES = ["application/json", "text/plain"]
EXCLUDE_PATH_PATTERNS = [
r"/_?healthz?$",
r"/_?health[_-]?checks?$",
r"/_?heart[_-]?beats?$",
r"/_?health[\-_]?checks?$",
r"/_?heart[\-_]?beats?$",
r"/ping$",
r"/ready$",
r"/live$",
]
EXCLUDE_USER_AGENT_PATTERNS = [
r"health[_- ]?check",
r"health[\-_ ]?check",
r"microsoft-azure-application-lb",
r"googlehc",
r"kube-probe",
Expand Down Expand Up @@ -307,9 +306,8 @@ def _should_mask_header(self, header_name: str) -> bool:
@staticmethod
def _match_patterns(value: str, patterns: List[str]) -> bool:
for pattern in patterns:
with suppress(re.error):
if re.search(pattern, value, re.I) is not None:
return True
if re.search(pattern, value, re.I) is not None:
return True
return False

@staticmethod
Expand Down
7 changes: 6 additions & 1 deletion tests/test_client_request_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ def test_request_log_exclusion(request_logger: RequestLogger, request_dict: Requ
response_dict["status_code"] = 404
request_logger.log_request(request_dict, response_dict)
assert len(request_logger.write_deque) == 1
response_dict["status_code"] = 200

response_dict["status_code"] = 200
request_dict["path"] = "/api/excluded"
request_logger.log_request(request_dict, response_dict)
assert len(request_logger.write_deque) == 1
Expand All @@ -131,6 +131,11 @@ def test_request_log_exclusion(request_logger: RequestLogger, request_dict: Requ
request_logger.log_request(request_dict, response_dict)
assert len(request_logger.write_deque) == 1

request_dict["path"] = "/"
request_dict["headers"] = [("User-Agent", "ELB-HealthChecker/2.0")]
request_logger.log_request(request_dict, response_dict)
assert len(request_logger.write_deque) == 1


def test_request_log_masking(request_logger: RequestLogger, request_dict: RequestDict, response_dict: ResponseDict):
from apitally.client.request_logging import BODY_MASKED, MASKED
Expand Down

0 comments on commit 4980efc

Please sign in to comment.