alexandrusoare commented on code in PR #45451:
URL: https://github.com/apache/superset/pull/45451#discussion_r4324287294
##########
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:
Agreed — a hook returning the advertised Query would raise AttributeError in
ChartFilter.
##########
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:
Fair — the helper was tested but not the path through it. Added two tests on
_check_object_access: one asserting the refusal, the reason reaching the
caller, and the derived key string
##########
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:
Fixed — the return value is normalised to a subquery before reading its
column, and the type widened to Query | Select | Subquery.
--
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]