security: harden data auth and deployment
This commit is contained in:
@@ -2,6 +2,7 @@ import contextvars
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
from logging.handlers import RotatingFileHandler
|
||||
from typing import Any, Mapping, Optional
|
||||
from urllib.parse import parse_qs
|
||||
@@ -27,6 +28,9 @@ _SENSITIVE_KEYWORDS = (
|
||||
"token",
|
||||
)
|
||||
_MAX_BODY_BYTES = 4096
|
||||
_SENSITIVE_PATH_PATTERNS = (
|
||||
re.compile(r"(/auth/invites/)[^/]+", re.IGNORECASE),
|
||||
)
|
||||
|
||||
|
||||
class RequestContextFilter(logging.Filter):
|
||||
@@ -47,6 +51,13 @@ def current_request_id() -> str:
|
||||
return REQUEST_ID_CONTEXT.get("-")
|
||||
|
||||
|
||||
def sanitize_path(path: str) -> str:
|
||||
sanitized = str(path or "")
|
||||
for pattern in _SENSITIVE_PATH_PATTERNS:
|
||||
sanitized = pattern.sub(r"\1[REDACTED]", sanitized)
|
||||
return sanitized
|
||||
|
||||
|
||||
def _is_sensitive_key(key: str) -> bool:
|
||||
lowered = key.strip().lower()
|
||||
return any(marker in lowered for marker in _SENSITIVE_KEYWORDS)
|
||||
@@ -55,10 +66,7 @@ def _is_sensitive_key(key: str) -> bool:
|
||||
def _redact_scalar(value: Any) -> Any:
|
||||
if value is None or isinstance(value, (int, float, bool)):
|
||||
return value
|
||||
text = str(value)
|
||||
if len(text) <= 4:
|
||||
return "***"
|
||||
return f"{text[:2]}***{text[-2:]}"
|
||||
return "[REDACTED]"
|
||||
|
||||
|
||||
def sanitize_value(value: Any, *, key_hint: Optional[str] = None, depth: int = 0) -> Any:
|
||||
@@ -161,6 +169,10 @@ def configure_logging(
|
||||
backupCount=max(1, int(log_file_backup_count or 10)),
|
||||
encoding="utf-8",
|
||||
)
|
||||
try:
|
||||
os.chmod(log_path, 0o600)
|
||||
except OSError:
|
||||
pass
|
||||
handlers.append(file_handler)
|
||||
|
||||
context_filter = RequestContextFilter()
|
||||
|
||||
Reference in New Issue
Block a user