JinwooHwang commented on code in PR #7941:
URL: https://github.com/apache/geode/pull/7941#discussion_r2511659616


##########
extensions/geode-modules/src/main/java/org/apache/geode/modules/session/filter/SafeDeserializationFilter.java:
##########
@@ -0,0 +1,474 @@
+/*
+ * 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.geode.modules.session.filter;
+
+import java.io.ObjectInputFilter;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.regex.Pattern;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Security filter for safe deserialization of session attributes.
+ *
+ * <p>
+ * This filter prevents unsafe deserialization attacks by implementing a 
strict whitelist
+ * of allowed classes and blocking known dangerous classes that can be used in 
gadget chain
+ * attacks.
+ *
+ * <p>
+ * <b>Security Features:</b>
+ * <ul>
+ * <li>Whitelist-based class filtering (default-deny)</li>
+ * <li>Blocks known dangerous classes (e.g., Commons Collections, Spring 
internals)</li>
+ * <li>Configurable depth and size limits</li>
+ * <li>Comprehensive security logging</li>
+ * </ul>
+ *
+ * @since 1.15.0
+ */
+public class SafeDeserializationFilter implements ObjectInputFilter {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(SafeDeserializationFilter.class);
+  private static final Logger SECURITY_LOG =
+      LoggerFactory.getLogger("org.apache.geode.security.deserialization");
+
+  // Maximum object graph depth to prevent stack overflow attacks
+  private static final long MAX_DEPTH = 50;
+
+  // Maximum number of object references to prevent memory exhaustion
+  private static final long MAX_REFERENCES = 10000;
+
+  // Maximum array size to prevent memory exhaustion
+  private static final long MAX_ARRAY_SIZE = 10000;
+
+  // Maximum total bytes to prevent resource exhaustion
+  private static final long MAX_BYTES = 10_000_000; // 10MB
+
+  /**
+   * Known dangerous classes that are commonly used in deserialization gadget 
chains.
+   * These should NEVER be deserialized from untrusted sources.
+   */
+  private static final Set<String> BLOCKED_CLASSES = new HashSet<>();
+
+  /**
+   * Patterns for dangerous class prefixes
+   */
+  private static final Set<Pattern> BLOCKED_PATTERNS = new HashSet<>();
+
+  /**
+   * Allowed class patterns (whitelist)
+   */
+  private static final Set<Pattern> ALLOWED_PATTERNS = new HashSet<>();
+
+  static {
+    // Block known gadget chain classes
+
+    // Apache Commons Collections - TransformedMap, LazyMap exploits
+    
BLOCKED_CLASSES.add("org.apache.commons.collections.functors.InvokerTransformer");
+    
BLOCKED_CLASSES.add("org.apache.commons.collections.functors.ChainedTransformer");
+    
BLOCKED_CLASSES.add("org.apache.commons.collections.functors.ConstantTransformer");
+    
BLOCKED_CLASSES.add("org.apache.commons.collections.functors.InstantiateTransformer");
+    
BLOCKED_CLASSES.add("org.apache.commons.collections.keyvalue.TiedMapEntry");
+    BLOCKED_CLASSES.add("org.apache.commons.collections.map.LazyMap");
+    
BLOCKED_CLASSES.add("org.apache.commons.collections4.functors.InvokerTransformer");
+    
BLOCKED_CLASSES.add("org.apache.commons.collections4.functors.ChainedTransformer");
+    
BLOCKED_CLASSES.add("org.apache.commons.collections4.functors.ConstantTransformer");
+    
BLOCKED_CLASSES.add("org.apache.commons.collections4.functors.InstantiateTransformer");
+    
BLOCKED_CLASSES.add("org.apache.commons.collections4.keyvalue.TiedMapEntry");
+    BLOCKED_CLASSES.add("org.apache.commons.collections4.map.LazyMap");
+
+    // Spring Framework - BeanFactory exploits
+    BLOCKED_CLASSES.add("org.springframework.beans.factory.ObjectFactory");
+    BLOCKED_CLASSES
+        
.add("org.springframework.core.SerializableTypeWrapper$MethodInvokeTypeProvider");
+    BLOCKED_CLASSES.add("org.springframework.aop.framework.AdvisedSupport");
+    
BLOCKED_CLASSES.add("org.springframework.aop.target.SingletonTargetSource");
+
+    // JDK internal classes that can be exploited
+    
BLOCKED_CLASSES.add("com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl");
+    BLOCKED_CLASSES.add("javax.management.BadAttributeValueExpException");
+    BLOCKED_CLASSES.add("java.rmi.server.UnicastRemoteObject");
+    BLOCKED_CLASSES.add("java.rmi.server.RemoteObjectInvocationHandler");
+
+    // Groovy exploits
+    BLOCKED_CLASSES.add("org.codehaus.groovy.runtime.ConvertedClosure");
+    BLOCKED_CLASSES.add("org.codehaus.groovy.runtime.MethodClosure");
+
+    // C3P0 JNDI exploits
+    BLOCKED_CLASSES.add("com.mchange.v2.c3p0.impl.PoolBackedDataSourceBase");
+    BLOCKED_CLASSES.add("com.mchange.v2.c3p0.JndiRefForwardingDataSource");
+
+    // Hibernate exploits
+    BLOCKED_CLASSES.add("org.hibernate.jmx.StatisticsService");
+    BLOCKED_CLASSES.add("org.hibernate.engine.spi.TypedValue");
+
+    // Block dangerous package patterns
+    
BLOCKED_PATTERNS.add(Pattern.compile("^org\\.apache\\.commons\\.collections\\.functors\\..*"));
+    
BLOCKED_PATTERNS.add(Pattern.compile("^org\\.apache\\.commons\\.collections4\\.functors\\..*"));
+    
BLOCKED_PATTERNS.add(Pattern.compile("^org\\.springframework\\.beans\\.factory\\..*"));
+    BLOCKED_PATTERNS.add(Pattern.compile("^org\\.springframework\\.aop\\..*"));
+    
BLOCKED_PATTERNS.add(Pattern.compile("^com\\.sun\\.org\\.apache\\.xalan\\..*"));
+    BLOCKED_PATTERNS.add(Pattern.compile("^javax\\.management\\..*"));
+    BLOCKED_PATTERNS.add(Pattern.compile("^java\\.rmi\\..*"));
+    BLOCKED_PATTERNS.add(Pattern.compile("^sun\\.rmi\\..*"));
+    
BLOCKED_PATTERNS.add(Pattern.compile("^org\\.codehaus\\.groovy\\.runtime\\..*"));
+    BLOCKED_PATTERNS.add(Pattern.compile("^com\\.mchange\\.v2\\.c3p0\\..*"));
+
+    // Allow safe Java classes
+    ALLOWED_PATTERNS.add(Pattern.compile("^java\\.lang\\.String$"));
+    ALLOWED_PATTERNS.add(Pattern.compile("^java\\.lang\\.Number$"));
+    ALLOWED_PATTERNS.add(Pattern.compile("^java\\.lang\\.Integer$"));
+    ALLOWED_PATTERNS.add(Pattern.compile("^java\\.lang\\.Long$"));
+    ALLOWED_PATTERNS.add(Pattern.compile("^java\\.lang\\.Float$"));
+    ALLOWED_PATTERNS.add(Pattern.compile("^java\\.lang\\.Double$"));
+    ALLOWED_PATTERNS.add(Pattern.compile("^java\\.lang\\.Boolean$"));
+    ALLOWED_PATTERNS.add(Pattern.compile("^java\\.lang\\.Byte$"));
+    ALLOWED_PATTERNS.add(Pattern.compile("^java\\.lang\\.Short$"));
+    ALLOWED_PATTERNS.add(Pattern.compile("^java\\.lang\\.Character$"));
+    ALLOWED_PATTERNS.add(Pattern.compile("^java\\.math\\.BigInteger$"));
+    ALLOWED_PATTERNS.add(Pattern.compile("^java\\.math\\.BigDecimal$"));
+
+    // Allow safe collection classes
+    ALLOWED_PATTERNS.add(Pattern.compile("^java\\.util\\.ArrayList$"));
+    ALLOWED_PATTERNS.add(Pattern.compile("^java\\.util\\.LinkedList$"));
+    ALLOWED_PATTERNS.add(Pattern.compile("^java\\.util\\.HashMap$"));
+    ALLOWED_PATTERNS.add(Pattern.compile("^java\\.util\\.LinkedHashMap$"));
+    ALLOWED_PATTERNS.add(Pattern.compile("^java\\.util\\.TreeMap$"));
+    ALLOWED_PATTERNS.add(Pattern.compile("^java\\.util\\.HashSet$"));
+    ALLOWED_PATTERNS.add(Pattern.compile("^java\\.util\\.LinkedHashSet$"));
+    ALLOWED_PATTERNS.add(Pattern.compile("^java\\.util\\.TreeSet$"));
+    
ALLOWED_PATTERNS.add(Pattern.compile("^java\\.util\\.concurrent\\.ConcurrentHashMap$"));
+    ALLOWED_PATTERNS.add(Pattern.compile("^java\\.util\\.Collections\\$.*"));
+
+    // Allow date/time classes
+    ALLOWED_PATTERNS.add(Pattern.compile("^java\\.util\\.Date$"));
+    ALLOWED_PATTERNS.add(Pattern.compile("^java\\.sql\\.Date$"));
+    ALLOWED_PATTERNS.add(Pattern.compile("^java\\.sql\\.Time$"));
+    ALLOWED_PATTERNS.add(Pattern.compile("^java\\.sql\\.Timestamp$"));
+    ALLOWED_PATTERNS.add(Pattern.compile("^java\\.time\\..*"));
+

Review Comment:
   Excellent observation! Changes have been pushed in the latest commit. Thank 
you very much for your review.



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