You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by en...@apache.org on 2026/09/28 11:18:52 UTC

(superset) 01/01: fix(security): keep guest RLS on sub-queries beside an unaliased source

This is an automated email from the ASF dual-hosted git repository.

EnxDev pushed a commit to branch enxdev/fix/rls-unaliased-source-correlation
in repository https://gitbox.apache.org/repos/asf/superset.git

commit ff30c6e39bdb7d9f9e689a802e26975e4971886b
Author: Enzo Martellucci <en...@gmail.com>
AuthorDate: Mon Sep 28 13:18:25 2026 +0200

    fix(security): keep guest RLS on sub-queries beside an unaliased source
    
    An unaliased source in the outer query (a derived table without an alias)
    is keyed '' in Scope.selected_sources, the same as the table of an
    unqualified column. _is_correlated then matched every unqualified column
    against it, so any sub-query with one looked correlated and lost the
    global guest RLS rules. Only qualified columns are compared now.
    
    Follow-up to #44650.
    
    Co-Authored-By: Claude Opus 5.5 (1M context) <no...@anthropic.com>
---
 superset/sql/parse.py                       |  6 +++++-
 tests/unit_tests/security/guest_rls_test.py | 22 ++++++++++++++++++++++
 tests/unit_tests/sql/parse_tests.py         | 13 +++++++++++++
 3 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/superset/sql/parse.py b/superset/sql/parse.py
index d7287add830..0749a14b263 100644
--- a/superset/sql/parse.py
+++ b/superset/sql/parse.py
@@ -2880,7 +2880,10 @@ def _is_correlated(scope: Scope) -> bool:
     sub-query has no table of its own under that name. An unqualified column can't
     be told apart from one of the sub-query's own, so it is treated as local, which
     errs toward the sub-query getting the stricter rules. (``Scope``'s own
-    ``is_correlated_subquery`` treats every unqualified column as external.)
+    ``is_correlated_subquery`` treats every unqualified column as external.) An
+    unaliased source, such as a derived table without an alias, is keyed ``''`` in
+    ``Scope.selected_sources``, the same as an unqualified column's table, so the
+    column must be qualified for the names to be compared at all.
 
     Only the sub-query's own columns count, not those of a sub-query nested in it
     (which ``Scope.columns`` includes): a nested correlated sub-query doesn't key the
@@ -2905,6 +2908,7 @@ def _is_correlated(scope: Scope) -> bool:
     local = {name.lower() for name in scope.selected_sources}
     return any(
         isinstance(node, exp.Column)
+        and node.table
         and node.table.lower() in enclosing
         and node.table.lower() not in local
         for node in walk_in_scope(scope.expression)
diff --git a/tests/unit_tests/security/guest_rls_test.py b/tests/unit_tests/security/guest_rls_test.py
index 5b9b98839ef..89b510d5f61 100644
--- a/tests/unit_tests/security/guest_rls_test.py
+++ b/tests/unit_tests/security/guest_rls_test.py
@@ -506,6 +506,28 @@ def test_global_guest_rule_applied_to_virtual_dataset_subquery(
     )
 
 
+def test_global_guest_rule_applied_to_subquery_beside_unaliased_derived_table(
+    app: Flask,
+    mocker: MockerFixture,
+) -> None:
+    """
+    An unaliased derived table in the outer query doesn't make a sub-query with
+    unqualified columns look correlated, so the sub-query keeps the global guest
+    RLS rules.
+    """
+    sql = _apply_virtual_dataset_rls_as_guest(
+        mocker,
+        [GuestTokenRlsRule(dataset=None, clause="org_id = 1")],
+        "SELECT x, (SELECT COUNT(*) FROM b WHERE v > 0) AS n FROM (SELECT * FROM a)",
+    )
+
+    subquery, outer = sql.split(") AS n", 1)
+    assert "b.org_id = 1" in subquery, f"Sub-query is not scoped. Got: {sql}"
+    assert "org_id" not in outer, (
+        f"Rows reaching the outer query must be left to its own filter. Got: {sql}"
+    )
+
+
 def test_global_guest_rule_left_to_outer_query_for_correlated_subquery(
     app: Flask,
     mocker: MockerFixture,
diff --git a/tests/unit_tests/sql/parse_tests.py b/tests/unit_tests/sql/parse_tests.py
index 4b822da4fae..616cb802905 100644
--- a/tests/unit_tests/sql/parse_tests.py
+++ b/tests/unit_tests/sql/parse_tests.py
@@ -6780,6 +6780,19 @@ def _compact_sql(statement: SQLStatement) -> str:
             "(id = bid)) AS n FROM a WHERE a.r = 1",
             id="unqualified-reference",
         ),
+        pytest.param(
+            "SELECT x, (SELECT COUNT(*) FROM b WHERE v > 0) AS n "
+            "FROM (SELECT * FROM a)",
+            "SELECT x, (SELECT COUNT(*) FROM b WHERE b.r = 1 AND b.g = 1 AND "
+            "(v > 0)) AS n FROM (SELECT * FROM a WHERE a.r = 1)",
+            id="unqualified-reference-beside-unaliased-derived-table",
+        ),
+        pytest.param(
+            "SELECT * FROM (SELECT * FROM a) WHERE k IN (SELECT k FROM b)",
+            "SELECT * FROM (SELECT * FROM a WHERE a.r = 1) WHERE k IN "
+            "(SELECT k FROM b WHERE b.r = 1 AND b.g = 1)",
+            id="in-subquery-beside-unaliased-derived-table",
+        ),
         pytest.param(
             "SELECT a.x, (SELECT name FROM b WHERE b.id = a.bid "
             "AND b.k IN (SELECT k FROM a)) AS n FROM a",