Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ This project adheres to `Semantic Versioning <https://semver.org/>`_.

Unreleased
===========================
* **Security fix.** ``DatabaseConnection.extras`` is free-form JSON merged into the connection
settings *after* the computed ones, so for an uploaded connection it could override ``NAME`` and
point the connection at another user's database file, or replace the ``USER``/``PASSWORD``/``HOST``
that were decrypted from the model. ``ENGINE``, and for uploads ``NAME``/``HOST``/``USER``/``PASSWORD``,
can no longer be set through ``extras``; a warning is logged when they are ignored.

* `#722`_: The query list (home page) no longer loads the entire query log history on every view. Run counts and
last runs are computed in SQL, so the page stays fast as logs accumulate (300k logs: ~9.7s -> ~0.9s). The query
list and query log pages also no longer run an extra database query per row.
Expand Down
24 changes: 23 additions & 1 deletion explorer/ee/db_connections/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def as_django_connection(self):

if self.extras:
extras_dict = json.loads(self.extras) if isinstance(self.extras, str) else self.extras
connection_settings.update(extras_dict)
connection_settings.update(self._allowed_extras(extras_dict))

try:
backend = load_backend(self.engine)
Expand All @@ -180,6 +180,28 @@ def as_django_connection(self):
except DatabaseError as e:
raise DatabaseError(f"Failed to create explorer connection: {e}") from e

def _allowed_extras(self, extras_dict):
"""
Drop the keys that would re-point this connection at the wrong database.

``extras`` is free-form JSON entered by whoever manages the connection, and it is
merged *after* the computed settings. For an upload-backed connection that means
``NAME`` would win over the tenant's own local copy of their uploaded SQLite file,
letting one user read another user's database by naming its path -- or point
``HOST``/``USER``/``PASSWORD`` somewhere unrelated, since those are decrypted from
the encrypted model fields into the settings above.
"""
pinned = {"ENGINE"}
if self.is_upload:
pinned |= {"NAME", "HOST", "USER", "PASSWORD"}
ignored = pinned & set(extras_dict)
if ignored:
logger.warning(
f"Ignoring {sorted(ignored)} in extras for connection '{self.alias}': "
f"these are managed by Explorer."
)
return {k: v for k, v in extras_dict.items() if k not in pinned}

def save(self, *args, **kwargs):
# If this instance is marked as default, unset the default on all other instances
if self.default:
Expand Down
58 changes: 58 additions & 0 deletions explorer/tests/test_db_connection_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,64 @@ def test_create_django_style_connection_with_extras(self, mock_load_backend):
self.assertEqual(args[0]["sslmode"], "require")
self.assertEqual(args[0]["connect_timeout"], 10)

@patch("explorer.ee.db_connections.models.load_backend")
@patch("explorer.utils.get_s3_bucket")
def test_upload_connection_name_cannot_be_overridden_by_extras(self, mock_get_s3_bucket, mock_load_backend):
# An upload's NAME is the tenant's own local copy of their file. extras is merged
# after the computed settings, so it must not be able to re-point it.
mock_load_backend.return_value = MagicMock()
conn = DatabaseConnection(
name="mine_1.db",
alias="mine",
engine=DatabaseConnection.SQLITE,
host="user_dbs/user_1/mine_1.db",
extras='{"NAME": "/tmp/other_tenant.db"}'
)
conn.download_sqlite_if_needed = lambda: None

conn.as_django_connection()

settings_dict = mock_load_backend.return_value.DatabaseWrapper.call_args[0][0]
self.assertEqual(settings_dict["NAME"], conn.local_name)
self.assertNotIn("other_tenant", settings_dict["NAME"])

@patch("explorer.ee.db_connections.models.load_backend")
@patch("explorer.utils.get_s3_bucket")
def test_upload_connection_credentials_cannot_be_overridden_by_extras(self, mock_get_s3_bucket, mock_load_backend):
mock_load_backend.return_value = MagicMock()
conn = DatabaseConnection(
name="mine_1.db",
alias="mine",
engine=DatabaseConnection.SQLITE,
host="user_dbs/user_1/mine_1.db",
user="real_user",
extras='{"USER": "someone_else", "PASSWORD": "stolen", "HOST": "evil.example.com"}'
)
conn.download_sqlite_if_needed = lambda: None

conn.as_django_connection()

settings_dict = mock_load_backend.return_value.DatabaseWrapper.call_args[0][0]
self.assertEqual(settings_dict["USER"], "real_user")
self.assertIsNone(settings_dict["HOST"])
self.assertNotEqual("stolen", settings_dict["PASSWORD"])

@patch("explorer.ee.db_connections.models.load_backend")
def test_extras_still_apply_to_non_upload_connections(self, mock_load_backend):
# Something like OPTIONS is legitimate and must keep working.
mock_load_backend.return_value = MagicMock()
conn = DatabaseConnection(
name="mydb",
alias="mydb",
engine="django.db.backends.postgresql",
extras='{"OPTIONS": {"sslmode": "require"}}'
)

conn.as_django_connection()

options = mock_load_backend.return_value.DatabaseWrapper.call_args[0][0]["OPTIONS"]
self.assertEqual(options, {"sslmode": "require"})


@skipIf(not EXPLORER_USER_UPLOADS_ENABLED, "User uploads not enabled")
class TestPandasToSQLite(TestCase):
Expand Down
Loading