2020import contextvars
2121import logging
2222import os
23+ import re
2324import sys
2425
2526import httpx
4950API_KEY = os .environ .get ("CONTRASTAPI_KEY" , "" )
5051TIMEOUT = 30.0
5152
53+ _LOG_SANITIZE = re .compile (
54+ r"/v1/(phone|email/mx|email/disposable|ip|domain|dns|whois|subdomains|certs|ssl|threat|tech|monitor|ioc|phishing|scan/headers|asn|password|archive|username|cve|cves|exploit|hash|epss)/[^/?]+"
55+ )
56+
57+
58+ def _safe_path (path : str ) -> str :
59+ """Redact PII from API paths for safe logging."""
60+ safe = re .sub (r"[\x00-\x1f\x7f]" , "" , path )
61+ return _LOG_SANITIZE .sub (r"/v1/\1/***" , safe )
62+
5263
5364def _headers () -> dict :
5465 h = {"Accept" : "application/json" }
@@ -62,6 +73,7 @@ def _headers() -> dict:
6273
6374
6475async def _get (path : str , params : dict | None = None ) -> dict | str :
76+ client_ip = _client_ip_var .get () or "unknown"
6577 async with httpx .AsyncClient () as client :
6678 try :
6779 resp = await client .get (
@@ -71,14 +83,18 @@ async def _get(path: str, params: dict | None = None) -> dict | str:
7183 headers = _headers (),
7284 )
7385 resp .raise_for_status ()
86+ logger .info ("mcp_tool GET %s %d %s" , _safe_path (path ), resp .status_code , client_ip )
7487 return resp .json ()
7588 except httpx .HTTPStatusError as e :
89+ logger .info ("mcp_tool GET %s %d %s" , _safe_path (path ), e .response .status_code , client_ip )
7690 return f"Error { e .response .status_code } "
7791 except httpx .HTTPError as e :
92+ logger .info ("mcp_tool GET %s err %s" , _safe_path (path ), client_ip )
7893 return f"Request failed: { e } "
7994
8095
8196async def _post (path : str , json_body : dict ) -> dict | str :
97+ client_ip = _client_ip_var .get () or "unknown"
8298 async with httpx .AsyncClient () as client :
8399 try :
84100 resp = await client .post (
@@ -88,10 +104,13 @@ async def _post(path: str, json_body: dict) -> dict | str:
88104 headers = _headers (),
89105 )
90106 resp .raise_for_status ()
107+ logger .info ("mcp_tool POST %s %d %s" , _safe_path (path ), resp .status_code , client_ip )
91108 return resp .json ()
92109 except httpx .HTTPStatusError as e :
110+ logger .info ("mcp_tool POST %s %d %s" , _safe_path (path ), e .response .status_code , client_ip )
93111 return f"Error { e .response .status_code } "
94112 except httpx .HTTPError as e :
113+ logger .info ("mcp_tool POST %s err %s" , _safe_path (path ), client_ip )
95114 return f"Request failed: { e } "
96115
97116
0 commit comments