github-advanced-security[bot] commented on code in PR #7542: URL: https://github.com/apache/shenyu/pull/7542#discussion_r4240085012
########## shenyu-common/src/main/java/org/apache/shenyu/common/dto/convert/rule/canary/CanaryConfigValidator.java: ########## @@ -0,0 +1,201 @@ +/* + * 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.shenyu.common.dto.convert.rule.canary; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; +import org.apache.commons.lang3.StringUtils; +import org.apache.shenyu.common.dto.ConditionData; +import org.apache.shenyu.common.dto.convert.rule.impl.DivideRuleHandle; +import org.apache.shenyu.common.utils.DateUtils; +import org.apache.shenyu.common.utils.GsonUtils; + +import java.util.Map; +import java.util.Objects; +import java.util.function.Consumer; +import java.util.regex.Pattern; + +/** + * Shared validation of Canary configuration before persistence or gateway caching. + */ +public final class CanaryConfigValidator { + + private CanaryConfigValidator() { + } + + /** + * Parse a Divide handle, checking JSON types before Gson can coerce values. + * An absent or null Canary configuration preserves legacy routing. + * + * @param handle the serialized rule handle + * @return the validated handle + * @throws IllegalArgumentException when the configuration is invalid + */ + public static DivideRuleHandle parseHandle(final String handle) { + try { + JsonObject root = object(GsonUtils.getInstance().fromJson(handle, JsonElement.class), "Divide handle"); + JsonElement canary = root.get("canary"); + if (Objects.nonNull(canary) && !canary.isJsonNull()) { + validate(object(canary, "canary")); + } + return GsonUtils.getInstance().fromJson(root, DivideRuleHandle.class); + } catch (JsonParseException ex) { + throw new IllegalArgumentException("Invalid Divide handle JSON", ex); + } + } + + /** + * Check extension names against the registry available at the caller. + * Admin uses its dictionaries; gateways use their installed SPI implementations. + * + * @param config the validated configuration + * @param parameterValidator parameter source validator + * @param operatorValidator condition operator validator + */ + public static void validateExtensions(final CanaryConfig config, final Consumer<String> parameterValidator, + final Consumer<String> operatorValidator) { + if (Objects.nonNull(config.getStickyKey())) { + parameterValidator.accept(config.getStickyKey().getParamType()); + } + if (Objects.nonNull(config.getConditions())) { + for (ConditionData condition : config.getConditions()) { + parameterValidator.accept(condition.getParamType()); + operatorValidator.accept(condition.getOperator()); + } + } + } + + private static void validate(final JsonObject config) { + if (config.has("enabled") && !(config.get("enabled").isJsonPrimitive() && config.getAsJsonPrimitive("enabled").isBoolean())) { + throw new IllegalArgumentException("canary.enabled must be a boolean"); + } + int percentage = integer(config, "percentage", 0); + if (percentage < 0 || percentage > 100) { + throw new IllegalArgumentException("canary.percentage must be between 0 and 100"); + } + int matchMode = integer(config, "matchMode", 0); + if (matchMode != 0 && matchMode != 1) { + throw new IllegalArgumentException("canary.matchMode must be 0 (AND) or 1 (OR)"); + } + if (config.has("fallbackPolicy")) { + String policy = string(config, "fallbackPolicy", "canary.fallbackPolicy", true); + if (!"STABLE".equals(policy) && !"REJECT".equals(policy)) { + throw new IllegalArgumentException("canary.fallbackPolicy must be STABLE or REJECT"); + } + } + validateLabels(config.get("stableLabels"), "canary.stableLabels", false); + validateLabels(config.get("canaryLabels"), "canary.canaryLabels", false); + if (config.has("stickyKey")) { + validateSource(object(config.get("stickyKey"), "canary.stickyKey"), "canary.stickyKey"); + } else if (config.has("enabled") && config.get("enabled").getAsBoolean() && percentage > 0 && percentage < 100) { + throw new IllegalArgumentException("canary.stickyKey is required when enabled with a percentage between 1 and 99"); + } + if (config.has("conditions")) { + JsonElement conditions = config.get("conditions"); + if (!conditions.isJsonArray()) { + throw new IllegalArgumentException("canary.conditions must be an array"); + } + int index = 0; + for (JsonElement element : conditions.getAsJsonArray()) { + String path = "canary.conditions[" + index++ + "]"; + JsonObject condition = object(element, path); + validateSource(condition, path); + String operator = string(condition, "operator", path + ".operator", true); + String value = string(condition, "paramValue", path + ".paramValue", !"isBlank".equals(operator)); + validateConditionValue(operator, value, path); + } + } + GsonUtils.getInstance().fromJson(config, CanaryConfig.class).validatePartitionLabels(); + } + + private static void validateConditionValue(final String operator, final String value, final String path) { + try { + if ("regex".equals(operator)) { + Pattern.compile(value.trim()); Review Comment: ## CodeQL / Regular expression injection This regular expression is constructed from a [user-provided value](1). This regular expression is constructed from a [user-provided value](2). [Show more details](https://github.com/apache/shenyu/security/code-scanning/84) -- 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]
