rusackas commented on code in PR #36783:
URL: https://github.com/apache/superset/pull/36783#discussion_r3408498280


##########
tests/unit_tests/models/test_core_database_password_encoding.py:
##########
@@ -0,0 +1,193 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+"""Unit tests for database password encoding in sqlalchemy_uri_decrypted."""
+
+from __future__ import annotations
+
+from unittest.mock import MagicMock, patch
+
+import pytest
+from sqlalchemy.engine.url import make_url
+
+from superset.models.core import Database
+
+
+def _create_db_with_password(password: str | None) -> Database:
+    """Helper to create a Database with a password."""
+    return Database(
+        database_name="test_db",
+        sqlalchemy_uri="postgresql://user@localhost:5432/testdb",
+        password=password,
+    )
+
+
+@patch("superset.models.core.has_app_context")
+def test_password_with_percent_literal(mock_has_app_context: MagicMock) -> 
None:
+    """Test password with literal percent character."""
+    mock_has_app_context.return_value = False
+    password = "pass%123"  # noqa: S105
+
+    db = _create_db_with_password(password)
+    decrypted_uri = db.sqlalchemy_uri_decrypted
+    parsed = make_url(decrypted_uri)
+
+    assert parsed.password == password
+
+
+@patch("superset.models.core.has_app_context")
+def test_password_with_single_percent(mock_has_app_context: MagicMock) -> None:
+    """Test password with single percent."""
+    mock_has_app_context.return_value = False
+    password = "%"  # noqa: S105
+
+    db = _create_db_with_password(password)
+    decrypted_uri = db.sqlalchemy_uri_decrypted
+    parsed = make_url(decrypted_uri)
+
+    assert parsed.password == password
+
+
+@patch("superset.models.core.has_app_context")
[email protected](
+    "password",
+    [
+        "p@ss!word",  # noqa: S105
+        "pass#word",  # noqa: S105
+        "pass&word",  # noqa: S105
+        "pass:word",  # noqa: S105
+        "pass/word",  # noqa: S105
+        "pass?word",  # noqa: S105
+        "pass=word",  # noqa: S105
+        "p@ss%w0rd",  # noqa: S105
+        "p@ss%25",  # noqa: S105
+    ],

Review Comment:
   Good call, added `pass word`, `pass\word`, and `pässwörd` to the 
parametrized cases. All roundtrip cleanly.



##########
superset/models/core.py:
##########
@@ -1149,20 +1150,35 @@ def get_schema_access_for_file_upload(  # pylint: 
disable=invalid-name
 
     @property
     def sqlalchemy_uri_decrypted(self) -> str:
+        """Return the decrypted SQLAlchemy URI with properly encoded 
password."""
         try:
             conn = make_url_safe(self.sqlalchemy_uri)
         except DatabaseInvalidError:
             # if the URI is invalid, ignore and return a placeholder url
             # (so users see 500 less often)
             return "dialect://invalid_uri"
+
+        # Determine plaintext password from config or model
         if has_app_context():
-            if custom_password_store := 
app.config["SQLALCHEMY_CUSTOM_PASSWORD_STORE"]:
-                conn = conn.set(password=custom_password_store(conn))
+            custom_password_store = 
app.config.get("SQLALCHEMY_CUSTOM_PASSWORD_STORE")
+            if custom_password_store and callable(custom_password_store):
+                raw_password = custom_password_store(conn)
             else:
-                conn = conn.set(password=self.password)
+                raw_password = self.password
+        else:
+            raw_password = self.password
+
+        # Encode the password such that special characters
+        # are preserved when rendering to string and reparsing the URL.
+        if raw_password is not None:
+            encoded_password = quote(raw_password, safe="")
+            conn = conn.set(password=encoded_password)

Review Comment:
   I dont think this double-encodes. With SQLAlchemy 1.4, `render_as_string` 
does not re-escape a password set via `URL.set`, so `quote()`-then-render 
reparses back to the exact original (the `test_password_with_percent_literal` / 
`test_roundtrip_invariant` cases cover this). Setting the raw password directly 
is what loses the `%` on reparse, which is the bug this PR fixes.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to