This is an automated email from the ASF dual-hosted git repository.

asf-gitbox-commits pushed a commit to branch 2.2.X
in repository https://gitbox.apache.org/repos/asf/mina.git


The following commit(s) were added to refs/heads/2.2.X by this push:
     new 4b6438c4f Forbid the unbounded accumulation of decoded data
4b6438c4f is described below

commit 4b6438c4f8587e00c6cd56303b11ffd5e5c54976
Author: emmanuel lecharny <[email protected]>
AuthorDate: Sat Nov 14 02:18:09 2026 +0100

    Forbid the unbounded accumulation of decoded data
---
 .../filter/codec/CumulativeProtocolDecoder.java    | 58 +++++++++++++++++++++-
 .../codec/CumulativeProtocolDecoderTest.java       | 50 +++++++++++++++++++
 2 files changed, 107 insertions(+), 1 deletion(-)

diff --git 
a/mina-core/src/main/java/org/apache/mina/filter/codec/CumulativeProtocolDecoder.java
 
b/mina-core/src/main/java/org/apache/mina/filter/codec/CumulativeProtocolDecoder.java
index 2a4530645..85278e65c 100644
--- 
a/mina-core/src/main/java/org/apache/mina/filter/codec/CumulativeProtocolDecoder.java
+++ 
b/mina-core/src/main/java/org/apache/mina/filter/codec/CumulativeProtocolDecoder.java
@@ -101,7 +101,24 @@ import org.apache.mina.core.session.IoSession;
 public abstract class CumulativeProtocolDecoder extends ProtocolDecoderAdapter 
{
     /** The buffer used to store the data in the session */
     private static final AttributeKey BUFFER = new 
AttributeKey(CumulativeProtocolDecoder.class, "buffer");
-    
+
+    /**
+     * The default maximum size in bytes of the cumulative session buffer. A
+     * peer sending data that the subclass never manages to decode (doDecode()
+     * keeps returning <code>false</code>) would otherwise make the buffer -
+     * and the heap - grow without limit on a single connection. The 16 MB
+     * default is far above any of the shipped decoders' message sizes; it can
+     * be changed globally with the
+     * <tt>org.apache.mina.codec.maxCumulativeSize</tt> system property, or per
+     * decoder with {@link #setMaxCumulativeSize(int)}. A value of <tt>0</tt>
+     * or less disables the limit (previous, unbounded behavior).
+     */
+    public static final int DEFAULT_MAX_CUMULATIVE_SIZE = Integer.getInteger(
+            "org.apache.mina.codec.maxCumulativeSize", 16 * 1024 * 1024);
+
+    /** The maximum size in bytes of the cumulative session buffer */
+    private int maxCumulativeSize = DEFAULT_MAX_CUMULATIVE_SIZE;
+
     /** 
      * A flag set to true if we handle fragmentation accordingly to the 
TransportMetadata setting. 
      * It can be set to false if needed (UDP with fragments, for instance). 
the default value is 'true'
@@ -143,6 +160,21 @@ public abstract class CumulativeProtocolDecoder extends 
ProtocolDecoderAdapter {
         // If we have a session buffer, append data to that; otherwise
         // use the buffer read from the network directly.
         if (buf != null) {
+            // Refuse to cumulate more than maxCumulativeSize bytes: a peer
+            // sending data the subclass never manages to decode must fail the
+            // session instead of growing the heap without limit.
+            if (maxCumulativeSize > 0) {
+                long cumulated = buf.position() + (long) in.remaining();
+
+                if (cumulated > maxCumulativeSize) {
+                    removeSessionBuffer(session);
+                    
+                    throw new ProtocolDecoderException("The cumulative session 
buffer grew to " + cumulated
+                            + " bytes, exceeding the " + maxCumulativeSize
+                            + " bytes limit (see 
CumulativeProtocolDecoder.setMaxCumulativeSize())");
+                }
+            }
+
             // Make sure that the buffer is auto-expanded.
             if (buf.isAutoExpand()) {
                 try {
@@ -275,4 +307,28 @@ public abstract class CumulativeProtocolDecoder extends 
ProtocolDecoderAdapter {
     public void setTransportMetadataFragmentation(boolean 
transportMetadataFragmentation) {
         this.transportMetadataFragmentation = transportMetadataFragmentation;
     }
+
+    /**
+     * @return the maximum size in bytes the cumulative session buffer may
+     * grow to before the decoder fails the session with a
+     * {@link ProtocolDecoderException}. A value of <tt>0</tt> or less means
+     * no limit.
+     */
+    public int getMaxCumulativeSize() {
+        return maxCumulativeSize;
+    }
+
+    /**
+     * Sets the maximum size in bytes the cumulative session buffer may grow
+     * to before the decoder fails the session with a
+     * {@link ProtocolDecoderException}, protecting the heap against a peer
+     * that sends data the subclass never manages to decode. Defaults to
+     * {@link #DEFAULT_MAX_CUMULATIVE_SIZE}.
+     *
+     * @param maxCumulativeSize the limit; a value of <tt>0</tt> or less
+     * disables it (unbounded cumulation)
+     */
+    public void setMaxCumulativeSize(int maxCumulativeSize) {
+        this.maxCumulativeSize = maxCumulativeSize;
+    }
 }
