[BUG] PasswordMasker's urlparse fallback drops the query/fragment and invents credentials
What is wrong
PasswordMasker has two ways to mask a database URL. When SQLAlchemy cannot build an engine for the URL - no driver installed for that dialect is enough - it falls back to _mask_db_url_no_sa (great_expectations/data_context/util.py:260-283), which re-assembles the URL from the parsed components it happens to name:
masked_url = (
f"{parsed_url.scheme}://{parsed_url.username}:{cls.MASKED_PASSWORD_STRING}"
f"@{parsed_url.hostname}{colon}{parsed_url.port or ''}{parsed_url.path or ''}"
)Everything that is not in that list is discarded, and any component that is absent is stringified as None. Measured on develop (8bfb2b2), Python 3.11.15, SQLAlchemy 2.0.54, alongside what SQLAlchemy itself renders for the same URL:
input : mssql+pyodbc://scott:tiger@h:1433/db?driver=ODBC+Driver+17&TrustServerCertificate=yes
sqlalchemy: mssql+pyodbc://scott:***@h:1433/db?TrustServerCertificate=yes&driver=ODBC+Driver+17
fallback : mssql+pyodbc://scott:***@h:1433/db <- the driver is gone
input : postgresql://scott:tiger@h:5432/db?sslmode=require
sqlalchemy: postgresql://scott:***@h:5432/db?sslmode=require
fallback : postgresql://scott:***@h:5432/db <- sslmode=require is gone
input : bigquery://my-project/dataset
sqlalchemy: bigquery://my-project/dataset
fallback : bigquery://None:***@my-project/dataset <- invents a masked password
input : postgresql://scott@h:5432/db
sqlalchemy: postgresql://scott@h:5432/db
fallback : postgresql://scott:***@h:5432/db <- claims a password there isn't one
input : postgresql://scott:tiger@[::1]:5432/db?sslmode=require
sqlalchemy: postgresql://scott:***@[::1]:5432/db?sslmode=require
fallback : postgresql://scott:***@::1:5432/db <- brackets lost, host/port ambiguous
input : mysql+pymysql://scott:tiger@/db?unix_socket=/tmp/mysql.sock
sqlalchemy: mysql+pymysql://scott:***@/db?unix_socket=%2Ftmp%2Fmysql.sock
fallback : mysql+pymysql://scott:***@None/db <- empty host becomes "None"
input : postgresql://scott:tiger@h:5432/db#section
sqlalchemy: postgresql://scott:***@h:5432/db#section
fallback : postgresql://scott:***@h:5432/dbThe masked string is what ends up in log records, in the exception_traceback of a failed Validation, and in a serialized DataContext, so the practical harm is diagnosis rather than a leak: the password is still replaced in every case above. But a URL that silently drops ?driver= or ?sslmode=require describes a different connection than the one configured, and "no credentials at all" being rendered as None:***@ misstates whether a secret exists.
The repo already states the intended property, in tests/data_context/test_data_context_utils.py:69: "The output of mask_db_url should be the same whether user_urlparse is set to True or False." For any URL with a query string that cannot hold today, because only one of the two paths keeps the query.
Reproduction
from great_expectations.data_context.util import PasswordMasker
PasswordMasker.mask_db_url(
"mssql+pyodbc://scott:tiger@h:1433/db?driver=ODBC+Driver+17&TrustServerCertificate=yes",
use_urlparse=True,
)
# 'mssql+pyodbc://scott:***@h:1433/db'
PasswordMasker.mask_db_url("bigquery://my-project/dataset", use_urlparse=True)
# 'bigquery://None:***@my-project/dataset'use_urlparse=True forces the branch; it is also taken automatically whenever sa.create_engine(url) raises, which is the ordinary situation for a dialect whose DBAPI is not installed in the process that happens to be serializing the config.
Expected behaviour
Replace the password and nothing else: keep the query, the fragment, the host exactly as written (including IPv6 brackets), and add no credentials that the URL did not carry.
Source: fivetran/great_expectations