Back to feed
Dev.to
Dev.to
6/29/2026
The Python exception that leaked tenant secrets

The Python exception that leaked tenant secrets

Short summary

A real production incident exposed how Python exceptions can silently leak secrets to logs when sensitive objects (with dataclass/Pydantic __repr__) are interpolated into exception messages. The author explains why this bypasses code review and provides three practical fixes: explicit __repr__ methods that exclude secrets, Pydantic's SecretStr for field-level masking, and Field(repr=False) for selective exclusion. Fix it at the object level to ensure secrets are never serialized, regardless of where they end up in logs.

  • Secrets leak to logs when sensitive objects are interpolated into exception messages via __repr__
  • The pattern bypasses code review because each piece (logging, sensitive data, exceptions) looks individually normal
  • Prevent it by giving sensitive models explicit __repr__ methods or using Pydantic's SecretStr field-level masking

Generated with AI, which can make mistakes.

Is this a good recommendation for you?

Comments

Failed to load comments. Please try again.

Explore more