pauloricardomg commented on code in PR #369: URL: https://github.com/apache/cassandra-sidecar/pull/369#discussion_r3574408677
########## server/src/main/java/org/apache/cassandra/sidecar/configmanagement/ConfigurationPatchValidator.java: ########## @@ -0,0 +1,295 @@ +/* + * 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.cassandra.sidecar.configmanagement; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.regex.Pattern; + +import org.jetbrains.annotations.NotNull; + +/** + * Validates a list of {@link ConfigurationPatchOperation} instances before application. + * + * <p>Validation includes: + * <ul> + * <li>Path format: must start with {@code /configuration/cassandraYaml/} or + * {@code /configuration/extraJvmOpts/}</li> + * <li>Value presence: required for add/replace/test, must be absent for remove</li> + * <li>Duplicate path detection: multiple mutation ops targeting the same path are rejected</li> + * <li>Path segments split by {@code /}; empty segments are rejected</li> + * </ul> + */ +public final class ConfigurationPatchValidator +{ + static final String PATH_PREFIX = "/configuration/"; + static final String CASSANDRA_YAML_SECTION = "cassandraYaml"; + static final String EXTRA_JVM_OPTS_SECTION = "extraJvmOpts"; + static final String CASSANDRA_YAML_PREFIX = PATH_PREFIX + CASSANDRA_YAML_SECTION + "/"; + static final String EXTRA_JVM_OPTS_PREFIX = PATH_PREFIX + EXTRA_JVM_OPTS_SECTION + "/"; + + // Allows: -Dproperty.name, -Xmx, -Xss, -XX:+Flag, -XX:-Flag, -XX:Flag + // Rejects: -javaagent, -agentpath, -agentlib, keys with = or shell metacharacters + static final Pattern JVM_OPT_KEY_PATTERN = Pattern.compile( + "^-(D[a-zA-Z][a-zA-Z0-9._-]*|X[a-z][a-zA-Z0-9]*|XX:[+-]?[a-zA-Z][a-zA-Z0-9_]*)$"); + + // JVM options that are rejected because they execute arbitrary commands or write to + // arbitrary filesystem paths. The value pattern permits absolute paths (Cassandra system + // properties legitimately need them), so path-bearing flags must be blocked by key instead. + static final Set<String> BLOCKED_JVM_OPTS = Set.of( + // Execute arbitrary commands + "-XX:OnOutOfMemoryError", + "-XX:OnError", + // Write to arbitrary filesystem paths + "-XX:ErrorFile", + "-XX:HeapDumpPath", + "-XX:LogFile", + "-XX:FlightRecorderOptions", + "-XX:StartFlightRecording", + "-Xloggc", + "-Xlog", + "-Xbootclasspath"); + + // Allows: alphanumeric, dots, colons, slashes, @, +, commas, spaces, hyphens (max 512 chars) + // Rejects: shell metacharacters (;|&$`), quotes, newlines, and other control characters + static final Pattern JVM_OPT_VALUE_PATTERN = Pattern.compile("^[a-zA-Z0-9._:/@+, -]{0,512}$"); Review Comment: Good catch - space is no longer allowed on d818e07 since it would break cassandra startup script (the Cassandra launcher word-splits unquoted values during eval). I also updated the value pattern to permit the JSON characters `" { } [ ]`. That is needed for JSON-valued options like `-Dcassandra.settings.default_compaction={"class_name":"SizeTieredCompactionStrategy","parameters":{...}}`. -- 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]

