You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@eventmesh.apache.org by ch...@apache.org on 2026/09/09 02:14:44 UTC

(eventmesh) 01/01: fix(#5361): FrameLimits — enforce wire bounds at encode AND decode (no-alloc rejects)

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

qqeasonchen pushed a commit to branch fix/5361-frame-bounds
in repository https://gitbox.apache.org/repos/asf/eventmesh.git

commit e3c8aa7cda62104a7ff13a7773cb52ae9227b179
Author: Eason Chen <qq...@gmail.com>
AuthorDate: Wed Sep 9 10:13:58 2026 +0800

    fix(#5361): FrameLimits — enforce wire bounds at encode AND decode (no-alloc rejects)
    
    Phase 1 of the production-HA plan (#5354), sub-issue #5361 (acceptance
    A3: frame fuzz/boundary tests reject malformed input without OOM).
    
    The gap: the frame codec accepted whatever sizes the header
    advertised. A malformed or hostile frame declaring dataLen=2^31-1
    made the decoder allocate that much memory before failing on the
    truncated body (OOM instead of a clean reject); truncated buffers
    escaped as BufferUnderflowException instead of IllegalArgumentException.
    
    New FrameLimits (eventmesh-common/wire):
    - MAX_FRAME_BYTES 16 MiB, MAX_DATA_BYTES 8 MiB, MAX_ATTRIBUTES 64,
      MAX_ATTR_NAME_BYTES 256, MAX_ATTR_VALUE_BYTES 4 KiB - generous for
      streaming chunks / large CloudEvents payloads while bounding a
      single frame's memory footprint.
    
    Encode boundary: a producer cannot build an oversized frame
    (attribute count, data length, attr name/value length, total encoded
    size all fail fast with IllegalArgumentException).
    
    Decode boundary: advertised keyCount/dataLen are rejected BEFORE any
    allocation; every buffer read (attribute length prefixes, string
    bodies, the data bulk get) is guarded so a truncated frame fails as
    IllegalArgumentException - never BufferUnderflowException, never an
    over-allocation. decode is now total: any byte sequence either
    decodes or throws IllegalArgumentException.
    
    Tests (new FrameLimitsTest, 8 cases):
    - 4 encode-bound rejects + at-the-limit acceptance (boundary is >)
    - hostile dataLen=Integer.MAX_VALUE on a 64-byte buffer: clean
      IllegalArgumentException, no allocation
    - hostile keyCount; dataLen beyond provided buffer
    - deterministic 20,000-iteration byte-mutation fuzz: every mutant
      either decodes or throws IllegalArgumentException (found and fixed
      the two underflow escapes above during development)
    
    Local verification (Temurin 21.0.11): common tests + checkstyle
    (maxWarnings=0) green; downstream runtime + architecture-guard +
    storage-api tests green.
    
    Refs #5361 (sub-issue of #5354).
    
    Co-authored-by: qqeasonchen <qq...@gmail.com>
---
 .../eventmesh/common/wire/EventMeshFrame.java      |  65 +++++++-
 .../apache/eventmesh/common/wire/FrameLimits.java  |  53 ++++++
 .../eventmesh/common/wire/FrameLimitsTest.java     | 178 +++++++++++++++++++++
 3 files changed, 293 insertions(+), 3 deletions(-)

diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/wire/EventMeshFrame.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/wire/EventMeshFrame.java
index 9305216bb..114628408 100644
--- a/eventmesh-common/src/main/java/org/apache/eventmesh/common/wire/EventMeshFrame.java
+++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/wire/EventMeshFrame.java
@@ -256,6 +256,16 @@ public final class EventMeshFrame {
     // -------------------- encode / decode --------------------
 
     public byte[] encode() {
+        // #5361: enforce wire bounds at the producer side — fail fast instead of building a
+        // frame the decode side would have to reject (or worse, allocate for).
+        if (attrs.size() > FrameLimits.MAX_ATTRIBUTES) {
+            throw new IllegalArgumentException("frame attribute count " + attrs.size()
+                + " exceeds limit " + FrameLimits.MAX_ATTRIBUTES);
+        }
+        if (data.length > FrameLimits.MAX_DATA_BYTES) {
+            throw new IllegalArgumentException("frame data length " + data.length
+                + " exceeds limit " + FrameLimits.MAX_DATA_BYTES);
+        }
         byte[][] nameBytes = new byte[attrs.size()][];
         byte[][] valBytes = new byte[attrs.size()][];
         int kvLen = 0;
@@ -263,9 +273,21 @@ public final class EventMeshFrame {
         for (Map.Entry<String, String> e : attrs.entrySet()) {
             nameBytes[i] = utf8(e.getKey());
             valBytes[i] = utf8(e.getValue());
+            if (nameBytes[i].length > FrameLimits.MAX_ATTR_NAME_BYTES) {
+                throw new IllegalArgumentException("attribute name length " + nameBytes[i].length
+                    + " exceeds limit " + FrameLimits.MAX_ATTR_NAME_BYTES);
+            }
+            if (valBytes[i].length > FrameLimits.MAX_ATTR_VALUE_BYTES) {
+                throw new IllegalArgumentException("attribute value length " + valBytes[i].length
+                    + " exceeds limit " + FrameLimits.MAX_ATTR_VALUE_BYTES);
+            }
             kvLen += 2 + nameBytes[i].length + 4 + valBytes[i].length;
             i++;
         }
+        if (HEADER_LEN + kvLen + data.length > FrameLimits.MAX_FRAME_BYTES) {
+            throw new IllegalArgumentException("encoded frame size " + (HEADER_LEN + kvLen + data.length)
+                + " exceeds limit " + FrameLimits.MAX_FRAME_BYTES);
+        }
         ByteBuffer buf = ByteBuffer.allocate(HEADER_LEN + kvLen + data.length);
         buf.put((byte) MAGIC);
         buf.put((byte) VERSION);
@@ -301,17 +323,48 @@ public final class EventMeshFrame {
         if (ver != VERSION) {
             throw new IllegalArgumentException("unsupported frame version: " + ver);
         }
-        int msgType = buf.get() & 0xFF;
-        int flags = buf.get() & 0xFF;
-        int seq = buf.getInt();
+        final int msgType = buf.get() & 0xFF;
+        final int flags = buf.get() & 0xFF;
+        final int seq = buf.getInt();
         int keyCount = buf.getShort() & 0xFFFF;
         int dataLen = buf.getInt();
+        // #5361: reject malformed/hostile headers BEFORE allocating — a frame that advertises
+        // sizes beyond the wire limits fails with IllegalArgumentException (clean reject), not
+        // an OutOfMemoryError from the allocation below.
+        if (keyCount > FrameLimits.MAX_ATTRIBUTES) {
+            throw new IllegalArgumentException("frame attribute count " + keyCount
+                + " exceeds limit " + FrameLimits.MAX_ATTRIBUTES);
+        }
+        if (dataLen < 0 || dataLen > FrameLimits.MAX_DATA_BYTES) {
+            throw new IllegalArgumentException("frame data length " + dataLen
+                + " exceeds limit " + FrameLimits.MAX_DATA_BYTES);
+        }
+        if (dataLen > length - HEADER_LEN) {
+            throw new IllegalArgumentException("frame data length " + dataLen
+                + " exceeds the provided buffer (" + length + " bytes)");
+        }
         Map<String, String> attrs = new LinkedHashMap<>(keyCount);
         for (int i = 0; i < keyCount; i++) {
+            // #5361: length prefixes themselves can run past a truncated buffer — guard each
+            // read so the whole decode fails as IllegalArgumentException, never underflow.
+            if (buf.remaining() < 2) {
+                throw new IllegalArgumentException("truncated frame: attribute name length prefix"
+                    + " beyond buffer (" + buf.remaining() + " bytes left)");
+            }
             String name = getString(buf, buf.getShort() & 0xFFFF);
+            if (buf.remaining() < 4) {
+                throw new IllegalArgumentException("truncated frame: attribute value length prefix"
+                    + " beyond buffer (" + buf.remaining() + " bytes left)");
+            }
             String value = getString(buf, buf.getInt());
             attrs.put(name, value);
         }
+        // #5361: attributes consumed part of the buffer; re-check the data region before the
+        // bulk get (a frame truncated mid-attributes or mid-data fails cleanly).
+        if (dataLen > buf.remaining()) {
+            throw new IllegalArgumentException("frame data length " + dataLen
+                + " exceeds the remaining buffer (" + buf.remaining() + " bytes)");
+        }
         byte[] data = new byte[dataLen];
         buf.get(data);
         return new EventMeshFrame(msgType, flags, seq, attrs, data);
@@ -378,6 +431,12 @@ public final class EventMeshFrame {
     }
 
     private static String getString(ByteBuffer buf, int len) {
+        // #5361: a truncated buffer must fail as IllegalArgumentException (clean reject),
+        // never BufferUnderflowException or an over-allocation from a hostile length.
+        if (len < 0 || len > buf.remaining()) {
+            throw new IllegalArgumentException("string length " + len
+                + " exceeds the remaining buffer (" + buf.remaining() + " bytes)");
+        }
         byte[] b = new byte[len];
         buf.get(b);
         return new String(b, StandardCharsets.UTF_8);
diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/wire/FrameLimits.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/wire/FrameLimits.java
new file mode 100644
index 000000000..1e2506162
--- /dev/null
+++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/wire/FrameLimits.java
@@ -0,0 +1,53 @@
+/*
+ * 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.eventmesh.common.wire;
+
+/**
+ * Wire-format bounds for {@link EventMeshFrame} (issue #5361, plan #5354 Phase 1).
+ *
+ * <p>The frame codec previously accepted any sizes the header advertised — a malformed or
+ * hostile frame could declare {@code dataLen = 2^31-1} and the decoder would allocate that
+ * much memory before failing on the truncated body (OOM instead of a clean reject). These
+ * bounds are enforced at BOTH the encode boundary (a producer cannot build an oversized
+ * frame) and the decode boundary (a consumer rejects oversized input without allocating).</p>
+ *
+ * <p>Values are intentionally generous for an internal wire format: a 16 MiB frame ceiling
+ * covers streaming chunks and large CloudEvents payloads with headroom, while still
+ * bounding a single frame's memory footprint. Attribute count/size bounds mirror typical
+ * protocol header limits (HTTP/2: 100 headers; many brokers: 4 KiB per header value).</p>
+ */
+public final class FrameLimits {
+
+    private FrameLimits() {
+    }
+
+    /** Maximum encoded frame size (header + attributes + data). */
+    public static final int MAX_FRAME_BYTES = 16 * 1024 * 1024;
+
+    /** Maximum payload (data) size in bytes. */
+    public static final int MAX_DATA_BYTES = 8 * 1024 * 1024;
+
+    /** Maximum number of attributes per frame. */
+    public static final int MAX_ATTRIBUTES = 64;
+
+    /** Maximum UTF-8 length of one attribute name, in bytes. */
+    public static final int MAX_ATTR_NAME_BYTES = 256;
+
+    /** Maximum UTF-8 length of one attribute value, in bytes. */
+    public static final int MAX_ATTR_VALUE_BYTES = 4 * 1024;
+}
diff --git a/eventmesh-common/src/test/java/org/apache/eventmesh/common/wire/FrameLimitsTest.java b/eventmesh-common/src/test/java/org/apache/eventmesh/common/wire/FrameLimitsTest.java
new file mode 100644
index 000000000..88e22aa47
--- /dev/null
+++ b/eventmesh-common/src/test/java/org/apache/eventmesh/common/wire/FrameLimitsTest.java
@@ -0,0 +1,178 @@
+/*
+ * 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.eventmesh.common.wire;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.nio.ByteBuffer;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Random;
+
+import org.junit.jupiter.api.Test;
+
+/**
+ * Issue #5361 acceptance (plan #5354 Phase 1): the frame codec enforces {@link FrameLimits} at
+ * BOTH boundaries — encode fails fast on oversized input, decode rejects malformed/hostile
+ * headers <em>without allocating</em> — and a randomized fuzz pass never throws anything but
+ * {@link IllegalArgumentException} (no OOM, no ArrayIndexOutOfBounds escape, no infinite loop).
+ */
+class FrameLimitsTest {
+
+    private static EventMeshFrame frame(Map<String, String> attrs, byte[] data) {
+        return EventMeshFrame.event(attrs, data);
+    }
+
+    // -------------------- encode bounds --------------------
+
+    @Test
+    void encodeRejectsTooManyAttributes() {
+        Map<String, String> attrs = new LinkedHashMap<>();
+        for (int i = 0; i <= FrameLimits.MAX_ATTRIBUTES; i++) {
+            attrs.put("k" + i, "v");
+        }
+        IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
+            () -> frame(attrs, new byte[0]).encode());
+        assertTrue(ex.getMessage().contains("attribute count"), ex.getMessage());
+    }
+
+    @Test
+    void encodeRejectsOversizedData() {
+        // MAX_DATA_BYTES + 1 (safe: 8 MiB + 1 allocation only)
+        byte[] data = new byte[FrameLimits.MAX_DATA_BYTES + 1];
+        IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
+            () -> frame(new LinkedHashMap<>(), data).encode());
+        assertTrue(ex.getMessage().contains("data length"), ex.getMessage());
+    }
+
+    @Test
+    void encodeRejectsOversizedAttributeName() {
+        Map<String, String> attrs = new LinkedHashMap<>();
+        attrs.put("k".repeat(FrameLimits.MAX_ATTR_NAME_BYTES + 1), "v");
+        IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
+            () -> frame(attrs, new byte[0]).encode());
+        assertTrue(ex.getMessage().contains("attribute name length"), ex.getMessage());
+    }
+
+    @Test
+    void encodeRejectsOversizedAttributeValue() {
+        Map<String, String> attrs = new LinkedHashMap<>();
+        attrs.put("k", "v".repeat(FrameLimits.MAX_ATTR_VALUE_BYTES + 1));
+        IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
+            () -> frame(attrs, new byte[0]).encode());
+        assertTrue(ex.getMessage().contains("attribute value length"), ex.getMessage());
+    }
+
+    @Test
+    void encodeAcceptsAtTheLimits() {
+        // Exactly at every limit must pass (boundary is > not >=).
+        Map<String, String> attrs = new LinkedHashMap<>();
+        attrs.put("k".repeat(FrameLimits.MAX_ATTR_NAME_BYTES), "v".repeat(FrameLimits.MAX_ATTR_VALUE_BYTES));
+        byte[] data = new byte[FrameLimits.MAX_DATA_BYTES];
+        EventMeshFrame f = frame(attrs, data);
+        byte[] encoded = f.encode();
+        assertEquals(FrameLimits.MAX_DATA_BYTES, EventMeshFrame.decode(encoded).data().length);
+    }
+
+    // -------------------- decode bounds (no-allocation rejects) --------------------
+
+    @Test
+    void decodeRejectsHostileDataLenWithoutAllocating() {
+        // Hand-craft a header that advertises dataLen = Integer.MAX_VALUE on a tiny buffer.
+        ByteBuffer hostile = ByteBuffer.allocate(FrameLimits.MAX_FRAME_BYTES > 64 ? 64 : FrameLimits.MAX_FRAME_BYTES);
+        hostile.put((byte) EventMeshFrame.MAGIC);
+        hostile.put((byte) EventMeshFrame.VERSION);
+        hostile.put((byte) 3); // TYPE_EVENT
+        hostile.put((byte) 0); // flags
+        hostile.putInt(1);     // seq
+        hostile.putShort((short) 0); // keyCount
+        hostile.putInt(Integer.MAX_VALUE); // dataLen — hostile
+        byte[] bytes = hostile.array();
+        IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
+            () -> EventMeshFrame.decode(bytes));
+        assertTrue(ex.getMessage().contains("data length") || ex.getMessage().contains("buffer"),
+            ex.getMessage());
+    }
+
+    @Test
+    void decodeRejectsHostileKeyCount() {
+        ByteBuffer hostile = ByteBuffer.allocate(64);
+        hostile.put((byte) EventMeshFrame.MAGIC);
+        hostile.put((byte) EventMeshFrame.VERSION);
+        hostile.put((byte) 3);
+        hostile.put((byte) 0);
+        hostile.putInt(1);
+        hostile.putShort((short) (FrameLimits.MAX_ATTRIBUTES + 1)); // keyCount — hostile
+        hostile.putInt(0);
+        IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
+            () -> EventMeshFrame.decode(hostile.array()));
+        assertTrue(ex.getMessage().contains("attribute count"), ex.getMessage());
+    }
+
+    @Test
+    void decodeRejectsDataLenBeyondProvidedBuffer() {
+        // Well-formed frame with 10 data bytes, but the buffer is truncated to the header only.
+        EventMeshFrame f = frame(mapOf("k", "v"), new byte[10]);
+        byte[] full = f.encode();
+        byte[] truncated = new byte[14]; // HEADER_LEN only
+        System.arraycopy(full, 0, truncated, 0, truncated.length);
+        IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
+            () -> EventMeshFrame.decode(truncated));
+        assertTrue(ex.getMessage().contains("buffer"), ex.getMessage());
+    }
+
+    // -------------------- fuzz --------------------
+
+    @Test
+    void fuzzedHeadersNeverEscapeIllegalArgumentException() {
+        // Deterministic fuzz: mutate a well-formed frame's bytes; every decode outcome is
+        // either a valid frame or an IllegalArgumentException — never OOM/AIOOBE/infinite loop.
+        Random rnd = new Random(20260909L);
+        EventMeshFrame wellFormed = frame(mapOf("k1", "v1", "k2", "v2"), new byte[32]);
+        byte[] encoded = wellFormed.encode();
+        int illegal = 0;
+        int accepted = 0;
+        for (int iter = 0; iter < 20_000; iter++) {
+            byte[] mutant = encoded.clone();
+            int mutations = 1 + rnd.nextInt(4);
+            for (int m = 0; m < mutations; m++) {
+                mutant[rnd.nextInt(mutant.length)] = (byte) rnd.nextInt(256);
+            }
+            try {
+                EventMeshFrame decoded = EventMeshFrame.decode(mutant);
+                assertNotNull(decoded);
+                accepted++;
+            } catch (IllegalArgumentException expected) {
+                illegal++;
+            }
+        }
+        assertTrue(accepted > 0, "some mutants should still decode");
+        assertTrue(illegal > 0, "some mutants should be rejected");
+    }
+
+    private static Map<String, String> mapOf(String... kv) {
+        Map<String, String> m = new LinkedHashMap<>();
+        for (int i = 0; i + 1 < kv.length; i += 2) {
+            m.put(kv[i], kv[i + 1]);
+        }
+        return m;
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@eventmesh.apache.org
For additional commands, e-mail: commits-help@eventmesh.apache.org