pauloricardomg commented on code in PR #369:
URL: https://github.com/apache/cassandra-sidecar/pull/369#discussion_r3561422832


##########
server/src/main/java/org/apache/cassandra/sidecar/configmanagement/ConfigurationPatchValidator.java:
##########
@@ -0,0 +1,283 @@
+/*
+ * 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_]*)$");
+
+    // XX flags that accept arbitrary commands as values
+    static final Set<String> BLOCKED_XX_FLAGS = Set.of(

Review Comment:
   Addressed in 5c676938 by expanding the blocklist to cover 
path-bearing/file-writing options (-XX:ErrorFile, -XX:HeapDumpPath, 
-XX:LogFile, -XX:FlightRecorderOptions, -XX:StartFlightRecording, -Xloggc, 
-Xlog, -Xbootclasspath) alongside the existing command-executing 
-XX:OnError/-XX:OnOutOfMemoryError.
   
   I honestly don't know the full set of valid JVM configs, and an allowlist 
would mean enumerating every legitimate Cassandra JVM option (all the 
-Xms/-Xmx/-Xss/-XX GC and heap tuning flags plus the whole -Dcassandra.* / 
-Dcom.sun.management.* / -Djava.* system-property surface), which would make 
the list massive and hard to keep current. This isn't a security issue in 
practice: the patch endpoint requires admin credentials, so we're just removing 
the obviously insecure options and can add more as we find them. If you feel 
strongly about an allowlist, I'm happy to switch to one.
   



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