diff --git 
a/mina-core/src/test/java/org/apache/mina/filter/codec/CumulativeProtocolDecoderTest.java
 
b/mina-core/src/test/java/org/apache/mina/filter/codec/CumulativeProtocolDecoderTest.java
index 4da775160..8d8eda157 100644
--- 
a/mina-core/src/test/java/org/apache/mina/filter/codec/CumulativeProtocolDecoderTest.java
+++ 
b/mina-core/src/test/java/org/apache/mina/filter/codec/CumulativeProtocolDecoderTest.java
@@ -184,7 +184,41 @@ public class CumulativeProtocolDecoderTest {
 
         faultyDecoder.dispose(session);
     }
+    
+    @Test
+    public void testCumulationIsBounded() throws Exception {
+        NeverDecodingDecoder boundedDecoder = new NeverDecodingDecoder();
+        boundedDecoder.setMaxCumulativeSize(8);
+
+        // First two chunks fit within the limit and are cumulated.
+        for (int i = 0; i < 2; i++) {
+            buf.clear();
+            buf.putInt(i);
+            buf.flip();
+            boundedDecoder.decode(session, buf, session.getDecoderOutput());
+        }
+
+        // The third chunk would grow the cumulative buffer beyond the limit.
+        buf.clear();
+        buf.putInt(2);
+        buf.flip();
+
+        try {
+            boundedDecoder.decode(session, buf, session.getDecoderOutput());
+            fail("The cumulative buffer should have been bounded");
+        } catch (ProtocolDecoderException e) {
+            // OK
+        }
 
+        // The over-sized cumulative buffer must have been discarded:
+        // new data starts cumulating from scratch instead of failing again.
+        buf.clear();
+        buf.putInt(3);
+        buf.flip();
+        boundedDecoder.decode(session, buf, session.getDecoderOutput());
+
+        boundedDecoder.dispose(session);
+    }
 
     private static class IntegerDecoder extends CumulativeProtocolDecoder {
         /**
@@ -245,6 +279,22 @@ public class CumulativeProtocolDecoderTest {
             return true;
         }
     }
+    
+    private static class NeverDecodingDecoder extends 
CumulativeProtocolDecoder {
+        /**
+         * Default constructor
+         */
+        public NeverDecodingDecoder() {
+            super();
+        }
+
+        @Override
+        protected boolean doDecode(IoSession session, IoBuffer in, 
ProtocolDecoderOutput out) throws Exception {
+            // Simulates a decoder waiting for a frame terminator that the
+            // peer never sends: everything is cumulated, nothing is decoded.
+            return false;
+        }
+    }
 
     private static class DuplicatingIntegerDecoder extends IntegerDecoder {
         /**

Reply via email to