wForget commented on code in PR #7400: URL: https://github.com/apache/kyuubi/pull/7400#discussion_r3114845115
########## externals/kyuubi-data-agent-engine/src/main/java/org/apache/kyuubi/engine/dataagent/tool/sql/SqlReadOnlyChecker.java: ########## @@ -0,0 +1,130 @@ +/* + * 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.kyuubi.engine.dataagent.tool.sql; + +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.Locale; +import java.util.Set; + +/** + * Lightweight read-only check for the {@code run_select_query} tool. + * + * <p><b>Design note — this is a guardrail, not a security boundary.</b> The real approval gate for + * writes is the separate {@code run_mutation_query} tool; an LLM with DML intent calls that + * directly, not via a crafted bypass of this check. This checker only catches the "LLM hallucinates + * and sends a mutation to the read-only tool" case, for which a first-token keyword whitelist is + * enough. Pulling a full SQL parser into the tool layer for a sanity check isn't warranted. + * + * <p>The whitelist is intentionally broad to cover common big-data read-only entry points. If a + * legitimate read-only keyword is missing, add it here rather than working around it elsewhere. + */ +final class SqlReadOnlyChecker { + + /** + * Read-only keywords accepted as the first token of a SQL statement. + * + * <ul> + * <li>{@code SELECT} — standard query + * <li>{@code WITH} — CTE-prefixed query Review Comment: Only checking the firstToken may not be enough, like: ``` FROM t1 INSERT INTO t2 SELECT c1, c2; WITH tmp AS (SELECT * FROM t1) INSERT INTO t2 SELECT * FROM tmp; ``` -- 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]
