github-actions[bot] commented on code in PR #62339:
URL: https://github.com/apache/doris/pull/62339#discussion_r3078553370


##########
fe/fe-core/src/test/java/org/apache/doris/nereids/privileges/TestCheckPrivileges.java:
##########
@@ -223,6 +223,77 @@ public void testPrivilegesAndPolicies() throws Exception {
         });
     }
 
+    @Test
+    public void testCtePrivilegeCheck() throws Exception {
+        FeConstants.runningUnitTest = true;
+        // Use the same external catalog for privilege control infrastructure
+        String catalogProvider
+                = 
"org.apache.doris.nereids.privileges.TestCheckPrivileges$CustomCatalogProvider";
+        String accessControllerFactory
+                = 
"org.apache.doris.nereids.privileges.CustomAccessControllerFactory";
+        String catalog = "custom_catalog_cte";
+
+        createCatalog("create catalog " + catalog + " properties("
+                + " \"type\"=\"test\","
+                + " \"catalog_provider.class\"=\"" + catalogProvider + "\","
+                + " \"" + CatalogMgr.ACCESS_CONTROLLER_CLASS_PROP + "\"=\"" + 
accessControllerFactory + "\""
+                + ")");
+
+        // Create internal OLAP tables (LogicalOlapScan supports 
withRelationId needed by CTE)
+        String cteDb = "cte_priv_db";
+        createDatabase(cteDb);
+        useDatabase(cteDb);
+        createTable("create table allowed_tbl (id int, name varchar(50)) "
+                + "distributed by hash(id) buckets 1 "
+                + "properties('replication_num' = '1')");
+        createTable("create table denied_tbl (id int, name varchar(50)) "
+                + "distributed by hash(id) buckets 1 "
+                + "properties('replication_num' = '1')");
+
+        String user = "test_cte_privilege_user";
+        addUser(user, true);
+        useUser(user);
+
+        try {
+            List<MakeTablePrivileges> privileges = ImmutableList.of(
+                    MakePrivileges.table("internal", cteDb, 
"allowed_tbl").allowSelectTable(user)
+            );
+
+            AccessControllerManager accessManager = 
Env.getCurrentEnv().getAccessManager();
+            CatalogAccessController catalogAccessController = 
accessManager.getAccessControllerOrDefault(catalog);
+            AccessControllerManager spyAccessManager = 
Mockito.spy(accessManager);
+            Mockito.doReturn(catalogAccessController).when(spyAccessManager)
+                    .getAccessControllerOrDefault("internal");
+            Deencapsulation.setField(Env.getCurrentEnv(), "accessManager", 
spyAccessManager);
+
+            withPrivileges(privileges, () -> {
+                // CTE with authorized table should succeed
+                query("with cte as (select * from allowed_tbl) select * from 
cte");
+
+                // CTE with unauthorized table should fail (this was the bug:
+                // consumer was checked first, setting privChecked=true on 
shared StatementContext,
+                // so producer's CheckPrivileges was skipped)
+                Assertions.assertThrows(AnalysisException.class, () ->
+                        query("with cte as (select * from denied_tbl) select * 
from cte")
+                );
+
+                // CTE referenced twice forces no-inline path through 
RewriteCteChildren
+                Assertions.assertThrows(AnalysisException.class, () ->
+                        query("with cte as (select * from denied_tbl) "
+                                + "select * from cte union all select * from 
cte")
+                );
+

Review Comment:
   `Env.getCurrentEnv().accessManager` is process-global shared FE state, but 
this test never restores the original instance after swapping in the spy. 
`TestWithFeService` keeps one FE env across test methods, so any later test in 
this class or another class can now observe the mocked controller and become 
order-dependent. `testPrivilegesAndPolicies()` already showed this class of 
leak with `useUser(...)`; this is the same problem at global-auth scope. Please 
snapshot the original `AccessControllerManager` before replacing it and restore 
it in `finally` after the assertions complete.



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