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

coheigea pushed a commit to branch 4.1.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git


The following commit(s) were added to refs/heads/4.1.x-fixes by this push:
     new 9cf3f3216ac Bound the memory used by the WS-Addressing message ID 
cache (#3789)
9cf3f3216ac is described below

commit 9cf3f3216ac0ab8aebe54eb43193bf1da77c5c44
Author: Colm O hEigeartaigh <[email protected]>
AuthorDate: Thu Nov 26 11:44:58 2026 +0000

    Bound the memory used by the WS-Addressing message ID cache (#3789)
    
    (cherry picked from commit 279101ec5cef4bf379367034d79e3f079c6ea65d)
---
 .../ws/addressing/impl/DefaultMessageIdCache.java  | 65 +++++++++++++++---
 .../cxf/ws/addressing/impl/MAPAggregatorImpl.java  | 18 ++++-
 .../addressing/impl/DefaultMessageIdCacheTest.java | 80 ++++++++++++++++++++++
 3 files changed, 153 insertions(+), 10 deletions(-)

diff --git 
a/rt/ws/addr/src/main/java/org/apache/cxf/ws/addressing/impl/DefaultMessageIdCache.java
 
b/rt/ws/addr/src/main/java/org/apache/cxf/ws/addressing/impl/DefaultMessageIdCache.java
index 8a002b7ab19..35e66891ecd 100644
--- 
a/rt/ws/addr/src/main/java/org/apache/cxf/ws/addressing/impl/DefaultMessageIdCache.java
+++ 
b/rt/ws/addr/src/main/java/org/apache/cxf/ws/addressing/impl/DefaultMessageIdCache.java
@@ -18,30 +18,79 @@
  */
 package org.apache.cxf.ws.addressing.impl;
 
+import java.nio.charset.StandardCharsets;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.Base64;
+import java.util.Collections;
+import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.Set;
-import java.util.concurrent.ConcurrentHashMap;
 
 import org.apache.cxf.ws.addressing.MessageIdCache;
 
 /**
- * An implementation that uses a simple set to store received message IDs.
- * Note that this implementation does not make any attempt to flush older
- * message IDs or to persist the message IDs outside of this instance.
+ * An implementation that stores the IDs of received messages in memory. To 
bound the memory it uses, it
+ * stores a digest of each message ID rather than the ID itself, and it holds 
at most a maximum number of IDs
+ * (100000 by default), forgetting the oldest ones once it is full. A 
duplicate message is therefore only
+ * detected while the ID of the original message is still in the cache. This 
implementation does not persist
+ * the message IDs outside of this instance.
  */
 public class DefaultMessageIdCache implements MessageIdCache {
 
+    public static final int DEFAULT_MAX_SIZE = 100000;
+
+    /**
+     * The set of message ID digests, oldest first.
+     */
+    private final Map<String, Boolean> messageIdSet;
+
+    public DefaultMessageIdCache() {
+        this(DEFAULT_MAX_SIZE);
+    }
+
     /**
-     * The set of message IDs.
+     * @param maxSize the maximum number of message IDs to hold
      */
-    private final Map<String, Boolean> messageIdSet =
-        new ConcurrentHashMap<>();
+    public DefaultMessageIdCache(int maxSize) {
+        if (maxSize <= 0) {
+            throw new IllegalArgumentException("The maximum size must be 
positive: " + maxSize);
+        }
+        this.messageIdSet = Collections.synchronizedMap(new 
BoundedMap(maxSize));
+    }
 
     public boolean checkUniquenessAndCacheId(String messageId) {
-        return this.messageIdSet.put(messageId, Boolean.TRUE) == null;
+        return this.messageIdSet.put(digest(messageId), Boolean.TRUE) == null;
     }
 
     protected Set<String> getMessageIdSet() {
         return this.messageIdSet.keySet();
     }
+
+    private static String digest(String messageId) {
+        try {
+            MessageDigest digest = MessageDigest.getInstance("SHA-256");
+            return 
Base64.getEncoder().encodeToString(digest.digest(messageId.getBytes(StandardCharsets.UTF_8)));
+        } catch (NoSuchAlgorithmException ex) {
+            // every Java platform is required to support SHA-256
+            throw new IllegalStateException(ex);
+        }
+    }
+
+    /**
+     * An insertion-ordered map that removes its oldest entry once it holds 
more than the maximum size.
+     */
+    private static final class BoundedMap extends LinkedHashMap<String, 
Boolean> {
+        private static final long serialVersionUID = 1L;
+        private final int maxSize;
+
+        BoundedMap(int maxSize) {
+            this.maxSize = maxSize;
+        }
+
+        @Override
+        protected boolean removeEldestEntry(Map.Entry<String, Boolean> eldest) 
{
+            return size() > maxSize;
+        }
+    }
 }
diff --git 
a/rt/ws/addr/src/main/java/org/apache/cxf/ws/addressing/impl/MAPAggregatorImpl.java
 
b/rt/ws/addr/src/main/java/org/apache/cxf/ws/addressing/impl/MAPAggregatorImpl.java
index e3aff4e603d..5a01fe426a5 100644
--- 
a/rt/ws/addr/src/main/java/org/apache/cxf/ws/addressing/impl/MAPAggregatorImpl.java
+++ 
b/rt/ws/addr/src/main/java/org/apache/cxf/ws/addressing/impl/MAPAggregatorImpl.java
@@ -92,6 +92,7 @@ public class MAPAggregatorImpl extends MAPAggregator {
     private static final Logger LOG =
         LogUtils.getL7dLogger(MAPAggregator.class);
     private static final ResourceBundle BUNDLE = LOG.getResourceBundle();
+    private static final int MAX_DISPLAYED_MESSAGE_ID_LENGTH = 256;
 
     private static final ClientLifeCycleListener DECOUPLED_DEST_CLEANER = new 
ClientLifeCycleListener() {
         public void clientCreated(Client client) {
@@ -1162,9 +1163,12 @@ public class MAPAggregatorImpl extends MAPAggregator {
             if (!allowDuplicates && messageID != null && messageID.getValue() 
!= null
                 && 
!messageIdCache.checkUniquenessAndCacheId(messageID.getValue())) {
 
+                // the message ID comes from the sender and can be very long, 
so don't copy all of it
+                // into the log or the fault
+                String displayedId = truncateMessageId(messageID.getValue());
                 LOG.log(Level.WARNING,
                         "DUPLICATE_MESSAGE_ID_MSG",
-                        messageID.getValue());
+                        displayedId);
 
                 // Only throw the fault if something else has not already 
marked the
                 // message as invalid.
@@ -1172,7 +1176,7 @@ public class MAPAggregatorImpl extends MAPAggregator {
                     String reason =
                         BUNDLE.getString("DUPLICATE_MESSAGE_ID_MSG");
                     String l7dReason =
-                        MessageFormat.format(reason, messageID.getValue());
+                        MessageFormat.format(reason, displayedId);
                     
ContextUtils.storeMAPFaultName(Names.DUPLICATE_MESSAGE_ID_NAME,
                                                    message);
                     ContextUtils.storeMAPFaultReason(l7dReason, message);
@@ -1216,6 +1220,16 @@ public class MAPAggregatorImpl extends MAPAggregator {
         }
     }
 
+    /**
+     * Shorten a message ID, which comes from the sender, before it is logged 
or returned in a fault.
+     */
+    static String truncateMessageId(String messageId) {
+        if (messageId.length() <= MAX_DISPLAYED_MESSAGE_ID_LENGTH) {
+            return messageId;
+        }
+        return messageId.substring(0, MAX_DISPLAYED_MESSAGE_ID_LENGTH) + "...";
+    }
+
     /**
      * Throws a {@code wsa:DestinationUnreachable} SOAP fault when a 
non-anonymous
      * {@code wsa:ReplyTo} is present but decoupled WS-Addressing is disabled.
diff --git 
a/rt/ws/addr/src/test/java/org/apache/cxf/ws/addressing/impl/DefaultMessageIdCacheTest.java
 
b/rt/ws/addr/src/test/java/org/apache/cxf/ws/addressing/impl/DefaultMessageIdCacheTest.java
new file mode 100644
index 00000000000..1606bf447d0
--- /dev/null
+++ 
b/rt/ws/addr/src/test/java/org/apache/cxf/ws/addressing/impl/DefaultMessageIdCacheTest.java
@@ -0,0 +1,80 @@
+/**
+ * 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.cxf.ws.addressing.impl;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assert.assertTrue;
+
+public class DefaultMessageIdCacheTest {
+
+    @Test
+    public void testDuplicateDetected() {
+        DefaultMessageIdCache cache = new DefaultMessageIdCache();
+        assertTrue(cache.checkUniquenessAndCacheId("urn:uuid:1"));
+        assertTrue(cache.checkUniquenessAndCacheId("urn:uuid:2"));
+        assertFalse(cache.checkUniquenessAndCacheId("urn:uuid:1"));
+        assertFalse(cache.checkUniquenessAndCacheId("urn:uuid:2"));
+        assertEquals(2, cache.getMessageIdSet().size());
+    }
+
+    @Test
+    public void testMaxSize() {
+        DefaultMessageIdCache cache = new DefaultMessageIdCache(3);
+        for (int i = 1; i <= 5; i++) {
+            assertTrue(cache.checkUniquenessAndCacheId("urn:uuid:" + i));
+        }
+        assertEquals(3, cache.getMessageIdSet().size());
+
+        // the most recent IDs are still detected as duplicates
+        assertFalse(cache.checkUniquenessAndCacheId("urn:uuid:5"));
+        assertFalse(cache.checkUniquenessAndCacheId("urn:uuid:3"));
+        // the oldest ones have been forgotten
+        assertTrue(cache.checkUniquenessAndCacheId("urn:uuid:1"));
+        assertEquals(3, cache.getMessageIdSet().size());
+    }
+
+    @Test
+    public void testLongMessageIdNotStored() {
+        // the cache keeps a fixed-size digest of each ID, however long the ID 
is
+        String longId = "urn:uuid:" + "x".repeat(10 * 1024 * 1024);
+        DefaultMessageIdCache cache = new DefaultMessageIdCache();
+        assertTrue(cache.checkUniquenessAndCacheId(longId));
+        assertFalse(cache.checkUniquenessAndCacheId(longId));
+        assertTrue(cache.getMessageIdSet().iterator().next().length() < 100);
+    }
+
+    @Test
+    public void testInvalidMaxSize() {
+        assertThrows(IllegalArgumentException.class, () -> new 
DefaultMessageIdCache(0));
+    }
+
+    @Test
+    public void testTruncateMessageIdForDisplay() {
+        // a duplicate message ID is logged and returned in the fault, so a 
huge one must be shortened
+        assertEquals("urn:uuid:1", 
MAPAggregatorImpl.truncateMessageId("urn:uuid:1"));
+        String longId = "urn:uuid:" + "x".repeat(10 * 1024 * 1024);
+        String displayed = MAPAggregatorImpl.truncateMessageId(longId);
+        assertEquals(256 + "...".length(), displayed.length());
+        assertTrue(displayed.startsWith("urn:uuid:xxx") && 
displayed.endsWith("..."));
+    }
+}

Reply via email to