codeant-ai-for-open-source[bot] commented on code in PR #45890:
URL: https://github.com/apache/superset/pull/45890#discussion_r4444703419


##########
superset/utils/json.py:
##########
@@ -362,6 +367,32 @@ def get_masked_fields(
     return masked
 
 
+def unrevealable_masked_fields(
+    old_payload: dict[str, Any] | None,
+    new_payload: dict[str, Any],
+    sensitive_fields: set[str],
+) -> list[str]:
+    """
+    Returns masked fields in ``new_payload`` that ``old_payload`` cannot 
reveal.
+
+    :func:`reveal_sensitive` swaps a mask back for the stored value at the same
+    path. Where the stored payload has no value there, the mask would be kept 
and
+    stored as if it were the secret, so callers that must not do that (imports
+    overwriting a connection) use this to ask for the real value instead.
+
+    :param old_payload: The stored payload, if any
+    :param new_payload: The incoming payload, possibly containing masks
+    :param sensitive_fields: The set of fields to check, as JSONPath 
expressions
+    :returns: JSONPath expressions for masked fields with nothing to reveal
+    """
+    return [
+        field
+        for field in get_masked_fields(new_payload, sensitive_fields)
+        if old_payload is None
+        or not any(match.value for match in parse(field).find(old_payload))

Review Comment:
   ✅ **CodeAnt verified this suggestion was addressed in subsequent commits and 
marked this thread resolved** as of `58b12b1`.
   
   The check now treats only `None`, an empty string, or `PASSWORD_MASK` as 
missing, so stored `0` and `False` values count as revealable.
   
   <sub>If that's not right, unresolve this thread and CodeAnt will leave it 
open.</sub>
   
   <!-- codeant-auto-resolve-reply -->



##########
superset/databases/utils.py:
##########
@@ -160,3 +162,86 @@ def make_url_safe(raw_url: str | URL) -> URL:
 
     else:
         return raw_url
+
+
+def _is_present(value: Any) -> bool:
+    """Is a credential field actually filled in? Whitespace does not count."""
+    if isinstance(value, str):
+        return bool(value.strip())
+    return bool(value)
+
+
+def validated_embedded_credentials(credentials: Any) -> dict[str, str] | None:
+    """
+    Return a complete embedded fallback credential, or ``None``.
+
+    The canonical shape is ``{username, password}`` or ``{username, 
auth_method:
+    "keypair", privatekey_body, privatekey_pass}``; a half-filled credential is
+    ignored rather than half-applied. Engines may override this default through
+    ``BaseEngineSpec.validate_embedded_credentials`` for both query-time and
+    authoring-time validation.
+    """
+    if not isinstance(credentials, dict):
+        return None
+
+    username = credentials.get("username")
+    if not isinstance(username, str) or not _is_present(username):
+        return None
+
+    auth_method = credentials.get("auth_method", "password")
+    if auth_method == "keypair":
+        privatekey_body = credentials.get("privatekey_body")
+        privatekey_pass = credentials.get("privatekey_pass", "")
+        if (
+            not isinstance(privatekey_body, str)
+            or not privatekey_body
+            or not isinstance(privatekey_pass, str)
+        ):

Review Comment:
   ✅ **CodeAnt verified this suggestion was addressed in subsequent commits and 
marked this thread resolved** as of `58b12b1`.
   
   Key-pair credentials now validate `privatekey_body` with `_is_present`, 
which rejects whitespace-only strings.
   
   <sub>If that's not right, unresolve this thread and CodeAnt will leave it 
open.</sub>
   
   <!-- codeant-auto-resolve-reply -->



##########
superset/databases/schemas.py:
##########
@@ -1146,25 +1236,37 @@ def validate_masked_encrypted_extra(
                 "File contains both `encrypted_extra` and 
`masked_encrypted_extra`"
             )
 
-        if db.session.query(Database).filter_by(uuid=data["uuid"]).first():
-            # Existing DB: sensitive values will be revealed from existing
-            # encrypted_extra in import_database()
-            return
-
         masked_encrypted_extra = json.loads(data["masked_encrypted_extra"])
 
-        # Determine engine spec from sqlalchemy_uri to get sensitive fields
-        sqlalchemy_uri = data["sqlalchemy_uri"]
-        url = make_url_safe(sqlalchemy_uri)
-        backend = url.get_backend_name()
-        driver = url.get_driver_name()
-        db_engine_spec = get_engine_spec(backend, driver=driver)
-
-        # Check if any sensitive field is still masked
-        masked_fields = get_masked_fields(
-            masked_encrypted_extra,
-            db_engine_spec.encrypted_extra_sensitive_field_paths(),
-        )
+        if existing := 
db.session.query(Database).filter_by(uuid=data["uuid"]).first():
+            if not self.overwrites_existing:
+                return
+            # Existing DB: masked values are revealed from its stored
+            # encrypted_extra in import_database(). Any the stored payload has 
no
+            # value for -- e.g. a connection promoted without its OAuth2 
client or
+            # embedded credential -- would otherwise be stored as the mask 
itself,
+            # so ask for those like a new DB.
+            db_engine_spec = existing.db_engine_spec
+            masked_fields = json.unrevealable_masked_fields(
+                json.loads(existing.encrypted_extra)
+                if existing.encrypted_extra
+                else None,
+                masked_encrypted_extra,
+                db_engine_spec.encrypted_extra_sensitive_field_paths(),

Review Comment:
   ✅ **CodeAnt verified this suggestion was addressed in subsequent commits and 
marked this thread resolved** as of `58b12b1`.
   
   Validation now passes `None` as the old payload when the connection identity 
changed, so its stored secrets are not treated as reusable.
   
   <sub>If that's not right, unresolve this thread and CodeAnt will leave it 
open.</sub>
   
   <!-- codeant-auto-resolve-reply -->



-- 
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