(self, record: logging.LogRecord)
| 247 | |
| 248 | class SensitiveDataFilter(logging.Filter): |
| 249 | def filter(self, record: logging.LogRecord) -> bool: |
| 250 | # Gather sensitive values which should not ever appear in the logs. |
| 251 | sensitive_values = [] |
| 252 | for key, value in os.environ.items(): |
| 253 | key_upper = key.upper() |
| 254 | if ( |
| 255 | len(value) > 2 |
| 256 | and value != 'default' |
| 257 | and any(s in key_upper for s in ('SECRET', '_KEY', '_CODE', '_TOKEN')) |
| 258 | ): |
| 259 | sensitive_values.append(value) |
| 260 | |
| 261 | # Replace sensitive values from env! |
| 262 | msg = record.getMessage() |
| 263 | for sensitive_value in sensitive_values: |
| 264 | msg = msg.replace(sensitive_value, '******') |
| 265 | |
| 266 | # Replace obvious sensitive values from log itself... |
| 267 | sensitive_patterns = [ |
| 268 | 'api_key', |
| 269 | 'aws_access_key_id', |
| 270 | 'aws_secret_access_key', |
| 271 | 'e2b_api_key', |
| 272 | 'github_token', |
| 273 | 'jwt_secret', |
| 274 | 'modal_api_token_id', |
| 275 | 'modal_api_token_secret', |
| 276 | 'llm_api_key', |
| 277 | 'sandbox_env_github_token', |
| 278 | 'runloop_api_key', |
| 279 | 'daytona_api_key', |
| 280 | ] |
| 281 | |
| 282 | # add env var names |
| 283 | env_vars = [attr.upper() for attr in sensitive_patterns] |
| 284 | sensitive_patterns.extend(env_vars) |
| 285 | |
| 286 | for attr in sensitive_patterns: |
| 287 | pattern = rf"{attr}='?([\w-]+)'?" |
| 288 | msg = re.sub(pattern, f"{attr}='******'", msg) |
| 289 | |
| 290 | # Apply SDK redaction utils to catch API key literals (e.g. sk_live_, |
| 291 | # sk-proj-, ghp_, etc.) and secret dict patterns (e.g. 'GITHUB_TOKEN': |
| 292 | # '...') that the pattern-based filter above does not cover. |
| 293 | msg = redact_api_key_literals(msg) |
| 294 | msg = redact_text_secrets(msg) |
| 295 | |
| 296 | # Update the record |
| 297 | record.msg = msg |
| 298 | record.args = () |
| 299 | |
| 300 | return True |
| 301 | |
| 302 | |
| 303 | def get_console_handler(log_level: int = logging.INFO) -> logging.StreamHandler: |
no outgoing calls