Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /SHAdd0WTAka_Zen-Ai-Pentest /api /security_logging.py
| """ | |
| Secure logging utilities to prevent log injection attacks | |
| """ | |
| import html | |
| def sanitize_for_log(value: str) -> str: | |
| """ | |
| Sanitize a string for safe logging. | |
| Prevents log injection by removing/replacing dangerous characters. | |
| """ | |
| if not isinstance(value, str): | |
| value = str(value) | |
| # Remove newlines and carriage returns (prevents log injection) | |
| value = value.replace("\n", "\\n").replace("\r", "\\r") | |
| # Remove null bytes | |
| value = value.replace("\x00", "") | |
| # Escape HTML to prevent XSS in log viewers | |
| value = html.escape(value) | |
| # Limit length to prevent log flooding | |
| if len(value) > 256: | |
| value = value[:253] + "..." | |
| return value | |
| def sanitize_path_component(value: str) -> str: | |
| """ | |
| Sanitize a path component to prevent path traversal. | |
| Removes any path separators and parent directory references. | |
| """ | |
| if not isinstance(value, str): | |
| value = str(value) | |
| # Remove path separators | |
| value = value.replace("/", "").replace("\\", "") | |
| # Remove parent directory references | |
| value = value.replace("..", "") | |
| # Remove null bytes | |
| value = value.replace("\x00", "") | |
| return value | |
| def format_log_message(template: str, **kwargs) -> str: | |
| """ | |
| Format a log message with sanitized values. | |
| Usage: logger.info(format_log_message("User {user} logged in", user=user_input)) | |
| """ | |
| sanitized = {k: sanitize_for_log(v) for k, v in kwargs.items()} | |
| return template.format(**sanitized) | |
Xet Storage Details
- Size:
- 1.51 kB
- Xet hash:
- 053439ef6da5ff1ea73a5dcae4473086bbb5aa8e9e60f55106aa12055ff3f67f
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.