wangzhigang1999 commented on code in PR #7400:
URL: https://github.com/apache/kyuubi/pull/7400#discussion_r3115162796


##########
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:
   Good catch, thank you @wForget — both of these are legitimate Spark/Hive 
bypasses. The whitelist has `FROM` (for the `FROM t SELECT ...` read variant) 
and `WITH` (for SELECT CTEs), and your examples exploit exactly those entries.
   
   **I will open a dedicated follow-up issue to track this and commit to 
closing the gap there.** Our practical observation is that the realistic risk 
is moderate: modern LLMs rarely produce `FROM t INSERT ...` or `WITH ... 
INSERT` on their own — those patterns are thin in training data, and the tool 
descriptions + `run_mutation_query` split already steer the model toward the 
straightforward `INSERT ... SELECT` shape. The exposure is mostly when a user 
pastes such a statement into the conversation verbatim. Still, a tool named 
"read-only" should actually be read-only for the configured dialects, and we 
intend to fix this properly. For now, we'd like to keep the data-agent series 
moving and handle the hardening in the tracked follow-up.



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