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

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

commit 8991d5a440e62c44a8ab7af7e946f6d164ad0f93
Author: Colm O hEigeartaigh <[email protected]>
AuthorDate: Fri Dec 4 17:29:13 2026 +0000

    Cap the inflated size of SAML SSO tokens and fix CompressionUtils.inflate
---
 .../cxf/fediz/core/samlsso/CompressionUtils.java   | 61 ++++++++------
 .../fediz/core/samlsso/CompressionUtilsTest.java   | 98 ++++++++++++++++++++++
 2 files changed, 135 insertions(+), 24 deletions(-)

diff --git 
a/plugins/core/src/main/java/org/apache/cxf/fediz/core/samlsso/CompressionUtils.java
 
b/plugins/core/src/main/java/org/apache/cxf/fediz/core/samlsso/CompressionUtils.java
index 696d54c3..355b45fe 100644
--- 
a/plugins/core/src/main/java/org/apache/cxf/fediz/core/samlsso/CompressionUtils.java
+++ 
b/plugins/core/src/main/java/org/apache/cxf/fediz/core/samlsso/CompressionUtils.java
@@ -19,52 +19,65 @@
 package org.apache.cxf.fediz.core.samlsso;
 
 import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.InputStream;
-import java.io.SequenceInputStream;
 import java.util.zip.DataFormatException;
 import java.util.zip.Deflater;
 import java.util.zip.Inflater;
 
 public final class CompressionUtils {
+
+    /**
+     * The default maximum size in bytes of an inflated token (1 MB).
+     */
+    public static final int DEFAULT_MAX_INFLATED_SIZE = 1024 * 1024;
+
     private CompressionUtils() {
 
     }
+
     public static InputStream inflate(byte[] deflatedToken)
         throws DataFormatException {
         return inflate(deflatedToken, true);
     }
+
     public static InputStream inflate(byte[] deflatedToken, boolean nowrap)
         throws DataFormatException {
-        Inflater inflater = new Inflater(nowrap);
-        inflater.setInput(deflatedToken);
+        return inflate(deflatedToken, nowrap, DEFAULT_MAX_INFLATED_SIZE);
+    }
 
-        byte[] input = new byte[deflatedToken.length * 2];
-        int inflatedLen = 0;
-        int inputLen = 0;
-        byte[] inflatedToken = input;
-        while (!inflater.finished()) {
-            inputLen = inflater.inflate(input);
-            if (!inflater.finished()) {
+    /**
+     * Inflate the given token, failing with a DataFormatException if the 
inflated
+     * size would exceed maxInflatedSize bytes.
+     */
+    public static InputStream inflate(byte[] deflatedToken, boolean nowrap, 
int maxInflatedSize)
+        throws DataFormatException {
+        Inflater inflater = new Inflater(nowrap);
+        try {
+            inflater.setInput(deflatedToken);
 
-                if (inputLen == 0) {
-                    if (inflater.needsInput()) {
-                        throw new DataFormatException("Inflater can not 
inflate all the token bytes");
-                    } else {
+            ByteArrayOutputStream inflated = new ByteArrayOutputStream(
+                Math.min(Math.max(deflatedToken.length * 2, 64), 
maxInflatedSize));
+            byte[] buffer = new byte[4096];
+            while (!inflater.finished()) {
+                int len = inflater.inflate(buffer);
+                if (len == 0) {
+                    if (inflater.finished()) {
                         break;
                     }
+                    // Truncated input, or a preset dictionary is required
+                    throw new DataFormatException("Inflater can not inflate 
all the token bytes");
                 }
-
-                inflatedToken = new byte[input.length + inflatedLen];
-                System.arraycopy(input, 0, inflatedToken, inflatedLen, 
inputLen);
-                inflatedLen += inputLen;
+                if (inflated.size() + len > maxInflatedSize) {
+                    throw new DataFormatException("Inflated token exceeds the 
maximum allowed size of "
+                                                  + maxInflatedSize + " 
bytes");
+                }
+                inflated.write(buffer, 0, len);
             }
+            return new ByteArrayInputStream(inflated.toByteArray());
+        } finally {
+            inflater.end();
         }
-        InputStream is = new ByteArrayInputStream(input, 0, inputLen);
-        if (inflatedToken != input) {
-            is = new SequenceInputStream(new 
ByteArrayInputStream(inflatedToken, 0, inflatedLen),
-                                         is);
-        }
-        return is;
     }
 
     public static byte[] deflate(byte[] tokenBytes) {
diff --git 
a/plugins/core/src/test/java/org/apache/cxf/fediz/core/samlsso/CompressionUtilsTest.java
 
b/plugins/core/src/test/java/org/apache/cxf/fediz/core/samlsso/CompressionUtilsTest.java
new file mode 100644
index 00000000..ac31c6df
--- /dev/null
+++ 
b/plugins/core/src/test/java/org/apache/cxf/fediz/core/samlsso/CompressionUtilsTest.java
@@ -0,0 +1,98 @@
+/**
+ * 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.fediz.core.samlsso;
+
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
+import java.util.zip.DataFormatException;
+import java.util.zip.Deflater;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class CompressionUtilsTest {
+
+    @Test
+    public void testRoundTrip() throws Exception {
+        byte[] original = 
"<samlp:Response>test</samlp:Response>".getBytes(StandardCharsets.UTF_8);
+        byte[] inflated = readAll(CompressionUtils.inflate(deflate(original)));
+        Assertions.assertArrayEquals(original, inflated);
+    }
+
+    @Test
+    public void testRoundTripSpanningMultipleBuffers() throws Exception {
+        // Highly compressible but non-trivial content, much larger than twice 
the deflated size
+        StringBuilder sb = new StringBuilder();
+        for (int i = 0; i < 20000; i++) {
+            sb.append("<saml:Attribute Name=\"attr").append(i % 
97).append("\"/>");
+        }
+        byte[] original = sb.toString().getBytes(StandardCharsets.UTF_8);
+        byte[] inflated = readAll(CompressionUtils.inflate(deflate(original)));
+        Assertions.assertArrayEquals(original, inflated);
+    }
+
+    @Test
+    public void testDecompressionBombRejected() throws Exception {
+        byte[] bomb = new byte[CompressionUtils.DEFAULT_MAX_INFLATED_SIZE + 1];
+        Arrays.fill(bomb, (byte) 'A');
+        byte[] deflated = deflate(bomb);
+        Assertions.assertTrue(deflated.length < 10 * 1024);
+        Assertions.assertThrows(DataFormatException.class, () -> 
CompressionUtils.inflate(deflated));
+    }
+
+    @Test
+    public void testExactlyMaxSizeAccepted() throws Exception {
+        byte[] original = new byte[1000];
+        Arrays.fill(original, (byte) 'A');
+        byte[] inflated = readAll(CompressionUtils.inflate(deflate(original), 
true, 1000));
+        Assertions.assertArrayEquals(original, inflated);
+        Assertions.assertThrows(DataFormatException.class,
+            () -> CompressionUtils.inflate(deflate(original), true, 999));
+    }
+
+    @Test
+    public void testTruncatedInputRejected() throws Exception {
+        byte[] deflated = 
deflate("<samlp:Response>test</samlp:Response>".getBytes(StandardCharsets.UTF_8));
+        byte[] truncated = Arrays.copyOf(deflated, deflated.length / 2);
+        Assertions.assertThrows(DataFormatException.class, () -> 
CompressionUtils.inflate(truncated));
+    }
+
+    private static byte[] deflate(byte[] data) {
+        Deflater deflater = new Deflater(Deflater.DEFLATED, true);
+        deflater.setInput(data);
+        deflater.finish();
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        byte[] buffer = new byte[4096];
+        while (!deflater.finished()) {
+            int len = deflater.deflate(buffer);
+            out.write(buffer, 0, len);
+        }
+        deflater.end();
+        return out.toByteArray();
+    }
+
+    private static byte[] readAll(InputStream is) throws Exception {
+        try (InputStream in = is) {
+            return in.readAllBytes();
+        }
+    }
+}

Reply via email to