@@ -92,11 +92,71 @@ def _should_log(cls, level: int) -> bool:
9292 """Check if message should be logged based on level."""
9393 return cls ._enabled and level >= cls ._log_level
9494
95+ @classmethod
96+ def _sanitize_message (cls , message : str ) -> str :
97+ """
98+ Best-effort sanitization to avoid logging sensitive data in clear text.
99+
100+ Currently masks:
101+ - Known wpa-sec API key from Configuration.wpasec_api_key
102+ - Command-line API key arguments like "-k <value>" and "--key <value>"
103+ - MAC addresses in standard hex notation (aa:bb:cc:dd:ee:ff)
104+ """
105+ try :
106+ # Import lazily to avoid circular imports during module initialization
107+ from ..config import Configuration # type: ignore
108+ except Exception :
109+ Configuration = None # type: ignore
110+
111+ sanitized = message
112+
113+ # Mask configured wpa-sec API key if present in message
114+ try :
115+ if Configuration is not None and getattr (Configuration , "wpasec_api_key" , None ):
116+ api_key = Configuration .wpasec_api_key
117+ if isinstance (api_key , str ) and api_key :
118+ masked_key = api_key [:4 ] + "*" * (len (api_key ) - 4 ) if len (api_key ) > 4 else "****"
119+ sanitized = sanitized .replace (api_key , masked_key )
120+ except Exception :
121+ # Never let sanitization break logging
122+ pass
123+
124+ # Mask common CLI key patterns: "-k <value>" and "--key <value>"
125+ try :
126+ import re
127+
128+ def _mask_cli_key (match ):
129+ flag = match .group (1 )
130+ return f"{ flag } ****"
131+
132+ sanitized = re .sub (r"(-k)\s+\S+" , _mask_cli_key , sanitized )
133+ sanitized = re .sub (r"(--key)\s+\S+" , _mask_cli_key , sanitized )
134+ except Exception :
135+ pass
136+
137+ # Mask MAC addresses: aa:bb:cc:dd:ee:ff -> aa:bb:cc:**:**:**
138+ try :
139+ import re
140+
141+ def _mask_mac (match ):
142+ full = match .group (0 )
143+ parts = full .split (":" )
144+ if len (parts ) == 6 :
145+ return ":" .join (parts [:3 ] + ["**" , "**" , "**" ])
146+ return full
147+
148+ sanitized = re .sub (r"\b([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}\b" , _mask_mac , sanitized )
149+ except Exception :
150+ pass
151+
152+ return sanitized
153+
95154 @classmethod
96155 def _format_message (cls , level : str , module : str , message : str ) -> str :
97156 """Format log message with timestamp and level."""
98157 timestamp = datetime .now ().strftime ('%Y-%m-%d %H:%M:%S' )
99- return f"[{ timestamp } ] [{ level :8s} ] [{ module :20s} ] { message } "
158+ safe_message = cls ._sanitize_message (message )
159+ return f"[{ timestamp } ] [{ level :8s} ] [{ module :20s} ] { safe_message } "
100160
101161 @classmethod
102162 def _write_to_file (cls , formatted_message : str ):
0 commit comments