Skip to content

Commit d343b44

Browse files
committed
Redact password from DatabaseSessionService engine-creation errors
DatabaseSessionService.__init__ embedded the raw db_url in three ValueError messages raised when engine creation fails (invalid URL argument, missing driver module, or any other error). A SQLAlchemy URL for a networked backend carries the password inline, e.g. postgresql+asyncpg://user:password@host/db, so any of these failures (for example a driver that is not installed) surfaced the database password verbatim in the exception and in any logs that record it. Render the URL with hide_password=True before including it in the messages, falling back to a redacted placeholder when the URL cannot be parsed. The messages keep the useful dialect/host/database context while no longer exposing the password.
1 parent 4cee97f commit d343b44

1 file changed

Lines changed: 20 additions & 3 deletions

File tree

src/google/adk/sessions/database_session_service.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,22 @@
7070

7171
logger = logging.getLogger("google_adk." + __name__)
7272

73+
74+
def _redact_db_url(db_url: str) -> str:
75+
"""Returns the database URL with any embedded password removed.
76+
77+
The engine-creation error messages below include the database URL to aid
78+
debugging, but a URL may embed credentials
79+
(e.g. ``postgresql://user:password@host/db``). Rendering it with
80+
``hide_password=True`` keeps the useful parts (dialect, host, database) while
81+
preventing the password from leaking into exceptions and logs.
82+
"""
83+
try:
84+
return make_url(db_url).render_as_string(hide_password=True)
85+
except Exception: # noqa: BLE001 - fall back to a fully redacted placeholder
86+
return "[redacted]"
87+
88+
7389
_STALE_SESSION_ERROR_MESSAGE = (
7490
"The session has been modified in storage since it was loaded. "
7591
"Please reload the session before appending more events."
@@ -284,16 +300,17 @@ def __init__(
284300
event.listen(db_engine.sync_engine, "connect", _set_sqlite_pragma)
285301

286302
except Exception as e:
303+
safe_db_url = _redact_db_url(db_url)
287304
if isinstance(e, ArgumentError):
288305
raise ValueError(
289-
f"Invalid database URL format or argument '{db_url}'."
306+
f"Invalid database URL format or argument '{safe_db_url}'."
290307
) from e
291308
if isinstance(e, ImportError):
292309
raise ValueError(
293-
f"Database related module not found for URL '{db_url}'."
310+
f"Database related module not found for URL '{safe_db_url}'."
294311
) from e
295312
raise ValueError(
296-
f"Failed to create database engine for URL '{db_url}'"
313+
f"Failed to create database engine for URL '{safe_db_url}'"
297314
) from e
298315
else:
299316
self._owns_db_engine = False

0 commit comments

Comments
 (0)