Skip to content

armgit / meet

Public Imported from GitHub
0

Open source video conferencing app powered by LiveKit. Built with Django and React.

🔒️(summary) add secret-aware validation errors

Florent Chehab2ea5c854367d
diff --git a/src/summary/summary/core/exceptions.py b/src/summary/summary/core/exceptions.py
new file mode 100644
index 00000000..70e0312d
--- /dev/null
+++ b/src/summary/summary/core/exceptions.py
@@ -0,0 +1,62 @@
+"""Validation errors suitable for logging and transport."""
+
+from typing import get_args
+
+from pydantic import SecretStr, ValidationError
+from pydantic_core import ErrorType, PydanticCustomError
+
+_BUILTIN_ERROR_TYPES = frozenset(get_args(ErrorType))
+
+
+def clean_validation_errors(errors: list[dict]) -> list[dict]:
+ """Mask SecretStr values, including those nested in validation input."""
+
+ def clean(value):
+ if isinstance(value, SecretStr):
+ return "[redacted]"
+ if isinstance(value, Exception):
+ # Do not retain validator exception objects, whose args may hold secrets.
+ return str(value)
+ if isinstance(value, dict):
+ return {key: clean(item) for key, item in value.items()}
+ if isinstance(value, list):
+ return [clean(item) for item in value]
+ if isinstance(value, tuple):
+ return tuple(clean(item) for item in value)
+ return value
+
+ return clean(errors)
+
+
+class CleanedValidationError(ValidationError):
+ """A new Pydantic exception with secret inputs replaced by a marker."""
+
+ def __init__(self, error, *args, secret_input=False, **kwargs):
+ """State is initialized by Pydantic during __new__."""
+
+ def __new__(cls, error, *args, secret_input=False, **kwargs):
+ """Accept an original error, or Pydantic's native construction arguments."""
+ if not isinstance(error, ValidationError):
+ # Pydantic uses this path when constructing or unpickling the error.
+ return super().__new__(cls, error, *args, **kwargs)
+
+ details = clean_validation_errors(error.errors(include_url=False))
+ for detail in details:
+ if secret_input:
+ # An adapter may have received the unwrapped value of a SecretStr.
+ detail["input"] = "[redacted]"
+ return cls._from_details(error.title, details)
+
+ @classmethod
+ def _from_details(cls, title, details):
+ """Reconstruct builtin and custom Pydantic error codes."""
+ for detail in details:
+ if detail["type"] not in _BUILTIN_ERROR_TYPES:
+ detail["type"] = PydanticCustomError(
+ detail["type"], detail["msg"], detail.get("ctx")
+ )
+ return cls.from_exception_data(title, details, hide_input=True)
+
+ def __reduce__(self):
+ """Preserve custom error codes when Celery pickles the exception."""
+ return self._from_details, (self.title, self.errors(include_url=False))
diff --git a/src/summary/tests/unit/test_validation_error_formatting.py b/src/summary/tests/unit/test_validation_error_formatting.py
new file mode 100644
index 00000000..78197cbc
--- /dev/null
+++ b/src/summary/tests/unit/test_validation_error_formatting.py
@@ -0,0 +1,26 @@
+"""Formatted validation errors hide input values, including after pickling."""
+
+import pickle
+import traceback
+
+from pydantic import ValidationError
+
+from summary.core.exceptions import CleanedValidationError
+
+CANARY = "private-validation-canary"
+
+
+def test_formatted_errors_hide_ordinary_input_after_pickle_roundtrip():
+ """Formatting hides raw input even when diagnostics intentionally retain it."""
+ original = ValidationError.from_exception_data(
+ "TestModel",
+ [{"type": "int_parsing", "loc": ("field",), "input": CANARY}],
+ )
+ cleaned = CleanedValidationError(original)
+ restored = pickle.loads(pickle.dumps(cleaned)) # noqa: S301 -- own test exception
+ for error in (cleaned, restored):
+ assert error.errors()[0]["input"] == CANARY
+ assert CANARY not in str(error)
+ assert CANARY not in repr(error)
+ assert CANARY not in "".join(traceback.format_exception(error))
+ assert error.errors()[0]["type"] == "int_parsing"
diff --git a/src/summary/tests/unit/test_validation_errors.py b/src/summary/tests/unit/test_validation_errors.py
new file mode 100644
index 00000000..bc187dfe
--- /dev/null
+++ b/src/summary/tests/unit/test_validation_errors.py
@@ -0,0 +1,112 @@
+"""Validation cleanup preserves diagnostics without retaining SecretStr values."""
+
+import json
+import pickle
+
+import pytest
+from celery.utils.serialization import get_pickleable_exception
+from pydantic import SecretStr, ValidationError
+from pydantic_core import PydanticCustomError
+
+from summary.core.exceptions import CleanedValidationError, clean_validation_errors
+
+CANARY = "private-validation-canary"
+
+
[email protected]("custom", [False, True])
+def test_cleaned_validation_error_preserves_details_and_pickles(custom):
+ """Builtin and custom validation errors retain their type, location and message."""
+ error_type = (
+ PydanticCustomError(
+ "custom_validation", "Invalid value: {reason}", {"reason": "test"}
+ )
+ if custom
+ else "int_type"
+ )
+ original = ValidationError.from_exception_data(
+ "TestModel",
+ [
+ {"type": error_type, "loc": ("secret",), "input": SecretStr(CANARY)},
+ {"type": "int_parsing", "loc": ("ordinary",), "input": "not-an-int"},
+ ],
+ )
+ cleaned = CleanedValidationError(original)
+ original_details = original.errors()
+ details = cleaned.errors()
+ assert isinstance(cleaned, ValidationError)
+ assert cleaned.title == original.title
+ assert cleaned.error_count() == original.error_count()
+ assert details[0] == {**original_details[0], "input": "[redacted]"}
+ assert details[1] == original_details[1]
+ assert isinstance(original.errors()[0]["input"], SecretStr)
+ assert CANARY not in str(cleaned)
+ assert CANARY not in cleaned.json()
+ serialized = pickle.dumps(cleaned)
+ assert CANARY.encode() not in serialized
+ restored = pickle.loads(serialized) # noqa: S301 -- own in-memory test exception
+ assert isinstance(restored, CleanedValidationError)
+ assert restored.errors() == details
+ assert isinstance(get_pickleable_exception(cleaned), CleanedValidationError)
+
+
+def test_cleanup_masks_nested_secrets_and_preserves_plain_input():
+ """Missing-field inputs keep ordinary values while masking nested secrets."""
+ original = ValidationError.from_exception_data(
+ "NestedModel",
+ [
+ {
+ "type": "missing",
+ "loc": ("required",),
+ "input": {
+ "ordinary": "keep me",
+ "nested": [SecretStr(CANARY), {"url": SecretStr(CANARY)}],
+ "tuple": (SecretStr(CANARY), 42),
+ },
+ }
+ ],
+ )
+ cleaned = CleanedValidationError(original)
+ assert cleaned.errors()[0]["input"] == {
+ "ordinary": "keep me",
+ "nested": ["[redacted]", {"url": "[redacted]"}],
+ "tuple": ("[redacted]", 42),
+ }
+ assert CANARY not in cleaned.json()
+ assert CANARY.encode() not in pickle.dumps(cleaned)
+
+
+def test_cleanup_does_not_retain_validator_exception_objects():
+ """Validator context keeps its readable message without secret-bearing args."""
+ original = ValidationError.from_exception_data(
+ "TestModel",
+ [
+ {
+ "type": "value_error",
+ "loc": (),
+ "input": SecretStr(CANARY),
+ "ctx": {"error": ValueError(SecretStr(CANARY))},
+ }
+ ],
+ )
+ cleaned = CleanedValidationError(original)
+ assert cleaned.errors()[0]["ctx"] == {"error": "**********"}
+ assert CANARY.encode() not in pickle.dumps(cleaned)
+
+
+def test_clean_validation_errors_keeps_all_other_fields():
+ """The API cleanup preserves diagnostics and nonsecret input."""
+ details = [
+ {
+ "type": "test",
+ "loc": ("body", "field"),
+ "msg": "Invalid value",
+ "url": "https://example.com/errors/test",
+ "ctx": {"limit": 3},
+ "input": "ordinary input",
+ }
+ ]
+ assert clean_validation_errors(details) == details
+ assert (
+ json.loads(json.dumps(clean_validation_errors(details)))[0]["input"]
+ == "ordinary input"
+ )