From 8f3bbf01ef4f27961756d65c2d841517be77196b Mon Sep 17 00:00:00 2001 From: morrySnow Date: Wed, 16 Sep 2026 19:46:05 +0800 Subject: [PATCH 1/3] [fix](fe) Require SELECT privilege for binlog TVF ### What problem does this PR solve? Issue Number: N/A Related PR: N/A Problem Summary: The binlog() table-valued function resolved and scanned its target table without checking the caller table privileges. A restricted user could therefore read row-binlog content from a table it could not query directly. Check SELECT privilege before loading table metadata so unauthorized calls return the standard table access-denied error. ### Release note The binlog() table-valued function now requires SELECT privilege on its target table. ### Check List (For Author) - Test: Unit Test - ./run-fe-ut.sh --run org.apache.doris.tablefunction.TableBinlogFunctionAuthTest - Added a regression test and validated its Groovy syntax locally; cluster execution is left to CI - Behavior changed: Yes. Unauthorized users can no longer query row-binlog content through binlog(). - Does this need documentation: No --- .../tablefunction/TableBinlogFunction.java | 11 +++ .../TableBinlogFunctionAuthTest.java | 61 +++++++++++++++ .../auth_p0/test_binlog_tvf_auth.groovy | 77 +++++++++++++++++++ 3 files changed, 149 insertions(+) create mode 100644 fe/fe-core/src/test/java/org/apache/doris/tablefunction/TableBinlogFunctionAuthTest.java create mode 100644 regression-test/suites/auth_p0/test_binlog_tvf_auth.groovy diff --git a/fe/fe-core/src/main/java/org/apache/doris/tablefunction/TableBinlogFunction.java b/fe/fe-core/src/main/java/org/apache/doris/tablefunction/TableBinlogFunction.java index dc84f39142435f..506759d684dd77 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/tablefunction/TableBinlogFunction.java +++ b/fe/fe-core/src/main/java/org/apache/doris/tablefunction/TableBinlogFunction.java @@ -29,9 +29,13 @@ import org.apache.doris.catalog.TableIf.TableType; import org.apache.doris.catalog.info.PartitionNamesInfo; import org.apache.doris.common.AnalysisException; +import org.apache.doris.common.ErrorCode; +import org.apache.doris.common.ErrorReport; import org.apache.doris.common.MetaNotFoundException; import org.apache.doris.common.util.Util; +import org.apache.doris.datasource.InternalCatalog; import org.apache.doris.mtmv.ivm.IvmUtil; +import org.apache.doris.mysql.privilege.PrivPredicate; import org.apache.doris.planner.OlapScanNode; import org.apache.doris.planner.PlanNodeId; import org.apache.doris.planner.ScanContext; @@ -99,6 +103,13 @@ public TableBinlogFunction(Map params) throws AnalysisException } this.dbName = db; + ConnectContext ctx = ConnectContext.get(); + if (!Env.getCurrentEnv().getAccessManager().checkTblPriv(ctx, InternalCatalog.INTERNAL_CATALOG_NAME, + dbName, tableName, PrivPredicate.SELECT)) { + ErrorReport.reportAnalysisException(ErrorCode.ERR_TABLE_ACCESS_DENIED_ERROR, + PrivPredicate.SELECT.getPrivs().toString(), tableName); + } + this.partitionNamesInfo = parsePartitionNamesInfo(validParams.get(PARTITION)); this.specifiedTabletIds = parseTabletIds(validParams.get(TABLET)); diff --git a/fe/fe-core/src/test/java/org/apache/doris/tablefunction/TableBinlogFunctionAuthTest.java b/fe/fe-core/src/test/java/org/apache/doris/tablefunction/TableBinlogFunctionAuthTest.java new file mode 100644 index 00000000000000..c1bdd18284aab5 --- /dev/null +++ b/fe/fe-core/src/test/java/org/apache/doris/tablefunction/TableBinlogFunctionAuthTest.java @@ -0,0 +1,61 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.tablefunction; + +import org.apache.doris.catalog.Env; +import org.apache.doris.common.AnalysisException; +import org.apache.doris.datasource.InternalCatalog; +import org.apache.doris.mysql.privilege.AccessControllerManager; +import org.apache.doris.mysql.privilege.PrivPredicate; +import org.apache.doris.qe.ConnectContext; +import org.apache.doris.qe.QueryState; + +import com.google.common.collect.ImmutableMap; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.mockito.MockedStatic; +import org.mockito.Mockito; + +public class TableBinlogFunctionAuthTest { + + @Test + public void testRequiresSelectPrivilegeBeforeLoadingTableMetadata() { + Env env = Mockito.mock(Env.class); + ConnectContext context = Mockito.mock(ConnectContext.class); + AccessControllerManager accessManager = Mockito.mock(AccessControllerManager.class); + Mockito.when(context.getState()).thenReturn(new QueryState()); + Mockito.when(env.getAccessManager()).thenReturn(accessManager); + Mockito.when(accessManager.checkTblPriv(context, InternalCatalog.INTERNAL_CATALOG_NAME, + "test_db", "test_table", PrivPredicate.SELECT)).thenReturn(false); + + try (MockedStatic mockedEnv = Mockito.mockStatic(Env.class); + MockedStatic mockedContext = Mockito.mockStatic(ConnectContext.class)) { + mockedEnv.when(Env::getCurrentEnv).thenReturn(env); + mockedContext.when(ConnectContext::get).thenReturn(context); + + AnalysisException exception = Assertions.assertThrows(AnalysisException.class, + () -> new TableBinlogFunction(ImmutableMap.of( + "db", "test_db", "table", "test_table"))); + + Assertions.assertTrue(exception.getMessage().contains("Access denied")); + Mockito.verify(accessManager).checkTblPriv(context, InternalCatalog.INTERNAL_CATALOG_NAME, + "test_db", "test_table", PrivPredicate.SELECT); + Mockito.verify(env, Mockito.never()).getInternalCatalog(); + } + } +} diff --git a/regression-test/suites/auth_p0/test_binlog_tvf_auth.groovy b/regression-test/suites/auth_p0/test_binlog_tvf_auth.groovy new file mode 100644 index 00000000000000..b5156f0c88c8b8 --- /dev/null +++ b/regression-test/suites/auth_p0/test_binlog_tvf_auth.groovy @@ -0,0 +1,77 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +suite("test_binlog_tvf_auth", "p0,auth") { + String user = "test_binlog_tvf_auth_user" + String password = "C123_567p" + + sql "DROP USER IF EXISTS '${user}'" + sql "DROP DATABASE IF EXISTS test_binlog_tvf_auth_db" + sql "CREATE DATABASE test_binlog_tvf_auth_db" + sql """ + CREATE TABLE test_binlog_tvf_auth_db.test_binlog_tvf_auth_table ( + k BIGINT, + v INT + ) ENGINE=OLAP + DUPLICATE KEY(k) + DISTRIBUTED BY HASH(k) BUCKETS 1 + PROPERTIES ( + "replication_num" = "1", + "binlog.enable" = "true", + "binlog.format" = "ROW" + ) + """ + sql "INSERT INTO test_binlog_tvf_auth_db.test_binlog_tvf_auth_table VALUES (1, 100)" + sql "SYNC" + sql "CREATE USER '${user}' IDENTIFIED BY '${password}'" + + if (isCloudMode()) { + def clusters = sql "SHOW CLUSTERS" + assertFalse(clusters.isEmpty()) + sql "GRANT USAGE_PRIV ON CLUSTER `${clusters[0][0]}` TO ${user}" + } + + connect(user, password, context.config.jdbcUrl) { + test { + sql "SELECT * FROM test_binlog_tvf_auth_db.test_binlog_tvf_auth_table" + exception "Access denied" + } + test { + sql """ + SELECT k, v + FROM binlog( + "db" = "test_binlog_tvf_auth_db", + "table" = "test_binlog_tvf_auth_table" + ) + LIMIT 1 + """ + exception "Access denied" + } + } + + sql "GRANT SELECT_PRIV ON test_binlog_tvf_auth_db.test_binlog_tvf_auth_table TO ${user}" + connect(user, password, context.config.jdbcUrl) { + sql """ + SELECT k, v + FROM binlog( + "db" = "test_binlog_tvf_auth_db", + "table" = "test_binlog_tvf_auth_table" + ) + LIMIT 1 + """ + } +} From be79fbb0eb0bb93941acbfd7a482f40dac1bdd04 Mon Sep 17 00:00:00 2001 From: morrySnow Date: Thu, 17 Sep 2026 10:50:46 +0800 Subject: [PATCH 2/3] [fix](fe) Move binlog TVF auth to checkAuth ### What problem does this PR solve? Issue Number: N/A Related PR: N/A Problem Summary: The binlog() TVF privilege check should participate in the standard TVF privilege-check phase. Performing authorization in the constructor couples access control to metadata initialization and bypasses the checkAuth contract. Override checkAuth(ConnectContext) to require SELECT privilege and keep construction authorization-free. ### Release note The binlog() TVF now performs its SELECT privilege check through the standard TVF checkAuth hook. ### Check List (For Author) - Test: Unit Test - ./run-fe-ut.sh --run org.apache.doris.tablefunction.TableBinlogFunctionAuthTest - Behavior changed: No. This preserves the SELECT requirement while moving it to the standard authorization phase. - Does this need documentation: No --- .../tablefunction/TableBinlogFunction.java | 18 +++++----- .../TableBinlogFunctionAuthTest.java | 36 +++++++++++++------ 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/tablefunction/TableBinlogFunction.java b/fe/fe-core/src/main/java/org/apache/doris/tablefunction/TableBinlogFunction.java index 506759d684dd77..aa3cd5eb1a01bf 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/tablefunction/TableBinlogFunction.java +++ b/fe/fe-core/src/main/java/org/apache/doris/tablefunction/TableBinlogFunction.java @@ -30,7 +30,6 @@ import org.apache.doris.catalog.info.PartitionNamesInfo; import org.apache.doris.common.AnalysisException; import org.apache.doris.common.ErrorCode; -import org.apache.doris.common.ErrorReport; import org.apache.doris.common.MetaNotFoundException; import org.apache.doris.common.util.Util; import org.apache.doris.datasource.InternalCatalog; @@ -103,13 +102,6 @@ public TableBinlogFunction(Map params) throws AnalysisException } this.dbName = db; - ConnectContext ctx = ConnectContext.get(); - if (!Env.getCurrentEnv().getAccessManager().checkTblPriv(ctx, InternalCatalog.INTERNAL_CATALOG_NAME, - dbName, tableName, PrivPredicate.SELECT)) { - ErrorReport.reportAnalysisException(ErrorCode.ERR_TABLE_ACCESS_DENIED_ERROR, - PrivPredicate.SELECT.getPrivs().toString(), tableName); - } - this.partitionNamesInfo = parsePartitionNamesInfo(validParams.get(PARTITION)); this.specifiedTabletIds = parseTabletIds(validParams.get(TABLET)); @@ -137,6 +129,16 @@ public TableBinlogFunction(Map params) throws AnalysisException } } + @Override + public void checkAuth(ConnectContext ctx) { + if (!Env.getCurrentEnv().getAccessManager().checkTblPriv(ctx, InternalCatalog.INTERNAL_CATALOG_NAME, + dbName, tableName, PrivPredicate.SELECT)) { + String message = ErrorCode.ERR_TABLE_ACCESS_DENIED_ERROR.formatErrorMsg( + PrivPredicate.SELECT.getPrivs().toString(), tableName); + throw new org.apache.doris.nereids.exceptions.AnalysisException(message); + } + } + @Override public String getTableName() { return "BinlogTableFunction"; diff --git a/fe/fe-core/src/test/java/org/apache/doris/tablefunction/TableBinlogFunctionAuthTest.java b/fe/fe-core/src/test/java/org/apache/doris/tablefunction/TableBinlogFunctionAuthTest.java index c1bdd18284aab5..bf536717af84cc 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/tablefunction/TableBinlogFunctionAuthTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/tablefunction/TableBinlogFunctionAuthTest.java @@ -17,45 +17,61 @@ package org.apache.doris.tablefunction; +import org.apache.doris.catalog.Database; import org.apache.doris.catalog.Env; -import org.apache.doris.common.AnalysisException; +import org.apache.doris.catalog.OlapTable; +import org.apache.doris.catalog.RowBinlogTableWrapper; +import org.apache.doris.catalog.TableIf.TableType; import org.apache.doris.datasource.InternalCatalog; import org.apache.doris.mysql.privilege.AccessControllerManager; import org.apache.doris.mysql.privilege.PrivPredicate; +import org.apache.doris.nereids.exceptions.AnalysisException; import org.apache.doris.qe.ConnectContext; -import org.apache.doris.qe.QueryState; import com.google.common.collect.ImmutableMap; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.mockito.MockedConstruction; import org.mockito.MockedStatic; import org.mockito.Mockito; public class TableBinlogFunctionAuthTest { @Test - public void testRequiresSelectPrivilegeBeforeLoadingTableMetadata() { + public void testCheckAuthRequiresSelectPrivilege() throws Exception { Env env = Mockito.mock(Env.class); ConnectContext context = Mockito.mock(ConnectContext.class); AccessControllerManager accessManager = Mockito.mock(AccessControllerManager.class); - Mockito.when(context.getState()).thenReturn(new QueryState()); + InternalCatalog catalog = Mockito.mock(InternalCatalog.class); + Database database = Mockito.mock(Database.class); + OlapTable table = Mockito.mock(OlapTable.class); + Mockito.when(env.getAccessManager()).thenReturn(accessManager); + Mockito.when(env.getInternalCatalog()).thenReturn(catalog); + Mockito.when(catalog.getDbOrMetaException("test_db")).thenReturn(database); + Mockito.when(database.getTableOrMetaException("test_table", TableType.OLAP)).thenReturn(table); + Mockito.when(table.needRowBinlog()).thenReturn(true); Mockito.when(accessManager.checkTblPriv(context, InternalCatalog.INTERNAL_CATALOG_NAME, "test_db", "test_table", PrivPredicate.SELECT)).thenReturn(false); try (MockedStatic mockedEnv = Mockito.mockStatic(Env.class); - MockedStatic mockedContext = Mockito.mockStatic(ConnectContext.class)) { + MockedConstruction ignored = + Mockito.mockConstruction(RowBinlogTableWrapper.class)) { mockedEnv.when(Env::getCurrentEnv).thenReturn(env); - mockedContext.when(ConnectContext::get).thenReturn(context); - AnalysisException exception = Assertions.assertThrows(AnalysisException.class, - () -> new TableBinlogFunction(ImmutableMap.of( - "db", "test_db", "table", "test_table"))); + TableBinlogFunction function = new TableBinlogFunction(ImmutableMap.of( + "db", "test_db", "table", "test_table")); + Mockito.verifyNoInteractions(accessManager); + AnalysisException exception = Assertions.assertThrows(AnalysisException.class, + () -> function.checkAuth(context)); Assertions.assertTrue(exception.getMessage().contains("Access denied")); Mockito.verify(accessManager).checkTblPriv(context, InternalCatalog.INTERNAL_CATALOG_NAME, "test_db", "test_table", PrivPredicate.SELECT); - Mockito.verify(env, Mockito.never()).getInternalCatalog(); + + Mockito.when(accessManager.checkTblPriv(context, InternalCatalog.INTERNAL_CATALOG_NAME, + "test_db", "test_table", PrivPredicate.SELECT)).thenReturn(true); + Assertions.assertDoesNotThrow(() -> function.checkAuth(context)); } } } From 5afc8150539ddaaf210a5f7a479775ddbfd2bfa4 Mon Sep 17 00:00:00 2001 From: morrySnow Date: Fri, 18 Sep 2026 10:57:46 +0800 Subject: [PATCH 3/3] [fix](test) Fix binlog TVF auth regression case --- regression-test/suites/auth_p0/test_binlog_tvf_auth.groovy | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/regression-test/suites/auth_p0/test_binlog_tvf_auth.groovy b/regression-test/suites/auth_p0/test_binlog_tvf_auth.groovy index b5156f0c88c8b8..c1e3dee38f485b 100644 --- a/regression-test/suites/auth_p0/test_binlog_tvf_auth.groovy +++ b/regression-test/suites/auth_p0/test_binlog_tvf_auth.groovy @@ -44,11 +44,14 @@ suite("test_binlog_tvf_auth", "p0,auth") { assertFalse(clusters.isEmpty()) sql "GRANT USAGE_PRIV ON CLUSTER `${clusters[0][0]}` TO ${user}" } + // The configured JDBC URL connects to regression_test before executing the query. + // Grant access to that default database while keeping the target table unauthorized. + sql "GRANT SELECT_PRIV ON regression_test TO ${user}" connect(user, password, context.config.jdbcUrl) { test { sql "SELECT * FROM test_binlog_tvf_auth_db.test_binlog_tvf_auth_table" - exception "Access denied" + exception "denied" } test { sql """ @@ -59,7 +62,7 @@ suite("test_binlog_tvf_auth", "p0,auth") { ) LIMIT 1 """ - exception "Access denied" + exception "denied" } }