Skip to content

Sanitize error responses to prevent internal detail leakage#70

Merged
7tg merged 1 commit intomainfrom
fix/sanitize-error-responses
Feb 8, 2026
Merged

Sanitize error responses to prevent internal detail leakage#70
7tg merged 1 commit intomainfrom
fix/sanitize-error-responses

Conversation

@7tg
Copy link
Owner

@7tg 7tg commented Feb 8, 2026

Summary

  • Add safe_error_message() utility that maps known Django exception types (IntegrityError, FieldError, OperationalError, ValidationError, ValueError, TypeError) to generic client-safe messages while logging the real exception server-side
  • Replace all 16 instances of raw str(e) in handler error responses across crud.py, actions.py, relations.py, and meta.py
  • Add sanitize_pydantic_errors() to strip input, ctx, and url fields from Pydantic validation errors in views.py
  • Add 11 new tests verifying that each exception type returns its safe message, real exceptions are logged, and Pydantic error sanitization works correctly

What could leak before this fix

  • IntegrityError → table/column names, constraint names
  • FieldError → valid field names and model schema
  • OperationalError → database host/connection info
  • FileNotFoundError → internal file paths
  • Pydantic e.errors() → internal field types, input values, context

Test plan

  • All 372 tests pass (361 existing + 11 new)
  • safe_error_message maps each exception type to its generic message
  • Real exception details are logged via logger.exception()
  • sanitize_pydantic_errors strips input, ctx, url fields
  • No raw str(e) or unsanitized e.errors() remain in source
  • Pre-commit hooks pass (ruff, ruff-format, mypy, djlint)

@7tg 7tg force-pushed the fix/sanitize-error-responses branch from 2e5a25f to 30f98ab Compare February 8, 2026 05:54
@codecov
Copy link

codecov bot commented Feb 8, 2026

Codecov Report

❌ Patch coverage is 85.71429% with 6 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
django_admin_mcp/views.py 40.00% 3 Missing ⚠️
django_admin_mcp/handlers/actions.py 60.00% 2 Missing ⚠️
django_admin_mcp/handlers/crud.py 80.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@7tg 7tg force-pushed the fix/sanitize-error-responses branch 2 times, most recently from 4354507 to 8ccbb10 Compare February 8, 2026 06:00
Replace all raw str(e) exception messages in API responses with
safe_error_message() that maps known exception types to generic
messages and logs the real error server-side. Also sanitize Pydantic
validation errors in views.py to strip input, ctx, and url fields.
@7tg 7tg force-pushed the fix/sanitize-error-responses branch from 8ccbb10 to f7ff7c6 Compare February 8, 2026 06:05
@7tg 7tg merged commit 09e7a12 into main Feb 8, 2026
16 of 17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant