qiuyanjun888 commented on code in PR #18398:
URL: 
https://github.com/apache/dolphinscheduler/pull/18398#discussion_r3573226424


##########
dolphinscheduler-task-plugin/dolphinscheduler-task-sql/src/main/java/org/apache/dolphinscheduler/plugin/task/sql/SqlTaskParameterRenderer.java:
##########
@@ -0,0 +1,284 @@
+/*
+ * 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.dolphinscheduler.plugin.task.sql;
+
+import org.apache.dolphinscheduler.common.utils.JSONUtils;
+import org.apache.dolphinscheduler.plugin.task.api.TaskException;
+import org.apache.dolphinscheduler.plugin.task.api.enums.DataType;
+import org.apache.dolphinscheduler.plugin.task.api.model.Property;
+
+import org.apache.commons.lang3.StringUtils;
+
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+final class SqlTaskParameterRenderer {
+
+    private SqlTaskParameterRenderer() {
+    }
+
+    static String render(String sql, Map<String, Property> paramsMap, int 
taskInstanceId) {
+        if (StringUtils.isEmpty(sql) || paramsMap == null || 
paramsMap.isEmpty()) {
+            return sql;
+        }
+        return renderParameters(sql, paramsMap, taskInstanceId);
+    }
+
+    private static String renderParameters(String sql, Map<String, Property> 
paramsMap, int taskInstanceId) {
+        StringBuilder renderedSql = new StringBuilder(sql.length());
+        boolean insideSingleQuotedString = false;
+        int index = 0;
+        while (index < sql.length()) {
+            char current = sql.charAt(index);
+            if (current == '\'' && insideSingleQuotedString) {
+                renderedSql.append(current);
+                if (index + 1 < sql.length() && sql.charAt(index + 1) == '\'') 
{
+                    renderedSql.append('\'');
+                    index += 2;
+                    continue;
+                }
+                insideSingleQuotedString = false;
+                index++;
+                continue;
+            }
+
+            Placeholder rawQuotedPlaceholder = matchQuotedPlaceholder(sql, 
index, '!', insideSingleQuotedString);
+            if (rawQuotedPlaceholder != null) {
+                Property property = getProperty(paramsMap, 
rawQuotedPlaceholder.paramName, taskInstanceId);
+                renderedSql.append(renderRawProperty(property));
+                index = rawQuotedPlaceholder.endIndex;
+                continue;
+            }
+
+            Placeholder sqlQuotedPlaceholder = matchQuotedPlaceholder(sql, 
index, '$', insideSingleQuotedString);
+            if (sqlQuotedPlaceholder != null) {
+                Property property = getProperty(paramsMap, 
sqlQuotedPlaceholder.paramName, taskInstanceId);
+                renderedSql.append(renderProperty(property, false, false));
+                index = sqlQuotedPlaceholder.endIndex;
+                continue;
+            }
+
+            Placeholder rawPlaceholder = matchPlaceholder(sql, index, '!');
+            if (rawPlaceholder != null) {
+                Property property = getProperty(paramsMap, 
rawPlaceholder.paramName, taskInstanceId);
+                renderedSql.append(renderRawProperty(property));
+                index = rawPlaceholder.endIndex;
+                continue;
+            }
+
+            Placeholder sqlPlaceholder = matchPlaceholder(sql, index, '$');
+            if (sqlPlaceholder != null) {
+                Property property = getProperty(paramsMap, 
sqlPlaceholder.paramName, taskInstanceId);
+                boolean identifierContext = !insideSingleQuotedString
+                        && isIdentifierContext(sql, index, 
sqlPlaceholder.endIndex);
+                renderedSql.append(renderProperty(property, identifierContext, 
insideSingleQuotedString));
+                index = sqlPlaceholder.endIndex;
+                continue;
+            }
+
+            renderedSql.append(current);
+            if (current == '\'') {
+                insideSingleQuotedString = true;
+            }
+            index++;
+        }
+        return renderedSql.toString();
+    }
+
+    private static Placeholder matchQuotedPlaceholder(String sql, int start, 
char marker,
+                                                      boolean 
insideSingleQuotedString) {
+        if (insideSingleQuotedString || start >= sql.length()) {
+            return null;
+        }
+        char quote = sql.charAt(start);
+        if (quote != '\'' && quote != '"') {
+            return null;
+        }
+        Placeholder placeholder = matchPlaceholder(sql, start + 1, marker);
+        if (placeholder == null || placeholder.endIndex >= sql.length() || 
sql.charAt(placeholder.endIndex) != quote) {
+            return null;
+        }
+        return new Placeholder(placeholder.paramName, placeholder.endIndex + 
1);
+    }
+
+    private static Placeholder matchPlaceholder(String sql, int start, char 
marker) {
+        if (start + 2 >= sql.length() || sql.charAt(start) != marker || 
sql.charAt(start + 1) != '{') {
+            return null;
+        }
+        int end = sql.indexOf('}', start + 2);
+        if (end < 0) {
+            return null;
+        }
+        return new Placeholder(sql.substring(start + 2, end), end + 1);
+    }
+
+    private static Property getProperty(Map<String, Property> paramsMap, 
String paramName, int taskInstanceId) {
+        Property property = paramsMap.get(paramName);
+        if (property == null) {
+            throw new TaskException(String.format(
+                    "No Property with paramName: %s is found in paramsMap of 
task instance with id: %s",
+                    paramName,
+                    taskInstanceId));
+        }
+        return property;
+    }
+
+    private static String renderRawProperty(Property property) {
+        return StringUtils.defaultString(property.getValue());
+    }
+
+    private static String renderProperty(Property property, boolean 
identifierContext, boolean insideStringLiteral) {
+        String value = property.getValue();
+        if (value == null) {
+            return "null";
+        }
+        if (insideStringLiteral) {
+            return escapeSqlString(value);
+        }
+        if (identifierContext) {
+            return value;
+        }

Review Comment:
   Fixed in `0ca7e2e` by validating `${...}` values in identifier-fragment 
context against a non-empty allowlist of letters, digits, underscores, and 
dollar signs. Invalid fragments now throw `TaskException` before SQL execution; 
raw `!{...}` replacement remains unchanged.
   
   Verification: targeted `SqlTaskTest` passed (29 tests), `spotless:check` 
passed, and `git diff --check` passed.



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

Reply via email to