Skip to content

Commit 9ea3ffd

Browse files
committed
fix: 严格限制分支规则#AI Commit#
1 parent ffc1d6d commit 9ea3ffd

2 files changed

Lines changed: 26 additions & 19 deletions

File tree

  • dss-orchestrator/orchestrators/dss-workflow

dss-orchestrator/orchestrators/dss-workflow/dss-flow-execution-server/src/main/scala/com/webank/wedatasphere/dss/flow/execution/entrance/utils/BranchExpressionUtils.scala

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ object BranchExpressionUtils extends Logging {
103103
if condition.nonEmpty && !isUnsupportedDefaultKeyword(condition) && (targetName.isDefined || onFailureTarget.isDefined) =>
104104
Some(BranchRule(condition, targetName, onFailureTarget))
105105
case _ => None
106-
}
106+
}.getOrElse(false)
107107
}
108108
}
109109

@@ -120,7 +120,7 @@ object BranchExpressionUtils extends Logging {
120120
case None =>
121121
warn(s"Invalid branch rule syntax: $line")
122122
None
123-
}
123+
}.getOrElse(false)
124124
}
125125
}
126126

@@ -149,11 +149,17 @@ object BranchExpressionUtils extends Logging {
149149
if (normalized.isEmpty) {
150150
false
151151
} else {
152-
val expr = stripExpressionWrapper(normalized)
153-
if (expr.equalsIgnoreCase("true")) return true
154-
if (expr.equalsIgnoreCase("false")) return false
152+
if (normalized.startsWith("${") && normalized.endsWith("}")) {
153+
warn(s"Invalid branch condition syntax, wrapper ${} is not allowed: $condition")
154+
return false
155+
}
156+
val expr = normalized
155157
if (isUnsupportedDefaultKeyword(expr)) return false
156158
val operators = Seq("==", "!=", ">=", "<=", ">", "<")
159+
if (!operators.exists(expr.contains)) {
160+
warn(s"Invalid branch condition syntax, explicit comparison is required: $condition")
161+
return false
162+
}
157163
operators.collectFirst {
158164
case operator if expr.contains(operator) =>
159165
val parts = expr.split(java.util.regex.Pattern.quote(operator), 2).map(_.trim)
@@ -171,17 +177,7 @@ object BranchExpressionUtils extends Logging {
171177
false
172178
}
173179
}
174-
}.getOrElse {
175-
resolveValue(expr, context) match {
176-
case Some(resolved) =>
177-
val matched = resolved.equalsIgnoreCase("true") || resolved.nonEmpty
178-
info(s"Branch condition evaluated: expr=$expr, value=$resolved, matched=$matched")
179-
matched
180-
case None =>
181-
warn(s"Branch condition unresolved token: expr=$expr, contextKeys=${context.keys.toSeq.sorted.mkString(",")}")
182-
false
183-
}
184-
}
180+
}.getOrElse(false)
185181
}
186182
}
187183

@@ -203,7 +199,7 @@ object BranchExpressionUtils extends Logging {
203199
context.get(normalized)
204200
.orElse(context.get(unquoted))
205201
.orElse(if (isLiteralToken(unquoted)) Some(unquoted) else None)
206-
}
202+
}.getOrElse(false)
207203
}
208204
}
209205

@@ -239,7 +235,7 @@ object BranchExpressionUtils extends Logging {
239235
case ">" | "<" | ">=" | "<=" =>
240236
warn(s"Branch numeric comparison requires numeric operands: left=$left, operator=$operator, right=$right")
241237
false
242-
}
238+
}.getOrElse(false)
243239
}
244240
}
245241

dss-orchestrator/orchestrators/dss-workflow/dss-workflow-server/src/main/java/com/webank/wedatasphere/dss/workflow/service/impl/DSSFlowServiceImpl.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ private List<BranchRuleHolder> parseBranchRules(String branchRuleText) {
611611
}
612612
}
613613
for (BranchRuleHolder holder : branchRuleMap.values()) {
614-
if (StringUtils.isNotBlank(holder.condition) && !isUnsupportedDefaultKeyword(holder.condition) && (StringUtils.isNotBlank(holder.targetName) || StringUtils.isNotBlank(holder.onFailure))) {
614+
if (StringUtils.isNotBlank(holder.condition) && !isUnsupportedDefaultKeyword(holder.condition) && isStrictBranchCondition(holder.condition) && (StringUtils.isNotBlank(holder.targetName) || StringUtils.isNotBlank(holder.onFailure))) {
615615
branchRules.add(holder);
616616
}
617617
}
@@ -645,6 +645,17 @@ private IndexedBranchRuleKey buildIndexedBranchRuleKey(String ruleType, String r
645645
}
646646
}
647647

648+
private boolean isStrictBranchCondition(String condition) {
649+
if (StringUtils.isBlank(condition)) {
650+
return false;
651+
}
652+
String expr = condition.trim();
653+
if (expr.startsWith("${") && expr.endsWith("}")) {
654+
return false;
655+
}
656+
return expr.contains("==") || expr.contains("!=") || expr.contains(">=") || expr.contains("<=") || expr.contains(">") || expr.contains("<");
657+
}
658+
648659
private boolean isUnsupportedDefaultKeyword(String condition) {
649660
if (StringUtils.isBlank(condition)) {
650661
return false;

0 commit comments

Comments
 (0)