msyavuz commented on code in PR #45451:
URL: https://github.com/apache/superset/pull/45451#discussion_r4324109099


##########
superset/utils/filters.py:
##########
@@ -176,3 +177,105 @@ def guest_embedded_dashboard_filter() -> 
Optional[ColumnElement[bool]]:
         # dashboards, never a plain internal id.
         conditions.append(and_(Dashboard.id.in_(int_ids), 
Dashboard.embedded.any()))
     return or_(*conditions) if conditions else false()
+
+
+def apply_extra_access_exclusions(
+    query: "Query",
+    asset_kind: str,
+    id_column: "ColumnElement[Any]",
+) -> "Query":
+    """Withhold the asset IDs an ``EXTRA_ACCESS_QUERY_EXCLUSIONS`` hook names.
+
+    ``EXTRA_ACCESS_QUERY_FILTERS`` can only widen a result set -- its 
conditions
+    are OR'd with the role paths -- so a deployment with its own notion of a
+    withheld asset cannot express that through it. This is the matching AND 
NOT.
+
+    Returns ``query`` unchanged when no hook is configured or there is no
+    authenticated user, so the default build is unaffected.
+    """
+
+    clause = extra_access_exclusion_clause(asset_kind, id_column)
+    return query if clause is None else query.filter(clause)
+
+
+def extra_access_exclusion_clause(
+    asset_kind: str,
+    id_column: "ColumnElement[Any]",
+) -> Optional["ColumnElement[bool]"]:
+    """The ``NOT EXISTS`` clause for an exclusion hook, or None when inactive.
+
+    Deliberately ``NOT EXISTS`` rather than ``NOT IN``: a hook subquery that
+    yields a single NULL id makes ``NOT IN`` evaluate to NULL for every row,
+    which would silently empty the entire list -- for admins too. ``NOT 
EXISTS``
+    simply does not match a NULL, so a malformed hook withholds nothing instead
+    of everything.
+    """
+    hook, _reason = _extra_access_exclusion_entry(asset_kind)
+    if not hook:
+        return None
+
+    from superset.utils.core import get_user_id
+
+    user_id = get_user_id()
+    if not user_id:
+        return None
+
+    excluded = hook(user_id)
+    excluded_id = list(excluded.c)[0]

Review Comment:
   `list(excluded.c)[0]` only works when the hook returns a `.subquery()`, but 
`ExclusionSpec`/`ExclusionEntry` type the hook as `Callable[[int], Query]`, and 
the existing `EXTRA_ACCESS_QUERY_FILTERS` hooks get passed to `.in_()`, which 
takes a Query or a select. So a hook that returns a plain `Query` or `select()` 
raises AttributeError in `ChartFilter`/`DashboardAccessFilter`, and that breaks 
every chart or dashboard list for every user, admins included. Either normalise 
the return value (e.g. call `.subquery()` when it's missing) or fix the type 
and docs.



##########
superset/commands/report/base.py:
##########
@@ -142,6 +146,17 @@ def _check_object_access(
         if not obj:
             exceptions.append(not_found_exc())
         else:
+            # The related-field filter narrows the picker, but a list filter is
+            # not an authorisation boundary: ``find_by_id`` above applies the
+            # ordinary access filter, so an excluded id posted directly would
+            # otherwise be accepted. Ask the report-target question here, where
+            # the id is actually resolved.
+            exclusion_kind = f"report_target_{kind}s"
+            if is_excluded_by_extra_access(exclusion_kind, object_id):

Review Comment:
   This new 403 on report POST/PUT is the actual authorisation boundary from 
the earlier review thread, but the only test (`is_excluded_by_extra_access` in 
isolation) never goes through `_check_object_access`. A regression in the 
`f"report_target_{kind}s"` key mapping or the raise would not be caught.



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