Harden auth and security controls with session auth and docs
This commit is contained in:
@@ -6,10 +6,13 @@ from uuid import UUID
|
||||
from sqlalchemy import delete, func, select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.config import get_settings
|
||||
from app.models.document import Document
|
||||
from app.models.processing_log import ProcessingLogEntry
|
||||
|
||||
|
||||
settings = get_settings()
|
||||
|
||||
MAX_STAGE_LENGTH = 64
|
||||
MAX_EVENT_LENGTH = 256
|
||||
MAX_LEVEL_LENGTH = 16
|
||||
@@ -37,9 +40,49 @@ def _trim(value: str | None, max_length: int) -> str | None:
|
||||
|
||||
|
||||
def _safe_payload(payload_json: dict[str, Any] | None) -> dict[str, Any]:
|
||||
"""Ensures payload values are persisted as dictionaries."""
|
||||
"""Normalizes payload persistence mode using metadata-only defaults for sensitive content."""
|
||||
|
||||
return payload_json if isinstance(payload_json, dict) else {}
|
||||
if not isinstance(payload_json, dict):
|
||||
return {}
|
||||
if settings.processing_log_store_payload_text:
|
||||
return payload_json
|
||||
return _metadata_only_payload(payload_json)
|
||||
|
||||
|
||||
def _metadata_only_payload(payload_json: dict[str, Any]) -> dict[str, Any]:
|
||||
"""Converts payload content into metadata descriptors without persisting raw text values."""
|
||||
|
||||
metadata: dict[str, Any] = {}
|
||||
for index, (raw_key, raw_value) in enumerate(payload_json.items()):
|
||||
if index >= 80:
|
||||
break
|
||||
key = str(raw_key)
|
||||
metadata[key] = _metadata_only_payload_value(raw_value)
|
||||
return metadata
|
||||
|
||||
|
||||
def _metadata_only_payload_value(value: Any) -> Any:
|
||||
"""Converts one payload value into non-sensitive metadata representation."""
|
||||
|
||||
if isinstance(value, dict):
|
||||
return _metadata_only_payload(value)
|
||||
if isinstance(value, (list, tuple)):
|
||||
items = list(value)
|
||||
return {
|
||||
"item_count": len(items),
|
||||
"items_preview": [_metadata_only_payload_value(item) for item in items[:20]],
|
||||
}
|
||||
if isinstance(value, str):
|
||||
normalized = value.strip()
|
||||
return {
|
||||
"text_chars": len(normalized),
|
||||
"text_omitted": bool(normalized),
|
||||
}
|
||||
if isinstance(value, bytes):
|
||||
return {"binary_bytes": len(value)}
|
||||
if isinstance(value, (int, float, bool)) or value is None:
|
||||
return value
|
||||
return {"value_type": type(value).__name__}
|
||||
|
||||
|
||||
def set_processing_log_autocommit(session: Session, enabled: bool) -> None:
|
||||
@@ -82,8 +125,8 @@ def log_processing_event(
|
||||
document_filename=_trim(resolved_document_filename, MAX_DOCUMENT_FILENAME_LENGTH),
|
||||
provider_id=_trim(provider_id, MAX_PROVIDER_LENGTH),
|
||||
model_name=_trim(model_name, MAX_MODEL_LENGTH),
|
||||
prompt_text=_trim(prompt_text, MAX_PROMPT_LENGTH),
|
||||
response_text=_trim(response_text, MAX_RESPONSE_LENGTH),
|
||||
prompt_text=_trim(prompt_text, MAX_PROMPT_LENGTH) if settings.processing_log_store_model_io_text else None,
|
||||
response_text=_trim(response_text, MAX_RESPONSE_LENGTH) if settings.processing_log_store_model_io_text else None,
|
||||
payload_json=_safe_payload(payload_json),
|
||||
)
|
||||
session.add(entry)
|
||||
|
||||
Reference in New Issue
Block a user